ActiveMQ之整合Spring

前言

在前面我们讲生产者和消费者的时候,代码编写的都比较基础,开发起来费时费力,下面我们将来介绍如何整合spring来简化开发;

整合配置

pom增加依赖:

 
 
     org.apache.activemq
     activemq-pool
     5.15.10
 
 
 
     org.springframework
     spring-jms
     5.2.1.RELEASE
 
 
 
     org.apache.xbean
     xbean-spring
     4.15
 
 
     org.springframework
     spring-aop
     5.1.5.RELEASE
 
 
 
     org.springframework
     spring-core
     4.3.23.RELEASE
 
 
     org.springframework
     spring-context
     4.3.23.RELEASE
 
 
     org.springframework
     spring-aop
     4.3.23.RELEASE
 
 
     org.springframework
     spring-orm
     4.3.23.RELEASE
 

新建Spring整合activeMQ配置文件:




     
    

    
    
        
            
            
                
                
            
        

        
        
    

    
    
        
        
    

    
    
        
    

    
    
        
        
        
        
        
        
            
        
    

队列生产者和消费者

package com.chaytech.activemq.spring;

import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

/**
 * activeMQ整合spring-生产者
 *
 * @author Chency
 * @email [email protected]
 * @Date 2020/03/24 21:41
 */
@Service
public class SpringJmsProducer {

    @Autowired
    private JmsTemplate jmsTemplate;

    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-activemq.xml");

        SpringJmsProducer springJmsProducer = (SpringJmsProducer)applicationContext.getBean("springJmsProducer");
        springJmsProducer.jmsTemplate.send((session) -> {
            return session.createTextMessage("spring整合activeMQ。。");
        });
    }
}
package com.chaytech.activemq.spring;

import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

/**
 * activeMQ整合spring-消费者
 *
 * @author Chency
 * @email [email protected]
 * @Date 2020/03/24 21:41
 */
@Service
public class SpringJmsConsumer {

    @Autowired
    private JmsTemplate jmsTemplate;

    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-activemq.xml");

        SpringJmsConsumer springJmsConsumer = (SpringJmsConsumer)applicationContext.getBean("springJmsConsumer");

        String message = (String) springJmsConsumer.jmsTemplate.receiveAndConvert();
        System.out.println("spring整合activeMQ消费者:" + message);
    }
}

主题生产者和消费者

主题生产者和消费者代码不用改变,只需修改配置文件即可,修改如下:

 

    




    
    
    
    
    
    
    
        
    

增加主题目的地,并将jmsTemplate中的defaultDestination指向我们新增的destinationTopic

消费者监听器

前面我们消费者在消费消息的时候用的阻塞方式,下面将演示如何使用监听器的方式来消费消息,一旦配置了监听器,就不需要启动消费者了,生产者在发送消息的时候,监听器会自动消费

首先我们要先自定义一个消息监听器:

package com.chaytech.activemq.spring;

import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
 * 自定义消息监听器
 *
 * @author Chency
 * @email [email protected]
 * @Date 2020/03/24 22:05
 */
@Component
public class CustomerMessageListener implements MessageListener {

    @Override
    public void onMessage(Message message) {
        if(message != null && message  instanceof TextMessage){
            TextMessage textMessage = (TextMessage) message;
            try {
                System.out.println("自定义消息监听器监听到的消息:" + textMessage.getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }
}

修改配置文件,增加监听器配置:



    
    
    

你可能感兴趣的:(ActiveMQ)