springboot整合activemq

本文介绍了如何在SpringBoot应用中整合ActiveMQ,包括配置ActiveMQ服务器,设置默认用户密码,添加依赖,创建消息生产者和消费者,以及进行单元测试。通过消息生产者发送消息到目的地,消息消费者监听并处理接收到的消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

本地启动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/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值