日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

ActiveMQ简单使用介绍

發(fā)布時間:2025/7/14 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ActiveMQ简单使用介绍 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

1. ActiveMQ簡述
ActiveMQ 是Apache出品,最流行的,能力強勁的開源消息總線。ActiveMQ 是一個完全支持JMS1.1和J2EE 1.4規(guī)范的 JMS Provider實現(xiàn)。
下載地址:http://activemq.apache.org/

2. 運行ActiveMQ
下載好壓縮包后,解壓,直接運行bin/win64(或32,據(jù)系統(tǒng)而定)下的activemq.bat即可,程序開啟后,可通過瀏覽器訪問http://localhost:8161/admin進入管理界面。如需帳號密碼,請輸入admin和admin。

3. 創(chuàng)建項目和測試
主要有生產(chǎn)者(對應(yīng)Sender)和消費者(對應(yīng)Receiver),然后使用多線程進行測試。其中的消息隊列對通過生產(chǎn)者:destination = session.createQueue(“TestFirst”)
自動在ActiveMQ中創(chuàng)建。
下面是詳細(xì)的代碼(創(chuàng)建工程后請將下載的ActiveMQ中l(wèi)ib文件夾里面的jar包添加進去):

Receiver.java

public class Receiver {public void listen() {// ConnectionFactory :連接工廠,JMS 用它創(chuàng)建連接ConnectionFactory connectionFactory;// Connection :JMS 客戶端到JMS Provider 的連接Connection connection = null;// Session: 一個發(fā)送或接收消息的線程Session session;// Destination :消息的目的地;消息發(fā)送給誰.Destination destination;// 消費者,消息接收者MessageConsumer consumer;connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,"tcp://localhost:61616");try {// 構(gòu)造從工廠得到連接對象connection = connectionFactory.createConnection();// 啟動connection.start();// 獲取操作連接session = connection.createSession(Boolean.FALSE,Session.AUTO_ACKNOWLEDGE);// 獲取session注意參數(shù)值xingbo.xu-queue是一個服務(wù)器的queue,須在在ActiveMq的console配置destination = session.createQueue("TestFirst");consumer = session.createConsumer(destination);while (true) {Thread.sleep(20);//設(shè)置接收者接收消息的時間,為了便于測試,這里誰定為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 用它創(chuàng)建連接ConnectionFactory connectionFactory;// Connection :JMS 客戶端到JMS Provider 的連接Connection connection = null;// Session: 一個發(fā)送或接收消息的線程Session session;// Destination :消息的目的地;消息發(fā)送給誰.Destination destination;// MessageProducer:消息發(fā)送者MessageProducer producer;// TextMessage message;// 構(gòu)造ConnectionFactory實例對象,此處采用ActiveMq的實現(xiàn)jarconnectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,"tcp://localhost:61616");try {// 構(gòu)造從工廠得到連接對象connection = connectionFactory.createConnection();// 啟動connection.start();// 獲取操作連接session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);// 獲取session注意參數(shù)值xingbo.xu-queue是一個服務(wù)器的queue,須在在ActiveMq的console配置destination = session.createQueue("TestFirst");// 得到消息生成者【發(fā)送者】producer = session.createProducer(destination);// 設(shè)置不持久化,此處學(xué)習(xí),實際根據(jù)項目決定producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);// 構(gòu)造消息,此處寫死,項目就是參數(shù),或者方法獲取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 + ":內(nèi)容" + i;TextMessage message = session.createTextMessage(msg + i);// 發(fā)送消息到目的地方System.out.println("發(fā)送消息:" + 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();//多個生產(chǎn)者int i = 80;while(i-->0)new MyThread(i).start();} }

本文說的并不詳細(xì),后期會補上。
先給幾個連接:

ActiveMQ入門實例:
http://www.cnblogs.com/xwdreamer/archive/2012/02/21/2360818.html
作者:xwdreamer

ActiveMQ初體驗:
http://www.cnblogs.com/diorlv/p/3328712.html
作者:diorlv

Android中應(yīng)用ActiveMQ(推送相關(guān)):
http://blog.csdn.net/junfeng120125/article/details/36420083
作者:永不放棄的IT碼農(nóng)

轉(zhuǎn)載于:https://my.oschina.net/lvzunwei/blog/687869

總結(jié)

以上是生活随笔為你收集整理的ActiveMQ简单使用介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。