我创建了一个应用程序,该应用程序在运行时为ftp服务器添加入站适配器,并在需要时注册它们以在某个阶段将其删除,该应用程序将从ftp服务器中提取一个csv文件并将其放置在本地一个具有ftp服务器名称的文件夹,因此我添加的每个服务器都将创建一个单独的本地文件夹,并将csv文件保存在其中,现在可以顺利完成此操作,第二部分是我要更改该文件的格式然后将其发送回各自的服务器,因此基本上我需要使用出站适配器,在这种情况下,我需要在运行时在创建入站适配器或添加服务器的同时创建出站适配器,这应该通过控制器与入站相同,我搜索了可能的解决方案,并尝试了以下解决方案,但该解决方案无法正常工作或未将文件发送到目的地,如何解决此问题的任何解决方案? 在配置类中,我添加了以下内容:
public IntegrationFlow ftpOutboundFlow(Branch myBranch){
return IntegrationFlows.from(OUTBOUND_CHANNEL)
.handle(Ftp.outboundAdapter(createNewFtpSessionFactory(myBranch), FileExistsMode.FAIL)
.useTemporaryFileName(true)
.remoteFileSeparator("/")
//.fileNameExpression("BEY/FEFOexportBEY.csv")
.remoteDirectory(myBranch.getFolderPath()))
.get();
}
@MessagingGateway
public interface MyGateway {
@Gateway(requestChannel = OUTBOUND_CHANNEL)
void sendToFtp(File file);
}
@Bean
public IntegrationFlow ftpOutboundChannel() {
return IntegrationFlows.from(OUTBOUND_CHANNEL)
.publishSubscribeChannel(p -> p
.subscribe(t -> t.handle(System.out::println)))
/*.transform(p -> {
LOG.info("Outbound intermediate Channel, message=rename file:" + p);
return p;
})*/
.channel(new NullChannel())
.get();
}
在Controller类中
@RequestMapping("/branch/showbranch/{id}")
public String getBranch (@PathVariable String id, Model model){
model.addAttribute("branch", branchService.getById(Long.valueOf(id)));
addFlowftp(id);
addFlowftpOutbound(id);
return "/branch/showbranch";
}
private void addFlowFtp(String name) {
branch = branchService.getById(Long.valueOf(name));
System.out.println(branch.getBranchCode());
IntegrationFlow flow = ftIntegration.fileInboundFlowFromFTPServer(branch);
this.flowContext.registration(flow).id(name).register();
}
private void addFlowftpOutbound(String name) {
branch = branchService.getById(Long.valueOf(name));
System.out.println(branch.getBranchCode());
IntegrationFlow flow = ftIntegration.ftpOutboundFlow(branch);
// this.flowContext.registration(flow).id(name).register();
myGateway.sendToFtp(new File("BEY/FEFOexportBEY.csv"));
}
在发送文件之前启用寄存器时,这就是我在控制台中得到的错误:
java.lang.IllegalArgumentException: An IntegrationFlow 'IntegrationFlowRegistration{integrationFlow=StandardIntegrationFlow{integrationComponents={org.springframework.integration.ftp.inbound.FtpInboundFileSynchronizer@aadab28=98.org.springframework.integration.ftp.inbound.FtpInboundFileSynchronizer#0, org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean@aa290b3=stockInboundPoller, org.springframework.integration.transformer.MethodInvokingTransformer@e5f85cd=98.org.springframework.integration.transformer.MethodInvokingTransformer#0, 98.channel#0=98.channel#0, org.springframework.integration.config.ConsumerEndpointFactoryBean@319dff2=98.org.springframework.integration.config.ConsumerEndpointFactoryBean#0}}, id='98', inputChannel=null}' with flowId '98' is already registered.
An existing IntegrationFlowRegistration must be destroyed before overriding.
第二次尝试后,我从第一种方法中删除了注册,仅尝试了第二种方法,但没有任何内容发送到FTP:
GenericMessage [payload=BEY\FEFOexportBEY.csv, headers={id=43cfc2db-41e9-0866-8e4c-8e95968189ff, timestamp=1542702869007}]
2018-11-20 10:34:29.011 INFO 13716 --- [nio-8081-exec-6] f.s.s.configuration.FTIntegration : Outbound intermediate Channel, message=rename file:BEY\FEFOexportBEY.csv
答案 0 :(得分:1)