生活随笔
收集整理的這篇文章主要介紹了
ActiveMQ—安装配置及使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
安裝配置及使用
轉自:http://blog.csdn.net/qq_21033663/article/details/52461543
(一)ActiveMQ介紹
ActiveMQ 是Apache出品,最流行的,能力強勁的開源消息總線。ActiveMQ 是一個完全支持JMS1.1和J2EE 1.4規范的 JMS Provider實現,盡管JMS規范出臺已經是很久的事情了,但是JMS在當今的J2EE應用中間仍然扮演著特殊的地位。?
特性列表:?
⒈ 多種語言和協議編寫客戶端。語言:?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?
⒐ 支持與Axis的整合?
⒑ 可以很容易的調用內嵌JMS provider,進行測試
(二)ActiveMQ安裝、配置、啟動、可視化界面
1、安裝?
下載地址:http://activemq.apache.org/download.html?
2、配置(conf目錄下)?
1)用戶名密碼設置?
?
2)開啟jmx監控?
activemq.xml中進行如下修改?
注:這里的配置不是必須,根據需要自行配置?
3、啟動?
直接運行bin目錄下:activemq.bat?
4、可視化界面?
瀏覽器中:http://localhost:8161/admin/index.jsp?
用戶名,密碼在:jetty-realm.properties中設置
(三)點對點式消息隊列(Queue)
消息生產者
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnectionFactory;public class QueueProducer {public static void main(String[] args) {String username = "system";String password = "manager";String brokerURL = "failover://tcp://localhost:61616";ConnectionFactory connectionFactory = null;Connection connection = null;Session session = null;Destination destination = null;MessageProducer messageProducer = null;connectionFactory = new ActiveMQConnectionFactory(username, password, brokerURL);try {connection = connectionFactory.createConnection();connection.start();session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);destination = session.createQueue("QueueTest");messageProducer = session.createProducer(destination);TextMessage message = null;for (int i=0; i<10; i++) {message = session.createTextMessage("Queue消息測試" +(i+1));messageProducer.send(message);System.out.println("發送成功:" + message.getText());}session.commit();} catch (Exception e) {e.printStackTrace();}finally{if(null != connection){try {connection.close();} catch (JMSException e) {e.printStackTrace();}}}}}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
消息消費者
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnectionFactory;public class QueueConsumer {public static void main(String[] args) {String username = "system";String password = "manager";String brokerURL = "failover://tcp://localhost:61616";ConnectionFactory connectionFactory = null;Connection connection = null;Session session = null;Destination destination = null;MessageConsumer messageConsumer = null;connectionFactory = new ActiveMQConnectionFactory(username, password, brokerURL);try {connection = connectionFactory.createConnection();connection.start();session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);destination = session.createQueue("QueueTest");messageConsumer = session.createConsumer(destination);while (true) {TextMessage textMessage = (TextMessage) messageConsumer.receive(100000);if(textMessage != null){System.out.println("成功接收消息:" + textMessage.getText());}else {break;}}} catch (JMSException e) {e.printStackTrace();}}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
(四)主題發布訂閱式(Topic)
主題發布者
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;import org.apache.activemq.ActiveMQConnectionFactory;public class TopicProducer {public static void main(String[] args) {String username = "system";String password = "manager";String brokerURL = "failover://tcp://localhost:61616";ConnectionFactory connectionFactory = null;Connection connection = null;Session session = null;Topic topic = null;MessageProducer messageProducer = null;connectionFactory = new ActiveMQConnectionFactory(username, password, brokerURL);try {connection = connectionFactory.createConnection();connection.start();session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);topic = session.createTopic("TopicTest"); messageProducer = session.createProducer(topic);messageProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);TextMessage message = null;for (int i=0; i<10; i++) {message = session.createTextMessage("Topic主題測試" +(i+1));messageProducer.send(message);System.out.println("發送成功:" + message.getText());}session.commit();} catch (Exception e) {e.printStackTrace();}finally{if(null != connection){try {connection.close();} catch (JMSException e) {e.printStackTrace();}}} }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
主題訂閱者
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.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;import org.apache.activemq.ActiveMQConnectionFactory;public class TopicConsumer {public static void main(String[] args) {String username = "system";String password = "manager";String brokerURL = "failover://tcp://localhost:61616";ConnectionFactory connectionFactory = null;Connection connection = null;Session session = null;Topic topic = null;MessageConsumer messageConsumer = null;connectionFactory = new ActiveMQConnectionFactory(username, password, brokerURL);try {connection = connectionFactory.createConnection();connection.start();session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);topic = session.createTopic("TopicTest"); messageConsumer = session.createConsumer(topic);messageConsumer.setMessageListener(new MyMessageListener());} catch (JMSException e) {e.printStackTrace();}}}class MyMessageListener implements MessageListener {@Overridepublic void onMessage(Message message) { TextMessage textMessage = (TextMessage) message; try { System.out.println("接收訂閱主題:" + textMessage.getText()); } catch (JMSException e) { e.printStackTrace(); } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
注:?
1、代碼中所需額外jar包在下載的mq文件夾中,例如我使用的:activemq-all-5.9.0.jar?
2、對于消息隊列,異步生產和消費;對于主題發布訂閱要先啟動訂閱者進行監聽,然后在發布方可接收到訂閱主題?
3、關于Queue與Topic的具體區別,詳見http://blog.csdn.net/u013490585/article/details/76083196
總結
以上是生活随笔為你收集整理的ActiveMQ—安装配置及使用的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。