ActiveMQ Artemis 广播只向组内的一个服务器发送消息,可以通过以下步骤和示例代码解决:
import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
String brokerUrl = "tcp://localhost:61616";
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUrl);
import javax.jms.Connection;
import javax.jms.JMSException;
Connection connection = connectionFactory.createConnection();
connection.start();
import javax.jms.Session;
boolean transacted = false;
int acknowledgeMode = Session.AUTO_ACKNOWLEDGE;
Session session = connection.createSession(transacted, acknowledgeMode);
import javax.jms.Topic;
String topicName = "yourTopicName";
Topic topic = session.createTopic(topicName);
import javax.jms.MessageProducer;
import javax.jms.TextMessage;
MessageProducer producer = session.createProducer(topic);
TextMessage message = session.createTextMessage("Hello World!");
producer.send(message);
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
MessageConsumer consumer = session.createConsumer(topic);
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
try {
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("Received message: " + textMessage.getText());
}
} catch (JMSException e) {
e.printStackTrace();
}
}
});
通过以上步骤和示例代码,可以实现 ActiveMQ Artemis 广播只向组内的一个服务器发送消息,并在其他服务器上接收该消息。