SpringBoot整合activemq

目录

步骤

1 加坐标 

2 配置

3 activemq使用

业务层和表现层完整代码

测试

4 设置消息存放位置名

5 消息监听

 6 消费后再发送到别的队列

 7 使用发布订阅模型

 总结


​​​​​​​关于activemq的安装在这里

步骤


1 加坐标 

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


2 配置

spring:
  activemq:
#    tcp协议 activemq服务端口:61616
    broker-url: tcp://localhost:61616
#    默认保存位置
  jms:
    template:
      default-destination: qing


3 activemq使用

package com.qing.service.impl;

import com.qing.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

@Service
public class MessageServiceActivemqImpl implements MessageService {

    //使用JMS技术
    @Autowired
    private JmsMessagingTemplate messagingTemplate;

    //使消息进入消息队列
    @Override
    public void sendMessage(String id) {
        System.out.println(id + "的message已加入到消息队列");
        //类型转换and send
        messagingTemplate.convertAndSend(id);


    }

    @Override
    public String doMessage() {
        //先接收再类型转换
        //做完了就remove出去了
        String id = messagingTemplate.receiveAndConvert(String.class);
        System.out.println("已完成短信发送业务" + id);
        return id;
    }
}

业务层和表现层完整代码

package com.qing.service.impl;

import com.qing.service.MessageService;
import com.qing.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private MessageService messageService;

    //发消息
    @Override
    public void order(String id) {
        System.out.println("订单处理开始");
        messageService.sendMessage(id);
        System.out.println("订单处理结束");
        System.out.println("-------------------");
    }
}

 OrderController 用来生产消息

package com.qing.controller;

import com.qing.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/orders")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("{id}")
    public void order(@PathVariable String id){

        //发消息
        orderService.order(id);
    }


}

MessageController用来消费消息
package com.qing.controller;

import com.qing.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/msgs")
public class MessageController {
    @Autowired
    private MessageService messageService;

    @GetMapping
    public String doMessage(){
        //处理消息
        String id = messageService.doMessage();
        return id;
    }
}

测试

 

 

 id 为7的消息也被消费了

 

4 设置消息存放位置名

设置上就不用配置里默认的名字了

5 消息监听

 监听到生产了消息,自动马上消费

package com.qing.service.impl;

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

@Component
public class ActivemqListener {

    //参数是消息放的位置
    @JmsListener(destination = "order.queueId")
    public void receive(String id) {
        System.out.println("自动消费" + id);

    }

}

 6 消费后再发送到别的队列

package com.qing.service.impl;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;

@Component
public class ActivemqListener {

    //参数是消息放的位置
    @JmsListener(destination = "order.queueId")
    //将该方法的返回值 消费后 再发送到其他队列order.otherQueue
    @SendTo("order.otherQueue")
    public String receive(String id) {
        System.out.println("自动消费" + id);
        return "new" + id;

    }

}

 

 

 

 备注:以上使用的是点对点模型

点对点模型:队列的消息只能被1个消费者消费

 7 使用发布订阅模型

server:
  port: 80
spring:
  activemq:
#    tcp协议 activemq服务端口:61616
    broker-url: tcp://localhost:61616
#    默认保存位置
  jms:
    template:
      default-destination: qing
#    使用发布订阅模型 (不写下面的默认是点对点模型)
    pub-sub-domain: true

 点对点模型:队列的消息只能被1个消费者消费
 发布订阅模型:消息可以被多个消费者消费,生产者和消费者完全独立,不需要感知对方的存在

 测试前

 使用发布订阅模型后

发送5次消息后 (这里的数据不变)

 看这里

 总结

 生产消息用下面红框的

 消费消息用下面的

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值