ActiveMQ 入門

 

1、介紹

ActiveMQ是一款流行的、強大的消息系統。

ActiveMQ是速度快,支持多種語客戶端、支持多種協議。

 

2、運行環境

5.10及以下版本 需要JRE1.6,5.10以上版本需要JRE1.7

 

3、ActiveMQ安裝

下載ActiveMQ,解壓到相應目錄下。

下載地址:http://activemq.apache.org/download.html

請根據自己的運行環境下載相應的版本。

 

4、運行ActiveMQ

windows 32位

cd [activemq_installdir]/bin/win32/activemq.bat

windows 64位

cd [activemq_installdir]/bin/win64/activemq.bat

 

linux 32位

cd [activemq_installdir]/bin/linux-x86-32/activemq start

或 cd [activemq_installdir]/bin/linux-x86-32/activemq console  可查看mq控制檯

linux 64位

cd [activemq_installdir]/bin/linux-x86-64/activemq start

或 cd [activemq_installdir]/bin/linux-x86-64/activemq console  可查看mq控制檯

 

ProducerTool.java  生產者

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);
		System.out.println(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();
		producer.send(msg);
	}

	// 關閉連接
	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();
	}
}


ConsumerTool.java 消費者

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.MessageListener;
import javax.jms.Message;
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();
	}

	// 消息處理函數
	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();
		}
	}
}



 

測試類:

TestProducer.java

import javax.jms.JMSException;

public class TestProducer {

	public static void main(String[] args) throws JMSException, Exception {
		ProducerTool producer = new ProducerTool();
		producer.produceMessage("Hello, world!");
		producer.close();
	}
	
}


TestConsumer.java

import javax.jms.JMSException;

public class TestConsumer {

	public static void main(String[] args) throws JMSException, Exception {
		ConsumerTool consumer = new ConsumerTool();
		consumer.consumeMessage();
		//暫停1分鐘,這樣1分鐘內的消息都可以接收
		Thread.sleep(60000);
		consumer.close();
	}
	
}


 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章