Spring 集成 SFTP 代码被调用两次

Spring Integration SFTP code getting called twice

我正在尝试基本 spring 集成以从 SFTP 下载文件。 下面是一段代码。问题是服务激活器被调用了两次。

简而言之,如果您看到日志,FileListener bean 中的日志会被调用两次,com.jcraft.jsch 日志也会被调用 记录两次,如果感觉有两个会话,因此我的程序被执行了两次。

求推荐。

<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> 

而 java 代码就这么简单。

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);

下面是日志。

 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.

更新:

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>

评论下面来自 web.xml 的代码解决了这个问题。

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

但我不确定为什么会这样,如果 webcontext 只是根上下文的扩展以仅加载 web 资源而不是根范围,那么它们中的任何一个都应该加载 sftp 适配器并对其进行初始化,而不是同时加载它们正在初始化..... 能否解释一下....