ActiveMQ

1、簡介。ActiveMQ 是Apache出品,最流行的,能力強勁的開源消息總線。

⒈ 多種語言和協議編寫客戶端。語言: Java,C,C++,C#,Ruby,Perl,Python,PHP。應用協議: OpenWire,Stomp REST,WS Notification,XMPP,AMQP

⒉ 完全支持JMS1.1和J2EE 1.4規範 (持久化,XA消息,事務)

⒊ 對Spring的支持,ActiveMQ可以很容易內嵌到使用Spring的系統裏面去,而且也支持Spring2.0的特性

⒋ 通過了常見J2EE服務器(如 Geronimo,JBoss 4,GlassFish,WebLogic)的測試,其中通過JCA 1.5 resource adaptors的配置,可以讓ActiveMQ可以自動的部署到任何兼容J2EE 1.4 商業服務器上

⒌ 支持多種傳送協議:in-VM,TCP,SSL,NIO,UDP,JGroups,JXTA⒍ 支持通過JDBC和journal提供高速的消息持久化⒎ 從設計上保證了高性能的集羣,客戶端-服務器,點對點⒏ 支持Ajax

2、ActiveMQ的消息形式

ActiveMQ對於消息的傳遞有兩種類型:

1). 一種是點對點的,即一個生產者和一個消費者一一對應。

2).發佈訂閱模式,即一個生產者產生消息並進行發送後,可以由多個消費者進行接受。

3).JMS定義 了五種不同的消息正文格式,以及調用的消息類型,允許你發送並接受以一些不同的形式的數據,提供現有消息格式的一些級別的兼容性。

* StreamMessge : Java原始值的數據流

* MapMessage : 一套名稱一對值

* TextMessage : 一個字符串對象

* ObjectMessage : 一個序列化的Java對象

* ByteMessage : 一個字節的數據流

3、ActiveMQ的使用方法

1).queue 生產者消費者一對一

/**
 * Producer
 * 生產者
 */
@Test
public void testQueueProducer() throws Exception {
	//1.創建一個連接工廠對象ConnectionFactory對象。需要指定mq服務的ip及端口
	ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.168:61616");
	//2.使用ConnectionFactory創建一個連接Connection對象
	Connection connection = connectionFactory.createConnection();
	//3.開啓連接。調用Connection對象的start方法
	connection.start();
	//4.使用Connection對象創建一個Session對象
	//第一個參數是是否開啓事務,一般不使用事務。保證數據的最終一致,可以使用消息隊列實現。
	//如果第一個參數爲true,第二個參數自動忽略。如果不開啓事務false,第二個參數爲消息的應答模式。一般自動應答就可以。		
	Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
	//5.使用Session對象創建一個Destination對象,兩種形式queue、topic。現在應該使用queue
	//參數就是消息隊列的名稱
	Queue queue = session.createQueue("test-queue");
	//6.使用Session對象創建一個Producer對象
	MessageProducer producer = session.createProducer(queue);
	//7.創建一個TextMessage對象
	/*TextMessage textMessage = new ActiveMQTextMessage();
	textMessage.setText("hello activemq");*/
	TextMessage textMessage = session.createTextMessage("hello activemq1111");
	//8.發送消息
	producer.send(textMessage);
	//9.關閉資源
	producer.close();
	session.close();
	connection.close();		
}

/* *
 * 消費者
 * Consumer
 */
@Test
public void testQueueConsumer() throws Exception {
	//創建一個連接工廠對象
	ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.168:61616");
	//使用連接工廠對象創建一個連接
	Connection connection = connectionFactory.createConnection();
	//開啓連接
	connection.start();
	//使用連接對象創建一個Session對象
	Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
	//使用Session創建一個Destination,Destination應該和消息的發送端一致。
	Queue queue = session.createQueue("test-queue");
	//使用Session創建一個Consumer對象
	MessageConsumer consumer = session.createConsumer(queue);
	//向Consumer對象中設置一個MessageListener對象,用來接收消息
	consumer.setMessageListener(new MessageListener() {
			
		@Override
		public void onMessage(Message message) {
			//取消息的內容
			if (message instanceof TextMessage) {
				TextMessage textMessage = (TextMessage) message;
				try {
					String text = textMessage.getText();
					//打印消息內容
					System.out.println(text);
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}				
		}
	});
	//系統等待接收消息
	/*while(true) {
		Thread.sleep(100);
	}*/
	System.in.read();
	//關閉資源
	consumer.close();
	session.close();
	connection.close();
}

2).topic 生產者消費者一對多(廣播模式)

 /* Producer
 * 生產者
 */
 @Test
 public void testTopicProducer() throws Exception {
	//創建一個連接工廠對象
	ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.168:61616");
	//創建連接
	Connection connection = connectionFactory.createConnection();
	//開啓連接
	connection.start();
	//創建Session
	Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
	//創建Destination,應該使用topic
	Topic topic = session.createTopic("test-topic");
	//創建一個Producer對象
	MessageProducer producer = session.createProducer(topic);
	//創建一個TextMessage對象
	TextMessage textMessage = session.createTextMessage("hello activemq topic");
	//發送消息
	producer.send(textMessage);
	//關閉資源
	producer.close();
	session.close();
	connection.close();
}

