Camel 路由中使用的 ActiveMQ 递归通配符

ActiveMQ recursive wildcards used in Camel route

Apache ActiveMQ 支持源的通配符,例如 Camel 路由中的 topics/queues。

The documentation 表明可以递归匹配这样的模式:

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

您不能像您尝试的那样通过复杂的模式进行匹配,只有记录在案队列名称的开头可以匹配,然后使用 > 或 * 作为通配符。仅此而已,不再支持。

请注意,在文档末尾,有一个插件可用于您的情况,如下所示:

<plugins>
   .....
   <destinationPathSeparatorPlugin pathSeparator="_" />
</plugins>

在这种情况下,通配符也适用于下划线。