详解spring boot整合JMS(ActiveMQ实现)

时间:2021-10-23 05:50:47

本文介绍了spring boot整合jms(activemq实现),分享给大家,也给自己留个学习笔记。

一、安装activemq

具体的安装步骤,请参考我的另一篇文章:http://www.zzvips.com/article/135795.html

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

详解spring boot整合JMS(ActiveMQ实现)

三、工程结构

详解spring boot整合JMS(ActiveMQ实现)

pom依赖如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
  xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelversion>4.0.0</modelversion>
 
  <groupid>com.chhliu.springboot.jms</groupid>
  <artifactid>springboot-jms</artifactid>
  <version>0.0.1-snapshot</version>
  <packaging>jar</packaging>
 
  <name>springboot-jms</name>
  <description>demo project for spring boot jms</description>
 
  <parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>1.4.3.release</version>
    <relativepath/> <!-- lookup parent from repository -->
  </parent>
 
  <properties>
    <project.build.sourceencoding>utf-8</project.build.sourceencoding>
    <project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
    <java.version>1.7</java.version>
  </properties>
 
  <dependencies>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-activemq</artifactid>
    </dependency>
 
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-test</artifactid>
      <scope>test</scope>
    </dependency>
  </dependencies>
 
  <build>
    <plugins>
      <plugin>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-maven-plugin</artifactid>
      </plugin>
    </plugins>
  </build>
</project>

四、修改application.properties配置文件

?
1
2
3
4
5
6
## 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注入失败
?
1
2
3
4
5
<dependency>
      <groupid>org.apache.activemq</groupid>
      <artifactid>activemq-pool</artifactid>
      <!-- <version>5.7.0</version> -->
    </dependency>

五、消息生产者

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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);
  }
}

六、消息消费者

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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

七、测试

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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!!!");
    }
  }
 
}

测试结果如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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这个消费者来进行下改造,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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(ActiveMQ实现)

进入browse界面观看:

详解spring boot整合JMS(ActiveMQ实现)

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

详解spring boot整合JMS(ActiveMQ实现)

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

九、对producer进行改造

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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);
  }
}

测试结果如下:

?
1
2
3
4
5
6
7
8
9
10
从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!!!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/liuchuanhong1/article/details/54603546