Невозможно автоматически связать MessageChannel в Spring Integration
Я пробую пример загрузки и скачивания FTP-файлов с помощью Spring Spring. Я хочу вручную загрузить файл в outputChannel. Я не хочу вызывать его, когда есть изменение в inputChannel. Таким образом, FTP-приложение должно иметь объявление для bean-компонента outputChannel. Я ссылаюсь на эту статью. Загрузка файлов непосредственно в определенную папку на FTP-сервере с использованием Spring Spring для этой цели (только для ручной загрузки, и этот способ распространен на большинстве страниц). Ниже приведен код загрузочного приложения Spring:
@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
Пожалуйста, помогите решить эту проблему.
2 ответа
Используйте @Qualifier с @Autowired . и оставьте имя переменной таким же, как имя компонента.
Прежде всего, я думаю, что вы допустили ошибку outputChannel
составная часть.
Если это о загрузке, так что вы должны использовать @ServiceActivator
а также FtpMessageHandler
,
С другой стороны, для такой проблемы вы должны объявить @Bean
для outputChannel
и вместе с @Autowired
, в FTPController
, вы должны добавить @Qualifier("outputChannel")
,