例如,Apache ActiveMQ 支持像 Camel 路由中的主题/队列这样的源的通配符。
文档显示可以递归匹配这样的模式:
PRICE.STOCK.>
火柴
PRICE.STOCK.FR.SOUTH
PRICE.STOCK.FR
PRICE.STOCK.UK.NORTH.MANCHESTER
等等...
但是在我的示例中,我必须匹配类似但以特定单词结尾的内容。
package org.ruffp.camel.quartz;
import org.apache.activemq.camel.component.ActiveMQComponent;
import org.apache.camel.CamelContext;
import org.apache.camel.EndpointInject;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
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 FromWildcardRouteTest extends CamelTestSupport {
@Produce(uri = "activemq:topic:TEST.START.NB.1.Mirrored")
private ProducerTemplate start1;
@Produce(uri = "activemq:topic:TEST_START.Mirrored")
private ProducerTemplate start2;
@EndpointInject(uri = "mock:DEST")
private MockEndpoint end;
@Test
public void testRoute() throws Exception {
resetMocks();
end.expectedMessageCount(2);
start1.sendBody("test-1");
start2.sendBody("test-2");
assertMockEndpointsSatisfied();
}
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
//@formatter:off
from("activemq:topic:*(.>).Mirrored").routeId("mirrored")
.setProperty("TEST_DESC").body()
.to(end);
//@formatter:on
}
};
}
@Override
protected CamelContext createCamelContext() throws Exception {
CamelContext context = super.createCamelContext();
String amqUrl = "vm://localhost?broker.persistent=false";
log.info("Creating Camel Context for AMQ: '{}'", amqUrl);
context.addComponent("activemq",
ActiveMQComponent.activeMQComponent(amqUrl));
return context;
}
}
我想要捕捉的主题要么包含零个点,要么包含多个点,.
一个或多个下划线_
,并以.Mirrored
.
一些示例(均以 为前缀activemq:topic:
):
- TEST_INBOUND.Mirrored -> catched
- UK.NORTH.TEST_INBOUND.Mirrored -> catched
- FR.SOUTH.TEST_INBOUND.Mirrored -> catched
- CH.TEST_INBOUND.Mirrored -> catched
- TEST_INBOUND -> not catched
- TEST_INBOUND_Mirrored -> not catched