JMS-ActiveMQ學習-3 ActiveMQ與Spring集成

Spring下開發消息的發送和接收程序

點對點模式

一、創建生產者項目

1.創建maven項目

2.添加spring-jms、jms規范、activemq依賴

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-apis</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
<version>5.15.4</version>
</dependency>
</dependencies>

3.創建applicationContext.xml和applicationContext-jms.xml

applicationContext.xml中的配置內容:

<import resource="applicationContext-jms.xml"/>

applicationContext-jms.xml中的配置內容:

<!--創建一個連接工廠-->
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<!--連接地址-->
<property name="brokerURL" value="tcp://localhost:61616"/>
<property name="userName" value="admin"/>
<property name="password" value="admin"/>
</bean>
<!--jms的操作模板-->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<!--配置目的地-->
<property name="defaultDestinationName" value="myQueue"/>
</bean>

4.具體實現接口和類
1>創建dao層接口和類
public interface MessageDao {
public void sendMessage(String message);
}
@Repository
public class MessageDaoImpl implements MessageDao {
@Autowired
private JmsTemplate jmsTemplate;
public void sendMessage(final String message) {
System.out.println("send message start");
jmsTemplate.send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(message);
}
});
}
}
2>創建service層接口和類
public interface MessageService {
public void sendMessage(String message);
}
@Service
public class MessageServiceImpl implements MessageService {
@Autowired
private MessageDao messageDao;
public void sendMessage(String message) {
messageDao.sendMessage(message);
}
}
3>創建Test類
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
MessageService messageService = context.getBean("messageServiceImpl",MessageServiceImpl.class);
messageService.sendMessage("發送消息了");
}
}

4>applicationContext.xml文件中添加
<context:component-scan base-package="com.test.activemq"/>

二、創建消費者項目

1.創建maven項目

2.添加spring-jms、jms規范、activemq依賴

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-apis</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
<version>5.15.4</version>
</dependency>
</dependencies>

3.創建applicationContext.xml和applicationContext-jms.xml

applicationContext.xml中的配置內容:

<import resource="applicationContext-jms.xml"/>

applicationContext-jms.xml中的配置內容:

<!--創建一個連接工廠-->
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<!--連接地址-->
<property name="brokerURL" value="tcp://localhost:61616"/>
<property name="userName" value="admin"/>
<property name="password" value="admin"/>
</bean>
<!--jms的操作模板-->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<!--配置目的地-->
<property name="defaultDestinationName" value="myQueue"/>
</bean>

4.具體實現接口和類
同步方式接收:
1>創建dao層接口和類
public interface MessageDao {
public String receiveMessage();
}
@Repository
public class MessageDaoImpl implements MessageDao {
@Autowired
private JmsTemplate jmsTemplate;
public String receiveMessage(){
String msg = "";
Message message = jmsTemplate.receive();
if (message instanceof TextMessage){
try {
msg = ((TextMessage) message).getText();
} catch (JMSException e) {
e.printStackTrace();
}
}
return msg;
}
}

2>創建service層接口和類
public interface MessageService {
public String receiveMessage();
}
@Service
public class MessageServiceImpl implements MessageService {
@Autowired
private MessageDao messageDao;
public String receiveMessage() {
return messageDao.receiveMessage();
}
}

3>創建Test類
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
MessageService messageService = context.getBean("messageServiceImpl",MessageServiceImpl.class);
String message = messageService.receiveMessage();
System.out.println("接收到的消息是:"+message);
}
}
4>applicationContext.xml文件中添加
<context:component-scan base-package="com.test.jms"/>
三、消費者點對點模式-異步接收
需要使用監聽器
1>創建maven項目
2>添加依賴
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-apis</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
<version>5.15.4</version>
</dependency>
</dependencies>
3>創建applicationContext.xml和applicationContext-jms.xml
<!--創建一個連接工廠-->
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<!--連接地址-->
<property name="brokerURL" value="tcp://localhost:61616"/>
<property name="userName" value="admin"/>
<property name="password" value="admin"/>
</bean>
<!--配置一個spring的jms消息監聽器-->
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="DestinationName" value="springQueue"/>
<property name="messageListener" ref="myMessageListener"/>

