zoukankan      html  css  js  c++  java
  • Springboot 2.2.1 与activeMq 集成1

      <!-- activeMQ所需要的依赖  start-->
            <!-- spring对jms的支持-->
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-jms -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jms</artifactId>
                <version>5.1.5.RELEASE</version>
            </dependency>
    
    
            <!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-client -->
            <dependency>
                <groupId>org.apache.activemq</groupId>
                <artifactId>activemq-client</artifactId>
                <version>5.15.9</version>
            </dependency>
    

     pom依赖,注意springboot版本。 

    //这里我配置好了,queue和top 两种同时支持

    package com.active.mq;
    
    import org.apache.activemq.ActiveMQConnectionFactory;
    import org.apache.activemq.command.ActiveMQQueue;
    import org.apache.activemq.command.ActiveMQTopic;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.Bean;
    import org.springframework.core.env.Environment;
    import org.springframework.jms.annotation.EnableJms;
    import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
    import org.springframework.jms.config.JmsListenerContainerFactory;
    import org.springframework.jms.core.JmsMessagingTemplate;
    import org.springframework.jms.core.JmsTemplate;
    
    import javax.jms.ConnectionFactory;
    import javax.jms.Queue;
    import javax.jms.Topic;
    
    /**
     * 启动类
     *
     * @author kql
     */
    @EnableJms
    @SpringBootApplication
    public class MqApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MqApplication.class, args);
        }
    
    
        @Autowired
        private Environment env;
    
        @Bean
        public ConnectionFactory connectionFactory() {
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
            connectionFactory.setBrokerURL(env.getProperty("spring.activemq.broker-url"));
            connectionFactory.setUserName(env.getProperty("spring.activemq.user"));
            connectionFactory.setPassword(env.getProperty("spring.activemq.password"));
            return connectionFactory;
        }
    
        @Bean
        public JmsTemplate genJmsTemplate() {
            return new JmsTemplate(connectionFactory());
    
        }
    
        @Bean
        public JmsMessagingTemplate jmsMessageTemplate() {
            return new JmsMessagingTemplate(connectionFactory());
        }
    
        /**
         * 创建一个bean,交给Spring管理
         */
        @Bean
        public Topic topic() {
            return new ActiveMQTopic("video.topic");
        }
    
        /**
         * 创建一个bean,交给Spring管理
         */
    
        @Bean
        public Queue queue() {
            return new ActiveMQQueue("test.queue");
        }
    
    
        /**
         * 同时支持topic与queue
         *
         * @param activeMQConnectionFactory
         * @return
         */
        @Bean
        public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) {
            DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
            bean.setPubSubDomain(true);
            bean.setConnectionFactory(activeMQConnectionFactory);
            return bean;
    
    
        }
    }

    application.yml 配置文件:

    #配置activemq
    spring:
      activemq:
        #activemq的url
        broker-url: tcp://localhost:61616
        #用户名
        user: admin
        #密码
        password: admin
        #是否使用线程池
        pool:
          enabled: false
        #是否信任所有包
        packages:
          trust-all: true
      #默认情况下,activemq使用的是queue模式,如果要使用topic模式,必须设置为true
      #jms:
      #  pub-sub-domain: true
  • 相关阅读:
    OO第三单元博客作业
    OO第二单元博客作业
    OO第一单元博客作业
    OO第四单元总结
    OO第三次作业总结
    OO第二单元作业总结
    OO第一单元作业总结
    面向对象总结博客
    面向对象第三单元总结博客
    面向对象第二单元总结博客
  • 原文地址:https://www.cnblogs.com/woshuaile/p/11907325.html
Copyright © 2011-2022 走看看