bin/macosx/activemq start 或 bin/activemq.bat start);https://localhost:8161/,默認帳號密碼 admin/admin;activemq-home/lib 下所有 JAR 包。// Sender
ConnectionFactory cf = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
Connection conn = cf.createConnection();
conn.start();
Session session = conn.createSession(true, Session.AUTO_ACKNOWLEDGE);
Destination dest = session.createQueue("JMeterQueue");
MessageProducer producer = session.createProducer(dest);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
for (int i=1; i<=5; i++) {
TextMessage msg = session.createTextMessage("ActiveMq send " + i);
producer.send(msg);
}
session.commit();
conn.close();
// Receiver (同理創建 Consumer, 使用 receive 或 MessageListener)
QueueConnectionFactory、QueueSession、Queue、QueueSender/QueueReceiver;mq.p2p.queue);TopicConnectionFactory、TopicSession、Topic、TopicPublisher/TopicSubscriber;mq.topic;public class QueueSender {
static final String BROKER_URL = "tcp://localhost:61616";
static final String DESTINATION = "mq.p2p.queue";
public static void run() throws Exception {
QueueConnectionFactory factory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, BROKER_URL);
QueueConnection conn = factory.createQueueConnection();
conn.start();
QueueSession session = conn.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(DESTINATION);
QueueSender sender = session.createSender(queue);
sender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
for (int i=0; i<5; i++) {
sender.send(session.createTextMessage("發送消息第" + (i+1) + "條"));
}
session.commit();
session.close(); conn.close();
}
}
createDurableSubscriber)、事務控制、消息確認模式(CLIENT_ACKNOWLEDGE);ActiveMQ 作為成熟開源消息中間件,掌握其基礎 API 與兩大核心模式,是構建異步解耦、削峰填谷、分布式事務系統的敲門磚。