6 Replies Latest reply on Dec 19, 2012 8:50 AM by garethahealy

    ActiveMQ consumer id

    garethahealy

      I have a camel route which reads off activemq and updates an RPC SOAP interface. This interface is locked down to one call per user - you have to pass in the username/password per call which is then checked server side, this is a 3rd party interface.

       

      Obviously, our queues have multiple consumers setup thus i've implemented some locking to stop multiple calls per user to the soap interface. A colleague suggested the idea of the activemq consumer telling the soap interface who it was, and from that we could then retrieve a username/password from config and make the call. i.e.:

       

      exchange.getFromEndpoint().getMyUniqueId() returns Consumer1 and so on. Thus we get the Consumer1 username/password, make the SOAP call and then carry on. This i think would stop the Consumer1 username/password being in use multiple times.

       

      Hope that makes sense!

        • 1. Re: ActiveMQ consumer id
          garethahealy

          I noticed on the web console (http://localhost:8161/admin/queueConsumers.jsp?JMSDestination=ROUTE) that theres a session id. I think i might be able to use this...just need to figure out how to get it from my camel route.

          • 2. Re: ActiveMQ consumer id
            davsclaus

            If you use JMS message groups, then any consumer is processing the message within same group in sequence.

             

            So if the username is the message group, then the username "foo" is only processed by one consumer, even if you have multiple routes / cluster etc.

             

            http://activemq.apache.org/message-groups.html

            http://activemq.apache.org/how-do-i-preserve-order-of-messages.html

            • 3. Re: ActiveMQ consumer id
              garethahealy

              Claus,

               

              I think this might fit my situation, i'll go down this route and see if it works. It was the managing of the groups i didnt want to do. i.e.: the producer has to cycle through each "group" and set it on the outbond message.

               

              I'd hoped there was a key/property that the camel route could access on the consumer, which would tell it which consumer the message had came off.

               

              i.e.: Message1 was handled by Consumer1

              Message2 was handled by Consumer2

              Message3 was handled by Consumer1

              Message4 was handled by Consumer2

               

              and so on...

              • 4. Re: ActiveMQ consumer id
                davsclaus

                On the Camel Exchange API there is a getFromEndpoint, which tells you which endpoint created the Exchange, and thus also the consumer (eg as thats the endpoint created the consumer).

                 

                Also if you assign route ids then you can get the getFromRouteId as well. See the javadoc

                http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/Exchange.html

                • 5. Re: ActiveMQ consumer id
                  garethahealy

                  From what you are saying, it doesnt sound like theres a quick/easy inbuilt way. Below is an example of what i am doing, just to better clarify the problem.

                   

                  Route that produces the message:

                   

                   

                  Route that consumes the message:

                  RouteDefinition route = new RouteDefinition();

                  route.from("activemq:Consumer.VendaUAT.VirtualTopic.sendStockToExternalServices")

                                          .unmarshal(jsonUnmarshalInventoryChangedRequest)

                  .doStuff();

                   

                  ActiveMQ Component setup:

                  ConcurrentConsumer2 has Message2

                  ...

                   

                  I'll have a play around with groups anyways, and report back with what i end up with.

                  • 6. Re: ActiveMQ consumer id
                    garethahealy

                    For anyone whos interested. I went with the following solution:

                     

                    ();

                    Integer i = 0;

                    while (i < consumerCount) {

                        String stockFrontendNameConsumer = "VendaUAT_Consumer" + i;

                        String consumerEndpointUri = "activemq:" + stockFrontendNameConsumer;

                         

                        RouteDefinition consumerRoute = new RouteDefinition();

                        consumerRoute.from(consumerEndpointUri)

                                    .unmarshal(jsonUnmarshalInventoryChangedRequest)

                                    .beanRef("addStock_Venda", "sendToFrontend")

                                    .routeId(stockFrontendNameConsumer);

                         

                        context.addRouteDefinition(consumerRoute);

                        consumerEndpoints.add(consumerEndpointUri);

                     

                        i++;

                    }

                     

                    RouteDefinition entryRoute = new RouteDefinition();

                    entryRoute.from("activemq:Consumer.VendaUAT.VirtualTopic.sendStockToExternalServices")

                            .loadBalance()

                            .roundRobin()

                            .to(consumerEndpoints.toArray(new String{consumerEndpoints.size()}))

                            .routeId("stockUpdateFor_VendaUAT");

                     

                    context.addRouteDefinition(entryRoute);

                     

                    This means i end up with a number of individual queues, that are load balanced.

                     

                    Cheers.