Reputation: 11
i have refered this answer and tried to pass dynamic file pattern
applicationContext to set environment variables and and calling SI Gateway
public class TestSpringIntegration {
public TestSpringIntegration() {
super();
}
public static void main(String[] args) {
TestSpringIntegration testSpringIntegration = new TestSpringIntegration();
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("SI-filters_files.xml");
setEnvironment(context);
// context.refresh();
ServiceGateway gateway =
context.getBean("inboundGateway",ServiceGateway.class);
}
private static void setEnvironment(final ConfigurableApplicationContext ctx) {
String name = "server_text1";
final StandardEnvironment env = new StandardEnvironment();
final Properties props = new Properties();
props.setProperty("filename.pattern", "'" + name + "'");
// props.setProperty("ftp.local.dir", ftpMetaData.getLocalDirectory());
final PropertiesPropertySource pps = new PropertiesPropertySource("ftpprops", props);
env.getPropertySources().addLast(pps);
ctx.setEnvironment(env);
}
}
this is my SI-filters_files.xml I have set environment properties above to get dynamic filePattern in file adapter.
<int:channel id="requestChannel" />
<int:channel id="deadLetterChannel" />
<int:channel id="outboundChannel" />
<int:gateway id="inboundGateway" service-interface="test.ServiceGateway"
default-request-channel="requestChannel" default-reply-timeout="5000" />
<file:inbound-channel-adapter id="filesIn"
directory="file://<IP_ADDRESS>/sharedLocation/" filename-pattern="${filename.pattern}"
channel="requestChannel" auto-startup="true">
<int:poller id="poller" fixed-delay="5000" />
</file:inbound-channel-adapter>
<int:service-activator input-channel="requestChannel"
output-channel="outboundChannel" ref="handler" />
<bean id="handler" class="test.FileHandler" />
<file:outbound-channel-adapter id="outboundChannel"
directory="file://E:/" />
Below is the exception I am getting:
2016-11-30 10:17:59 INFO ClassPathXmlApplicationContext:581 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@dcf3e99: startup date [Wed Nov 30 10:17:59 CST 2016]; root of context hierarchy 2016-11-30 10:17:59 INFO XmlBeanDefinitionReader:317 - Loading XML bean definitions from class path resource [SI-filters_files.xml] 2016-11-30 10:17:59 INFO PropertiesFactoryBean:172 - Loading properties file from URL [jar:file:/C:/Users/F1038/.m2/repository/org/springframework/integration/spring-integration-core/4.3.5.RELEASE/spring-integration-core-4.3.5.RELEASE.jar!/META-INF/spring.integration.default.properties] 2016-11-30 10:17:59 INFO IntegrationRegistrar:330 - No bean named 'integrationHeaderChannelRegistry' has been explicitly defined. Therefore, a default DefaultHeaderChannelRegistry will be created. 2016-11-30 10:17:59 INFO DefaultListableBeanFactory:818 - Overriding bean definition for bean 'outboundChannel' with a different definition: replacing [Generic bean: class [org.springframework.integration.channel.DirectChannel]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] with [Generic bean: class [org.springframework.integration.channel.DirectChannel]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] 2016-11-30 10:17:59 WARN ClassPathXmlApplicationContext:549 - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'org.springframework.integration.file.config.FileListFilterFactoryBean#0' defined in null: Could not resolve placeholder 'filename.pattern' in string value "${filename.pattern}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'filename.pattern' in string value "${filename.pattern}" Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'org.springframework.integration.file.config.FileListFilterFactoryBean#0' defined in null: Could not resolve placeholder 'filename.pattern' in string value "${filename.pattern}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'filename.pattern' in string value "${filename.pattern}" at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:223) at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.processProperties(PropertySourcesPlaceholderConfigurer.java:180) at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:152) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:284) at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:166) at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:523) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83) at test.TestSpringIntegration.main(Unknown Source) Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'filename.pattern' in string value "${filename.pattern}" at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174) at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:219) at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:193) at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172) at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveStringValue(BeanDefinitionVisitor.java:282) at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:209) at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitPropertyValues(BeanDefinitionVisitor.java:141) at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:82) at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:220)
Upvotes: 0
Views: 870
Reputation: 11
applicationContext.xml
ServiceGateway gateway = context.getBean("inboundGateway", ServiceGateway.class);
String json = " {\"vesselVisitCode\": \"XV213443\", \"fileName\": \"server_text1.txt\",\"originalFilename\": \"server.txt\" }";
String json1 = "{\"fileName\": \"server_text1.txt\"}";
gateway.post(json1);
Earlier I had done it like (a work around i guess )...
<context:property-placeholder />
<context:component-scan base-package="test"></context:component-scan>
<int:channel id="requestChannel" />
<int:channel id="deadLetterChannel" />
<int:channel id="outboundChannel" />
<int:gateway id="inboundGateway" service-interface="test.ServiceGateway"
default-request-channel="requestChannel" default-reply-timeout="5000" />
<int:splitter input-channel="requestChannel"
output-channel="processingChannel" />
<int:transformer id="tx" input-channel="processingChannel"
ref="jsonHandler" method="jsonBreak" output-channel="loggingChannel" />
<int:channel id="loggingChannel" />
<int:channel id="processingChannel">
<int:dispatcher task-executor="executor" />
</int:channel>
<task:executor id="executor" pool-size="5" />
<file:inbound-channel-adapter id="filesIn"
directory="file://170.197.229.119/ftp/" channel="loggingChannel" >
<int:poller id="poller" fixed-delay="100"/>
</file:inbound-channel-adapter>
<bean id="jsonHandler" class="test.JSONHandler" />
<bean id="handler" class="test.SelectMessage" />
<file:outbound-channel-adapter id="outboundChannel"
directory="file://E:/" />
JSONHandler.java
public class JSONHandler {
@Autowired
SelectMessage message;
public String jsonBreak(String json) {
Gson gson = new Gson();
JsonElement element = gson.fromJson(json, JsonElement.class);
JsonObject jsonObj = element.getAsJsonObject();
JsonElement jsonElement = jsonObj.get("fileName");
String asString = jsonElement.getAsString();
message.setFileName(asString);
System.out.println("in Handler----------------------------------" + json);
return asString;
}
}
SelectMessage.java
public class SelectMessage implements MessageSelector {
@Autowired
private ApplicationContext appContext;
private String fileName;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public boolean accept(Message<?> message) {
try {
if (message.getPayload() instanceof File && ((File) message.getPayload()).getName().startsWith((getFileName()))) {
System.out.println(" --------------------return file on true match --------------------");
return true;
}
return false;
} catch (Exception e) {
return false;
}
}
}
Upvotes: 0
Reputation: 174729
With that constructor, the context is refreshed automatically, before you added the property source.
Use the no-arg constructor, pass the XML into setConfigLocations
, set up your property source; call context.refresh()
.
Or, use this constructor:
public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh)
See the dynamic-ftp-sample for an example.
Upvotes: 0