日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

jdk1.6集成activemq的2种方式

發(fā)布時間:2024/9/27 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jdk1.6集成activemq的2种方式 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

          • 一、maven方式
            • 1. 依賴
            • 2. 發(fā)送端
            • 3. 接收端
            • 4. 工具類
          • 二、引入jar方式
            • 2.1.下載jar
            • 2.2. 按需導入
          • 三、添加賬號密碼

一、maven方式
1. 依賴
<dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-core</artifactId><version>5.5.0</version></dependency>
2. 發(fā)送端

簡潔版本

package mq;import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.MessageProducer; import javax.jms.Queue; import javax.jms.Session; import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnectionFactory;public class Sender {public static void main(String[] args) throws Exception {// 1.創(chuàng)建連接工廠ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.0.119:61616");// 2.創(chuàng)建連接Connection connection = connectionFactory.createConnection();// 3.啟動連接connection.start();// 4.獲取session(會話對象) 參數(shù)1:是否啟動事務 參數(shù)2:消息確認方式Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);// 5.創(chuàng)建隊列對象Queue queue = session.createQueue("test-queue");// 6.創(chuàng)建消息生產者對象MessageProducer producer = session.createProducer(queue);// 7.創(chuàng)建消息對象(文本消息)TextMessage textMessage = session.createTextMessage("發(fā)送點點對消息模擬第一輪測試!");// 8.發(fā)送消息producer.send(textMessage);// 9.關閉資源producer.close();session.close();connection.close();} }

測試版本

package com.gblfy.activemq.qq;import javax.jms.*;import org.apache.activemq.ActiveMQConnectionFactory;public class Sender {public static String ADDRESS = "192.168.0.119:61616";public static String QUEUE = "test-queue";public static String SENDMSG = "發(fā)送點點對消息模擬第一輪測試!";public static void main(String[] args) throws Exception {//連接工廠ConnectionFactory connectionFactory;//連接Connection connection = null;//會話 接受或者發(fā)送消息的線程Session session = null;//消息的目的地Queue queue;//消息生產者MessageProducer messageProducer = null;TextMessage textMessage;try {// 1.創(chuàng)建連接工廠connectionFactory = new ActiveMQConnectionFactory("tcp://" + ADDRESS);// 2.創(chuàng)建連接connection = connectionFactory.createConnection();// 3.啟動連接connection.start();// 4.獲取session(會話對象) 參數(shù)1:是否啟動事務 參數(shù)2:消息確認方式session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);// 5.創(chuàng)建隊列對象queue = session.createQueue(QUEUE);// 6.創(chuàng)建消息生產者對象messageProducer = session.createProducer(queue);// 7.創(chuàng)建消息對象(文本消息)textMessage = session.createTextMessage(SENDMSG);// 8.發(fā)送消息messageProducer.send(textMessage);} catch (Exception e) {e.printStackTrace();} finally {if (connection != null) {try {// 9.關閉資源messageProducer.close();session.close();connection.close();} catch (JMSException e) {e.printStackTrace();}}}} }
3. 接收端

簡潔版本

package com.gblfy.activemq.qq;import org.apache.activemq.ActiveMQConnectionFactory;import javax.jms.*;public class Consumer {public static void main(String[] args) throws Exception {// 1.創(chuàng)建連接工廠ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.0.119:61616");// 2.創(chuàng)建連接Connection connection = connectionFactory.createConnection();// 3.啟動連接connection.start();// 4.獲取session(會話對象) 參數(shù)1:是否啟動事務 參數(shù)2:消息確認方式Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);// 5.創(chuàng)建隊列對象Queue queue = session.createQueue("test-queue");// 6.創(chuàng)建消息消費者對象MessageConsumer consumer = session.createConsumer(queue);// 7.設置監(jiān)聽consumer.setMessageListener(new MessageListener() {@Overridepublic void onMessage(Message message) {TextMessage textMessage = (TextMessage) message;try {System.out.println("提取的消息:" + textMessage.getText());} catch (JMSException e) {e.printStackTrace();}}});// 8.等待鍵盤輸入System.in.read();// 9.關閉資源consumer.close();session.close();connection.close();} }

測試版本

package com.gblfy.activemq.qq;import org.apache.activemq.ActiveMQConnectionFactory;import javax.jms.*; import java.io.IOException;public class Consumer {public static String ADDRESS = "192.168.0.119:61616";public static String QUEUE = "test-queue";public static void main(String[] args) throws Exception {//連接工廠ConnectionFactory connectionFactory;//連接Connection connection = null;//會話 接受或者發(fā)送消息的線程Session session = null;//消息的目的地Queue queue;//消息生產者MessageConsumer consumer = null;// 1.創(chuàng)建連接工廠try {connectionFactory = new ActiveMQConnectionFactory("tcp://" + ADDRESS);// 2.創(chuàng)建連接connection = connectionFactory.createConnection();// 3.啟動連接connection.start();// 4.獲取session(會話對象) 參數(shù)1:是否啟動事務 參數(shù)2:消息確認方式session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);// 5.創(chuàng)建隊列對象queue = session.createQueue(QUEUE);// 6.創(chuàng)建消息消費者對象consumer = session.createConsumer(queue);// 7.設置監(jiān)聽consumer.setMessageListener(new MessageListener() {@Overridepublic void onMessage(Message message) {TextMessage textMessage = (TextMessage) message;try {System.out.println("提取的消息:" + textMessage.getText());} catch (JMSException e) {e.printStackTrace();}}});// 8.等待鍵盤輸入System.in.read();} catch (JMSException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {// 9.關閉資源consumer.close();session.close();connection.close();}} }
4. 工具類
package com.gblfy.activemq.qq;import org.apache.activemq.ActiveMQConnectionFactory;import javax.jms.*;public class MQUtil {//連接工廠ConnectionFactory connectionFactory;//連接Connection connection = null;//會話 接受或者發(fā)送消息的線程Session session = null;//消息的目的地Queue queue;public Queue createConnectionFactory(String address, String queueName) {try {// 1.創(chuàng)建連接工廠connectionFactory = new ActiveMQConnectionFactory("tcp://" + address);// 2.創(chuàng)建連接connection = connectionFactory.createConnection();// 3.啟動連接connection.start();// 4.獲取session(會話對象) 參數(shù)1:是否啟動事務 參數(shù)2:消息確認方式session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);// 5.創(chuàng)建隊列對象queue = session.createQueue(queueName);} catch (JMSException e) {e.printStackTrace();}return queue;}public void sendMsg(String address, String queueName, String message) {MessageProducer messageProducer = null;TextMessage textMessage;try {// 6.創(chuàng)建消息生產者對象queue = createConnectionFactory(address, queueName);messageProducer = session.createProducer(queue);// 7.創(chuàng)建消息對象(文本消息)textMessage = session.createTextMessage(message);// 8.發(fā)送消息messageProducer.send(textMessage);} catch (Exception e) {e.printStackTrace();} finally {if (connection != null) {try {// 9.關閉資源messageProducer.close();session.close();connection.close();} catch (JMSException e) {e.printStackTrace();}}}}public void recMsg(String address, String queueName) {//消息生產者MessageConsumer consumer = null;try {// 6.創(chuàng)建消息消費者對象queue = createConnectionFactory(address, queueName);consumer = session.createConsumer(queue);// 7.設置監(jiān)聽consumer.setMessageListener(new MessageListener() {@Overridepublic void onMessage(Message message) {TextMessage textMessage = (TextMessage) message;try {System.out.println("提取的消息:" + textMessage.getText());} catch (JMSException e) {e.printStackTrace();}}});// 8.等待鍵盤輸入System.in.read();} catch (Exception e) {e.printStackTrace();} finally {if (connection != null) {try {// 9.關閉資源consumer.close();session.close();connection.close();} catch (JMSException e) {e.printStackTrace();}}}}public static void main(String[] args) {String ADDRESS = "192.168.0.119:61616";String QUEUE = "test-queue";String SENDMSG = "發(fā)送點點對消息模擬第一輪測試!";MQUtil mqUtil = new MQUtil();mqUtil.sendMsg(ADDRESS, QUEUE, SENDMSG);mqUtil.recMsg(ADDRESS, QUEUE);} }
二、引入jar方式

2.1.下載jar

建議用maven坐標先把需要的jar下載到本地倉庫,再把本地倉庫中的jar復制到工程的lib文件夾下面

<dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-core</artifactId><version>5.5.0</version></dependency>

如果出現(xiàn)SLF4J:Failed to load class org.slf4j.impl.StaticLoggerBinder
請?zhí)Dhttps://gblfy.blog.csdn.net/article/details/107018564

2.2. 按需導入

三、添加賬號密碼

源碼分析:

public ActiveMQConnectionFactory(String userName, String password, String brokerURL) {setUserName(userName);setPassword(password);setBrokerURL(brokerURL);}

從源碼看出在創(chuàng)建連接工廠的時候,在添加賬號面的參數(shù)即可,注意參數(shù)順序。

例如:

//連接用戶名private static final String USERNAME = "admin";//連接密碼private static final String PASSWORD = "admin";//連接地址private static final String BROKEURL = "10.5.6.19:61616";connectionFactory = new ActiveMQConnectionFactory(USERNAME,"tcp://" + BROKEURL,PASSWORD);

總結

以上是生活随笔為你收集整理的jdk1.6集成activemq的2种方式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。