Spring整合ActiveMQ
- 環境配置
- Spring整合ActiveMQ之佇列生產者
- Spring整合ActiveMQ之佇列消費者
- Spring整合ActiveMQ之主題生產消費
- Spring整合ActiveMQ之監聽器配置
前面三節講了ActiveMQ的安裝、測驗,JMS介紹以及四大組成元素,還有ActiveMQ的可靠性(持久化、事務、簽收),還有Broker等等,
但是光學會還不夠,我們還要把ActiveMQ應用到我們已有的框架中,如Spring和SpringBoot等等,
這一節主要學習如何在Spring中使用ActiveMQ,
環境配置
首先創建一個spring專案,在pom檔案中匯入我們需要的jar包
<!-- activemq所需要的jar包 -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.15.9</version>
</dependency>
<dependency>
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-spring</artifactId>
<version>4.17</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jms -->
<!-- activeMQ對JMS的支持,整合Spring和ActiveMQ -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>5.15.9</version>
</dependency>
然后匯入Spring必須的jar包,
Spring整合ActiveMQ之佇列生產者
- 配置ApplicationContext.xml檔案
<!-- 自動掃描 -->
<context:component-scan base-package="com.zxg.activemq"></context:component-scan>
<!-- 配置生產者 -->
<bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
<property name="connectionFactory">
<!-- 真正可以生產Connection的ConnectionFactory,對應的JMS服務廠商提供 -->
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<!-- tcp://自己的服務器地址:埠-->
<property name="brokerURL" value="tcp://129.*.*.*:61616"></property>
</bean>
</property>
<property name="maxConnections" value="100"></property>
</bean>
<!-- 這個是佇列目的地,點對點的 -->
<bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="spring-active-queue"></constructor-arg>
</bean>
<!-- Spring提供的JMS工具類,他可以進行訊息發送,接收等 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsFactory"></property>
<property name="defaultDestination" ref="destinationQueue"></property>
<property name="messageConverter" >
<bean class="org.springframework.jms.support.converter.SimpleMessageConverter"></bean>
</property>
</bean>
- 配置Service層
public interface SpringMQ_Produce {
void send(String messageStr);
}
@Service("springMQ_Produce")
public class SpringMQ_ProduceImpl implements SpringMQ_Produce{
@Autowired
private JmsTemplate jmsTemplate;
public void send(String messageStr) {
@SuppressWarnings("resource")
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
SpringMQ_ProduceImpl produce = (SpringMQ_ProduceImpl) ctx.getBean("springMQ_Produce");
produce.jmsTemplate.send((session)->{
TextMessage textMessage = session.createTextMessage(messageStr);
return textMessage;
});
System.out.println("send success");
}
}
- 測驗
public class TestProduceService {
private SpringMQ_Produce springMQ_Produce;
ApplicationContext ctx;
{
ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
springMQ_Produce = (SpringMQ_Produce) ctx.getBean("springMQ_Produce");
}
@Test
public void test() {
springMQ_Produce.send("Spring 整合ActiveMQ!!!");
}
}
- 運行結果
然后查看MQ服務器控制臺

發現訊息已經發布成功了,
Spring整合ActiveMQ之佇列消費者
為了方便,就直接在Service層實作測驗
@Service
public class SpringMQ_Consumer {
@Autowired
private JmsTemplate jmsTemplate;
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
SpringMQ_Consumer consumer = (SpringMQ_Consumer) ctx.getBean("springMQ_Consumer");
String re = (String) consumer.jmsTemplate.receiveAndConvert();
System.out.println(re);
}
}
查看運行結果,正好是生產者發布的訊息,并且訊息佇列出隊發生變化,


Spring整合ActiveMQ之主題生產消費
首先修改組態檔applicationContext.xml
<!-- 主題,一對多 -->
<bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg index="0" value="spring-active-topic"></constructor-arg>
</bean>
<!-- Spring提供的JMS工具類,他可以進行訊息發送,接收等 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsFactory"></property>
<!-- <property name="defaultDestination" ref="destinationQueue"></property> -->
<property name="defaultDestination" ref="destinationTopic"></property>
<property name="messageConverter" >
<bean class="org.springframework.jms.support.converter.SimpleMessageConverter"></bean>
</property>
</bean>
然后消費者與生產者的代碼不用改,我們的配置資訊全在applicationContext.xml中,
首先啟動消費者進行監聽
再啟動生產者發布訊息,


Spring整合ActiveMQ之監聽器配置
實作在spring里面實作消費者不啟動,直接通過配置監聽完成,
簡單一點,就是把之前消費者中的監聽器代碼改為使用一個監聽類來實作即可,
首先更改組態檔applicationContext.xml
<!-- 配置監聽程式 -->
<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsFactory"></property>
<property name="destination" ref="destinationQueue"></property>
<!-- public class myMessageListener implements MessageListener -->
<property name="messageListener" ref="myMessageListener"></property>
</bean>
然后寫我們的監聽類
@Component
public class MyMessageListener implements MessageListener{
@Override
public void onMessage(Message message) {
if(null!=message && message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("textMessage:"+textMessage);
}
}
}
現在測驗
只需啟動我們的生產者,查看運行結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/226833.html
標籤:java
下一篇:使用多執行緒計算階乘累加 1!+2!+3!+...+19!+20!。其中一個執行緒計算階乘,另一執行緒實作累加并輸出結果。
