Spring Integration "Нет доступных bean-компонентов с именем toFtpChannel" - PullRequest
0 голосов
/ 11 октября 2018

Мне нужно загрузить изображение на FTP-сервер.Поэтому я создал конфигурацию интеграции с SessionFactory, MessageHandler и MessageGateway для загрузки файлов на FTP-сервер:

@Configuration
@IntegrationComponentScan
public class FtpConfiguration {

    @Bean
    public SessionFactory<FTPFile> ftpSessionFactory() {
        DefaultFtpSessionFactory defaultFtpSessionFactory = new DefaultFtpSessionFactory();
        //setup
        return new CachingSessionFactory<>(defaultFtpSessionFactory);
    }

    @Bean
    @ServiceActivator(inputChannel = "toFtpChannel")
    public MessageHandler handler() {
        FtpMessageHandler handler = new FtpMessageHandler(ftpSessionFactory());
        handler.setAutoCreateDirectory(true);
        handler.setRemoteDirectoryExpression(new LiteralExpression(""));
        handler.setFileNameGenerator(message -> (String) message.getHeaders().get("filename"));
        return handler;
    }

    //to show you that I've tried both
    /*@Bean
    public IntegrationFlow ftpOutboundFlow() {
        return IntegrationFlows.from("toFtpChannel")
                .handle(Ftp.outboundAdapter(ftpSessionFactory(), FileExistsMode.REPLACE)
                        .useTemporaryFileName(false)
                        .fileNameExpression("headers['" + FileHeaders.FILENAME + "']")
                        .remoteDirectory("")
                ).get();
    }*/

    @MessagingGateway
    public interface UploadGateway {

        @Gateway(requestChannel = "toFtpChannel")
        void upload(@Payload byte[] file, @Header("filename") String filename, @Header("path") String path);
    }

}

Успешно создайте приложение.А потом я пытаюсь загрузить какой-нибудь файл:

@Autowired
UploadGateway uploadGateway;


@Override
public void uploadImage(byte[] scanBytes, String filename, String path) {
    try {
        uploadGateway.upload(scanBytes, filename, path);
    } catch (Exception e) {
        log.error("WRONG", e);
    }
}

А потом он говорит: "Нет доступных bean-компонентов с именем toFtpChannel" Я пробовал почти все учебники, что делатьЯ делаю неправильно?

Зависимости:

 <dependency>
        <groupId>org.springframework.integration</groupId>
        <artifactId>spring-integration-file</artifactId>
        <version>RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-integration</artifactId>
        <version>RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.integration</groupId>
        <artifactId>spring-integration-ftp</artifactId>
        <version>RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.integration</groupId>
        <artifactId>spring-integration-java-dsl</artifactId>
        <version>RELEASE</version>
    </dependency>

Ответы [ 2 ]

0 голосов
/ 11 октября 2018

Не похоже, что ваше приложение действительно Spring Boot: мы не видим @SpringBootApplication аннотацию.Именно эта настройка запускает правильную автоматическую настройку, в том числе для Spring Integration.

Если вам все еще не нравится Spring Boot, значит, вам не хватает стандартной @EnableIntegration аннотации: https://docs.spring.io/spring-integration/docs/current/reference/html/overview.html#configuration-enable-integration

0 голосов
/ 11 октября 2018

Канал запроса должен существовать, прежде чем вы сможете сослаться на него в аннотации.Объявите канал, и эта проблема должна исчезнуть

@Bean
public MessageChannel toFtpChannel() {
    return new DirectChannel();
}

В этом примере создается DirectChannel, но вы можете выбрать любую реализацию в соответствии с вашей семантикой.

Надеюсь, это поможет!

...