2015-01-05 2 views
0

maven 웹 프로젝트를 사용하여 Spring 통합 파일을 sftp로 보내려고합니다. 폴링 기능이 필요합니다. SftpInbound.java를 시작하면 폴링이 작동합니다. 서버를 시작할 때 폴링을해야합니다. 자바 파일 및 구성의 내용 SftpInbound.java서버 통합시 스프링 통합 -sftp 인바운드 어댑터와 서버 시작시

package com.myproj.integration.bsy.sftp; 

import java.io.File; 

import org.springframework.context.ConfigurableApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.integration.endpoint.SourcePollingChannelAdapter; 
import org.springframework.integration.file.remote.RemoteFileTemplate; 
import org.springframework.integration.file.remote.session.CachingSessionFactory; 
import org.springframework.integration.file.remote.session.SessionFactory; 
import org.springframework.messaging.Message; 
import org.springframework.messaging.PollableChannel; 
import org.springframework.scheduling.annotation.Scheduled; 
import com.myproj.integration.bsy.sftp.*; 

import com.jcraft.jsch.ChannelSftp.LsEntry; 

public class SftpInboundReceive { 

    @Scheduled(fixedRate=5000) 
    public void inboundSftpPoll(){ 
     ConfigurableApplicationContext context = 
       new ClassPathXmlApplicationContext("/META-INF/spring/integration/sftp/SftpInboundReceive-context.xml", this.getClass()); 
     RemoteFileTemplate<LsEntry> template = null; 
     String file1 = "a.txt"; 
     String file2 = "b.txt"; 
     String file3 = "c.bar"; 
     new File("local-dir", file1).delete(); 
     new File("local-dir", file2).delete(); 
     try { 
      PollableChannel localFileChannel = context.getBean("receiveChannel", PollableChannel.class); 
      @SuppressWarnings("unchecked") 
      SessionFactory<LsEntry> sessionFactory = context.getBean(CachingSessionFactory.class); 
      template = new RemoteFileTemplate<LsEntry>(sessionFactory); 
      System.out.println("here 1" +template); 

      SourcePollingChannelAdapter adapter = context.getBean("sftpInbondAdapter",SourcePollingChannelAdapter.class); 
      adapter.start(); 
      Message<?> received = localFileChannel.receive(); 
      System.out.println("Received first file message 1: " + received); 
      received = localFileChannel.receive(); 
      System.out.println("Received second file message: " + received); 
      received = localFileChannel.receive(1000); 
      System.out.println("Third file was received as expected" +received); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
     finally { 
      SftpUtils.cleanUp(template, file1, file2, file3); 
      //context.close(); 
     } 
    } 
    public static void main(String args[]) 
    { 
     SftpInboundReceive oInboundReceiveSample = new SftpInboundReceive(); 
     oInboundReceiveSample.inboundSftpPoll(); 
    } 
} 

<?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-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-4.1.xsd 
     http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-4.1.xsd 
     http://www.springframework.org/schema/integration/sftp http://www.springframework.org/schema/integration/sftp/spring-integration-sftp-4.1.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-4.1.xsd"> 


    <!-- <import resource="SftpSampleCommon.xml"/> --> 


      <context:property-placeholder order="1" 
      location="classpath:/sftpuser.properties" ignore-unresolvable="true"/> 


     <bean id="sftpSessionFactory" 
      class="org.springframework.integration.file.remote.session.CachingSessionFactory"> 
      <constructor-arg ref="defaultSftpSessionFactory" /> 
     </bean> 

     <!-- host=xxx.xx.128.143 port=22 username=xxxuser passphrase= private.keyfile=classpath:META-INF/keys/sftp_rsa --> 


     <bean id="defaultSftpSessionFactory" 

      class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory"> 

      <property name="host" value="${sftp.host}" /> 
      <property name="port" value="${sftp.port}" /> 
      <property name="user" value="${sftp.username}" /> 
      <property name="privateKey" value="${private.keyfile}" /> 
      <property name="privateKeyPassphrase" value="${passphrase}" /> 
     </bean> 



     <!-- username & password from property file... tested <bean id="defaultSftpSessionFactory" 
      class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory"> 
      <property name="host" value="${sftp.host}"/> <property name="port" value="${sftp.port}"/> 
      <property name="user" value="${sftp.username}"/> <property name="password" 
      value="${sftp.password}"/> </bean> --> 



     <!-- hardcoded, username & password... tested <bean id="defaultSftpSessionFactory" 
      class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory"> 
      <property name="host" value="xxx.xx.128.143"/> <property name="port" value="22"/> 
      <property name="user" value="xxxuser"/> <property name="password" value="[email protected]"/> 
      </bean> --> 



