zoukankan      html  css  js  c++  java
  • springboot集成activemq

     

    最近在研究activemq这块,无意中看到了,springboot竟然也做了activemq-starter,心里高兴就快速的实验了一把,只是做了小sample,没有深入研究,上代码。

    pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.eju.ess.sample</groupId>
        <artifactId>producer-sample</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>producer-sample</name>
        <url>http://maven.apache.org</url>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.4.1.RELEASE</version>
        </parent>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-log4j2</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-activemq</artifactId>
                <exclusions>
                    <exclusion>
                        <artifactId>logback-core</artifactId>
                        <groupId>ch.qos.logback</groupId>
                    </exclusion>
                    <exclusion>
                        <artifactId>logback-classic</artifactId>
                        <groupId>ch.qos.logback</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <scope>provided</scope>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                        <encoding>utf8</encoding>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>
    
    

    activemq-starter插件的日志组件使用的是logback,我不太会用,就继续用我的log4j2,因此将他排除掉了。

    producer-sample

    application.properties

    spring.activemq.broker-url=tcp://192.168.0.100:61616
    spring.activemq.user=admin
    spring.activemq.password=admin
    

    Startup.java

    package com.eju.ess;
    
    import javax.jms.Queue;
    
    import lombok.extern.slf4j.Slf4j;
    
    import org.apache.activemq.command.ActiveMQQueue;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.Bean;
    
    @Slf4j
    @SpringBootApplication
    public class Startup {
    
        @Bean
        public Queue queue() {
            return new ActiveMQQueue("sample.queue");
        }
    
        public static void main(String[] args) {
            SpringApplication.run(Startup.class, args);
        }
    }
    
    

    Producer.java

    package com.eju.ess;
    
    import javax.jms.Queue;
    
    import lombok.extern.slf4j.Slf4j;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jms.core.JmsMessagingTemplate;
    import org.springframework.scheduling.annotation.EnableScheduling;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.stereotype.Component;
    
    @Slf4j
    @Component
    @EnableScheduling
    public class Producer {
    
        @Autowired
        private JmsMessagingTemplate jmsMessagingTemplate;
    
        @Autowired
        private Queue queue;
    
        @Scheduled(fixedDelay = 3000)
        // 每3s执行1次
        public void send() {
            log.info(">> send !!!");
            this.jmsMessagingTemplate.convertAndSend(this.queue, "hi,activeMQ");
        }
    
    }
    

    consumer-sample

    application.properties

    spring.activemq.broker-url=tcp://192.168.0.100:61616
    spring.activemq.user=admin
    spring.activemq.password=admin
    

    Startup.java

    package com.eju.ess;
    
    import lombok.extern.slf4j.Slf4j;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @Slf4j
    @SpringBootApplication
    public class Startup {
    
        public static void main(String[] args) {
            SpringApplication.run(Startup.class, args);
        }
    }
    
    

    Consumer.java

    package com.eju.ess;
    
    import lombok.extern.slf4j.Slf4j;
    
    import org.springframework.jms.annotation.JmsListener;
    import org.springframework.stereotype.Component;
    
    @Slf4j
    @Component
    public class Consumer {
        @JmsListener(destination = "sample.queue")
        public void receiveQueue(String text) {
            log.info(">> {}", text);
        }
    }
    
    

    ok,很简单,没有什么要特别说的,下面列举了spring-boot-activemq-starter的所有配置,根据情况选择,

    # ACTIVEMQ (ActiveMQProperties)
    spring.activemq.broker-url= # URL of the ActiveMQ broker. Auto-generated by default. For instance `tcp://localhost:61616`
    spring.activemq.in-memory=true # Specify if the default broker URL should be in memory. Ignored if an explicit broker has been specified.
    spring.activemq.password= # Login password of the broker.
    spring.activemq.user= # Login user of the broker.
    spring.activemq.packages.trust-all=false # Trust all packages.
    spring.activemq.packages.trusted= # Comma-separated list of specific packages to trust (when not trusting all packages).
    spring.activemq.pool.configuration.*= # See PooledConnectionFactory.
    spring.activemq.pool.enabled=false # Whether a PooledConnectionFactory should be created instead of a regular ConnectionFactory.
    spring.activemq.pool.expiry-timeout=0 # Connection expiration timeout in milliseconds.
    spring.activemq.pool.idle-timeout=30000 # Connection idle timeout in milliseconds.
    spring.activemq.pool.max-connections=1 # Maximum number of pooled connections.
    

    下载

  • 相关阅读:
    50个提高PHP程序运行效率的方法
    虚拟主机FTP上传文件为什么要用二进制上传
    Status Bar 总结
    TableView 总结
    阿里Java开发手册(泰山版)个人记录
    下载excel模板
    微信公众号-发送模板消息
    ffmpeg获取视频时长
    微信公众号授权
    根据word模板生成word、转换成pdf、打成war包
  • 原文地址:https://www.cnblogs.com/pejsidney/p/9203481.html
Copyright © 2011-2022 走看看