How to create multiple instances of activemq theme subscribers using virtual recipients?

I have a publisher who pushes posts to the topic. I have several subscribers, each of which performs a different task when they consume a message from this topic. Now I want my system to scale to multiple instances of the same process on different hosts / same host. for example, I want to run multiple copies of my application A on different hosts, so that if one instance of A is slow, other instances can pull out subsequent messages and move forward. I found out that this is possible using virtual directions. I followed these steps - http://activemq.apache.org/virtual-destinations.html

But how do I set up multiple subscribers on the same topic with the same client ID? when I try to do this, I get errors. when I try differently, it does not work. can someone help?

I usually start the subscriber by following these steps:

ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, ActiveMQConnection.DEFAULT_BROKER_URL;); activeMQConnection = connectionFactory.createConnection(); activeMQConnection.setClientID("subscriber1"); activeMQConnection.setExceptionListener(exceptionListener); activeMQSession = activeMQConnection.createSession(false, Session.CLIENT_ACKNOWLEDGE); activeMQTopic = activeMQSession.createTopic("myTopic"); activeConsumer = activeMQSession.createDurableSubscriber(activeMQTopic, "myTopic"); activeConsumer.setMessageListener(messageListener); activeMQConnection.start(); 

when I try to create a second subscriber and pass the topic name as "VirtualTopic.myTopic", nothing happens.

thanks

+4
source share
2 answers

The virtual theme feature is very simple and powerful enough as soon as you understand it.

  • When using virtual themes - there is no need for reliable consumers. This is because for each client you will receive a regular queue instance. If you have 5 clients (applications A, B, C, D, E), you will receive 5 queues created and filled with a copy of messages each time a message is sent to a virtual topic.

  • In fact, this is a limitation of a long-term consumer - that only one connection to the client interface is allowed. Being a regular queue, you can create as many consumers as you want, and the queue will guarantee that 1 message will be received by only one consumer. Therefore, if you have application A that takes 1 minute to process, you can create 5 instances that listen on the same queue. When you post 5 messages in 1 second, each of your applications will receive its own message for processing.

  • There are insufficiently documented requirements that are not intuitive. For the virtual theme to work, you need

    • Use VirtualTopic. in your topic name, for example VirtualTopic.Orders (this prefix can be customized)
    • Use Consumer. in the queue name. Like Consumer.ApplicationA.VirtualTopic.Orders , where ApplicationA is actually your client identifier
    • Use regular subscribers, not durable for the queue above.

Example:

 string activeMqConsumerTopic = "Consumer.AmqTestConsumer.VirtualTopic.Orders"; IQueue queue = SessionUtil.GetQueue(session, activeMqConsumerTopic); IMessageConsumer consumer = session.CreateConsumer(queue); 

A queue is created automatically when the first consumer instance subscribes to it. From now on, all messages sent to the topic are duplicated / copied to all related queues.

Hope this helps.

+5
source

Virtual themes are the answer for you. However, you need to define a naming standard for all virtual topic queues. Here is the answer to this question:

Virtual themes help in the following perspectives: 1. Download message balancing 2. Fast subscriber failure 3. Reuse the same Factory connection for different manufacturers and consumers. (Durable Subscribers needs a unique JMS Client identifier and cannot be reused for any other manufacturer or consumer)

here's how to do it, the below example creates the VTCON prefix. *. Therefore, each queue with this prefix and topic name at the end will trigger a message.

<virtualDestinations> <virtualTopic name="TEST.TP01" prefix="VTCON.*." selectorAware="false"/> </virtualDestinations>

http://workingwithqueues.blogspot.com/2012/05/activemq-virtual-topics-or-virtual.html

+1
source

Source: https://habr.com/ru/post/1388996/


All Articles