2016-09-27 46 views
0

我嘗試使用彈簧集成的FTP文件上傳和下載示例。我想手動將文件上傳到outputChannel。我不想在inputChannel發生變化時調用它。所以,FTPApplication應該只有一個outputChannel bean的聲明。我爲此目的引用這篇文章Uploading files directly to specific folder in ftp server using spring spring(僅用於手動上傳,這種方式在大多數頁面中都是常見的)。下面是春季啓動的應用程序代碼:無法在Spring集成中自動裝入MessageChannel

@SpringBootApplication 
public class FtpApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(FtpApplication.class, args); 
    } 

    @Bean 
    public SessionFactory<FTPFile> ftpSessionFactory() { 
     DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory(); 
     sf.setHost("localhost"); 
     sf.setPort(21); 
     sf.setUsername("root"); 
     sf.setPassword("root"); 
     return new CachingSessionFactory<FTPFile>(sf); 
    } 

    @Bean 
    public FtpInboundFileSynchronizer ftpInboundFileSynchronizer() { 
     FtpInboundFileSynchronizer fileSynchronizer = new FtpInboundFileSynchronizer(ftpSessionFactory()); 
     fileSynchronizer.setDeleteRemoteFiles(false); 
     fileSynchronizer.setRemoteDirectory("/"); 
     fileSynchronizer.setFilter(new FtpSimplePatternFileListFilter("*.xml")); 
     return fileSynchronizer; 
    } 

    @Bean 
    @InboundChannelAdapter(value="inputChannel", channel = "inputChannel") 
    public MessageSource<File> ftpMessageSource() { 
     FtpInboundFileSynchronizingMessageSource source = new FtpInboundFileSynchronizingMessageSource(
       ftpInboundFileSynchronizer()); 
     source.setLocalDirectory(new File("ftp-inbound")); 
     source.setAutoCreateLocalDirectory(true); 
     source.setLocalFilter(new AcceptOnceFileListFilter<File>()); 
     return source; 
    } 

    @Bean 
    @ServiceActivator(inputChannel = "inputChannel") 
    public MessageHandler handler() { 
     return new MessageHandler() { 
      @Override 
      public void handleMessage(Message<?> message) throws MessagingException { 
       System.out.println(message.getPayload()); 
      } 
     }; 
    } 

    @Bean(name = PollerMetadata.DEFAULT_POLLER) 
    public PollerMetadata defaultPoller() { 
     PollerMetadata pollerMetadata = new PollerMetadata(); 
     pollerMetadata.setTrigger(new PeriodicTrigger(10)); 
     return pollerMetadata; 
    } 

    @Bean 
    @InboundChannelAdapter(value="outputChannel", channel = "outputChannel") 
    public MessageSource<File> ftpMessageSource1() { 
     FtpInboundFileSynchronizingMessageSource source = new FtpInboundFileSynchronizingMessageSource(
       ftpInboundFileSynchronizer()); 
     source.setLocalDirectory(new File("ftp-outbound")); 
     source.setAutoCreateLocalDirectory(true); 
     source.setLocalFilter(new AcceptOnceFileListFilter<File>()); 
     return source; 
    } 

} 

如下圖所示的控制器代碼:

@RestController 
public class FTPController { 

    @Autowired 
    MessageChannel outputChannel; 

    @RequestMapping(value = "/ftpuploadtest", method = RequestMethod.GET) 
    public void getM36Messages() { 
     File file = new File("ftp.txt"); 
     Message<File> fileMessage = MessageBuilder.withPayload(file).build(); 
     outputChannel.send(fileMessage); 
    } 
} 

當我運行的應用程序,我得到以下錯誤:

Description: 
Field outputChannel in com.ftp.FTPController required a single bean, but 3 were found: 
    - nullChannel: defined in null 
    - errorChannel: defined in null 
    - inputChannel: a programmatically registered singleton 

Action: 

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed 

請幫助解決這個問題。

回答

0

首先我想你在outputChannel組件上犯了一些錯誤。

如果這是大約上傳,所以你必須使用@ServiceActivatorFtpMessageHandler

在另一方面你有這樣的問題申報@BeanoutputChannel,並與@Autowired一起,在FTPController,你應該添加@Qualifier("outputChannel")

+0

您也不應該在兩個入站通道適配器中使用同一個同步器。 –

+0

@Artem:如果我使用ServiceActivator註釋,我需要提及一個inputChannel。這裏我想手動將文件上傳到輸出通道。當輸入通道發生變化時不應調用它。我應該做些什麼?這就是爲什麼我沒有去SeviceActivator註釋的原因。對不起,如果我聽起來你很愚蠢。 – User1230321

+0

請解釋任務。完全不清楚爲什麼您將消息頻道視爲上傳內容。 –

0

使用@Qualifier,使用@Autowired。並保持變量名與bean名稱相同。

相關問題