美文网首页SpringBoot极简教程 · Spring Boot 晴空万里
SpringBoot整合ActiveMQ:专业负责MQ20年~

SpringBoot整合ActiveMQ:专业负责MQ20年~

作者: Mr_Elliot | 来源:发表于2018-08-27 17:14 被阅读3次

    ACTIVEMQ

    ActiveMQ

    下载MQ后启动一下(Windows版本)

    #启动信息
    ...
    jvm 1    |  INFO | Listening for connections at ws://JackWen:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600
    jvm 1    |  INFO | Connector ws started
    jvm 1    |  INFO | Apache ActiveMQ 5.14.0 (localhost, ID:JackWen-3764-1535341346100-0:1) started
    jvm 1    |  INFO | For help or more information please see: http://activemq.apache.org
    jvm 1    |  WARN | Store limit is 102400 mb (current store usage is 4 mb). The data directory: D:\ActiveMQ\apache-activemq-5.14.0\bin\win32\..\..\data\kahadb only has 56508 mb of usable space. - resetting to maximum available disk space: 56508 mb
    jvm 1    |  INFO | No Spring WebApplicationInitializer types detected on classpath
    jvm 1    |  INFO | ActiveMQ WebConsole available at http://0.0.0.0:8161/
    jvm 1    |  INFO | ActiveMQ Jolokia REST API available at http://0.0.0.0:8161/api/jolokia/
    jvm 1    |  INFO | Initializing Spring FrameworkServlet 'dispatcher'
    jvm 1    |  INFO | No Spring WebApplicationInitializer types detected on classpath
    jvm 1    |  INFO | jolokia-agent: Using policy access restrictor classpath:/jolokia-access.xml
    jvm 1    |  INFO | Connector vm://localhost started
    jvm 1    |  WARN | Transport Connection to: tcp://127.0.0.1:4013 failed: java.net.SocketException: Connection reset
    
    

    访问:http://localhost:8161/

    与SpringBoot项目整合

    pom.xml

    <!--activeMQ-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-activemq</artifactId>
            </dependency>
            <dependency>
                <groupId>org.apache.activemq</groupId>
                <artifactId>activemq-pool</artifactId>
            </dependency>
    

    application.properties

    #ActiveMQ
    spring.activemq.broker-url=tcp://localhost:61616
    
    mq

    生产者:implements MessageCreator

    /**
     * 消息生产者
     */
    public class Producer implements MessageCreator {
        @Override
        public Message createMessage(Session session) throws JMSException {
            return session.createTextMessage("消息生产者创建的一条消息");
        }
    }
    

    消费者:@JmsListener(destination = "xxx")

    @Component
    public class Consumer {
        @JmsListener(destination = "message_LPMQ")
        public void getMessage(String message){
            System.out.println(message);
        }
    }
    

    SpringBoot启动类测试:@Autowired JmsTemplate and implements CommandLineRunner

    @SpringBootApplication
    public class SpringbootdemoApplication implements CommandLineRunner {
     public static void main(String[] args) {
    
            SpringApplication.run(SpringbootdemoApplication.class, args);
        }
    
        /**
         * MQ: 发送消息
         */
        @Autowired
        private JmsTemplate jmsTemplate;
    
      
        @Override
        public void run(String... args) throws Exception {
            jmsTemplate.send("message_LPMQ",new Producer());
        }
    }
    
    test
    消息中心

    相关文章

      网友评论

        本文标题:SpringBoot整合ActiveMQ:专业负责MQ20年~

        本文链接:https://www.haomeiwen.com/subject/pqjtwftx.html