如何查找多播队列?
How do I lookup a multicast queue?
配置如下 broker.xml
中的 topic/queue:
<address name="Topic1">
<multicast>
<queue name="Queue1"/>
<queue name="Queue2"/>
</multicast>
</address>
如何为 Queue1/Queue2 创建发送消息的生产者?我正在使用 ActiveMQ Artemis 2.6.3。
我使用以下方式创建连接工厂、连接和队列查找
Hashtable<String, Object> properties = new Hashtable<>();
properties.put("connectionFactory.ConnectionFactory", (tcp://localhost:61618,tcp://localhost:61616)?ha=true&retryInterval=3000&reconnectAttempts=-1&initialConnectAttempts=10&maxRetryInterval=3000&clientFailureCheckPeriod=1000);
properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory");
InitialContext jndiContext = new InitialContext(properties);
ConnectionFactory connFactory = (ConnectionFactory) jndiContext.lookup(connectionFactory);
Connection connection = connFactory.createConnection(userName, password);
Session session = connection.createSession(true,javax.jms.Session.AUTO_ACKNOWLEDGE);
//Using following way looking up Multicast Queue2 on Address Topic1
Destination dest = new ActiveMQTopic("Topic1::Queue2");
MessageProducer producer = session.createProducer(dest);
producer.send(message, Message.DEFAULT_DELIVERY_MODE, Message.DEFAULT_PRIORITY, msgTTL);
更改上述代码并尝试发送消息后,消息不会发送到队列“Queue2”
一般来说,您要根据自己的需要选择正确的路由类型。选项是:
- anycast:提供点对点语义(例如 JMS 队列);发送到该地址的邮件仅路由到 一个 任播队列
- 多播:提供发布-订阅语义(例如 JMS 主题);发送到该地址的消息被路由到所有个多播队列
您有一个名为 Topic
的地址,其中包含 2 个多播队列 - Queue1
和 Queue2
。因此,发送到 Topic
的消息将同时发送到 Queue1
和 Queue2
。如果您只想将消息发送到一个队列,您可以考虑使用不同的配置(例如,具有任播队列的地址)。
但是,如果您发现出于某种原因确实需要现有配置,那么您可以通过语法 <address>::<queue>
使用完全限定的队列名称(即 FQQN)发送消息。我看到您已经在尝试这样做,例如:
Destination dest = new ActiveMQTopic("Topic1::Queue2");
但是,您使用的 ActiveMQ Artemis 版本不支持生产者的 FQQN。要使用此功能,我建议您至少升级到 2.7.0 或最好升级到 the latest release which is 2.17.0.
与@justin 相同的解决方案 spring 和 artemis 2.7.0:
JmsTemplate jmsTemplate = new JmsTemplate(new
ActiveMQConnectionFactory("tcp://localhost:61616"));
//this is the important part
jmsTemplate.setPubSubDomain(true);
jmsTemplate.convertAndSend("Topic1::Queue2", "hello");
配置如下 broker.xml
中的 topic/queue:
<address name="Topic1">
<multicast>
<queue name="Queue1"/>
<queue name="Queue2"/>
</multicast>
</address>
如何为 Queue1/Queue2 创建发送消息的生产者?我正在使用 ActiveMQ Artemis 2.6.3。
我使用以下方式创建连接工厂、连接和队列查找
Hashtable<String, Object> properties = new Hashtable<>();
properties.put("connectionFactory.ConnectionFactory", (tcp://localhost:61618,tcp://localhost:61616)?ha=true&retryInterval=3000&reconnectAttempts=-1&initialConnectAttempts=10&maxRetryInterval=3000&clientFailureCheckPeriod=1000);
properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory");
InitialContext jndiContext = new InitialContext(properties);
ConnectionFactory connFactory = (ConnectionFactory) jndiContext.lookup(connectionFactory);
Connection connection = connFactory.createConnection(userName, password);
Session session = connection.createSession(true,javax.jms.Session.AUTO_ACKNOWLEDGE);
//Using following way looking up Multicast Queue2 on Address Topic1
Destination dest = new ActiveMQTopic("Topic1::Queue2");
MessageProducer producer = session.createProducer(dest);
producer.send(message, Message.DEFAULT_DELIVERY_MODE, Message.DEFAULT_PRIORITY, msgTTL);
更改上述代码并尝试发送消息后,消息不会发送到队列“Queue2”
一般来说,您要根据自己的需要选择正确的路由类型。选项是:
- anycast:提供点对点语义(例如 JMS 队列);发送到该地址的邮件仅路由到 一个 任播队列
- 多播:提供发布-订阅语义(例如 JMS 主题);发送到该地址的消息被路由到所有个多播队列
您有一个名为 Topic
的地址,其中包含 2 个多播队列 - Queue1
和 Queue2
。因此,发送到 Topic
的消息将同时发送到 Queue1
和 Queue2
。如果您只想将消息发送到一个队列,您可以考虑使用不同的配置(例如,具有任播队列的地址)。
但是,如果您发现出于某种原因确实需要现有配置,那么您可以通过语法 <address>::<queue>
使用完全限定的队列名称(即 FQQN)发送消息。我看到您已经在尝试这样做,例如:
Destination dest = new ActiveMQTopic("Topic1::Queue2");
但是,您使用的 ActiveMQ Artemis 版本不支持生产者的 FQQN。要使用此功能,我建议您至少升级到 2.7.0 或最好升级到 the latest release which is 2.17.0.
与@justin 相同的解决方案 spring 和 artemis 2.7.0:
JmsTemplate jmsTemplate = new JmsTemplate(new
ActiveMQConnectionFactory("tcp://localhost:61616"));
//this is the important part
jmsTemplate.setPubSubDomain(true);
jmsTemplate.convertAndSend("Topic1::Queue2", "hello");