0

複数のエントリポイント(サーブレットと直接)を持つルートがあります。サーブレットを介してアクティブ化されると、特定の作業を実行する必要があります。この作業は、サーブレット要求に対して実行する必要があります(悪意のあるアクターが存在する場合でも)。直接の交換の場合、この作業を行ってはなりません。コードの例を次に示します。

// 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 に答える