2015-05-14 88 views
1

當前我正在使用choice()來確定竊聽隊列的切換是否打開。在駱駝中有更好的方法嗎?切換駱駝竊聽隊列

這是我的代碼:

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

很難說什麼是 「更好的方式」。可以使用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"); 
      } 
     }; 
    } 
} 
+1

首先,謝謝。在這種情況下,當souldWiretp標誌爲false時,消息不會進入出隊列。 – panagdu

+0

對不起,當然需要雙重結局。請參閱我的上次編輯 – cslysy

+0

感謝您的回覆和更新。現在看起來不錯:) – panagdu