AvtiveMQ与SpringBoot结合

首先来了解下ActivieMQ的应用场景,消息队列中间件是分布式系统中重要的组件,主要解决应用耦合,异步消息,流量削锋等问题。实现高性能,高可用,可伸缩和最终一致性架构是大型分布式系统不可缺少的中间件。
详细访问传送门
1.下载ActiveMQ访问地址
得到的是一个zip压缩包,解压后根据系统位数选择运行,笔者是64位所以选择64的目录,点击activemq.bat运行

访问网址http://localhost:8161/ 即可
|- 在 xxx\apache-activemq-5.15.3\conf 目录下打开jetty.xml 配置端口用户密码等信息
360截图17571120101119117.png

|- 配置activeMQ信息

  1. 导入依赖
  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
   </dependency>
  1. 生产者
@Component
public class JmsProducer {
   private final static Logger logger = LoggerFactory.getLogger(JMSConsumer.class);
   @Resource
   private JmsTemplate jmsTemplate;

   public void sendMessage(Destination destination, String message) {
       this.jmsTemplate.convertAndSend(destination,message);
   }
}
  1. 消费者
@Component
public class JmsConsumer {

    private final static Logger logger = LoggerFactory.getLogger(JMSConsumer.class);

    @JmsListener(destination = "springboot.queue.test")
    public void receiveQueue(String msg) {
        logger.info("接收到消息:{}",msg);
    }
}
  1. 编写简单测试
@Resource
    private JmsProducer jmsProducer;

    @Test
    public void testJms() {
        Destination destination = new ActiveMQQueue("springboot.queue.test");

        for (int i=0;i<10;i++) {
            jmsProducer.sendMessage(destination,"hello" + i);
        }
    }

6.查看运行
a.png
通过这个简单的实例只能让项目跑起来,下一篇文章带各位深入了解ActiveMQ,如有错误,请评论指正

猜你喜欢

转载自www.cnblogs.com/tanoak/p/10545248.html