Search code examples
javaspring-integrationjschspring-integration-sftp

Spring Integration SFTP code getting called twice


I am trying basic spring integration to download file from SFTP. Below is a code. The issue is that service-activator gets called twice.

In short, if you see the log, the log's in FileListener bean get called twice and also the com.jcraft.jsch logs get logged twice, if feel there are two sessions and hence my program is getting executed twice.

Please suggest.

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jms="http://www.springframework.org/schema/jms" xmlns:int="http://www.springframework.org/schema/integration"
    xmlns:int-sftp="http://www.springframework.org/schema/integration/sftp"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="
       http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/jms 
        http://www.springframework.org/schema/jms/spring-jms.xsd
        http://www.springframework.org/schema/integration/sftp
        http://www.springframework.org/schema/integration/sftp/spring-integration-sftp.xsd">

    <bean id="sftpSessionFactory"
        class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
        <property name="host" value="11.222.333.44" />
        <property name="user" value="*****" />
        <property name="password" value="******"></property>
        <property name="allowUnknownKeys" value="true" />
    </bean>

    <int-sftp:inbound-channel-adapter id="sftpadapter"
        auto-startup="true" session-factory="sftpSessionFactory"
        channel="requestChannel" filename-pattern="*.csv" remote-directory="/someDirectory/"
        local-directory="D:\\somelocalDirectory\\"
        auto-create-local-directory="true" delete-remote-files="false">
    </int-sftp:inbound-channel-adapter>

    <int:channel id="requestChannel">
        <int:queue />
    </int:channel>

    <int:service-activator input-channel="requestChannel"
        ref="FileListener" method="handle" />

    <int:poller id="defaultpoller" default="true" fixed-rate="10000"
        max-messages-per-poll="1"></int:poller>

    <bean id="FileListener" class="com.somePackage.FileListener">
    </bean> 

ANd the java code is as simple as this.

public class InboundFileListener2 {

    private static Log logger = LogFactory.getLog(InboundFileListener2.class);

    public void handle(Message<?> message) {
        String someId = null;
        String fileName = null;

        try {

            someId = UUID.randomUUID().toString();
            fileName = getFilename(message);

            logger.debug(" CorrelationId : "
                    + someId + " FileName is : " + fileName);

Below is the log.

 SomeId : a77b2bc4-b874-40e7-998d-6ca17aeec196 FileName is : somefile_05032017142503.csv
 SomeId : c50a95e9-83f5-4bb8-a3d9-15da0ff553ee FileName is : somefile_05032017142503.csv
Connecting to 11.222.333.44 port 22
Connecting to 11.222.333.44 port 22
Connection established
Connection established
aes256-ctr is not available.
aes192-ctr is not available.
aes256-cbc is not available.
aes192-cbc is not available.

UPDATED:

web.xml

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>some-name</display-name>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

Solution

  • Commenting below code from web.xml fixed the issue.

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    

    But I am not sure why it happened, if webcontext is just an extension to root context to load just resources for web other than root scope, either of them should load the sftp adapter and initialize it rather than both of then initializing it..... Can some please explain....