</bean>
<bean id="myMessageListener" class="com.test.jms.listener.MyMessageListener"/>
4>創建MyMessageListener類
/**
* 實現消息監聽器
*/
public class MyMessageListener implements MessageListener {
/**
* 當消息監聽器監聽到消息后,會自動回調該onMessage方法
* 并且把監聽到的消息回傳給該方法
* @param message
*/
public void onMessage(Message message) {
if (message instanceof TextMessage){
try {
System.out.println("接收到的消息是:"+((TextMessage) message).getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
5>創建Test測試類
public class Test {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
}
}
啟動發現可以收到消息 console控制臺打印出來的消息是: 接收到的消息是:發送消息了

發布訂閱模式

同步和異步接收只是針對消費者而言的

發布訂閱模式-同步接收

一、創建生產者

生產者和消費者的都配置成發布訂閱模式

同步接收使用receive()接收

創建生產者需要在點對點模式的基礎上在applicationContext-jms.xml文件中添加:

<!--創建一個連接工廠-->
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<!--連接地址-->
<property name="brokerURL" value="tcp://localhost:61616"/>
<property name="userName" value="admin"/>
<property name="password" value="admin"/>
</bean>
<!--jms的操作模板-->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<!--配置目的地-->
<property name="defaultDestinationName" value="springTopic"/>
<!--pubSubDomain這個property對應模式true代表發布訂閱,false代表點對點,默認是false,-->
<property name="pubSubDomain" value="true"/>
</bean>
同理,消費者也是在點對點模式的基礎上做上述配置,后消費者先啟動,再啟動生產者

發布訂閱模式-異步接收

?在異步基礎上添加發布訂閱的配置(生產者和消費者都需要配置)

<!--創建一個連接工廠-->
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<!--連接地址-->
<property name="brokerURL" value="tcp://localhost:61616"/>
<property name="userName" value="admin"/>
<property name="password" value="admin"/>
</bean>
<!--jms的操作模板-->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<!--配置目的地-->
<property name="defaultDestinationName" value="springTopic"/>
<property name="pubSubDomain" value="true"/>
</bean>

?

?






轉載于:https://www.cnblogs.com/healthinfo/p/9567362.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/396995.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/396995.shtml
英文地址,請注明出處:http://en.pswp.cn/news/396995.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

看libevent所遇到的英語生詞

libevent – an event notification library The libevent API &#xff08;libevent應用程序&#xff09;provides a mechanism&#xff08;機制&#xff09; to execute&#xff08;執行&#xff09; a callback function&#xff08;回調函數&#xff09; when a specific&a…

java中迭代器要導包嗎_java 中迭代器的使用方法詳解

java 中迭代器的使用方法詳解前言&#xff1a;迭代器模式將一個集合給封裝起來&#xff0c;主要是為用戶提供了一種遍歷其內部元素的方式。迭代器模式有兩個優點&#xff1a;①提供給用戶一個遍歷的方式&#xff0c;而沒有暴露其內部實現細節&#xff1b;②把元素之間游走的責任…

android socket 長連接_TCP/IP,http,socket,長連接,短連接

點擊上方藍色字體&#xff0c;選擇“標星公眾號”優質文章&#xff0c;第一時間送達上一篇&#xff1a;這300G的Java資料是我師傅當年給我的&#xff0c;免費分享給大家下一篇&#xff1a;這200G的Java實戰資料是我師傅當年教我的第二招作者 | ksfzhaohui來源 | my.oschina.net…

二、Python安裝擴展庫

第一步:推薦easy_install工具 下載地址:https://pypi.python.org/pypi/setuptools 下載"ez_setup.py"文件; 通過運行cmd命令找到ez_setup.py文件所在目錄,通過命令[python ez_setup.py]執行安裝easy_install 安裝成功截圖 第二步:安裝擴展酷 例如安裝"suds"…

ORACLE 10.2.01升級10.2.05 for windows 詳細文檔

最近要做一個數據庫的升級工作&#xff0c;提前在自己的PC機上練習了一下&#xff0c;這種文檔在網上很多&#xff0c;但是大多都是使用命令編輯腳本&#xff0c;其實數據庫還有一個DBUA的升級工具可以使用&#xff0c;使升級工作方便了很多。 OS環境&#xff1a;windows XP 32…

php 導出mysql 結構_導出結構和數據(如phpmyadmin)

在這里,您可以找到一個全面的解決方案來轉儲MySQL結構和數據,比如在PMA中(不使用exec、passthru等):它是Dszymczuk項目的一個分支,有我的增強功能。用法很簡單//MySQL connection parameters$dbhost localhost;$dbuser dbuser;$dbpsw pass;$dbname dbname;//Connects to my…

tableViewCell的操作

在iOS的開發過程中&#xff0c;tableView的使用永遠都是最常用的控件。今天學習了一下tableViewCell的操作。代碼并不是很復雜&#xff0c;如果有OC開發經驗的人&#xff0c;應該很容易看懂的。 class ViewController: UIViewController ,UITableViewDelegate, UITableViewData…

stm32正交編碼器 原理圖_惡劣環境下應用的電感式增量編碼器和絕對編碼器

編碼器可分為兩種基本類型 - 增量編碼器和絕對編碼器。增量編碼器的顯著特征是它報告角度的變化。換句話說&#xff0c;當增量編碼器通電時&#xff0c;它不會報告其角位置&#xff0c;直到它具有測量的參考點。絕對編碼器明確地在比例或范圍內報告其位置。換句話說&#xff0c…

【SqlServer】Sqlserver中的DOS命令操作

輸入osql ?查看是否支持當前版本&#xff0c;如果是SQL Server 2005以上用Sqlcmd , 以下用Osql連接數據庫&#xff08;a&#xff09;Osql -S localhost -U username -P password(SQL Server身份驗證&#xff0c;需要用戶民和密碼)&#xff08;b&#xff09;Osql -S localhos…

微信小程序內訓筆記

2016年9月22日凌晨微信官方正式宣布“小程序”開始內測&#xff0c;有“微信之父”之稱、騰訊集團高級執行副總裁張小龍在2016年末對外宣布“小程序“應用將于2017年1月9日正式推出 這一次微信還是按照慣例&#xff0c;通過機器跑出的數據&#xff0c;首先將“小程序”開放給了…

python基礎代碼的含義_Python基礎學習篇

原標題&#xff1a;Python基礎學習篇 1、編碼 默認情況下&#xff0c;Python 3 源碼文件以 UTF-8 編碼&#xff0c;所有字符串都是unicode 字符串。 當然你也可以為源碼文件指定不同的編碼&#xff1a;# -*- coding: cp-1252 -*- 2、標識符 第一個字符必須是字母表中字母或下劃…

java面向對象super_【JavaSE】面向對象之super、final

一、super關鍵字它是一個指代變量&#xff0c;用于在子類中指代父類對象。1.作用指代父類對象區分子父類同名的成員變量&#xff0c;區分父類中成員變量和子類中同名的局部變量2.使用與this相同&#xff0c;調用父類成員變量和成員方法&#xff1a;super.xx super.xxx()調用父類…

Week_1_Physical Electronics and Semiconductors

Semiconductors Fundamentals Type of solids 轉載于:https://www.cnblogs.com/ronnielee/p/9579783.html

【Linux高頻命令專題(23)】tar

概述 通過SSH訪問服務器&#xff0c;難免會要用到壓縮&#xff0c;解壓縮&#xff0c;打包&#xff0c;解包等&#xff0c;這時候tar命令就是是必不可少的一個功能強大的工具。linux中最流行的tar是麻雀雖小&#xff0c;五臟俱全&#xff0c;功能強大。 tar命令可以為linux的文…

2. Add Two Numbers

直接用一個carry記錄進位就可以 1 //NEW2 class Solution {3 public ListNode addTwoNumbers(ListNode l1, ListNode l2) {4 ListNode root new ListNode(0);5 return addTwoNumbers(l1, l2, root);6 }7 public ListNode addTwoNumbers(ListNode …

安裝Windows更新程序遇到錯誤:0x80070422

看看服務那里 windows update服務是不是被禁用了&#xff1f; 還有一個問題可能是由于Windows Modules Installer被禁用了。

談談對python的理解_淺談對python pandas中 inplace 參數的理解

這篇文章主要介紹了對python pandas中 inplace 參數的理解&#xff0c;具有很好的參考價值&#xff0c;希望對大家有所幫助。一起跟隨小編過來看看吧 pandas 中 inplace 參數在很多函數中都會有&#xff0c;它的作用是&#xff1a;是否在原對象基礎上進行修改 inplace True&am…

java中 hashset_Java中的HashSet

HashSet擴展AbstractSet并實現Set接口。它創建一個使用哈希表進行存儲的集合。哈希表通過使用稱為哈希的機制來存儲信息。在散列中&#xff0c;鍵的信息內容用于確定唯一值&#xff0c;稱為其散列碼。然后&#xff0c;將哈希碼用作存儲與鍵關聯的數據的索引。鍵到其哈希碼的轉換…

mac下的svn服務器建立

MAC下的SVN服務器建立: from : http://blog.csdn.net/q199109106q/article/details/8655204 在Windows環境中&#xff0c;我們一般使用TortoiseSVN來搭建svn環境。在Mac環境下&#xff0c;由于Mac自帶了svn的服務器端和客戶端功能&#xff0c;所以我們可以在不裝任何第三方軟件…

SQL手冊

一.SQL簡介 二.SQL數據類型 三.SQL語法 四.SQL SELECT語句 五.SQL INSERT語句 六.SQL UPDATE語句 七.SQL DELETE語句 八.SQL DROP語句 九.SQL CREDTE語句 十.SQL ALTER 語句總結 十一.SQL事務 十二.函數總結 十三.數據庫其他操作 十四.MySQL 、SQL MS Access、和 SQL Server 數…