ActiveMQ整合Spring(多消费者)

完整项目下载:activemq和spring整合
项目目录:
src/test/java目录下有main方法测试发送 接受消息
com.ryx.amp包下有发送和监听的两个类  可以使用main方法测试
配置文件中已配置:多个消费者(多线程消费队列),activemq消息异常重发策略

整合过程

maven依赖



	org.apache.activemq
	activemq-all
	5.11.1


	org.apache.activemq
	activemq-pool
	5.11.1

applicationContext-amq.xml



	
	
	
	
	
	
		
        
        
         
          
        
	
	
	  
      
          
              
                  
                
                
              
          
    
    
      
          
          
          
          
          
          
      
	
	
	
		
		
	
	
	
	
		
		
		
	
	
	
	
	
	  
	      
	      
	    
	    
        
        
        
        
        
	 
	
	
	
	
	
		
	
	
	
	
		
			${queueName}
		
	
	
	
	


	
		
		
		
		
		
		
		
		
		
		
		
	
	

Web.xml



	contextConfigLocation
	classpath:spring/applicationContext-*.xml

ProducerMessageSender.java

package com.ryx.amq;


import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;


public class ProducerMessageSender {


	@Autowired
	private JmsTemplate jmsTemplate;
	
	public void SendMessage(final Integer count){
		jmsTemplate.send(new MessageCreator() {
			
			@Override
			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage("这是一首简单的小情歌"+"---->"+count);
			}
		});
	}
}

ConsumerSessionAwareMessageListener.java

package com.ryx.amq;


import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;


import org.apache.activemq.command.ActiveMQTextMessage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.SessionAwareMessageListener;


/**
 * 队列监听器
 * @author broadthinking
 *
 */
public class ConsumerSessionAwareMessageListener implements SessionAwareMessageListener {


	private static final Log log = LogFactory.getLog(ConsumerSessionAwareMessageListener.class);


	@Autowired
	private JmsTemplate activeMqJmsTemplate;
	@Autowired
	private Destination sessionAwareQueue;


	public synchronized void onMessage(Message message, Session session) throws JMSException {
			ActiveMQTextMessage msg = (ActiveMQTextMessage) message;
			final String ms = msg.getText();
			System.out.println(Thread.currentThread().getName()+"------>"+ms);
			//这里是测试异常重发
			//throw new RuntimeException();
	}
}


你可能感兴趣的:(ActiveMQ)