栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot整合ActiveMQ

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot整合ActiveMQ

一、准备 pom文件
 
        org.springframework.boot
        spring-boot-starter-activemq
        2.1.5.RELEASE
  
二、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());
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/921846.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号