2020-12-21

ActiveMq隊列spring實現(xiàn),案列如下

(1)pom.xml引入相關jar

<!-- activeMQ相關 begin-->

<dependency>

<groupId>org.apache.activemq</groupId>

<artifactId>activemq-all</artifactId>

<version>5.11.1</version></dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jms</artifactId>

<version>4.1.4.RELEASE</version>

</dependency>

(2)添加生產者配置activemq-sender.xml


<description>JMS發(fā)布者應用配置</description>


? ? <!-- CachingConnectionFactory 連接工廠 (有緩存功能)-->

? ? <bean id="cachingConnectionFactory"

? ? ? ? class="org.springframework.jms.connection.CachingConnectionFactory">

? ? ? ? <!-- Session緩存數(shù)量 -->

? ? ? ? <property name="sessionCacheSize" value="20" />

? ? ? ? <property name="targetConnectionFactory">?

? ? ? ? ? ? <bean class="org.apache.activemq.ActiveMQConnectionFactory">?

? ? ? ? ? ? ? ? <!-- MQ地址 賬戶名 密碼-->?

? ? ? ? ? ? ? ? <property name="brokerURL" value="tcp://192.168.56.129:61616" />

? ? ? ? ? ? ? ? <property name="userName" value="parry" />

? ? ? ? ? ? ? ? <property name="password" value="parry123" />

? ? ? ? ? ? ? ? <!-- 是否異步發(fā)送 -->

? ? ? ? ? ? ? ? <property name="useAsyncSend" value="true"/>

? ? ? ? ? ? </bean>?

? ? ? ? </property>?

? ? </bean>


? ? <!-- 接收消息的目的地(一個主題)點對點隊列 -->

? ? <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">

? ? ? ? <!-- 設置消息主題的名字 -->

? ? ? ? <constructor-arg index="0" value="messages" />

? ? </bean>


? ? <!-- 接收配置JMS模版 -->

? ? <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">

? ? ? ? <property name="connectionFactory" ref="cachingConnectionFactory" />

? ? ? ? <property name="defaultDestination" ref="destination" />

? ? ? ? <!-- value為true為發(fā)布/訂閱模式疹鳄; value為false為點對點模式-->

? ? ? ? <property name="pubSubDomain" value="false"/>

? ? </bean>

(3)添加消費者配置activemq-consumer.xml

<description>JMS訂閱者應用配置</description>

? ? <!-- CachingConnectionFactory 連接工廠 (有緩存功能)-->

? ? <bean id="cachingConnectionFactory"

? ? ? ? class="org.springframework.jms.connection.CachingConnectionFactory">

? ? ? ? <!-- Session緩存數(shù)量 -->

? ? ? ? <property name="sessionCacheSize" value="20" />

? ? ? ? <property name="targetConnectionFactory">?

? ? ? ? ? ? <bean class="org.apache.activemq.ActiveMQConnectionFactory">?

? ? ? ? ? ? ? ? <!-- MQ地址 賬戶名 密碼-->?

? ? ? ? ? ? ? ? <property name="brokerURL" value="tcp://192.168.56.129:61616" />

? ? ? ? ? ? ? ? <property name="userName" value="parry" />

? ? ? ? ? ? ? ? <property name="password" value="parry123" />

? ? ? ? ? ? ? ? <!-- 是否異步發(fā)送 -->

? ? ? ? ? ? ? ? <property name="useAsyncSend" value="true"/>

? ? ? ? ? ? </bean>?

? ? ? ? </property>?

? ? </bean>


? ? <!-- 接收消息的目的地(一個主題)點對點隊列 -->

? ? <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">

? ? ? ? <!-- 設置消息主題的名字 -->

? ? ? ? <constructor-arg index="0" value="messages" />

? ? </bean>


? ? <!-- 消費者配置 (自己定義) -->

? ? <bean id="consumer" class="com.parry.MQ.funcion.Listener" />


? ? <!-- 消息監(jiān)聽容器 -->

? ? <bean id="myListenerContainer"

? ? ? ? class="org.springframework.jms.listener.DefaultMessageListenerContainer">

? ? ? ? <property name="connectionFactory" ref="cachingConnectionFactory" />

? ? ? ? <property name="destination" ref="destination" />

? ? ? ? <property name="messageListener" ref="consumer" />

? ? ? ? <!-- 如果消息的接收速率,大于消息處理的速率時,可以采取線程池方式 -->

? ? ? ? <property name="taskExecutor" ref="queueMessageExecutor"/>

? ? ? ? <!-- 設置固定的線程數(shù) -->

? ? ? ? <property name="concurrentConsumers" value="30"/>

? ? ? ? <!-- 設置動態(tài)的線程數(shù) -->

? ? ? ? <property name="concurrency" value="20-50"/>

? ? ? ? <!-- 設置最大的線程數(shù) -->

? ? ? ? <property name="maxConcurrentConsumers" value="80"/>

? ? </bean>

? ? <bean id="queueMessageExecutor"

? ? ? ? class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">

? ? ? ? <property name="corePoolSize" value="30" />

? ? ? ? <property name="maxPoolSize" value="80" />

? ? ? ? <property name="daemon" value="true" />

? ? ? ? <property name="keepAliveSeconds" value="120" />

? ? </bean>

