Spring 集成 xml 到 java dsl - 如何定义 inbound/outbound 通道适配器、轮询器等

Spring Integration xml to java dsl - how to define inbound/outbound channel adaptors, pollers, etc

这是我的 spring 集成 xml:我用来学习的一个简单的东西...

<int-file:inbound-channel-adapter id="executionMessageFileInputChannel"
                                  directory="file:${fpml.messages.input}"
                                  prevent-duplicates="false" filename-pattern="*.xml">
    <int:poller fixed-delay="20000" max-messages-per-poll="20"/>
</int-file:inbound-channel-adapter>

<int:service-activator input-channel="executionMessageFileInputChannel"
                       output-channel="executionMessageFileArchiveChannel"
                       ref="dummyService" method="myMethod"/>


<int-file:outbound-channel-adapter id="executionMessageFileArchiveChannel"
                                   directory="file:${fpml.messages.archive}"
                                   delete-source-files="true" auto-create-directory="true"/>

我真的找不到很好的教程..你能指点我吗 很好的集成教程java dsl? 另外,请 帮我把它从 xml 转换成 dsl。

更新:(在 之后):

我设法翻译到这里。

@MessagingGateway
public interface Archive {
    @Gateway(requestChannel = "archiveFile.input")
    void archive();
}

@Bean
    public IntegrationFlow archiveFile() {
        return IntegrationFlows
                .from(Files.inboundAdapter(new File(dirPath))
                                .patternFilter("*.xml")
                                .preventDuplicatesFilter(false),
                        e -> e.poller(Pollers.fixedDelay(20000)
                                .maxMessagesPerPoll(20)))
                .handle("app","myMethod")
                .handle(Files.outboundAdapter(new File(outDirPath)).deleteSourceFiles(true).autoCreateDirectory(true))
                .get();
    }

只是不确定我这样做是否正确。我一翻译就发布,我会测试一下。

已测试:出现以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'archiveFile' defined in si.jdsl.App: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.integration.dsl.IntegrationFlow]: Factory method 'archiveFile' threw exception; nested exception is java.lang.IllegalArgumentException: The 'filter' (org.springframework.integration.file.filters.CompositeFileListFilter@48e64352) is already configured for the FileReadingMessageSource

有什么想法吗?

更新 2:

谢谢加里,这解决了过滤器问题:服务激活器出现问题。以下是我的服务激活器:

@Bean
    @ServiceActivator(inputChannel = "archiveFile.input")
    public Message<File> myMethod (File inputFile){
        Map<String, Object> contextHeader = new HashMap<String, Object>();
        return new GenericMessage<File>(inputFile, contextHeader);
    }

Initialization of bean failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myMethod' defined in si.jdsl.App: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.io.File]: : No qualifying bean of type [java.io.File] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.io.File] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

请让我知道我错过了什么?

使用 Files 命名空间工厂。参见 the DSL reference manual. There's a general tutorial here which walks through a line-by-line conversion of the cafe sample app. (Java 6/7 version here)。

编辑:

这看起来像一个错误,DSL 抱怨您设置了两个过滤器,但它不允许。

在这种情况下,你实际上不需要这个

.preventDuplicatesFilter(false),

因为这是您提供另一个过滤器时的默认值。

如果您确实需要编写过滤器,您可以使用

.filter(myFilter())

其中 myFilter 是具有模式过滤器等的 CompositeFileListFilter bean

编辑 2:

@Bean是在初始化时构造的,显然这是一个运行时方法。

See the documentation.

@Bean 被注释为 @ServiceActivator 时,它必须是 MessageHandler 类型。要使用 POJO 消息传递,您需要一个 @MessageEndpoint bean...

@Bean
public MyPojo myPojo() {
    return new MyPojo();
}

@MessageEndpoint
public static class MyPojo {

    @ServiceActivator(inputChannel = "archiveFile.input")
    public Message<File> myMethod (File inputFile){
        Map<String, Object> contextHeader = new HashMap<String, Object>();
        return new GenericMessage<File>(inputFile, contextHeader);
    }

}

您可以在 POJO 中有多个消息传递方法。

@Bean
    @InboundChannelAdapter(value = "fileInputChannel", poller = @Poller(fixedDelay = "1000"))
    public MessageSource<File> fileReadingMessageSource() {
        CompositeFileListFilter<File> filters =new CompositeFileListFilter<>();
        filters.addFilter(new SimplePatternFileListFilter("*.log"));
        filters.addFilter(new LastModifiedFileFilter());

        FileReadingMessageSource source = new FileReadingMessageSource();
        source.setAutoCreateDirectory(true);
        source.setDirectory(new File(DIRECTORY));
        source.setFilter(filters);

        return source;
    }