问题描述
- ActiveMQ怎么实现多个生产者向一个队列目标发送信息?求教,最好有代码demo
-
ActiveMQ怎么实现多个生产者向一个队列目标发送信息?求教,最好有代码demo
解决方案
package org.ygy.mq.lesson02;
import java.util.Date;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class SimpleQueueProducer {
public void produce() throws Exception {
ConnectionFactory factory = new ActiveMQConnectionFactory();
Connection connection = factory.createConnection();
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("queue_lesson");
MessageProducer producer = session.createProducer(destination);
for(int i=0; i<3; i++ ) {
TextMessage message = session.createTextMessage(new Date() + " ");
producer.send(message);
Thread.sleep(1000);
}
connection.close();
}
}
时间: 2025-01-30 00:21:51