此文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);
}
}