这是我的 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
是在初始化时构造的,显然这是一个运行时方法。
当一个@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/