繁体   English   中英

Spring Integration xml to java dsl - 如何定义入站/出站通道适配器,轮询器等

[英]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。

更新:(Gary的回应之后 ):

我设法把它翻译到这个。

@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:在si.jdsl.App中定义了名为'archiveFile'的bean时出错:通过工厂方法实例化Bean失败; 嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.integration.dsl.IntegrationFlow]:工厂方法'archiveFile'抛出异常; 嵌套异常是java.lang.IllegalArgumentException:已经为FileReadingMessageSource配置了'filter'(org.springframework.integration.file.filters.CompositeFileListFilter@48e64352)

有什么想法吗?

更新2:

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

@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);
    }

bean的初始化失败; 嵌套异常是org.springframework.beans.factory.UnsatisfiedDependencyException:在si.jdsl.App中定义名为'myMethod'的bean时出错:通过构造函数参数表达的不满意的依赖关系,索引0的类型为[java.io.File] :::否为依赖项找到类型[java.io.File]的限定bean:预期至少有1个bean,它有资格作为此依赖项的autowire候选者。 依赖注释:{}; 嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型为[java.io.File]的限定bean:期望至少有一个bean可以作为此依赖项的autowire候选者。 依赖注释:{}

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

使用Files命名空间工厂。 请参阅DSL参考手册 有一个通用教程这里通过管路,通过在线变换网吧示例应用程序的散步。 这里是 Java 6/7版本)。

编辑

这看起来像一个错误,DSL抱怨你正在设置两个过滤器,它不会允许它。

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

.preventDuplicatesFilter(false),

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

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

.filter(myFilter())

其中myFilter是一个带有模式过滤器等的CompositeFileListFilter bean。

编辑2

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

请参阅文档

使用@ServiceActivator注释@Bean ,它必须是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;
    }

暂无
暂无

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

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