Search code examples
apache-camelactivemq-classicwildcardjms-topic

ActiveMQ recursive wildcards used in Camel route


Apache ActiveMQ supports wildcards for a source like topics/queues in a Camel route for instance.

The documentation shows that there is the possibility to matche recursively a pattern like this:

PRICE.STOCK.> 

Matches

PRICE.STOCK.FR.SOUTH
PRICE.STOCK.FR
PRICE.STOCK.UK.NORTH.MANCHESTER

and so on...

However in my example I have to match something similar but ending with a specific word.

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;
        }
    }

The topics I want to catch are either containing zero or many dots . one or many underscores _ and finishing by .Mirrored.

Some examples (all prefixed by 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

Solution

  • You cannot match by complex pattern such as you try, only as documented that the starting of the queue name can match and then use > or * as wildcards. That's it nothing more is supported.

    Note that at the end of the doc, there is a plugin that can be used in your case like this:

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

    and in that case the wildcards would work with underscore as well.