Dynamic routing in camel en-queues messages infinitely

左心房为你撑大大i 提交于 2019-12-08 03:49:25

问题



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>


来源:https://stackoverflow.com/questions/25280916/dynamic-routing-in-camel-en-queues-messages-infinitely

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!