Not able to run Dyamic FTP example- Spring integration -
i trying run dynamic ftp example in spring integration. changed mode sftp. while running test showing expression evaluation failed due below message
org.springframework.beans.factory.xml.xmlbeandefinitionstoreexception: line 30 in xml document class path resource [meta-inf/spring/integration/dynamic-ftp-outbound-adapter-context.xml] invalid; nested exception org.xml.sax.saxparseexception; linenumber: 30; columnnumber: 31; cvc-complex-type.2.4.c: matching wildcard strict, no declaration can found element 'int-sftp:outbound-channel-adapter'.
my files follow
**pom.xml** <?xml version="1.0" encoding="utf-8"?> <project xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"> <modelversion>4.0.0</modelversion> <groupid>org.springframework.integration.samples</groupid> <artifactid>dynamicftp</artifactid> <version>0.0.1</version> <name>dynamic ftp </name> <description>dynamic ftp</description> <!-- <repositories> <repository> <id>repo.spring.io.milestone</id> <name>spring framework maven milestone repository</name> <url>https://repo.spring.io/libs-milestone</url> </repository> </repositories> --> <dependencies> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupid>org.hamcrest</groupid> <artifactid>hamcrest-all</artifactid> <version>1.3</version> <scope>test</scope> </dependency> <dependency> <groupid>org.springframework.integration</groupid> <artifactid>spring-integration-sftp</artifactid> <version>4.1.2.release</version> <scope>compile</scope> </dependency> <dependency> <groupid>org.springframework.integration</groupid> <artifactid>spring-integration-ftp</artifactid> <version>4.1.0.release</version> <scope>compile</scope> </dependency> <dependency> <groupid>org.springframework.integration</groupid> <artifactid>spring-integration-core</artifactid> <version>4.1.0.release</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-test</artifactid> <version>4.1.1.release</version> <scope>test</scope> </dependency> <dependency> <groupid>log4j</groupid> <artifactid>log4j</artifactid> <version>1.2.17</version> <scope>compile</scope> </dependency> <dependency> <groupid>org.mockito</groupid> <artifactid>mockito-core</artifactid> <version>1.9.5</version> <scope>test</scope> </dependency> </dependencies> </project> **dynamicftpchannelresolver** public class dynamicftpchannelresolver { //in production environment value higher //this demonstrate concept of limiting max number of //dynamically created application contexts we'll hold in memory when execute //the code junit public static final int max_cache_size = 2; private final linkedhashmap<string, messagechannel> channels = new linkedhashmap<string, messagechannel>() { private static final long serialversionuid = 1l; @override protected boolean removeeldestentry( entry<string, messagechannel> eldest) { //this returning true means least used //channel , application context closed , removed boolean remove = size() > max_cache_size; if(remove) { messagechannel channel = eldest.getvalue(); configurableapplicationcontext ctx = contexts.get(channel); if(ctx != null) { //shouldn't null ideally ctx.close(); contexts.remove(channel); } } return remove; } }; private final map<messagechannel, configurableapplicationcontext> contexts = new hashmap<messagechannel, configurableapplicationcontext>(); /** * resolve customer channel, each customer gets private * application context , channel inbound channel * application context. * * @param customer * @return channel */ public messagechannel resolve(string customer) { messagechannel channel = this.channels.get(customer); if (channel == null) { channel = createnewcustomerchannel(customer); } return channel; } private synchronized messagechannel createnewcustomerchannel(string customer) { messagechannel channel = this.channels.get(customer); if (channel == null) { configurableapplicationcontext ctx = new classpathxmlapplicationcontext( new string[] { "/meta-inf/spring/integration/dynamic-ftp-outbound-adapter-context.xml" }, false); this.setenvironmentforcustomer(ctx, customer); ctx.refresh(); channel = ctx.getbean("toftpchannel", messagechannel.class); this.channels.put(customer, channel); //will works same reference presented this.contexts.put(channel, ctx); } return channel; } /** * use spring 3.1. environment support set properties * customer-specific application context. * * @param ctx * @param customer */ private void setenvironmentforcustomer(configurableapplicationcontext ctx, string customer) { standardenvironment env = new standardenvironment(); properties props = new properties(); // populate properties customer // props.setproperty("host", "host.for." + customer); // props.setproperty("user", "user"); // props.setproperty("password", "password"); props.setproperty("host", "172.18.554.23"); props.setproperty("user", "iuser"); props.setproperty("port", "22"); props.setproperty("password", "ipwddd"); props.setproperty("remote.directory", "/u01/dpp/dppuser/ftp/"); propertiespropertysource pps = new propertiespropertysource("ftpprops", props); env.getpropertysources().addlast(pps); ctx.setenvironment(env); system.out.println("the properties set" +pps); } } **dynamic-ftp-outbound-context.xml** <?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:int="http://www.springframework.org/schema/integration" xmlns:int-ftp="http://www.springframework.org/schema/integration/ftp" xmlns:int-sftp="http://www.springframework.org/schema/integration /sftp" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/integration/ftp http://www.springframework.org/schema/integration/ftp/spring-integration-ftp.xsd http://www.springframework.org/schema/integration/sftp http://www.springframework.org/schema/integration/ftp/spring-integration-sftp.xsd"> <context:property-placeholder /> <bean id="ftpclientfactory" class="org.springframework.integration.sftp.session.defaultsftpsessionfactory"> <property name="host" value="${host}"/> <property name="port" value="${port}"/> <property name="username" value="${user}"/> <property name="password" value="${password}"/> </bean> <int:channel id="toftpchannel"/> <int-sftp:outbound-channel-adapter id="ftpoutbound" channel="toftpchannel" remote-directory="${remote.directory}" session-factory="sftpsessionfactory" mode="replace" remote-file-separator="/"> <int:poller fixed-rate="100" time-unit="milliseconds" max-messages-per-poll="100"/> </int-sftp:outbound-channel-adapter> <bean id="sftpsessionfactory" class="org.springframework.integration.file.remote.session.cachingsessionfactory"> <constructor-arg ref="ftpclientfactory" /> </bean> </beans> **dyamic-ftp-ountbound-sample-context.xml** <?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:int="http://www.springframework.org/schema/integration" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="channelresolver" class="com.tcs.dynamicftp.dynamicftpchannelresolver" /> <int:channel id="todynrouter" /> <int:router input-channel="todynrouter" expression="@channelresolver.resolve(headers['customer'])"/> </beans> **the test file** import static org.hamcrest.matchers.instanceof; import static org.junit.assert.assertequals; import static org.junit.assert.assertthat; import static org.junit.assert.asserttrue; import java.io.bufferedwriter; import java.io.file; import java.io.filewriter; import java.net.unknownhostexception; import org.junit.test; import org.springframework.context.configurableapplicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; import org.springframework.integration.support.messagebuilder; import org.springframework.messaging.message; import org.springframework.messaging.messagechannel; import org.springframework.messaging.messagingexception; public class ftpoutboundchanneladaptersample { @test public void rundemo() throws exception{ configurableapplicationcontext ctx = new classpathxmlapplicationcontext("meta-inf/spring/integration /dynamicftpoutboundchanneladaptersample-context.xml"); messagechannel channel = ctx.getbean("todynrouter", messagechannel.class); file file = file.createtempfile("temp", "txt"); bufferedwriter bw = new bufferedwriter(new filewriter(file)); bw.write("this temporary file content"); bw.close(); message<file> message = messagebuilder.withpayload(file) .setheader("customer", "cust1") .build(); try { channel.send(message); } catch (messagingexception e) { assertthat(e.getcause().getcause().getcause(), instanceof(unknownhostexception.class)); asserttrue(e.getcause().getcause().getcause().getmessage().startswith("host.for.cust1")); } // send can see in log don't create ac again. try { channel.send(message); } catch (messagingexception e) { assertthat(e.getcause().getcause().getcause(), instanceof(unknownhostexception.class)); asserttrue(e.getcause().getcause().getcause().getmessage().startswith("host.for.cust1")); } // send different customer; again, check log see new ac built message = messagebuilder.withpayload(file) .setheader("customer", "cust2").build(); try { channel.send(message); } catch (messagingexception e) { assertthat(e.getcause().getcause().getcause(), instanceof(unknownhostexception.class)); asserttrue(e.getcause().getcause().getcause().getmessage().startswith("host.for.cust2")); } // send different customer; again, check log see new ac built //and first 1 created (cust1) should closed , removed per max cache size restriction message = messagebuilder.withpayload(file) .setheader("customer", "cust3").build(); try { channel.send(message); } catch (messagingexception e) { assertthat(e.getcause().getcause().getcause(), instanceof(unknownhostexception.class)); asserttrue(e.getcause().getcause().getcause().getmessage().startswith("host.for.cust3")); } //send cust1 again, since 1 has been invalidated before, should //see new ac created (with ac of cust2 destroyed , removed) message = messagebuilder.withpayload(file) .setheader("customer", "cust1").build(); try { channel.send(message); } catch (messagingexception e) { assertthat(e.getcause().getcause().getcause(), instanceof(unknownhostexception.class)); assertequals("host.for.cust1", e.getcause().getcause().getcause().getmessage()); } ctx.close(); } }
you have error in xmlns
configuration...
xmlns:int-sftp="http://www.springframework.org/schema/integration /sftp"
there should no spaces in it.
edit:
you have error:
http://www.springframework.org/schema/integration/ftp/spring-integration-sftp.xsd">
should be
http://www.springframework.org/schema/integration/sftp/spring-integration-sftp.xsd">
i use ide manage namespace stuff; doing manually error-prone, have found.
i tested locally no problems. however,
<property name="username" value="${user}"/>
should have failed because property user
on sftp factory; why did add <poller/>
?
the channel not pollable.
Comments
Post a Comment