最近項目中可能會用到隊列,特網上一番查找,發現Apache的ActiveMQ還不錯,便嘗試了一把。
一、下載Apache ActiveMQ
https://repository.apache.org/content/repositories/snapshots/org/apache/activemq/apache-activemq/5.10-SNAPSHOT/
最新的穩定版是5.10:
二、建立消費者類
package com.test;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ConsumerTool implements MessageListener {
private String user = ActiveMQConnection.DEFAULT_USER;
private String password = ActiveMQConnection.DEFAULT_PASSWORD;
private String url = ActiveMQConnection.DEFAULT_BROKER_URL;
private String subject = "TOOL.DEFAULT";
private Destination destination = null;
private Connection connection = null;
private Session session = null;
private MessageConsumer consumer = null;
// 初始化
private void initialize() throws JMSException, Exception {
//連接工廠是用戶創建連接的對象,這裏使用的是ActiveMQ的ActiveMQConnectionFactory根據url,username和password創建連接工廠。
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
user, password, url);
//連接工廠創建一個jms connection
connection = connectionFactory.createConnection();
//是生產和消費的一個單線程上下文。會話用於創建消息的生產者,消費者和消息。會話提供了一個事務性的上下文。
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //不支持事務
//目的地是客戶用來指定他生產消息的目標還有他消費消息的來源的對象,兩種消息傳遞方式:點對點和發佈/訂閱
destination = session.createQueue(subject);
//會話創建消息的生產者將消息發送到目的地
consumer = session.createConsumer(destination);
}
// 消費消息
public void consumeMessage() throws JMSException, Exception {
initialize();
connection.start();
System.out.println("Consumer:->Begin listening...");
// 開始監聽
consumer.setMessageListener(this);
//Message message = consumer.receive();
}
// 關閉連接
public void close() throws JMSException {
System.out.println("Consumer:->Closing connection");
if (consumer != null)
consumer.close();
if (session != null)
session.close();
if (connection != null)
connection.close();
}
// 消息處理函數
@Override
public void onMessage(Message message) {
try {
if (message instanceof TextMessage) {
TextMessage txtMsg = (TextMessage) message;
String msg = txtMsg.getText();
System.out.println("Consumer:->Received: " + msg);
} else {
System.out.println("Consumer:->Received: " + message);
}
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
三、建立生產者類
package com.test;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ProducerTool {
private String user = ActiveMQConnection.DEFAULT_USER;
private String password = ActiveMQConnection.DEFAULT_PASSWORD;
private String url = ActiveMQConnection.DEFAULT_BROKER_URL;
private String subject = "TOOL.DEFAULT";
private Destination destination = null;
private Connection connection = null;
private Session session = null;
private MessageProducer producer = null;
// 初始化
private void initialize() throws JMSException, Exception {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
user, password, url);
connection = connectionFactory.createConnection();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue(subject);
producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
}
// 發送消息
public void produceMessage(String message) throws JMSException, Exception {
initialize();
TextMessage msg = session.createTextMessage(message);
connection.start();
System.out.println("Producer:->Sending message: " + message);
producer.send(msg);
System.out.println("Producer:->Message sent complete!");
}
// 關閉連接
public void close() throws JMSException {
System.out.println("Producer:->Closing connection");
if (producer != null)
producer.close();
if (session != null)
session.close();
if (connection != null)
connection.close();
}
}
四、編寫測試類
package com.test;
import javax.jms.JMSException;
import org.apache.activemq.ActiveMQConnection;
public class Test {
/**
* @param args
*/
public static void main(String[] args) throws JMSException, Exception {
// TODO Auto-generated method stub
ConsumerTool consumer = new ConsumerTool();
ProducerTool producer = new ProducerTool();
System.out.println(ActiveMQConnection.DEFAULT_BROKER_URL+"------------");
// 開始監聽
consumer.consumeMessage();
// 延時500毫秒之後發送消息
Thread.sleep(500);
for (int i = 0; i < 10; i++) {
producer.produceMessage("Hello, world! " + i);
}
producer.close();
// 延時500毫秒之後停止接受消息
Thread.sleep(500);
consumer.close();
}
}
在運行測試類之前,需要啓動Apache activeMQ服務,如下圖所示,雙擊窮的那個activemq.bat啓動MQ消息服務:
啓動後,在瀏覽器中輸入http://127.0.0.1:8161/,如能訪問,則說明啓動成功:
五、運行測試類中的main方法,變可以看到運行結果: