java - Spring Integration xml 到 java dsl - 如何定义入站/出站 channel 适配器、轮询器等

标签 java xml spring spring-integration dsl

这是我的 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。

更新:(在 Gary's Response 之后):

我设法翻译到这里。

@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 .有通用教程here其中逐步介绍了咖啡馆示例应用程序的逐行转换。 (Java 6/7 版本 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 中有多个消息传递方法。

关于java - Spring Integration xml 到 java dsl - 如何定义入站/出站 channel 适配器、轮询器等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31715620/

相关文章:

java - 小程序中的后台进程

java - 如何在标签完好无损的情况下解析 html

java - SimpleExpandableListAdapter 的示例

java - 如何确保 http 请求来自特定用户?

xml - 在vb中删除xml文件节点属性的最简单方法是什么?

android.view.InflateException : Binary XML file line #20: Error inflating class fragment

xml - 获取祖先节点的后代节点的位置,不一定是兄弟节点

spring - 如何在 Spring Boot 中设置 JMX 的身份验证凭据?

java - 无法将 Spring Boot Admin Server 2 和 Spring Boot Admin Client 2 与 Spring Boot 2 集成在单个应用程序中

java - Struts2 + Spring + JPA( hibernate ): action mapping problem