Reputation: 1475
I'm trying out FTP file upload and download example using spring integration. I want to manually upload a file to the outputChannel. I don't want invoke it when there is a change in inputChannel. So, the FTPApplication should just have a declaration for the outputChannel bean. Im refering this article Uploading files directly to specific folder in ftp server using spring spring for this purpose(Only for manually uploading and this way is common in most pages). Below is the Spring boot application code:
@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;
}
}
The controller code is as shown below:
@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);
}
}
When I run the application I get the following error:
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
Please help to resolve this.
Upvotes: 0
Views: 3357
Reputation: 77
use @Qualifier , with @Autowired . and keep the variable name same as bean name.
Upvotes: 0
Reputation: 121550
First of all I think you made some mistake around outputChannel
component.
If that is about upload, so you have to use @ServiceActivator
and FtpMessageHandler
.
On the other hand for such a problem you have to declare @Bean
for the outputChannel
and together with the @Autowired
, in the FTPController
, you should add @Qualifier("outputChannel")
.
Upvotes: 0