springboot支持activemq 12

activemq 是常用的消息队列,本文将讲述springboot如何整合activemq。

1、环境约束

  • win10 64位操作系统
  • idea2018.1.5
  • maven-3.0.5
  • jdk-8u162-windows-x64
  • activemq5.12

2、前提约束

3、操作

  • 在pom.xml的增加以下依赖:
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
  • 修改application.properties为application.yml,在application.yml中添加以下内容:
spring:
  activemq:
    broker-url: tcp://localhost:61616
    user: admin
    password: admin
  • 在src/main/java文件夹新增配置类net.wanho.springboot.activemq.activemqdemo.config.ActiveMQConfig.java
package net.wanho.springboot.activemq.activemqdemo.config;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.jms.Topic;

@Configuration
@EnableJms
public class ActiveMQConfig {
    //springboot默认只配置queue类型消息,如果要使用topic类型的消息,则需要配置该bean
    @Bean
    public JmsListenerContainerFactory jmsTopicListenerContainerFactory(ConnectionFactory connectionFactory){
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        //这里必须设置为true,false则表示是queue类型
        factory.setPubSubDomain(true);
        return factory;
    }

    @Bean
    public Queue queue() {
        return new ActiveMQQueue("springboot.queue") ;
    }

    @Bean
    public Topic topic() {
        return new ActiveMQTopic("springboot.topic") ;
    }
}
  • 在src/main/java文件夹新增生产者类net.wanho.springboot.activemq.activemqdemo.Producer.java
package net.wanho.springboot.activemq.activemqdemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.Queue;
import javax.jms.Topic;

@RestController
public class Producer {
    @Autowired
    private JmsMessagingTemplate jmsTemplate;

    @Autowired
    private Queue queue;

    @Autowired
    private Topic topic;

    //发送queue类型消息
    @GetMapping("/queue")
    public void sendQueueMsg(String msg){
        jmsTemplate.convertAndSend(queue, msg);
    }

    //发送topic类型消息
    @GetMapping("/topic")
    public void sendTopicMsg(String msg){
        jmsTemplate.convertAndSend(topic, msg);
    }

}
  • 在src/main.java文件夹新增消费者类net.wanho.springboot.activemq.activemqdemo.Consumer.java
package net.wanho.springboot.activemq.activemqdemo;

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

@Component
public class Consumer {

    //接收queue类型消息
    //destination对应配置类中ActiveMQQueue("springboot.queue")设置的名字
    @JmsListener(destination="springboot.queue")
    public void ListenQueue(String msg){
        System.out.println("接收到queue消息:" + msg);
    }

    //接收topic类型消息
    //destination对应配置类中ActiveMQTopic("springboot.topic")设置的名字
    //containerFactory对应配置类中注册JmsListenerContainerFactory的bean名称
    @JmsListener(destination="springboot.topic", containerFactory = "jmsTopicListenerContainerFactory")
    public void ListenTopic(String msg){
        System.out.println("接收到topic消息:" + msg);
    }
}

4、测试

  • 运行主启动类
  • 在浏览器输入以下url,回车,在控制台便能看到“接收到queue消息:jiangsuwanhe”
http://localhost:8080/queue?msg=jiangsuwanhe

以上是一对一模式,我们马上发布订阅模式的测试。

  • 在浏览器输入以下url,回车,在控制台便能看到“接收到topic消息:helloworld”
http://localhost:8080/topic?msg=helloworld

以上就是springboot整合activemq。

posted @ 2020-03-27 16:16  张力的程序园  阅读(143)  评论(0)    收藏  举报