ActiveMQ简单使用介绍
2019獨角獸企業重金招聘Python工程師標準>>>
1. ActiveMQ簡述
ActiveMQ 是Apache出品,最流行的,能力強勁的開源消息總線。ActiveMQ 是一個完全支持JMS1.1和J2EE 1.4規范的 JMS Provider實現。
下載地址:http://activemq.apache.org/
2. 運行ActiveMQ
下載好壓縮包后,解壓,直接運行bin/win64(或32,據系統而定)下的activemq.bat即可,程序開啟后,可通過瀏覽器訪問http://localhost:8161/admin進入管理界面。如需帳號密碼,請輸入admin和admin。
3. 創建項目和測試
主要有生產者(對應Sender)和消費者(對應Receiver),然后使用多線程進行測試。其中的消息隊列對通過生產者:destination = session.createQueue(“TestFirst”)
自動在ActiveMQ中創建。
下面是詳細的代碼(創建工程后請將下載的ActiveMQ中lib文件夾里面的jar包添加進去):
Receiver.java
public class Receiver {public void listen() {// ConnectionFactory :連接工廠,JMS 用它創建連接ConnectionFactory connectionFactory;// Connection :JMS 客戶端到JMS Provider 的連接Connection connection = null;// Session: 一個發送或接收消息的線程Session session;// Destination :消息的目的地;消息發送給誰.Destination destination;// 消費者,消息接收者MessageConsumer consumer;connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,"tcp://localhost:61616");try {// 構造從工廠得到連接對象connection = connectionFactory.createConnection();// 啟動connection.start();// 獲取操作連接session = connection.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE);// 獲取session注意參數值xingbo.xu-queue是一個服務器的queue,須在在ActiveMq的console配置destination = session.createQueue("TestFirst");consumer = session.createConsumer(destination);while (true) {Thread.sleep(20);//設置接收者接收消息的時間,為了便于測試,這里誰定為100sTextMessage message = (TextMessage) consumer.receive(100000);if (null != message) {System.err.println("收到消息:" + message.getText());} else {break;}}} catch (Exception e) {e.printStackTrace();} finally {try {if (null != connection)connection.close();} catch (Throwable ignore) {}}} }Sender.java
public class Sender {private static final int SEND_NUMBER = 5;private int userId;public Sender(int userId) {this.userId = userId;}public void send() {// ConnectionFactory :連接工廠,JMS 用它創建連接ConnectionFactory connectionFactory;// Connection :JMS 客戶端到JMS Provider 的連接Connection connection = null;// Session: 一個發送或接收消息的線程Session session;// Destination :消息的目的地;消息發送給誰.Destination destination;// MessageProducer:消息發送者MessageProducer producer;// TextMessage message;// 構造ConnectionFactory實例對象,此處采用ActiveMq的實現jarconnectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,"tcp://localhost:61616");try {// 構造從工廠得到連接對象connection = connectionFactory.createConnection();// 啟動connection.start();// 獲取操作連接session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);// 獲取session注意參數值xingbo.xu-queue是一個服務器的queue,須在在ActiveMq的console配置destination = session.createQueue("TestFirst");// 得到消息生成者【發送者】producer = session.createProducer(destination);// 設置不持久化,此處學習,實際根據項目決定producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);// 構造消息,此處寫死,項目就是參數,或者方法獲取sendMessage(session, producer);session.commit();} catch (Exception e) {e.printStackTrace();} finally {try {if (null != connection)connection.close();} catch (Throwable ignore) {}}}public void sendMessage(Session session, MessageProducer producer)throws Exception {for (int i = 1; i <= SEND_NUMBER; i++) {String msg = "用戶" + userId + ":內容" + i;TextMessage message = session.createTextMessage(msg + i);// 發送消息到目的地方System.out.println("發送消息:" + msg);producer.send(message);}} }MyThread.java
public class MyThread extends Thread {private boolean flag;private int i = 0;MyThread(){flag = true;}MyThread(int i){flag = false;this.i = i;}public void run() {int i = 1000;if(flag)new Receiver().listen();elsewhile(--i>0)new Sender(this.i).send();}public static void main(String[] args){//一個消費者new MyThread().start();//多個生產者int i = 80;while(i-->0)new MyThread(i).start();} }本文說的并不詳細,后期會補上。
先給幾個連接:
ActiveMQ入門實例:
http://www.cnblogs.com/xwdreamer/archive/2012/02/21/2360818.html
作者:xwdreamer
ActiveMQ初體驗:
http://www.cnblogs.com/diorlv/p/3328712.html
作者:diorlv
Android中應用ActiveMQ(推送相關):
http://blog.csdn.net/junfeng120125/article/details/36420083
作者:永不放棄的IT碼農
轉載于:https://my.oschina.net/lvzunwei/blog/687869
總結
以上是生活随笔為你收集整理的ActiveMQ简单使用介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Git Push 不用再次输入用户名和密
- 下一篇: Mac OS X上的lipo命令详解