【问题标题】:ActiveMQ Artemis 2.10.1 + JMS 2.0 - shared subscription not workingActiveMQ Artemis 2.10.1 + JMS 2.0 - 共享订阅不起作用
【发布时间】:2020-03-24 14:50:30
【问题描述】:

软件:

  • Apache Artemis 2.10.1
  • TomEE plus 8.0

我创建了一个有 2 个消费者的主题。每个消费者每个都有 1 个 MDB。我有一种主要的方法来进行配置和所有操作。

尽管我只发送一条消息并指定这是一个共享订阅,但两个 MDB 都会使用该消息。不知道如何解决这个问题。当然没有错误。但这不是我的代码所期望的功能。

@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(); 

我不确定我为什么要在 mytopic(订阅名称)上创建这个共享持久消费者。我尝试了各种不同的方法来完成我的任务。

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>

【问题讨论】:

  • 更新了问题。请检查。我不确定为什么要在 mytopic(订阅名称)上创建共享的持久 cnsumer。我正在尝试各种不同的方法来完成我的任务。是的,MDB1 正在听 BTopic,而 MDB2 正在听 CTopic。但是 Btopic 和 Ctopic 都在同一个地址队列上。更新我的实际问题。
  • 感谢您更好地表达我的问题,请帮我解决这个问题。

标签: activemq-artemis jms2


【解决方案1】:

您的配置多处不正确。

首先,JMS 主题由支持multicastaddress 实现,仅此而已。这在the ActiveMQ Artemis documentation 中注明。因此,您的broker.xml 应该有这个:

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

其次,您的 MDB 应该使用相同的订阅名称而不是 clientId 订阅 ATOPIC,例如:

@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")
})      

第三,您不应该在ATOPIC 上手动创建共享持久消费者。

【讨论】:

  • 进行了这些更改,仍然触发了两个 MDB
  • 我刚刚在 ActiveMQ Artemis 测试套件中测试了这个场景,一切都按预期工作。我的猜测是,与 TomEE 的集成中可能存在某些问题。您能否提供带有 TomEE 和 ActiveMQ Artemis 的minimal reproducible example(例如在 GitHub 上的项目中)?
  • 接收消息的代码:connectionFactory = new ActiveMQConnectionFactory(input.getUrl()); connection=(ActiveMQConnection)connectionFactory.createConnection(input.getUsername(), input.getPassword()); session = connection.createQueueSession(input.isTransacted(), Session.AUTO_ACKNOWLEDGE); connection.start(); session = connection.createSession(true, Session.SESSION_TRANSACTED); destination = session.createTopic(input.getQueueTopicName()); consumer = session.createConsumer(destination); rtn = consumer.receive(); session.commit();
  • 这段代码是干什么用的?你不是在使用 MDB 来接收消息吗?
  • 这是用于启动我的 java 程序的代码。即主要方法。还有一个问题。为什么在 Artemis 控制台中为我的地址设置生成了这么多 longValued 队列?我可以知道学习 Artemis 的所有好资源是什么。自从将近 2 周以来,我一直陷入困境。
猜你喜欢
  • 2021-06-09
  • 1970-01-01
  • 2019-09-30
  • 2020-11-18
  • 2020-10-20
  • 2014-10-06
  • 1970-01-01
  • 2016-07-11
  • 1970-01-01
相关资源
最近更新 更多