gpt4 book ai didi

java - 切换 Camel 中的窃听队列

转载 作者:行者123 更新时间:2023-11-30 08:12:09 25 4
gpt4 key购买 nike

目前,我正在使用 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();
}
}

最佳答案

很难说什么是“更好的方法”。使用 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");
}
};
}
}

关于java - 切换 Camel 中的窃听队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30235701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com