     <!-- Inbound channel adapter for SFTP call . with poll facility --> 
     <int-sftp:inbound-channel-adapter id="sftpInbondAdapter" 
      auto-startup="true" channel="receiveChannel" session-factory="sftpSessionFactory" 
      local-directory="file:/target/foo" remote-directory="${sftp.inboundremotedir}" 
      auto-create-local-directory="true" delete-remote-files="false" 
      filename-pattern="*.txt"> 
      <int:poller fixed-rate="100000" max-messages-per-poll="1" /> 
     </int-sftp:inbound-channel-adapter> 

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

    </beans> 

스택 트레이스가

p-bio-8080-exec-3][org.springframework.security.web.FilterChainProxy]/at position 5 of 12 in additional filter chain; firing Filter: 'DefaultLoginPageGeneratingFilter' 
    16:02:58.368 DEBUG [http-bio-8080-exec-3][org.springframework.security.web.FilterChainProxy]/at position 6 of 12 in additional filter chain; firing Filter: 'BasicAuthenticationFilter' 
    16:02:58.368 DEBUG [http-bio-8080-exec-3][org.springframework.security.web.FilterChainProxy]/at position 7 of 12 in additional filter chain; firing Filter: 'RequestCacheAwareFilter' 
    16:02:58.368 DEBUG [http-bio-8080-exec-3][org.springframework.security.web.FilterChainProxy]/at position 8 of 12 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter' 
    16:02:58.370 DEBUG [http-bio-8080-exec-3][org.springframework.security.web.FilterChainProxy]/at position 9 of 12 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter' 
    16:02:58.371 DEBUG [http-bio-8080-exec-3][org.springframework.security.web.authentication.AnonymousAuthenticationFilter] Populated SecurityContextHolder with anonymous token: 'org.sprin[email protected]9055e4a6: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.sprin[email protected]957e: RemoteIpAddress: 127.0.0.1; SessionId: null; Granted Authorities: ROLE_ANONYMOUS' 
    16:02:58.371 DEBUG [http-bio-8080-exec-3][org.springframework.security.web.FilterChainProxy]/at position 10 of 12 in additional filter chain; firing Filter: 'SessionManagementFilter' 
    16:02:58.371 DEBUG [http-bio-8080-exec-3][org.springframework.security.web.FilterChainProxy]/at position 11 of 12 in additional filter chain; firing Filter: 'ExceptionTranslationFilter' 
    16:02:58.371 DEBUG [http-bio-8080-exec-3][org.springframework.security.web.FilterChainProxy]/at position 12 of 12 in additional filter chain; firing Filter: 'FilterSecurityInterceptor' 
    16:02:58.371 DEBUG [http-bio-8080-exec-3][org.springframework.security.web.util.matcher.AntPathRequestMatcher] Checking match of request : '/'; against '/services/employee/*' 
    16:02:58.371 DEBUG [http-bio-8080-exec-3][org.springframework.security.web.access.intercept.FilterSecurityInterceptor] Public object - authentication not attempted 
    16:02:58.371 TRACE [http-bio-8080-exec-3][org.springframework.web.context.support.XmlWebApplicationContext] Publishing event in Root WebApplicationContext: org.springframework.security.access.event.PublicInvocationEvent[source=FilterInvocation: URL: /] 
    16:02:58.372 DEBUG [http-bio-8080-exec-3][org.springframework.beans.factory.support.DefaultListableBeanFactory] Returning cached instance of singleton bean 'org.springframework.integration.internalMessagingAnnotationPostProcessor' 
    16:02:58.372 DEBUG [http-bio-8080-exec-3][org.springframework.security.web.FilterChainProxy]/reached end of additional filter chain; proceeding with original chain 
    16:02:58.375 TRACE [http-bio-8080-exec-3][org.springframework.web.servlet.DispatcherServlet] Bound request context to thread: SecurityContextHolderAwareRequestWrapper[ org.springframework.security.web.context.HttpSessi[email protected]1b0b34e] 
    16:02:58.376 DEBUG [http-bio-8080-exec-3][org.springframework.web.servlet.DispatcherServlet] DispatcherServlet with name 'Information Exchange Gateway Integration' processing GET request for [/DummyDataIntg/] 
    16:02:58.376 TRACE [http-bio-8080-exec-3][org.springframework.web.servlet.DispatcherServlet] Testing handler map [org.springframework.i[email protected]cdca7] in DispatcherServlet with name 'Information Exchange Gateway Integration' 
    16:02:58.378 WARN [http-bio-8080-exec-3][org.springframework.web.servlet.PageNotFound] No mapping found for HTTP request with URI [/DummyDataIntg/] in DispatcherServlet with name 'Information Exchange Gateway Integration' 
    16:02:58.378 DEBUG [http-bio-8080-exec-3][org.springframework.security.web.context.HttpSessionSecurityContextRepository] SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession. 
    16:02:58.378 TRACE [http-bio-8080-exec-3][org.springframework.web.servlet.DispatcherServlet] Cleared thread-bound request context: SecurityContextHolderAwareRequestWrapper[ org.springframework.security.web.context.HttpSessi[email protected]1b0b34e] 
    16:02:58.378 DEBUG [http-bio-8080-exec-3][org.springframework.web.servlet.DispatcherServlet] Successfully completed request 
    16:02:58.378 TRACE [http-bio-8080-exec-3][org.springframework.web.context.support.XmlWebApplicationContext] Publishing event in WebApplicationContext for namespace 'Information Exchange Gateway Integration-servlet': ServletRequestHandledEvent: url=[/DummyDataIntg/]; client=[127.0.0.1]; method=[GET]; servlet=[Information Exchange Gateway Integration]; session=[null]; user=[null]; time=[6ms]; status=[OK] 
    16:02:58.378 TRACE [http-bio-8080-exec-3][org.springframework.web.context.support.XmlWebApplicationContext] Publishing event in Root WebApplicationContext: ServletRequestHandledEvent: url=[/DummyDataIntg/]; client=[127.0.0.1]; method=[GET]; servlet=[Information Exchange Gateway Integration]; session=[null]; user=[null]; time=[6ms]; status=[OK] 
    16:02:58.378 DEBUG [http-bio-8080-exec-3][org.springframework.beans.factory.support.DefaultListableBeanFactory] Returning cached instance of singleton bean 'org.springframework.integration.internalMessagingAnnotationPostProcessor' 
    16:02:58.378 DEBUG [http-bio-8080-exec-3][org.springframework.security.web.access.ExceptionTranslationFilter] Chain processed normally 
    16:02:58.378 DEBUG [http-bio-8080-exec-3][org.springframework.security.web.context.SecurityContextPersistenceFilter] SecurityContextHolder now cleared, as request processing completed 
    16:04:37.403 INFO [task-scheduler-4][org.springframework.integration.file.FileReadingMessageSource] Created message: [GenericMessage [payload=\target\foo\b.txt, headers={timestamp=1420540477403, id=f8c32928-411b-99b7-f4a0-0dd1b119fc44}]] 
    16:04:37.403 ERROR [task-scheduler-4][org.springframework.integration.handler.LoggingHandler] \target\foo\b.txt 
    16:06:17.403 INFO [task-scheduler-9][org.springframework.integration.file.FileReadingMessageSource] Created message: [GenericMessage [payload=\target\foo\brjb.txt, headers={timestamp=1420540577403, id=061634bf-0562-962b-e583-53a302cdb0d4}]] 
    16:06:17.403 ERROR [task-scheduler-9][org.springframework.integration.handler.LoggingHandler] \target\foo\brjb.txt 
    16:07:57.403 INFO [task-scheduler-10][org.springframework.integration.file.FileReadingMessageSource] Created message: [GenericMessage [payload=\target\foo\d.txt, headers={timestamp=1420540677403, id=abea1188-fc5a-15b0-c40c-73ea686a88c0}]] 
    16:07:57.403 ERROR [task-scheduler-10][org.springframework.integration.handler.LoggingHandler] \target\foo\d.txt 
    16:09:37.403 INFO [task-scheduler-4][org.springframework.integration.file.FileReadingMessageSource] Created message: [GenericMessage [payload=\target\foo\g.txt, headers={timestamp=1420540777403, id=461a8e48-6ebf-5d1c-ab3f-ce28e54e00b8}]] 
    16:09:37.403 ERROR [task-scheduler-4][org.springframework.integration.handler.LoggingHandler] \target\foo\g.txt 
    16:11:17.403 INFO [task-scheduler-3][org.springframework.integration.file.FileReadingMessageSource] Created message: [GenericMessage [payload=\target\foo\h.txt, headers={timestamp=1420540877403, id=4cbeeceb-5949-0e1c-1492-45ec17172480}]] 
    16:11:17.403 ERROR [task-scheduler-3][org.springframework.integration.handler.LoggingHandler] \target\foo\h.txt 
    16:12:57.403 INFO [task-scheduler-9][org.springframework.integration.file.FileReadingMessageSource] Created message: [GenericMessage [payload=\target\foo\p.txt, headers={timestamp=1420540977403, id=3767b4bb-4de4-3178-f693-5ac0bd94a766}]] 
    16:12:57.403 ERROR [task-scheduler-9][org.springframework.integration.handler.LoggingHandler] \target\foo\p.txt 
    16:14:37.403 INFO [task-scheduler-6][org.springframework.integration.file.FileReadingMessageSource] Created message: [GenericMessage [payload=\target\foo\wiki.txt, headers={timestamp=1420541077403, id=bd3d0082-9788-fc31-1596-ab7a79186c17}]] 
    16:14:37.403 ERROR [task-scheduler-6][org.springframework.integration.handler.LoggingHandler] \target\foo\wiki.txt 
