• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

SpringBoot集成JmsTemplate(队列模式和主题模式)及xml和JavaConfig配置详解

springboot 搞代码 4年前 (2022-01-09) 17次浏览 已收录 0个评论

1.导入jar包:

 <!--jmsTemplate-->
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-activemq</artifactId>
  </dependency>
  <dependency>
   <groupId>org.apache.activemq</groupId>
   <artifactId>activemq-pool</artifactId>
  </dependency>

2.填写配置文件(application.properties)

#设置JMS(AMQ)
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.pool.enabled=true
#spring.jms.pub-sub-domain=true
spring.activemq.pool.max-connections=50
spring.activemq.pool.expiry-timeout=10000
spring.activemq.pool.idle-timeout=30000

上面需要注意的是,如果开启订阅者和发布者模式下面的代码会使监听器失效。

3.编写控制器代码

@RestController
@RequestMapping("/Jms")
public class ProducerController {
 
 @Autowired
 private JmsProducerService jmsProducerService;
 
 @Re<a style="color:transparent">来@源gao*daima.com搞@代#码网</a>questMapping("/send")
 public void contextLoads() throws InterruptedException {
  Destination destination = new ActiveMQQueue("mytest.queue");
  for(int i=0; i<10; i++){
   jmsProducerService.sendMessage(destination, "我是超人啊");
  }
  System.out.println("发送成功");
 }
 
}

4.服务层代码:

package com.zzf.finals.service.impl;
 
import com.zzf.finals.service.JmsProducerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
 
import javax.jms.Destination;
 
@Service
public class JmsProducerServiceImpl implements JmsProducerService {
 
 @Autowired
 private JmsTemplate jmsTemplate;
 
 @Override
 public void sendMessage(Destination destination, String message) {
  this.jmsTemplate.convertAndSend(destination,message);
 }
}

5.最后加上监听器类

package com.zzf.finals.domain;
 
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
 
@Component
public class Consumer {
 
 @JmsListener(destination = "mytest.queue")
 public void receiveQueue(String text) {
  System.out.println("Message:"+text);
 }
}

OK~

但是这样有另外一个问题:如果开启了订阅者和发布者模式则无法发送和接收queue消息。

这里我提供两种写法xml和java配置:

首先贴上我的xml配置代码

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd">
 
 <!--连接池,内部引入一个连接工厂-->
 <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
   destroy-method="stop">
  <property name="connectionFactory">
   <bean class="org.apache.activemq.ActiveMQConnectionFactory">
    <property name="brokerURL">
     <value>tcp://localhost:61616</value>
    </property>
   </bean>
  </property>
  <property name="maxConnections" value="100"></property>
 </bean>
 
 
 <bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
  <constructor-arg name="name" value="spring-queue"/>
 </bean>
 
 <!--测试Topic-->
 <bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">
  <constructor-arg name="name" value="spring-topic"/>
 </bean>
 
 <!--配置消息容器-->
 <bean id="TopicContainers" class="org.springframework.jms.config.DefaultJmsListenerContainerFactory">
  <property name="pubSubDomain" value="true"/>
  <property name="connectionFactory" ref="jmsFactory"/>
 </bean>
 
 <!--配置队列消息容器-->
 <bean id="QueueContainers" class="org.springframework.jms.config.DefaultJmsListenerContainerFactory">
  <property name="connectionFactory" ref="jmsFactory"/>
 </bean>
 
</beans>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:SpringBoot集成JmsTemplate(队列模式和主题模式)及xml和JavaConfig配置详解

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址