Search code examples
javaspringspring-bootspring-integrationspring-integration-sftp

How to replyChannel in false or null?


I'm trying remove files in SFTP, I'm using SftpOutboundGateway, all functionality it's ok, The file is removed in SFTP but when it removed, send a Exception:

MessageHandlingException: error occurred in message handler [handler]; nested exception is org.springframework.messaging.core.DestinationResolutionException: no output-channel or replyChannel header available.

How to omit no output-channel or replyChannel?

@Configuration
public class IntegrationConfiguration {

    //...properties

    private static Logger logger = LogManager.getLogger();;

    @Bean
    LocalDateTime currentLocalDateTime() {
        return LocalDateTime.now();
    }

    /**
     * SFTP Session Factory
     *
     * @return SessionFactory
     */
    @Bean
    public SessionFactory<LsEntry> sftpSessionFactory() {
        DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory();
        factory.setHost(sftpHost);
        System.out.println(sftpHost);
        factory.setPort(sftpPort);
        factory.setUser(sftpUser);
        if (sftpPrivateKey != null) {
            factory.setPrivateKey(sftpPrivateKey);
            factory.setPrivateKeyPassphrase(sftpPrivateKeyPassphrase);
        } else {
            factory.setPassword(sftpPassword);
        }

        factory.setAllowUnknownKeys(true);
        return new CachingSessionFactory<LsEntry>(factory);
    }

    @Bean
    public SftpRemoteFileTemplate template() {
        return new SftpRemoteFileTemplate(sftpSessionFactory());
    }

    @Bean
    @InboundChannelAdapter(channel = "sftStream", poller = @Poller(maxMessagesPerPoll = "5", cron="0 0-55 9 * * ?")) //fixedDelay = "50000"
    public MessageSource<InputStream> ftpMessageSource() {
        SftpStreamingMessageSource messageSource = new SftpStreamingMessageSource(template());
        messageSource.setRemoteDirectory(sftpRemoteDirectory);
        logger.info("File filter: " + fileListeFilter);
        logger.info("Range Date: " + rangeDate);
        messageSource.setFilter(new SftpSimplePatternFileListFilter(fileListeFilter)); //*.ack
        messageSource.setFileInfoJson(false);
        messageSource.setMaxFetchSize(5);
        return messageSource;
    }

    /**
     * Filter message File
     * 
     * @param message
     * @return
     */
    @Filter(inputChannel = "sftStream", outputChannel = "deleteSftpFile", discardChannel = "filterDiscardFile")
    public boolean filterSFTPFile(Message<?> message) {

        boolean filter = false;
        SftpFileInfo sftpFileInfo = (SftpFileInfo) message.getHeaders().get("file_remoteFileInfo");

        logger.info(message);

        LocalDateTime sftpFiledate = LocalDateTime.ofInstant(Instant.ofEpochMilli(sftpFileInfo.getModified()),
                ZoneId.systemDefault());

        if (compareSftpFileDate(sftpFiledate)) {
            logger.info("File will be deledted " + sftpFileInfo.getRemoteDirectory() + sftpFileInfo.getFilename());
            filter = true;
        }

        return filter;
    }

    /**
     * Discard file.
     * 
     * @param sftpFileInfo
     */
    @ServiceActivator(inputChannel = "filterDiscardFile")
    public void handleDiscardedFile(@Header("file_remoteFileInfo") SftpFileInfo sftpFileInfo) {
        logger.info("Message is received and it was discarded by filterSFTPFile(): " + sftpFileInfo.getRemoteDirectory()
                + sftpFileInfo.getFilename());
    }

    /**
     * Send message to delete file in SFTP
     * 
     * @return
     */
    @Bean
    @ServiceActivator(inputChannel = "deleteSftpFile")
    public MessageHandler handler() {

        return new SftpOutboundGateway(sftpSessionFactory(), "rm",
                "headers['file_remoteDirectory'] + headers['file_remoteFile']");
    }

Solution

  • You need to configure it like this:

    SftpOutboundGateway sftpOutboundGateway = new SftpOutboundGateway(...);
    sftpOutboundGateway.setOutputChannelName("nullChannel");
    return sftpOutboundGateway;
    

    if you are not interested in the reply from the SftpOutboundGateway.

    https://docs.spring.io/spring-integration/docs/5.0.6.RELEASE/reference/html/messaging-channels-section.html#channel-special-channels

    https://docs.spring.io/spring-integration/docs/5.0.6.RELEASE/reference/html/configuration.html#annotations_on_beans