Springboot整合ActiveMQ

整合步骤

(1)在pom.xml中引入ActiveMQ起步依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
    <version>1.5.6.RELEASE</version>
</dependency>

(2)在Application类中新增方法,配置queue

//配置queue
@Bean
public Queue queue() {
    return new ActiveMQQueue("demo.queue");
}

(3)创建消息生产者


@Controller
@ResponseBody
public class QueueController {
    @Resource
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Resource
    private Queue queue;

    /**
     * 消息生产者
     */
    @RequestMapping("/send")
    public void send(){
        this.jmsMessagingTemplate.convertAndSend(this.queue,"发送消息");
    }
}

(4)创建消息消费者


/**
 * 消息消费者
 */
@Component
public class Consumer {

    @JmsListener(destination = "demo.queue")
    public void readQueue(String text){
        System.out.println(text);
    }
}

测试:启动服务后,在浏览器执行 http://localhost:8080/send 即可看到控制台输出消息提示。Spring Boot内置了ActiveMQ的服务,所以我们不用单独启动也可以执行应用程序。


若想使用外部的服务,在项目的resources目录下的配置文件application.propeties中添加

spring.activemq.broker-url=tcp://192.168.80.10:61616

即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一种轻量级的应用程序框架,可以将主要精力集中在开发业务逻辑上,而不必担心处理框架导致的繁琐情况。而ActiveMQ是一种可靠的、开源的JMS消息队列系统,能够保证传输的消息在多个客户端之间的可靠性。 Spring Boot可以与ActiveMQ轻松集成,使应用程序能够轻松地使用消息中间件来处理消息队列。要在Spring Boot中集成ActiveMQ,需要使用官方提供的ActiveMQ Starter依赖库,该库提供了必要的配置和依赖项。 首先,在pom.xml中添加以下依赖项: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> ``` 接下来,在application.properties文件中添加以下配置项: ``` spring.activemq.broker-url=tcp://localhost:61616 spring.activemq.user=admin spring.activemq.password=admin ``` 这将配置ActiveMQ连接到本地主机的61616端口,并设置默认的用户名和密码。 最后,可以使用Spring的JMS组件来创建JMS连接和消息接收器,以便接收和处理消息。以下是一个简单的示例: ``` @Component public class MessageReceiver { @JmsListener(destination = "myQueue") public void receiveMessage(String message) { System.out.println("Received message: " + message); } } ``` 该类使用Spring的@JmsListener注释来指定要监听的队列,然后使用Spring DI自动注入消息。 综上所述,通过使用Spring Boot和ActiveMQ集成,可以轻松快捷地构建应用程序的消息队列功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值