我有一个包含多个入口点(servlet和direct)的路由。当通过servlet激活时,它需要做一些工作。必须为servlet请求完成这项工作(即使存在坏的actor)。如果通过直接进行交换,则不得进行此项工作。以下是代码中的示例:
// In a Route Builder somewhere.
from("servlet:///myService").inOut("direct:myService");
from("direct:myService").process(new ConditionalProcessor());
// Implementation of processor above.
public class ConditionalProcessor implements Processor {
@Override
public void process(Exchange exchange) throws Exception {
if(comesFromServlet(exchange)){
// Logic for Servlet.
} else {
// Logic for direct and other.
}
}
/**
* Must return true if the exchange started as a request to the servlet.
* Otherwise must return false.
*
* @param exchange
* @return
*/
public boolean comesFromServlet(Exchange exchange){
// What goes here?
}
}
答案 0 :(得分:1)
Exchange上还有一个API,可以告诉您从哪个端点创建它。 http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Exchange.html#getFromEndpoint()
exchange.getFromEndpoint().getEndp
另一种方法是,如果您为路线分配ID,您也可以获得此
String fromRoute = exchange.getFromRouteId();
您可以使用.routeId(" myRouteId")为路线分配ID
from("servlet:///myService").routeId("myRouteId")
答案 1 :(得分:0)
我受到另一篇文章this comment的启发。这是我的解决方案:
// In a Route Builder somewhere.
from("servlet:///myService")
.setHeader(ConditionalProcessor.PROPERTY, constant(true))
.inOut("direct:myService");
from("direct:myService").process(new ConditionalProcessor());
// Implementation of processor above.
public class ConditionalProcessor implements Processor {
public static final String PROPERTY = "came.from.servlet";
@Override
public void process(Exchange exchange) throws Exception {
if(comesFromServlet(exchange)){
// Logic for Servlet.
} else {
// Logic for direct and other.
}
}
public boolean comesFromServlet(Exchange exchange){
return exchange.getProperty(PROPERTY, true, Boolean.class);
}
}