Dynamic routing in camel en-queues messages infini

2019-06-07 19:00发布

问题:


I am working on camel’s dynamic router to derive and dispatch to the relevant queue by referring http://camel.apache.org/dynamic-router.html

Following is the camel config xml:

<route id="dynamicRouter" autoStartup="true">
    <from uri="vm://service1?concurrentConsumers=10&amp;timeout=0" />
    <choice>
        <when>
            <simple>${body.documentStatus} == 'SUCCESS' </simple>
            <log message="routing to dynamic router" />
            <dynamicRouter>
            <!-- use a method call on a bean as dynamic router -->
            <method ref="messageRouter" method="route" />
            </dynamicRouter>
        </when>
    </choice>
</route>

Following is my dynamic router bean:

public class MessageRouter {

private static final String QUEUE_BROKER = "activemq"; 
private static final String DEFAULT_QUEUE = "queue"; 
/**
 * 
 * @param obj
 *            message
 * @return the dynamically generated queue name
 */ 
public String route(ServiceObject obj) {
    RecordObject record = obj.getRecordObject();

    if(record != null){
        return QUEUE_BROKER + ":" 
                + record.getId() + "." + DEFAULT_QUEUE; 
    }
    return null; 
}

}

I am able to en-queue messages in dynamically created queue but messages are getting enqueued infinitely.
Any help in identifying what could be the reason would be of great help.

Thanks in advance!!!

回答1:

Read the documentation about dynamic router at

  • http://camel.apache.org/dynamic-router.html

And see the beware box, it says the method must return null to signal to the dynamic router to break out.

So in your code above, then this code line

RecordObject record = obj.getRecordObject();

... the record object is never null and therefore the dynamic router keeps going forever.

If you only want this dynamic routing one time then use the dynamic receipient list eip instead, see

  • http://camel.apache.org/how-to-use-a-dynamic-uri-in-to.html
  • http://camel.apache.org/recipient-list.html

And your xml route is

<recipientList>
<!-- use a method call on a bean as dynamic router -->
<method ref="messageRouter" method="route" />
</recipientList>