Spring Boot with JMS - ActiveMQ

作者: 慢慢来了 | 来源:发表于2017-03-23 16:40 被阅读1361次

今天开始写Spring Boot 整合JMS ActiveMQ,用Spring Boot 来整合也是相当的简单。ActiveMQ是一种开源的,实现了JMS1.1规范的面向消息(MOM)的中间件,为应用程序提供高效的、可扩展的、稳定的和安全的企业级消息通信。

  1. 下载ActiveMQ,官网地址:
    http://activemq.apache.org/

  2. 本地启动ActiveMQ解压后根据自身的环境启动,如下:

ActiveMQ bat ActiveMQ Server

3.打开浏览器登录http://localhost:8161

ActiveMQ

输入帐号密码,默认密码可以通过查看conf文件下面的users.properties文件查看,如下:

users.properties

在application.properties中加入配置

spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin
通过Maven构建Spring Boot 工程并命名spirng-boot-jms,POM引入依赖:
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <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>
编写消息生产者:
import com.spring.boot.jms.producter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.jms.Destination;

/**
 * Description: 消息生产者
 * author: 慢慢来了
 * date:  2017/3/23 14:07
 */
@Component
public class Producter{

    @Autowired
    private JmsMessagingTemplate jmsTemplate;

    public void sendMessage(Destination destination, final String message) {
        jmsTemplate.convertAndSend(destination, message);
    }

}

Destination为接收者队列也就是消息的目的地(消息发送给谁);message消息内容;JmsMessagingTemplate 也可以用JmsTemplate替换,但必须自已在其构造方法中自动配置如:

@Component
public class MyBean {

    private final JmsTemplate jmsTemplate;

    @Autowired
    public MyBean(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }

    // ...

}
编写消息消费者:
package com.spring.boot.jms.consumer;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

/**
 * Description: 消息消费者
 * author: 慢慢来了
 * date:  2017/3/23 14:07
 */
@Component
public class Consumer {

    @JmsListener(destination = "msg.p2p.queue")
    public void processMessage(String content) {

        System.out.println("Receiving a message :" + content);

    }
}

编写单元测试:
package com.spring.boot.jms;

import com.spring.boot.jms.producter.Producter;
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;

import javax.jms.Destination;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpirngBootJmsApplicationTests {

    @Autowired
    private Producter producter;

    @Test
    public void contextLoads() {
        Destination p2pMsg = new ActiveMQQueue("msg.p2p.queue");

        producter.sendMessage(p2pMsg , "hello , this is jms msg");
    }

}

运行结果,消费者输出"Receiving a message :hello , this is jms msg":

Java Test

ActiveMQ:

ActiveMQ Queue
  • 本篇只是演示了单点消息的发送也接收,下篇就会演示消费者收到消息后,处理把结果通知消息生产者;如果文章对你有所帮助请点赞吧~~~~~

参考资料:

http://docs.spring.io/spring-boot/docs/1.5.2.RELEASE/reference/htmlsingle/

相关文章

网友评论

    本文标题:Spring Boot with JMS - ActiveMQ

    本文链接:https://www.haomeiwen.com/subject/hpoonttx.html