我有驼峰路由构建器,它只检查请求的类型:
public void configure() throws Exception {
from(IN_ENDPOINT)
.choice()
.when(isReadReq())
.to(readRouteBuilder)
.when(isWriteReq())
.to(writeRouteBuilder);
然后将消息进一步传递给完成实际工作的路由构建器。 readRouteBuilder看起来像:
ReadRouteBuilder extends RouteBuilder {
private final Processor exceptionProcessor;
private final Processor responseProcessor;
@Override
public void configure() throws Exception {
from(endpoint)
.process(exceptionProcessor)
.process(responseProcessor);
}
这些构建器由spring初始化,我使用不同的实现od处理器,具体取决于消息中的客户端ID。可能有很多clientIds。 我的问题是如何使骆驼(或春天)意识到我在处理客户端A和其他客户端B等时使用特定的spring bean?
答案 0 :(得分:0)
正如您所提到的,您的不同客户需要不同的实施方案。 所以你必须得到clientId来识别它们。然后在接到电话后立即将其设置在交换对象中。即
public void configure() throws Exception {
from(IN_ENDPOINT).process(exchange -> {
// Get the clientId from incoming msg and set it in exchange
exchange.setProperty("CLIENT_ID" , getClient())
})
.choice()
.when(isReadReq())
.to(readRouteBuilder)
.when(isWriteReq())
.to(writeRouteBuilder);
现在,您拥有交换对象中的clientId,可以在路线的任何位置使用它来识别您的客户端。现在,要调用不同的实现,您可以这样做:
ReadRouteBuilder extends RouteBuilder {
private final Processor exceptionProcessor;
private final Processor responseProcessor;
@Override
public void configure() throws Exception {
from(endpoint)
// you can configure exception processor in OnException clause
.onException(Exception.class).process(exceptionProcessor)
.end()
.when(matches("CLIENT_A")).process("processorForClientA")
.when(matches("CLIENT_B")).process("processorForClientB")
.when(matches("CLIENT_C")).process("processorForClientC")
.when(matches("CLIENT_D")).process("processorForClientD")
// And so forth
.otherwise().process(defaultProcessor)
.process(responseProcessor);
}
protected Predicate matches( String clientId) {
return
exchangeProperty("CLIENT_ID").isEqualToIgnoreCase(clientId);
}