user10270958
user10270958

Reputation: 33

how to implement integration router in java

i am trying to convert below XML snippet into Java version, appreciate any help here

<int:router input-channel="channel_in" default-output-channel="channel_default" 
  expression="payload.name" ignore-channel-name-resolution-failures="true">
    <int:mapping value="foo" channel="channel_one" />
    <int:mapping value="bar" channel="channel_two" />
</int:router>

Here is what i did for my concrete example

@Router(inputChannel = "routerChannel")
    public String route(Account message) {
        if (message.getType().equals("check")) {
            return "checkChannel";
        } else if (message.getType().equals("credit")) {
            return "creditChannel";
        } 
        return "errorChannel";
}

@Bean
public DirectChannel checkChannel() {
    return new DirectChannel();
}

when i do above i am seeing below error

org.springframework.messaging.MessageDeliveryException: Dispatcher has no subscribers for channel 'application:8090.checkChannel'.;

Upvotes: 1

Views: 691

Answers (1)

Artem Bilan
Artem Bilan

Reputation: 121272

All the Spring Integration custom tag has a description like this:

<xsd:element name="router" type="routerType">
    <xsd:annotation>
        <xsd:documentation>
            Defines a Consumer Endpoint for the
            'org.springframework.integration.router.AbstractMessageProcessingRouter' implementation
            that serves as an adapter for invoking a method on any
            Spring-managed object as specified by the "ref" and "method" attributes.
        </xsd:documentation>
    </xsd:annotation>
</xsd:element>

So, it becomes pretty clear that we need some AbstractMessageProcessingRouter implementation to be present in the Java Configuration.

Also we have a paragraph in the Reference Manual like this:

With XML configuration and Spring Integration Namespace support, the XML Parsers hide how target beans are declared and wired together. For Java & Annotation Configuration, it is important to understand the Framework API for target end-user applications.

According your expression="payload.name" we need to look for the ExpressionEvaluatingRouter and then also read a chapter about @Bean configuration:

    @Bean
    @Router(inputChannel = "channel_in")
    public ExpressionEvaluatingRouter expressionRouter() {
        ExpressionEvaluatingRouter router = new ExpressionEvaluatingRouter("payload.name");
        router.setDefaultOutputChannelName("channel_default");
        router.setChannelMapping("foo", "channel_one");
        router.setChannelMapping("bar", "channel_two");
        return router;
    }

Upvotes: 1

Related Questions