ActiveMq深入简出(二)---- ActiveMq与SpringBoot集成

ActiveMq与SpringBoot集成

基于SpringBoot快速搭建ActiveMq开发环境,实现消息队列的点对点,发布订阅

SpringBoot基础项目搭建

1.SpringBoot环境搭建

1.pom.xml 配置文件

引入SpringBoot依赖

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.8.RELEASE
         
    
 



            org.springframework.boot
            spring-boot
        
        
            org.springframework.boot
            spring-boot-autoconfigure
        
        
        
            org.springframework.boot
            spring-boot-starter-web
            
            
                
            
        

        
            org.apache.tomcat
            tomcat-servlet-api
            7.0.42
            provided
        
        
            org.springframework.boot
            spring-boot-starter-test
        
    
    
        UTF-8
        UTF-8
        1.8
    

引入ActiveMq依赖

 
            org.springframework.boot
            spring-boot-starter-activemq
 
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);
    }
}

你可能感兴趣的:(ActiveMq深入简出(二)---- ActiveMq与SpringBoot集成)