error log in java file**strong text** 
20:25:52.807 ERROR [task-scheduler-1][org.springframework.integration.handler.LoggingHandler] org.springframework.messaging.MessagingException: Problem occurred while synchronizing remote to local directory; nested exception is org.springframework.messaging.MessagingException: Failed to execute on session; nested exception is org.springframework.core.NestedIOException: Failed to list files; nested exception is 2: No such file 
    at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer.synchronizeToLocalDirectory(AbstractInboundFileSynchronizer.java:209) 
    at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizingMessageSource.doReceive(AbstractInboundFileSynchronizingMessageSource.java:167) 
    at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizingMessageSource.doReceive(AbstractInboundFileSynchronizingMessageSource.java:57) 
    at org.springframework.integration.endpoint.AbstractMessageSource.receive(AbstractMessageSource.java:64) 
    at org.springframework.integration.endpoint.SourcePollingChannelAdapter.receiveMessage(SourcePollingChannelAdapter.java:124) 
    at org.springframework.integration.endpoint.AbstractPollingEndpoint.doPoll(AbstractPollingEndpoint.java:192) 
    at org.springframework.integration.endpoint.AbstractPollingEndpoint.access$000(AbstractPollingEndpoint.java:55) 
    at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:149) 
    at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:146) 
    at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller$1.run(AbstractPollingEndpoint.java:298) 
    at org.springframework.integration.util.ErrorHandlingTaskExecutor$1.run(ErrorHandlingTaskExecutor.java:52) 
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50) 
    at org.springframework.integration.util.ErrorHandlingTaskExecutor.execute(ErrorHandlingTaskExecutor.java:49) 
    at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller.run(AbstractPollingEndpoint.java:292) 
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) 
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81) 
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) 
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:178) 
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:292) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:744) 
Caused by: org.springframework.messaging.MessagingException: Failed to execute on session; nested exception is org.springframework.core.NestedIOException: Failed to list files; nested exception is 2: No such file 
    at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:343) 
    at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer.synchronizeToLocalDirectory(AbstractInboundFileSynchronizer.java:167) 
    ... 22 more 
