Linux下ActiveMQ的搭建與使用

ActiveMQ的安裝

0、準備:進入http://activemq.apache.org/下載ActiveMQ

使用的版本是5.12.0

一、安裝環境:

  1. 需要jdk
  2. 安裝Linux系統。生產環境都是Linux系統。

二、安裝步驟

第一步: 把ActiveMQ 的壓縮包上傳到Linux系統。

[root@localhost ~]# ll
總用量 190600
drwxr-xr-x. 10 root root      4096 8月  10 2015 apache-activemq-5.12.0
-rw-r--r--.  1 root root  46560868 9月  13 02:13 apache-activemq-5.12.0-bin.tar.gz
drwxr-xr-x.  9 root root      4096 9月  10 08:06 apache-tomcat-7.0.47
-rw-r--r--.  1 root root   8234674 9月  10 08:05 apache-tomcat-7.0.47.tar.gz
drwxr-xr-x.  8 uucp  143      4096 3月  18 2014 jdk1.7.0_55
-rw-r--r--.  1 root root 139463702 9月  13 02:17 jdk-7u55-linux-i586.tar.gz
第二步:解壓縮。

[root@localhost ~]# tar -zxvf apache-activemq-5.12.0-bin.tar.gz

第三步:啓動。

使用bin目錄下的activemq命令啓動:

[root@localhost bin]# pwd
/root/apache-activemq-5.12.0/bin

[root@localhost bin]# ll
總用量 152
-rwxr-xr-x. 1 root root 21748 8月  10 2015 activemq
-rwxr-xr-x. 1 root root  6189 8月  10 2015 activemq-diag
-rw-r--r--. 1 root root 15958 8月  10 2015 activemq.jar
-rw-r--r--. 1 root root  4881 8月  10 2015 env
drwxr-xr-x. 2 root root  4096 9月  13 02:14 linux-x86-32
drwxr-xr-x. 2 root root  4096 9月  13 02:14 linux-x86-64
drwxr-xr-x. 2 root root  4096 9月  13 02:14 macosx
-rwxr-xr-x. 1 root root 83820 8月  10 2015 wrapper.jar
[root@localhost bin]# ./activemq start

關閉activemq

[root@localhost bin]# ./activemq stop

查看activemq啓動狀態:

[root@localhost bin]# ./activemq status

注意:如果ActiveMQ整合spring使用不要使用activemq-all-5.12.0.jar包。建議使用5.11.2

三、進入管理後臺:

http://192.168.25.129:8161/admin/                      用戶名:admin           密碼:admin

四、ActiveMQ的使用

添加依賴:

<!-- activemq -->
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-all</artifactId>
    <version>5.12.0</version>
</dependency>

測試案例:

package com.e3mall.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.junit.Test;

public class ActiveMqTest {
	/**
	 * 
	 * @Description: 點到點形式發送消息
	 * @author WSC
	 * @date 2019年9月25日
	 * @throws Exception
	 *             void
	 */
	@Test
	public void testQueueProducer() throws Exception {
		// 1、創建一個連接工廠對象ConnectionFactory,需要設置服務的ip和端口
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.129:61616");
		// 2、使用工廠對象創建一個Connection對象
		Connection connection = connectionFactory.createConnection();
		// 3、開啓連接,調用Connection的start()方法
		connection.start();
		// 4、使用Connection對象創建一個會話Session對象
		// 第一個參數:是否開啓事務。如果爲true:開啓事務,第二個參數無意義。如果爲false:關閉事務,一般不開啓事務
		// 第二個參數:會話應答模式。自動應答:Session.AUTO_ACKNOWLEDGE和手動應答:Session.CLIENT_ACKNOWLEDGE。一般爲自動應答
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		// 5、使用session對象創建一個Destination對象。兩種形式:Queue和Topic,現在使用queue
		Queue queue = session.createQueue("testQueue01");
		// 6、使用session對象創建一個Producer對象
		MessageProducer messageProducer = session.createProducer(queue);
		// 7、創建一個Message對象,可以使用TextMessage
		/*
		 * TextMessage textMessage = new ActiveMQTextMessage();
		 * textMessage.setText("Hello My First Queue");
		 */
		TextMessage textMessage = session.createTextMessage("testQueue");
		// 8、發送消息
		messageProducer.send(textMessage);
		/// 9、關閉資源
		messageProducer.close();
		session.close();
		connection.close();
	}

	@Test
	public void testQueueConsumer() throws Exception {
		// 1、創建一個連接工廠ConnectionFactory,需要制定ip和port
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.129:61616");
		// 2、創建一個Connection對象
		Connection connection = connectionFactory.createConnection();
		// 3、開啓連接,調用Connection的start()方法開啓
		connection.start();
		// 4、使用connection對象創建一個Session對象
		// 第一個參數:是否開啓事務。如果爲true開啓事務,第二個參數無意義。如果爲false關閉事務;一般爲false
		// 第二個參數:會話應答模式。自動應答Session.AUTO_ACKNOWLEDGE和手動應答Session.CLIENT_ACKNOWLEDGE。一般爲自動應答
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		// 5、創建一個Queue
		Queue queue = session.createQueue("testQueue01");
		// 6、創建以個Consumer
		MessageConsumer consumer = session.createConsumer(queue);
		// 7、接收消息
		consumer.setMessageListener(new MessageListener() {

			@Override
			public void onMessage(Message arg0) {
				TextMessage message = (TextMessage) arg0;
				// 8、處理結果
				try {
					System.out.println(message.getText());
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		});
		// 等待接收消息
		System.in.read();
		// 9、關閉資源
		consumer.close();
		session.close();
		connection.close();
	}

	@Test
	public void testTopicProducer() throws Exception {
		// 1、創建ConnectionFactory工廠,需要制定ip和端口號
		ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://192.168.25.129:61616");
		// 2、創建Connection對象
		Connection connection = factory.createConnection();
		// 3、開啓連接
		connection.start();
		// 4、使用Connection對象創建一個會話Session
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		// 5、創建一個Topic對象
		Topic topic = session.createTopic("testTopic01");
		// 6、使用session對象創建一個Producer
		MessageProducer producer = session.createProducer(topic);
		// 7、創建消息對象
		TextMessage message = session.createTextMessage("testTopic Message");
		// 8、發送消息
		producer.send(message);
		// 9、關閉資源
		producer.close();
		session.close();
		connection.close();
	}

	@Test
	public void testTopicConsumer() throws Exception {
		ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://192.168.25.129:61616");
		Connection connection = factory.createConnection();
		connection.start();
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		Topic topic = session.createTopic("testTopic01");
		MessageConsumer consumer = session.createConsumer(topic);
		consumer.setMessageListener(new MessageListener() {
			@Override
			public void onMessage(Message message) {
				TextMessage textMessage = (TextMessage) message;
				try {
					System.out.println(textMessage.getText());
				} catch (JMSException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		System.out.println("topic3消費者啓動了。。。。");
		System.in.read();

		consumer.close();
		session.close();
		connection.close();
	}

}

 

 

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