简体   繁体   English

Spring 集成:远程文件持久化到数据库后删除(sftp)

[英]Spring Integration: delete (sftp) remote file after it has been persisted to database

(for context, please see my previous question ) (有关上下文,请参阅我之前的问题

Still struggling to get a file from an SFTP server, persist it's content to a database and remove that file afterwards, if it was persisted without errors.仍在努力从 SFTP 服务器获取文件,将其内容持久保存到数据库,然后删除该文件(如果持久保存没有错误)。 I just can get the handling of handlers, gateways and flows right.我可以正确处理处理程序、网关和流程。

I need guidanc, please我需要指导,请

What I have:我拥有的:


@Configuration
@EnableIntegration
class Sftp2DB {

    @Bean
    @InboundChannelAdapter(channel = "transform")
    public MessageSource<InputStream> source() {
        return Sftp
            .inboundStreamingAdapter(template(this.sessionFactory))
            .remoteDirectory("inbound")
            .get();
    }

    @Transformer(inputChannel="transform", outputChannel = "persist")
    public Message<MyEntity> transform(final Message<InputStream> in) throws IOException {
        var entity = new MyEntity();
        entity.setContent(in.getPayload().readAllBytes());
        entity.setFilename(in.getHeaders().get(FileHeaders.FILENAME, String.class));
        return MessageBuilder.withPayload(entity).build();
    }

    @ServiceActivator(inputChannel = "persist", outputChannel = "remove")
    public JpaOutboundGateway persist() {
        return Jpa
            .updatingGateway(this.entityManager)
            .entityClass(MyEntity.class)
            .persistMode(PersistMode.PERSIST)
            .get();
    }

    @ServiceActivator(inputChannel = "remove")
    public AbstractRemoteFileOutboundGateway<LsEntry> remove() {
        return Sftp
            .outboundGateway(
                this.sessionFactory,
                "rm", 
                String.format("header['%s'] + '/' + header['%s']", FileHeaders.REMOTE_DIRECTORY, FileHeaders.REMOTE_FILE)
            )
            .get();
            
    }
}

What I get:我得到什么:

2022-11-24 12:50:13.815 ERROR 948 --- [ scheduling-1] osintegration.handler.LoggingHandler: org.springframework.messaging.MessageHandlingException: error occurred in message handler [ServiceActivator for [org.springframework.integration.handler.MethodInvokingMessageProcessor@3be14a03] (Sftp2DB.remove.serviceActivator)]; 2022-11-24 12:50:13.815 错误 948 --- [scheduling-1] osintegration.handler.LoggingHandler: org.springframework.messaging.MessageHandlingException: 消息处理程序 [ServiceActivator for [org.springframework.integration.handler .MethodInvokingMessageProcessor@3be14a03] (Sftp2DB.remove.serviceActivator)]; nested exception is org.springframework.messaging.core.DestinationResolutionException: no output-channel or replyChannel header available, failedMessage=GenericMessage [payload=org.springframework.integration.jpa.outbound.JpaOutboundGateway@6a0e79fb, headers={id=788f63b5-ad62-de6b-bbb1-ecde94d23576, timestamp=1669290613815}]嵌套异常是 org.springframework.messaging.core.DestinationResolutionException:没有可用的输出通道或回复通道 header,failedMessage=GenericMessage [payload=org.springframework.integration.jpa.outbound.JpaOutboundGateway@6a0e79fb,headers={id=788f63b5-ad62 -de6b-bbb1-ecde94d23576,时间戳=1669290613815}]

There are two types of @ServiceActivator (and @Transformer etc).有两种类型的@ServiceActivator (和@Transformer等)。

POJO methods (like your transformer) and beans that define message handlers.定义消息处理程序的 POJO 方法(如您的转换器)和 bean。

Your service activators need to be defined as @Bean s (like you did with your inbound channel adapter).您的服务激活器需要定义为@Bean s(就像您对入站通道适配器所做的那样)。

See https://docs.spring.io/spring-integration/docs/current/reference/html/configuration.html#annotations and https://docs.spring.io/spring-integration/docs/current/reference/html/configuration.html#annotations_on_beans请参阅https://docs.spring.io/spring-integration/docs/current/reference/html/configuration.html#annotationshttps://docs.spring.io/spring-integration/docs/current/reference/html /configuration.html#annotations_on_beans

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM