简体   繁体   中英

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

This is my spring integration xml: A simple stuff i'm using for learning...

<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"/>

I couldn't really find a good tutorial on this.. could you please point me to a good tutorial for integration java dsl? Also, please help me convert this from xml to dsl.

UPDATE: (after Gary's Response ) :

I managed to translate it till this.

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

Just not sure if this I'm doing it right. Posted it as soon as I translated, will test it out.

Tested it : getting the following error:

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

Any thoughts?

UPDATE 2:

Thanks Gary, That solved the filter issue: Getting issue with service activator. Following is my service activator:

@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: {}

Please let me know what i'm missing?

Use the Files namespace factory. See 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 ).

EDIT :

This looks like a bug, the DSL is complaining that you are setting two filters and it won't allow it.

In this case, you actually don't need this

.preventDuplicatesFilter(false),

because that's the default when you provide another filter.

If you do need to compose a filter you can use

.filter(myFilter())

where myFilter is a CompositeFileListFilter bean with pattern filter etc.

EDIT 2 :

@Bean s are constructed at initialization time, clearly this is a runtime method.

See the documentation .

When a @Bean is annotated with @ServiceActivator , it must be of type MessageHandler . To use POJO messaging, you need a @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);
    }

}

You can have multiple messaging methods in the 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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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