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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

ActiveMQ集成Spring

發布時間:2024/9/30 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ActiveMQ集成Spring 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、環境準備

這里以 Maven 作為項目管理工具來創建工程。

項目目錄結構:

1.1在pom.xml中添加依賴:

<properties><spring-version>4.3.9.RELEASE</spring-version></properties><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring-version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId><version>${spring-version}</version></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-core</artifactId><version>5.7.0</version><!-- 排除 ActiveMQ 自身依賴的 Spring --><exclusions><exclusion><artifactId>spring-context</artifactId><groupId>org.springframework</groupId></exclusion></exclusions></dependency></dependencies>

1.2定義 Spring 的配置文件

以點對點或隊列模型為例進行集成,關于發布者/訂閱者模型會以注釋的形式貼出來。為了減少篇幅,Spring 配置文件中的頭文件不再貼出。

公共的 Spring 配置文件common.xml:

<context:annotation-config/><!-- ActiveMQ 提供的ConnectionFactory --><bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><!-- 配置 brokerURL,這里換為你自己開啟 ActiveMQ 服務的地址--><property name="brokerURL" value="tcp://192.168.248.136:61616"/></bean><!-- 可以理解為 Spring 提供的連接池 --><bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"><property name="targetConnectionFactory" ref="activeMQConnectionFactory"/></bean><!-- 點對點或隊列模型配置隊列的目的地 --><bean id="activeMQQueue" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg name="name" value="spring-jms-queue"/></bean><!--發布者/訂閱者模型配置主題的目的地 --> <bean id="activeMQTopic" class="org.apache.activemq.command.ActiveMQTopic"><constructor-arg name="name" value="spring-jms-topic"/></bean>

生產者 Spring 配置文件producer.xml:

<!-- 導入公共配置 --><import resource="common.xml"/><!-- 配置 JmsTemplate --><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><property name="connectionFactory" ref="connectionFactory"/></bean><!-- 把 ProducerServiceImpl 交給Spring IoC 容器管理--><bean class="com.jas.jms.producer.ProducerServiceImpl"/>

消費者 Spring 配置文件consumer.xml:

<!-- 導入公共配置 --><import resource="common.xml"/><!-- 配置自定義消費者消息監聽器 --><bean id="consumerMessageListener" class="com.jas.jms.consumer.ConsumerMessageListener"/><!-- 配置消息監聽器的容器 --><bean id="container" class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory"/><property name="destination" ref="activeMQQueue"/><!--配置發布者/訂閱者模型的目的地<property name="destination" ref="activeMQTopic"/>--><property name="messageListener" ref="consumerMessageListener"/></bean>

二、編寫對應的代碼

2.1生產者

ProducerService接口

package com.jas.jms.producer;public interface ProducerService {/*** 生產者發送消息* @param message*/void sendMessage(String message); }

ProducerServiceImpl實現類

package com.jas.jms.producer;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator;import javax.annotation.Resource; import javax.jms.*;public class ProducerServiceImpl implements ProducerService {@AutowiredJmsTemplate jmsTemplate;/*** 這里以 @Resource 方式注入目的地對象* 如果是發布者/訂閱者模式,只選要修改 name 中的值為“activeMQTopic”即可*/@Resource(name = "activeMQQueue")Destination destination;@Overridepublic void sendMessage(final String message) {jmsTemplate.send(destination, new MessageCreator() {@Overridepublic Message createMessage(Session session) throws JMSException {TextMessage textMessage = session.createTextMessage(message);return textMessage;}});System.out.println("消息已發送:" + message);} }

生產者調用類Producer

package com.jas.jms.producer;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Producer {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("producer.xml");ProducerService producerService = context.getBean(ProducerService.class);// 生產者發送十次消息for (int i = 0; i < 10; i++) {producerService.sendMessage("test message:" + i);}// 關閉 IoC 容器context.close();} }

2.2消費者

消費者消息監聽ConsumerMessageListener

package com.jas.jms.consumer;import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage;public class ConsumerMessageListener implements MessageListener {@Overridepublic void onMessage(Message message) {TextMessage textMessage = (TextMessage) message;try {System.out.println("接收已接收:" + textMessage.getText());} catch (JMSException e) {e.printStackTrace();}} }

消費者調用類Producer

package com.jas.jms.consumer;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Consumer {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");} }

2.3測試

首先開啟兩個消費者服務,在開啟一個生產者服務。

消費者1輸出:

消費者2輸出:

2.4發布者/訂閱者模型

在common.xml配置文件中已經配置了發布者/訂閱者模式,只需要在consumer.xml配置文件中注釋掉<property name="destination" ref="activeMQQueue"/>,并啟用activeMQQueue。

還需要修改的一個地方是修改ProducerServiceImpl類中Destination destination;上面的注解為@Resource(name = "activeMQTopic")即可。

PS

代碼 GitHub 地址:https://github.com/coderjas/activemq-study
什么是消息隊列(Message queue):https://blog.csdn.net/codejas/article/details/79930909

(完)

總結

以上是生活随笔為你收集整理的ActiveMQ集成Spring的全部內容,希望文章能夠幫你解決所遇到的問題。

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