资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

springboot整合JMS的示例分析

这篇文章将为大家详细讲解有关spring boot整合JMS的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

创新互联专注于英山网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供英山营销型网站建设,英山网站制作、英山网页设计、英山网站官网定制、重庆小程序开发公司服务,打造英山网络公司原创品牌,更为您提供英山网站排名全网营销落地服务。

一、安装ActiveMQ

具体的安装步骤,请参考我的另一篇文章:https://www.jb51.net/article/127117.htm

二、新建spring boot工程,并加入JMS(ActiveMQ)依赖

spring boot整合JMS的示例分析

三、工程结构

spring boot整合JMS的示例分析

pom依赖如下:

 
 
  4.0.0 
 
  com.chhliu.springboot.jms 
  springboot-jms 
  0.0.1-SNAPSHOT 
  jar 
 
  springboot-jms 
  Demo project for Spring Boot Jms 
 
   
    org.springframework.boot 
    spring-boot-starter-parent 
    1.4.3.RELEASE 
      
   
 
   
    UTF-8 
    UTF-8 
    1.7 
   
 
   
     
      org.springframework.boot 
      spring-boot-starter-activemq 
     
 
     
      org.springframework.boot 
      spring-boot-starter-test 
      test 
     
   
 
   
     
       
        org.springframework.boot 
        spring-boot-maven-plugin 
       
     
   

四、修改application.properties配置文件

## URL of the ActiveMQ broker. Auto-generated by default. For instance `tcp://localhost:61616` 
# failover:(tcp://localhost:61616,tcp://localhost:61617) 
# tcp://localhost:61616 
spring.activemq.broker-url=tcp://localhost:61616 
spring.activemq.in-memory=true 
spring.activemq.pool.enabled=false //如果此处设置为true,需要加如下的依赖包,否则会自动配置失败,报JmsMessagingTemplate注入失败
 
      org.apache.activemq 
      activemq-pool 
       
    

五、消息生产者

package com.chhliu.springboot.jms; 
 
import javax.jms.Destination; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.jms.core.JmsMessagingTemplate; 
import org.springframework.stereotype.Service; 
 
@Service("producer") 
public class Producer { 
  @Autowired // 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装 
  private JmsMessagingTemplate jmsTemplate; 
  // 发送消息,destination是发送到的队列,message是待发送的消息 
  public void sendMessage(Destination destination, final String message){ 
    jmsTemplate.convertAndSend(destination, message); 
  } 
}

六、消息消费者

package com.chhliu.springboot.jms; 
 
import org.springframework.jms.annotation.JmsListener; 
import org.springframework.stereotype.Component; 
 
@Component 
public class Consumer { 
    // 使用JmsListener配置消费者监听的队列,其中text是接收到的消息 
  @JmsListener(destination = "mytest.queue") 
  public void receiveQueue(String text) { 
    System.out.println("Consumer收到的报文为:"+text); 
  } 
}

消费者2的代码同上,注意,消息消费者的类上必须加上@Component,或者是@Service,这样的话,消息消费者类就会被委派给Listener类,原理类似于使用SessionAwareMessageListener以及MessageListenerAdapter来实现消息驱动POJO

七、测试

package com.chhliu.springboot.jms; 
 
import javax.jms.Destination; 
 
import org.apache.activemq.command.ActiveMQQueue; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.test.context.SpringBootTest; 
import org.springframework.test.context.junit4.SpringRunner; 
 
@RunWith(SpringRunner.class) 
@SpringBootTest 
public class SpringbootJmsApplicationTests { 
   
  @Autowired 
  private Producer producer; 
   
  @Test 
  public void contextLoads() throws InterruptedException { 
    Destination destination = new ActiveMQQueue("mytest.queue"); 
     
    for(int i=0; i<100; i++){ 
      producer.sendMessage(destination, "myname is chhliu!!!"); 
    } 
  } 
 
}

测试结果如下:

Consumer2收到的报文为:myname is chhliu!!! 
Consumer收到的报文为:myname is chhliu!!! 
Consumer2收到的报文为:myname is chhliu!!! 
Consumer收到的报文为:myname is chhliu!!! 
Consumer2收到的报文为:myname is chhliu!!! 
Consumer收到的报文为:myname is chhliu!!! 
Consumer2收到的报文为:myname is chhliu!!! 
Consumer收到的报文为:myname is chhliu!!! 
Consumer2收到的报文为:myname is chhliu!!! 
Consumer收到的报文为:myname is chhliu!!! 
Consumer2收到的报文为:myname is chhliu!!! 
Consumer收到的报文为:myname is chhliu!!! 
Consumer2收到的报文为:myname is chhliu!!!

经过上面的几个步骤,spring boot和Jms就基本上整合完成了,是不是使用起来很方便了!

八、实现双向队列

1、下面首先来对Consumer2这个消费者来进行下改造,代码如下:

package com.chhliu.springboot.jms; 
 
import org.springframework.jms.annotation.JmsListener; 
import org.springframework.messaging.handler.annotation.SendTo; 
import org.springframework.stereotype.Component; 
 
@Component 
public class Consumer2 { 
 
  @JmsListener(destination = "mytest.queue") 
  @SendTo("out.queue") 
  public String receiveQueue(String text) { 
    System.out.println("Consumer2收到的报文为:"+text); 
    return "return message"+text; 
  } 
}

从上面的代码可以看出,我们在receiveQueue方法上面多加了一个注解@SendTo("out.queue"),该注解的意思是将return回的值,再发送的"out.queue"队列中,下面我们再来跑一下前面的测试,在监控页面中,我们发现,"out.queue"队列中已经有内容了,如下:

spring boot整合JMS的示例分析

进入Browse界面观看:

spring boot整合JMS的示例分析

最后看下收到的具体信息:

spring boot整合JMS的示例分析

我们发现,该队列中的消息,就是我们返回的值!

九、对Producer进行改造

通过上面的示例,我们现在对Producer进行改造,使其既能生产报文,又能消费队列中的报文,代码如下:

package com.chhliu.springboot.jms; 
 
import javax.jms.Destination; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.jms.annotation.JmsListener; 
import org.springframework.jms.core.JmsMessagingTemplate; 
import org.springframework.stereotype.Service; 
 
@Service("producer") 
public class Producer { 
  @Autowired 
  private JmsMessagingTemplate jmsTemplate; 
   
  public void sendMessage(Destination destination, final String message){ 
    jmsTemplate.convertAndSend(destination, message); 
  } 
   
  @JmsListener(destination="out.queue") 
  public void consumerMessage(String text){ 
    System.out.println("从out.queue队列收到的回复报文为:"+text); 
  } 
}

测试结果如下:

从out.queue队列收到的回复报文为:return messagemyname is chhliu!!! 
Consumer收到的报文为:myname is chhliu!!! 
Consumer2收到的报文为:myname is chhliu!!! 
从out.queue队列收到的回复报文为:return messagemyname is chhliu!!! 
Consumer收到的报文为:myname is chhliu!!! 
Consumer2收到的报文为:myname is chhliu!!! 
从out.queue队列收到的回复报文为:return messagemyname is chhliu!!! 
Consumer收到的报文为:myname is chhliu!!! 
Consumer2收到的报文为:myname is chhliu!!! 
从out.queue队列收到的回复报文为:return messagemyname is chhliu!!!

关于“spring boot整合JMS的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


网页名称:springboot整合JMS的示例分析
转载注明:http://cdkjz.cn/article/ihcepe.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220