Spring-boot教程(十三)使用整合JMS之ActiveMQ

此文cv自:https://blog.csdn.net/qq_33392133/article/details/78604402

自己还没实践,感觉用到的可能性不大,这里重在学习 手动配置第三方框架,学习@Configuration、@ConfigurationProperties、@PropertySource(value

由于springboot2.0对于activemq只提供了自动配置,所以对于多模块的项目来说实现有困难,所以采用手动配置...

在service模块中操作以下内容。

1、先在pom.xml文件中引包:

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-activemq</artifactId>
    </dependency>
平时我们将ActiveMQ的配置信息写在项目的application.yml或application.properties文件中,如下:
spring:
  activemq:
      broker-url: tcp://192.168.2.30:61616
      in-memory: true
      password: xwx20121025
      user: admin
      pool:
        enabled: false

然后在Appilcation.class中添加注解@EnableJms即可生效使用。 
注意:①消费者类一定要注入到容器;②在监听类中要加入@JmsListener(destination =XXX)注解;③监听的方法一定不能是private) 
此篇文章只讲解使用JavaConfig配置,上面这种请自行百度,有很多例子。

1、配置文件存放在本地磁盘位置 
(E:/XWX/java/conf/activeMQ.properties)

activemq.brokerUrl=tcp://192.168.2.30:61616
activemq.inMemory=false
activemq.pass=admin
activemq.password=admin
activemq.poolEnable=false
activemq.poolExpiryTimeout=10000
activemq.poolIdleTimeout=30000
activemq.poolMaxConnections=50

2、java获取配置文件信息

@Component
@ConfigurationProperties(prefix = "activemq")
@PropertySource(value = "E:/XWX/java/conf/activeMQ.properties")
public class LocalActiveMQProperties {
    private String brokerUrl;
    private boolean inMemory;
    private String user;
    private String password;
    private boolean poolEnable;
    private Long poolExpiryTimeout;
    private Integer poolIdleTimeout;
//getter、setter方法省略
    } 

3、注入LocalActiveMQProperties.class 到容器中 
此处的注入的优先级一定要在ActiveMQConfig之后,否则会报出NULLEXPECTION。由于第5步中新增了个启动avtivemq的注解@EnableActiveMQ,引入了LocalActiveMQProperties.class,所以此处我没有使用@Bean将该类注入到容器中。

4、配置并注入ActiveMQProperties

@Configuration
public class ActiveMQConfig {
    @Autowired
    private LocalActiveMQProperties properties;

    @Primary
    @Bean
    public ActiveMQProperties activeMQProperties() {
        Logger.info("activeMQ消息队列配置:" + JSON.toJSONString(properties), getClass());
        ActiveMQProperties activeMQProperties = new ActiveMQProperties();
        activeMQProperties.setBrokerUrl(properties.getBrokerUrl());
        activeMQProperties.setInMemory(properties.isInMemory());
        if (!StringUtils.isBlank(properties.getUser())) {
            activeMQProperties.setUser(properties.getUser());
        }
        if (!StringUtils.isBlank(properties.getPassword())) {
            activeMQProperties.setUser(properties.getPassword());
        }
        ActiveMQProperties.Pool pool = new ActiveMQProperties.Pool();
        pool.setEnabled(properties.isPoolEnable());
        if (properties.isPoolEnable()) {
            pool.setExpiryTimeout(properties.getPoolExpiryTimeout());
            pool.setIdleTimeout(properties.getPoolIdleTimeout());
            pool.setMaxConnections(properties.getPoolMaxConnections());
        }
        activeMQProperties.setPool(pool);
        return activeMQProperties;
    }
}

@Primary此注解很重要,因为此处进行了ActiveMQProperties 的重新注入,会和springboot默认配置冲突,将新配置信息标识为当前使用,则生效。

5、加入启动注解

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({
        LocalActiveMQProperties.class,
        ActiveMQConfig.class
})
@EnableJms
public @interface EnableActiveMQ {
}

此处,我引入了启动消息队列的注解@EnableJms,则引用@EnableActiveMQ就可以自动开启了消息队列。

6、Application.class中引入@EnableActiveMQ注解 
如下:

@EnableActiveMQ
public class Application
        extends SpringBootServletInitializer {

    public static void main(String... args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application.class);
    }

}

好了,再跳转到第1步前括号里面的注意

猜你喜欢

转载自blog.csdn.net/wqc19920906/article/details/81609973
今日推荐