ActiveMQ学习(四)SpringBoot整合ActiveMQ

262 阅读3分钟

​ 本文已参与【新人创作礼】活动,一起开启掘金创作之路。

目录

一、准备

pom文件

二、Queue队列模式

1.生产者

1)yml文件

2)configBean类,返回ActiveMQueue

3)生产者代码 

4)主启动类

5)测试代码

2.消费者

1)yml文件

2)消费者代码

3.添加功能,实现每隔3秒钟,往MQ推送消息

1)生产者代码添加以下

2)主启动类添加如下注释

三、Topic队列模式

1.生产者

1)yml文件

2)configBean类,返回ActiveMQTopic

3)生产者代码

2.消费者

1)pom文件

2)消费者代码(非持久化订阅)

3)配置Bean(持久化订阅)

4)消费者代码


一、准备

pom文件

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

二、Queue队列模式

1.生产者

1)yml文件

# web占用的端口
server:
  port: 7777

spring:
  activemq:
    # activemq的broker的url
    broker-url: tcp://192.168.16.106:61616
    # 连接activemq的broker所需的账号和密码
    user: admin
    password: admin
  jms:
    # 目的地是queue还是topic, false(默认) = queue    true =  topic
    pub-sub-domain: false

#  自定义队列名称。这只是个常量
myqueue: boot-activemq-queue

2)configBean类,返回ActiveMQueue

@Component
public class ConfigBean {
    @Value("${myqueue}")
    private String myQueue;

    @Bean
    public Queue queue(){
        return new ActiveMQQueue(myQueue);
    }
}

3)生产者代码 

@Component
public class Queue_Produce {
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Queue queue;

    public void produceMsg(){
        jmsMessagingTemplate.convertAndSend(queue,"******: "+ UUID.randomUUID().toString().substring(0,6));
    }
}

4)主启动类

@SpringBootApplication
@EnableJms
public class Main_Application {
    public static void main(String[] args) {
        SpringApplication.run(Main_Application.class,args);
    }
}

5)测试代码

@SpringBootTest(classes = MainApp.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestActiveMQ {
    @Autowired
    private Queue_Producer queue_producer;

    @Test
    public void testSend() {
        queue_producer.producerMsg();
    }
}

2.消费者

1)yml文件

server:
  port: 8888
spring:
  activemq:
    # activemq的broker的url
    broker-url: tcp://192.168.16.106:61616
    # 连接activemq的broker所需的账号和密码
    user: admin
    password: admin
  jms:
    # 目的地是queue还是topic, false(默认) = queue    true =  topic
    pub-sub-domain: false

#  自定义队列名称。这只是个常量
myqueue: boot-activemq-queue

2)消费者代码

@Component
public class Queue_Consume {
//监听过后会随着springboot一起启动,有消息就执行加了该注解的方法
    @JmsListener(destination = "${myqueue}")
    public void receive(TextMessage textMessage) throws Exception{
        System.out.println("*******消费者收到消息: "+textMessage.getText());
    }
}

3.添加功能,实现每隔3秒钟,往MQ推送消息

1)生产者代码添加以下

//间隔时间3秒钟定投
    //直接启动主启动类即可
    @Scheduled(fixedDelay = 3000)
    public void produceMsgScheduled(){
        jmsMessagingTemplate.convertAndSend(queue,"******Scheduled: "+ UUID.randomUUID().toString().substring(0,6));
        System.out.println("*******Scheduled send ok 3s");
    }

2)主启动类添加如下注释

//是否开启定时任务调度功能
@EnableScheduling

三、Topic队列模式

1.生产者

1)yml文件

server:
  port: 8888
spring:
  activemq:
    # activemq的broker的url
    broker-url: tcp://192.168.16.106:61616
    # 连接activemq的broker所需的账号和密码
    user: admin
    password: admin
  jms:
    # 目的地是queue还是topic, false(默认) = queue    true =  topic
    pub-sub-domain: true

#  自定义队列名称。这只是个常量
mytopic: boot-activemq-topic

2)configBean类,返回ActiveMQTopic

@Component
public class ConfigBean {
    @Value("${mytopic}")
    private String mytopic;

    @Bean
    public Topic topic(){
        return new ActiveMQTopic(mytopic);
    }
}

3)生产者代码

@Component
public class Topic_Produce {
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Topic topic;

    public void produceMsg(){
        jmsMessagingTemplate.convertAndSend(topic,"******: "+ UUID.randomUUID().toString().substring(0,6));
    }
}

2.消费者

1)pom文件

server:
  port: 7766 #启动第二个消费者将端口改为7766
spring:
  activemq:
    # activemq的broker的url
    broker-url: tcp://192.168.16.106:61616
    # 连接activemq的broker所需的账号和密码
    user: admin
    password: admin
  jms:
    # 目的地是queue还是topic, false(默认) = queue    true =  topic
    pub-sub-domain: true

#  自定义队列名称。这只是个常量
mytopic: boot-activemq-topic

2)消费者代码(非持久化订阅)

@Component
public class Topic_Consume {
    @JmsListener(destination = "${mytopic}")
    public void receive(TextMessage textMessage) throws Exception{
        System.out.println("*******消费者收到消息: "+textMessage.getText());
    }
}

3)配置Bean(持久化订阅)

配置文件的方式无法进行配置持久化订阅。所以需要自己去生成一个持久化订阅

@Component
@EnableJms
public class ActiveMQConfigBean {
    @Value("${spring.activemq.broker-url}")
    private String brokerUrl;
    @Value("${spring.activemq.user}")
    private String user;
    @Value("${spring.activemq.password}")
    private String password;

    public ConnectionFactory connectionFactory(){
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL(brokerUrl);
        connectionFactory.setUserName(user);
        connectionFactory.setPassword(password);
        return connectionFactory;
    }


    @Bean(name = "jmsListenerContainerFactory")
    public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
        DefaultJmsListenerContainerFactory defaultJmsListenerContainerFactory = new DefaultJmsListenerContainerFactory();
        defaultJmsListenerContainerFactory.setConnectionFactory(connectionFactory());
        defaultJmsListenerContainerFactory.setSubscriptionDurable(true);
        defaultJmsListenerContainerFactory.setClientId("我是持久订阅者一号");
        return defaultJmsListenerContainerFactory;
    }
}

4)消费者代码

@Component
public class Topic_Consumer {

    //需要在监听方法指定连接工厂
    @JmsListener(destination = "${mytopic}",containerFactory = "jmsListenerContainerFactory")
    public void consumer(TextMessage textMessage) throws JMSException {
        System.out.println("*******消费者收到消息" + textMessage.getText());
    }
}

\