ActiveMQ Artemis 2.10.1 + JMS 2.0 - shared subscription not working

752 Views Asked by At

Software:

  • Apache Artemis 2.10.1
  • TomEE plus 8.0

I have created a topic with 2 consumers. Each consumer has 1 MDB each. I have one main method where I do the configuration and all.

Even though I am sending only one message and specified that this is a shared subscription the message is consumed by both the MDBs. Not sure how to resolve this. Of course there is no error. But this is not the expected functionality from my code.

@MessageDriven(name = "TOPICMDB1", activationConfig = { 
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
    @ActivationConfigProperty(propertyName = "destination", propertyValue = "BTOPIC"),
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "subscriptionName", propertyValue = "mytopic"),
    @ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "durable"),
    @ActivationConfigProperty(propertyName = "shareSubscriptions", propertyValue="true"),
    @ActivationConfigProperty(propertyName = "clientId", propertyValue = "MyClientId_1")
})
@MessageDriven(name = "TOPICMDB2", activationConfig = {
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
    @ActivationConfigProperty(propertyName = "destination", propertyValue = "CTOPIC"),
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"), 
    @ActivationConfigProperty(propertyName = "subscriptionName", propertyValue = "mytopic"),
    @ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "durable"),
    @ActivationConfigProperty(propertyName = "shareSubscriptions", propertyValue="true"),
    @ActivationConfigProperty(propertyName = "clientId", propertyValue = "MyClientId_2")
})      
connectionFactory = new ActiveMQConnectionFactory(input.getUrl());
connection = (ActiveMQConnection) connectionFactory.createConnection(input.getUsername(), input.getPassword());
session = connection.createTopicSession(input.isTransacted(), Session.AUTO_ACKNOWLEDGE);
connection.start();
session = connection.createTopicSession(true, Session.SESSION_TRANSACTED);  
destination = session.createTopic("ATOPIC");
consumer = session.createSharedDurableConsumer( (Topic) destination, "mytopic");
rtn = consumer.receive();
session.commit(); 

I am not sure why I am creating this shared durable consumer on mytopic (subscription name). I was trying all different ways to get my task accomplished.

tomee.xml:

<Resource id="ATOPIC"
          class-name="org.apache.activemq.artemis.api.jms.ActiveMQJMSClient"
          constructor="name"
          factory-name="createTopic"
          type="javax.jms.Topic">
   name=ATOPIC
</Resource>

broker.xml:

<address name = "ATOPIC">
   <multicast>
      <queue name = "BTOPIC"/>
      <queue name = "CTOPIC"/>
   </multicast>
</address>
1

There are 1 best solutions below

7
On

Your configuration is incorrect in multiple places.

First, a JMS topic is implemented by an address which supports multicast, nothing more. This is noted in the ActiveMQ Artemis documentation. Therefore, your broker.xml should have this:

<address name = "ATOPIC">
   <multicast/>
</address>

Second, your MDBs should be subscribing to ATOPIC using the same subscription name and no clientId, e.g.:

@MessageDriven(name = "TOPICMDB1", activationConfig = { 
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
    @ActivationConfigProperty(propertyName = "destination", propertyValue = "ATOPIC"),
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "subscriptionName", propertyValue = "mytopicSubscription"),
    @ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "Durable"),
    @ActivationConfigProperty(propertyName = "shareSubscriptions", propertyValue="true")
})
@MessageDriven(name = "TOPICMDB2", activationConfig = {
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
    @ActivationConfigProperty(propertyName = "destination", propertyValue = "ATOPIC"),
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"), 
    @ActivationConfigProperty(propertyName = "subscriptionName", propertyValue = "mytopicSubscription"),
    @ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "Durable"),
    @ActivationConfigProperty(propertyName = "shareSubscriptions", propertyValue="true")
})      

Third, you shouldn't be manually creating a shared durable consumer on ATOPIC.