切换Camel中的窃听队列

时间:2015-05-14 11:02:12

标签: java apache-camel

目前,我正在使用choice()来确定是否已打开窃听队列的切换。在Camel中有更好的方法吗?

这是我的代码:

package routes;

import org.apache.camel.builder.RouteBuilder;

public class MyRoute extends RouteBuilder{


    private String inQueue;
    private String outQueue;
    private String wiretapQueue;
    private Boolean shouldWiretap;

    @Override
    public void configure(){

        from(inQueue)
                .choice()
                    .when(constant(shouldWiretap))
                        .wireTap(wiretapQueue)
                    .end()
                .end()
                .to(outQueue);
    }

    public void setInQueue(String inQueue) {
        this.inQueue = inQueue;
    }

    public void setOutQueue(String outQueue) {
        this.outQueue = outQueue;
    }

    public void setWiretapQueue(String wiretapQueue) {
        this.wiretapQueue = wiretapQueue;
    }

    public void setShouldWiretap(boolean shouldWiretap) {
        this.shouldWiretap = shouldWiretap;
    }
}

我还写了一个简单的测试:

package routes;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;


public class MyRouteTest extends CamelTestSupport {


    protected RouteBuilder[] createRouteBuilders() throws Exception {
        MyRoute route= new MyRoute();
        route.setInQueue("direct:in");
        route.setOutQueue("mock:out");
        route.setWiretapQueue("mock:wiretap");
        route.setShouldWiretap(false);
        return new RouteBuilder[] {route};
    }

    @Test
    public void testRouteWithoutWiretap() throws InterruptedException {
        template.sendBody("direct:in", "test");

        MockEndpoint mockOut = getMockEndpoint("mock:out");
        mockOut.expectedMessageCount(1);
        mockOut.assertIsSatisfied();

        MockEndpoint mockWiretap = getMockEndpoint("mock:wiretap");
        mockWiretap.expectedMessageCount(0);
        mockWiretap.assertIsSatisfied();
    }
}

1 个答案:

答案 0 :(得分:1)

很难说什么是“更好的方式”。可以使用message filter确保缩短时间。

例如:

public class MyRouteTest extends CamelTestSupport {

    @Test
    public void test() throws InterruptedException {
        template.sendBody("direct:in", "testBody");

        MockEndpoint wiretapEndpoint = getMockEndpoint("mock:wiretap");
        wiretapEndpoint.expectedMessageCount(0);
        wiretapEndpoint.assertIsSatisfied();

        MockEndpoint outEndpoint = getMockEndpoint("mock:out");
        outEndpoint.expectedMessageCount(1);
        outEndpoint.assertIsSatisfied();
    }

    @Override
    protected RouteBuilder createRouteBuilder() {
        return new RouteBuilder() {

            boolean shouldWireTap = false;

            @Override
            public void configure() {
                from("direct:in")
                    .filter().constant(shouldWireTap)
                        .wireTap("mock:wiretap").end()
                    .end()
                .to("mock:out");
            }
        };
    }
}