/**
 * Consumer
 * 消費者
 */
 @Test
 public void testTopicConsumser() throws Exception {
	//創建一個連接工廠對象
	ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.168:61616");
	//使用連接工廠對象創建一個連接
	Connection connection = connectionFactory.createConnection();
	//開啓連接
	connection.start();
	//使用連接對象創建一個Session對象
	Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
	//使用Session創建一個Destination,Destination應該和消息的發送端一致。
	Topic topic = session.createTopic("test-topic");
	//使用Session創建一個Consumer對象
	MessageConsumer consumer = session.createConsumer(topic);
	//向Consumer對象中設置一個MessageListener對象,用來接收消息
	consumer.setMessageListener(new MessageListener() {
			
		@Override
		public void onMessage(Message message) {
			//取消息的內容
			if (message instanceof TextMessage) {
				TextMessage textMessage = (TextMessage) message;
				try {
					String text = textMessage.getText();
					//打印消息內容
					System.out.println(text);
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}				
		}
	});
	//系統等待接收消息
	/*while(true) {
		Thread.sleep(100);
	}*/
	System.out.println("topic消費者1.。。。");
	System.out.println("topic消費者2.。。。");
	System.out.println("topic消費者3.。。。");
	System.in.read();
    //關閉資源
	consumer.close();
	session.close();
	connection.close();
}

3、ActiveMQ整合Spring

1).因爲整合Spring需要用到JMS中的jmsTemplate,所以要加入Spring的jar包以及jsmTemplate的Jar包

2).Spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
	
	<!-- JMS服務廠商提供的ConnectionFactory -->
	<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<constructor-arg name="brokerURL" value="tcp://192.168.25.168:61616"/>
	</bean>
	<!-- spring對象ConnectionFactory的封裝 -->
	<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
		<property name="targetConnectionFactory" ref="targetConnectionFactory"></property>
	</bean>
	<!-- 配置JMSTemplate -->
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory" ref="connectionFactory"/>
	</bean>
	<!-- 配置消息的Destination對象 -->
	<bean id="test-queue" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg name="name" value="test-queue"></constructor-arg>
	</bean>
	<bean id="itemAddtopic" class="org.apache.activemq.command.ActiveMQTopic">
		<constructor-arg name="name" value="item-add-topic"></constructor-arg>
	</bean>
	<!-- 配置消息的接收者 -->
	<bean id="myMessageListener" class="com.taotao.search.listener.MyMessageListener"/>
	<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="destination" ref="test-queue" />
		<property name="messageListener" ref="myMessageListener" />
	</bean>
	<bean id="itemAddMessageListener" class="com.taotao.search.listener.ItemAddMessageListener"/>
	<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="destination" ref="itemAddTopic" />
		<property name="messageListener" ref="itemAddMessageListener" />
	</bean>
</beans>
3).編寫監聽ActiveMQ的類MyMessageListener、需要實現MessageListener接口,完成後將這個Bean配置到Spring容器中。

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.xml.soap.Text;

/**
 * 接收Activemq發送的消息
 * <p>Title: MyMessageListener</p>
 * <p>Description: </p> 
 * @version 1.0
 */
public class MyMessageListener implements MessageListener {

	@Override
	public void onMessage(Message message) {
		try {
			//接收到消息
			TextMessage textMessage = (TextMessage) message;
			String text = textMessage.getText();
			System.out.println(text);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}



發佈了75 篇原創文章 · 獲贊 8 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章