ActiveMq与SpringBoot集成
基于SpringBoot快速搭建ActiveMq开发环境,实现消息队列的点对点,发布订阅
SpringBoot基础项目搭建
1.SpringBoot环境搭建
1.pom.xml 配置文件
引入SpringBoot依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<!--打包配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--是否采用内置tomcat-->
<exclusions>
<!--<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>-->
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>7.0.42</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
引入ActiveMq依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
2.SpringBoot启动
AppLoader
/**
* 通过 tomcat启动
* basePackages 指定扫描基础包
* Created by hmily on 2017/8/30.
*/
@EnableConfigurationProperties
@SpringBootApplication
@ComponentScan(basePackages = "com.pcp")
public class AppLoader extends SpringBootServletInitializer {
/**
* 程序入口
*
*/
public static void main(String[] args) {
SpringApplication.run(AppLoader.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(AppLoader.class);
}
}
3.ActiveMq 配置
application.properties
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false
##开启发布订阅模式,Springboot默认Queues,采用代码配置
#spring.jms.pub-sub-domain=true
MqConfig
/**==============================================================================================
* 消息订阅分为:普通订阅,持久订阅
* 普通订阅 :activemq只是向当前启动的消费者发送消息。
* 持久订阅 :记录消费者的信息,实现离线消息的推送
*
*
*
*===============================================================================================
* DESC:activeMq 配置
* USER:hmily
* DATE:2017/12/28
* TIME:14:11
*/
@Configuration
public class MqConfig {
@Autowired
private ActiveMQConnectionFactory connectionFactory;
/**发布定义模式Topic**/
@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerTopic() {
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setPubSubDomain(true);
bean.setConnectionFactory(connectionFactory);
return bean;
}
/**点对点Queue**/
@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerQueue() {
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setConnectionFactory(connectionFactory);
return bean;
}
}
2.ActiveMq使用
1.Producer
/**springBoot ActiveMq 模版**/
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
/**
* 产生Topic
**/
@Test
public void testTopic(){
Destination topic = new ActiveMQTopic("topic.test");
jmsMessagingTemplate.convertAndSend(topic,"hello topic");
}
/**
* 产生Queue
**/
@Test
public void testQueue(){
Destination queue = new ActiveMQQueue("queue.test");
jmsMessagingTemplate.convertAndSend(queue,"hello queue");
}
2.Consumer
/**
* DESC:消息消费者
* USER:hmily
* DATE:2017/12/28
* TIME:14:32
*/
@Service
public class DemoConsumer {
@JmsListener(destination = "topic.test", containerFactory = "jmsListenerContainerTopic")
public void testTopicCusumer(String test){
System.out.println(test);
}
@JmsListener(destination = "queue.test", containerFactory = "jmsListenerContainerQueue")
public void testQueueCusumer(String test){
System.out.println(test);
}
}
网友评论