我在Spring XML中使用动态路由器bean定义了路由,如下所示
<route id="dynamic-recipientList-route" >
<from uri="direct-vm:b2bfwk-post" />
<log message="dynamic-recipientList-route received=${body}" />
<dynamicRouter>
<method ref="dynamicRouter" method="route" />
</dynamicRouter>
</route>
dynamicRouter bean java代码实现如下:
public String route(Exchange inExch) {
LOG.info("Enter DynamicRouterBean ... " + inExch.getIn().getBody());
return whereToGo(inExch);
}
/**
* Method which computes where to go next
*/
private String whereToGo(Exchange inExch) {
int invoked = 0;
Object current = inExch.getProperty("invoked");
if(current != null){
invoked = Integer.valueOf(current.toString());
LOG.info("In progress of dynamic route... invoked = " + invoked + " ... " + inExch.getIn().getBody());
}
else{
//Message enter dynamic router for the first time... invoked = 0
LOG.info("Message enter dynamic router for the first time... invoked = 0 ... " + inExch.getIn().getBody());
}
invoked++;
inExch.setProperty("invoked", invoked);
if (invoked == 1) {
// 1st time
LOG.info("route to step 1 ... " + inExch.getIn().getBody());
return "direct-vm://step1";
} else if (invoked == 2) {
// 2nd time
LOG.info("route to step 2 ... " + inExch.getIn().getBody());
return "direct-vm://step2";
} else {
// no more, so return null to indicate end of dynamic router
LOG.info("All Steps finished for message " + inExch.getIn().getBody());
return null;
}
}
和&#34;步骤&#34;路由配置如下
<route id="app-route-step1" >
<from uri="direct-vm:step1" />
<log message="step1 received=${headers}, ${body}" />
<delay><constant>1000</constant></delay>
<to uri="direct-vm:b2bfwk-post" />
</route>
<route id="app-route-step2" >
<from uri="direct-vm:step2" />
<log message="step2 received=${headers}, ${body}" />
<delay><constant>1000</constant></delay>
<to uri="direct-vm:b2bfwk-post" />
</route>
bean将添加一个属性&#34; invoked&#34;进入Exchange,每次调用DynamicRouter bean时,值将增加1。当它达到3时,路由器bean将返回null并且Exchange应该停止路由。
但是,我从日志中发现,在返回null之后,Exchange对象仍然再次路由到动态路由器bean,&#34; invoked = 4&#34; ...再次返回null,并且此Exchange对象的路由已停止。我想Exchange对象的路由应该停止在&#34; invoked = 3&#34;因为动态路由器返回null。似乎一个额外的路由意外地发生了......我的路由器bean代码中可能存在任何错误配置或潜在问题?
下面补充日志...注意&#34;所有步骤已完成......&#34;声明出现了两次......
2015-08-26 12:46:42,418 INFO multiconsumer-route - multiconsumer-route received, header={breadcrumbId=ID--53870-1440564388141-0-1, CamelFileAbsolute=false, CamelFileAbsolutePath=D:\EclipseLunaSR2\eclipse_workspace\camelInAction\dynamic-route\target\inbox\smallfile.csv, CamelFileContentType=application/vnd.ms-excel, CamelFileLastModified=1440560415206, CamelFileLength=16, CamelFileName=smallfile.csv, CamelFileNameConsumed=smallfile.csv, CamelFileNameOnly=smallfile.csv, CamelFileParent=target\inbox, CamelFilePath=target\inbox\smallfile.csv, CamelFileRelativePath=smallfile.csv, JMSCorrelationID=null, JMSDeliveryMode=2, JMSDestination=Queue[camelInAction.sample.inventory], JMSExpiration=0, JMSMessageID=ID:EMS-SERVER.20B455DAC6251A7:1, JMSPriority=4, JMSRedelivered=false, JMSReplyTo=null, JMSTimestamp=1440564402407, JMSType=null, JMSXGroupID=null, JMSXUserID=null, testHeader=headerValue}
2015-08-26 12:46:42,419 INFO dynamic-recipientList-route - dynamic-recipientList-route received=123,0,bumper,4
2015-08-26 12:46:42,422 INFO DynamicRouterBean - Enter DynamicRouterBean ... 123,0,bumper,4
2015-08-26 12:46:42,422 INFO DynamicRouterBean - Message enter dynamic router for the first time... invoked = 0 ... 123,0,bumper,4
2015-08-26 12:46:42,422 INFO DynamicRouterBean - route to step 1 ... 123,0,bumper,4
2015-08-26 12:46:42,433 INFO app-route-step1 - step1 received=123,0,bumper,4
2015-08-26 12:46:42,433 INFO dynamic-recipientList-route - dynamic-recipientList-route received=123,0,bumper,4
2015-08-26 12:46:42,434 INFO DynamicRouterBean - Enter DynamicRouterBean ... 123,0,bumper,4
2015-08-26 12:46:42,434 INFO DynamicRouterBean - In progress of dynamic route... invoked = 1 ... 123,0,bumper,4
2015-08-26 12:46:42,434 INFO DynamicRouterBean - route to step 2 ... 123,0,bumper,4
2015-08-26 12:46:42,434 INFO app-route-step2 - step2 received=123,0,bumper,4
2015-08-26 12:46:42,435 INFO dynamic-recipientList-route - dynamic-recipientList-route received=123,0,bumper,4
2015-08-26 12:46:42,435 INFO DynamicRouterBean - Enter DynamicRouterBean ... 123,0,bumper,4
2015-08-26 12:46:42,435 INFO DynamicRouterBean - In progress of dynamic route... invoked = 2 ... 123,0,bumper,4
2015-08-26 12:46:42,435 INFO DynamicRouterBean - All Steps finished for message 123,0,bumper,4
2015-08-26 12:46:42,437 INFO DynamicRouterBean - Enter DynamicRouterBean ... 123,0,bumper,4
2015-08-26 12:46:42,437 INFO DynamicRouterBean - In progress of dynamic route... invoked = 3 ... 123,0,bumper,4
2015-08-26 12:46:42,437 INFO DynamicRouterBean - All Steps finished for message 123,0,bumper,4
2015-08-26 12:46:42,438 INFO DynamicRouterBean - Enter DynamicRouterBean ... 123,0,bumper,4
2015-08-26 12:46:42,438 INFO DynamicRouterBean - In progress of dynamic route... invoked = 4 ... 123,0,bumper,4
2015-08-26 12:46:42,438 INFO DynamicRouterBean - All Steps finished for message 123,0,bumper,4
答案 0 :(得分:1)
在到达该路线之前,我不知道您的代码是做什么的,但我得到以下代码:
@Test
public void testName() throws Exception {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:a").dynamicRouter().method(new WhereToGo());
from("direct-vm://step1").log("Step1: ${body}");
from("direct-vm://step2").log("Step2: ${body}");
}
});
template.sendBody("direct:a", "data");
Thread.sleep(6000);
}
给出以下内容:
13:42:56.000 [main] INFO n.n.WhereToGoTest - Enter DynamicRouterBean ... data
13:42:56.000 [main] INFO n.n.WhereToGoTest - Message enter dynamic router for the first time... invoked = 0 ... data
13:42:56.001 [main] INFO n.n.WhereToGoTest - route to step 1 ... data
13:42:56.009 [main] INFO route2 - Step1: data
13:42:56.012 [main] INFO n.n.WhereToGoTest - Enter DynamicRouterBean ... data
13:42:56.012 [main] INFO n.n.WhereToGoTest - In progress of dynamic route... invoked = 1 ... data
13:42:56.012 [main] INFO n.n.WhereToGoTest - route to step 2 ... data
13:42:56.013 [main] INFO route3 - Step2: data
13:42:56.014 [main] INFO n.n.WhereToGoTest - Enter DynamicRouterBean ... data
13:42:56.014 [main] INFO n.n.WhereToGoTest - In progress of dynamic route... invoked = 2 ... data
13:42:56.014 [main] INFO n.n.WhereToGoTest - All Steps finished for message data