0

我有一个带有多个入口点(servlet 和 direct)的路由。通过servlet激活时需要做一定的工作。必须为 servlet 请求完成这项工作(即使存在不良行为者)。如果是通过直接进行的交流,则不得进行此项工作。这是代码中的示例:

// 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?
    }

}
4

2 回答 2

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")
于 2012-11-16T14:28:13.393 回答
0

我受到另一篇文章的评论的启发。这是我的解决方案:

// 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);
    }

}
于 2012-11-15T20:06:49.133 回答