Springboot 2.0 整合 ActiveMQ

2 篇文章 0 订阅
1 篇文章 0 订阅

CentOS7 服务器安装 ActiveMQ 5.15.4

wget http://www.apache.org/dyn/closer.cgi?filename=/activemq/5.15.4/apache-activemq-5.15.4-bin.tar.gz&action=download
tar -zxvf apache-activemq-5.15.4-bin.tar.gz
cd apache-activemq-5.15.4/bin/linux-x86-64
./activemq start

在 localhost:8161 可以进入 ActiveMQ 的控制面板

Springboot 2.0 整合

依赖引入

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

<dependency>  
    <groupId>org.apache.activemq</groupId>  
    <artifactId>activemq-pool</artifactId>  
</dependency>

application.properties 配置

# 整合 jms
spring.activemq.broker-url=tcp://10.11.124.102:61616

spring.activemq.user=admin
spring.activemq.password=admin

spring.activemq.pool.enabled=true
spring.activemq.pool.max-connections=100

程序启动类

  • 通过 @EnableJms 开启支持 jms
@EnableJms
@SpringBootApplication
public class QueueApplication {
    ……
}

服务层

接口设计( IProducerService )
public interface IProducerService {

    /**
     * Description: 指定消息队列,以及对应的消息
     * @param destination
     * @param message
     */
    public void sendMessage(Destination destination, final String message);

    /**
     * Description: 使用默认消息队列,发送消息
     * @param message
     */
    public void sendMessage(final String message);

}
具体实现类( ProducerServiceImpl )
@Service(value = "producerService")
public class ProducerServiceImpl implements IProducerService {

    @Autowired
    private JmsMessagingTemplate jmsTemplate;

    @Override
    public void sendMessage(Destination destination,    String message) {
        // TODO Auto-generated method stub
        jmsTemplate.convertAndSend(destination, message);

    }

    @Override
    public void sendMessage(String message) {
        // TODO Auto-generated method stub
        jmsTemplate.convertAndSend(this.queue, message);

    }

}

控制层( OrderController )

@RestController
@RequestMapping(value = "/api")
public class OrderController {

    @Autowired
    private IProducerService producerService;

    @GetMapping(value = "/order")
    public Object order(String msg) {
        Destination destination = new ActiveMQQueue("order.queue");
        producerService.sendMessage(destination, msg);
        return JsonData.buildSuccess();
    }

}

消费者( OrderConsumer )

@Component
public class OrderConsumer {

    @JmsListener(destination = "order.queue")
    public void receiveQueue(String text) {
        System.out.println("OrderConsumer 收到的消息:" + text);
    }

}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值