Caused by: org.springframework.core.NestedIOException: Failed to list files; nested exception is 2: No such file 
    at org.springframework.integration.sftp.session.SftpSession.list(SftpSession.java:103) 
    at org.springframework.integration.sftp.session.SftpSession.list(SftpSession.java:50) 
    at org.springframework.integration.file.remote.session.CachingSessionFactory$CachedSession.list(CachingSessionFactory.java:205) 
    at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer$1.doInSession(AbstractInboundFileSynchronizer.java:171) 
    at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer$1.doInSession(AbstractInboundFileSynchronizer.java:167) 
    at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:334) 
    ... 23 more 
Caused by: 2: No such file 
    at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2846) 
    at com.jcraft.jsch.ChannelSftp._stat(ChannelSftp.java:2198) 
    at com.jcraft.jsch.ChannelSftp._stat(ChannelSftp.java:2215) 
    at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1565) 
    at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1526) 
    at org.springframework.integration.sftp.session.SftpSession.list(SftpSession.java:91) 
    ... 28 more 

답변

1

당신은 북동 XML 파일 SftpInboundReceive-context.xml에 Servlet 3 환경에서 web.xml 또는 WebApplicationInitializer을 사용하여 웹 응용 프로그램에서 Spring Context를 시작하는 방법을 살펴 보았다. 이 경우 SftpInboundReceive-context.xml은 일반 ApplicationContext의 일부가 될 수 있으며 폴링 기능 (<int-sftp:inbound-channel-adapter>)은 응용 프로그램 시작시 서버 시작시 자동으로 시작됩니다. 이는 마지막 응용 프로그램의 웹 컨텍스트를 볼 때 발생합니다.

은 스프링 프레임 워크에 대한 더 많은 문서를 읽어 보시기 바랍니다 : http://projects.spring.io/spring-framework/

봄 통합 단지 EIP 확장과 동일한 구성 및 라이프 사이클 규칙을 따라야합니다.

봄 Intregration의 SFTP 샘플을 사용하고 있습니다. Tomcat과 Spring Boot에 대한 샘플도 찾을 수 있습니다.

+0

sftp-inbound-context cml은 이미 web.xml에 정의 된 상위 컨텍스트 파일로 가져옵니다. –

+0

그리고? 질문은 무엇입니까? Spring Integration stuff를 시작하기 위해'SftpInboundReceive'가 더 이상 필요 없습니다. –

+0

왜냐하면 .. xml 설정으로 폴링 할 수 없었기 때문입니다. 나는 sftpinboundreceive 자바 파일을 가지고 시험했다. –

관련 문제