∨÷浴(4)新建一個發(fā)送消息的方法

import javax.jms.JMSException;

import javax.jms.Message;

import javax.jms.Session;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jms.core.JmsTemplate;

import org.springframework.jms.core.MessageCreator;

import org.springframework.stereotype.Component;

/**

* 發(fā)送消息

* @author Administrator

*

*/

@Component

public class QueueSender {


? ? @Autowired

? ? private JmsTemplate myJmsTemplate;

? ? /**

? ? * 發(fā)送一條消息到指定的隊列(目標)

? ? *

? ? * @param queueName

? ? *? ? ? ? ? ? 隊列名稱

? ? * @param message

? ? *? ? ? ? ? ? 消息內容

? ? */

? ? public void send(String queueName, final String message) {

? ? ? ? myJmsTemplate.send(queueName, new MessageCreator() {

? ? ? ? ? ? public Message createMessage(Session session) throws JMSException {

? ? ? ? ? ? ? ? return session.createTextMessage(message);

? ? ? ? ? ? }

? ? ? ? });

? ? }

}

 (5)添加監(jiān)聽器

package com.parry.MQ.funcion;

import javax.jms.JMSException;

import javax.jms.Message;

import javax.jms.MessageListener;

import javax.jms.TextMessage;

/**

* 接收者監(jiān)聽類

* @author Administrator

*

*/

public class Listener implements MessageListener {

? ? public void onMessage(Message message) {

? ? ? ? // 業(yè)務處理

? ? ? ? try {

? ? ? ? ? ? TextMessage message2 = (TextMessage) message;

? ? ? ? ? ? System.out.println("接收到信息:" + message2.getText());

? ? ? ? } catch (JMSException e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? }

}

(6)寫個一請求測試一下

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import com.parry.MQ.funcion.QueueSender;

@Controller

public class App {


? ? @Autowired

? ? private QueueSender sender;


? ? @RequestMapping("test")

? ? @ResponseBody

? ? public String Test() {


? ? ? ? sender.send("messages", "你好瘪弓,這是我的第一條消息垫蛆!");

? ? ? ? return "Hello world";

? ? }

}

(7)開啟服務,訪問路徑測試



?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末腺怯,一起剝皮案震驚了整個濱河市袱饭,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌瓢喉,老刑警劉巖宁赤,帶你破解...
    沈念sama閱讀 211,561評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異栓票,居然都是意外死亡决左,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,218評論 3 385
  • 文/潘曉璐 我一進店門走贪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來佛猛,“玉大人,你說我怎么就攤上這事坠狡〖陶遥” “怎么了?”我有些...
    開封第一講書人閱讀 157,162評論 0 348
  • 文/不壞的土叔 我叫張陵逃沿,是天一觀的道長婴渡。 經常有香客問我,道長凯亮,這世上最難降的妖魔是什么边臼? 我笑而不...
    開封第一講書人閱讀 56,470評論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮假消,結果婚禮上柠并,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好臼予,可當我...
    茶點故事閱讀 65,550評論 6 385
  • 文/花漫 我一把揭開白布鸣戴。 她就那樣靜靜地躺著,像睡著了一般粘拾。 火紅的嫁衣襯著肌膚如雪窄锅。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,806評論 1 290
  • 那天半哟,我揣著相機與錄音酬滤,去河邊找鬼极景。 笑死凉敲,一個胖子當著我的面吹牛剩彬,可吹牛的內容都是我干的杆故。 我是一名探鬼主播歌粥,決...
    沈念sama閱讀 38,951評論 3 407
  • 文/蒼蘭香墨 我猛地睜開眼魏宽,長吁一口氣:“原來是場噩夢啊……” “哼呆馁!你這毒婦竟也來了梗摇?” 一聲冷哼從身側響起糯崎,我...
    開封第一講書人閱讀 37,712評論 0 266
  • 序言:老撾萬榮一對情侶失蹤几缭,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后沃呢,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體年栓,經...
    沈念sama閱讀 44,166評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,510評論 2 327
  • 正文 我和宋清朗相戀三年薄霜,在試婚紗的時候發(fā)現(xiàn)自己被綠了某抓。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,643評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡惰瓜,死狀恐怖否副,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情崎坊,我是刑警寧澤备禀,帶...
    沈念sama閱讀 34,306評論 4 330
  • 正文 年R本政府宣布,位于F島的核電站奈揍,受9級特大地震影響曲尸,放射性物質發(fā)生泄漏。R本人自食惡果不足惜男翰,卻給世界環(huán)境...
    茶點故事閱讀 39,930評論 3 313
  • 文/蒙蒙 一另患、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧奏篙,春花似錦柴淘、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,745評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至肺稀,卻和暖如春第股,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背话原。 一陣腳步聲響...
    開封第一講書人閱讀 31,983評論 1 266
  • 我被黑心中介騙來泰國打工夕吻, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人繁仁。 一個月前我還...
    沈念sama閱讀 46,351評論 2 360
  • 正文 我出身青樓涉馅,卻偏偏與公主長得像,于是被迫代替她去往敵國和親黄虱。 傳聞我的和親對象是個殘疾皇子稚矿,可洞房花燭夜當晚...
    茶點故事閱讀 43,509評論 2 348

推薦閱讀更多精彩內容