Spring Integration SFTP代码被调用两次

时间:2017-07-03 13:15:15

标签: java spring-integration jsch spring-integration-sftp

我正在尝试从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>

1 个答案:

答案 0 :(得分:0)

从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适配器并初始化它而不是两者都初始化它.....有些人可以解释....