spring的rabbitmq配置

1、applicationContext-base.xml

 




	
	
		
	

	
	

	

	
		
		
			
		
		
			
				
				
				
			
		
		
			
		
		
			
				
				
				
				
				
				
				
				
			
		
	

	
	
		
	

	
	
		
		
		
		
	

	

	
	
		
	

	
	

	
	

	
		
		
		
		
		
		
		
		
			
				
			
		
	
	

	
		
		
		
		
		
		
	

	
		
		
		
	

	

	
	


	
	
		
		
	

	
		
			
				86400000
			
			
				100
			
		
	

	
		
			
		
	

	
		
		
	

	
	
	
		
	

	
		
			
				
				
			
		
		
			
				
			
		
	

	


	
	
	
		
			
		
		
		
		
	
	

	
		
			
				
					classpath*:config/*.properties
				
			
			
		
		
		
	

	
		
			
				
					classpath*:config/*.properties
					classpath*:config/test/*.properties
				
			
			
		
		
		
	

	
		
			
				
					classpath*:config/*.properties
					classpath*:config/production/*.properties
				
			
			
			
			
		
	



 2、Exchanges、routing keys、binding keys的配置

 

 




	
		
	
	
		
			
		
	


	
	
		
			
		
	

 

import javax.annotation.Resource;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 查
 * @version 0.0.0.1
 * @since 2015年3月30日 下午3:22:49
 */
@Service("producerMq")
@Transactional
public class ProducerMq
{

	@Resource
	private AmqpTemplate amqpTemplate;

	//同步示例
	public void sendDataToCrQueue(Object obj)
	{
		amqpTemplate.convertAndSend("my-mq-exchange", "queue_one_key", obj);
	}
	
}

 

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 大
 * @version 0.0.0.1
 * @since 2015年3月30日 下午3:23:12
 */
@Controller
public class MessageController
{
	@Resource
	private ProducerMq producer;

	@RequestMapping("/producer")
	public void producer() throws Exception
	{
		for (int i = 0; i < 100; i++)
		{
			producer.sendDataToCrQueue("data" + i);
		}
	}

}

 

 

 

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;

/**
 * 队列监听器
 * 
 * @author zhongqing.lin
 * @version 0.0.0.1
 * @since 2015年3月30日 下午7:02:59
 */
@Component
public class QueueOneLitener
{

	@RabbitListener(queues = "queue_one", exclusive = false,containerFactory="rabbitListenerContainerFactory",admin="rabbitAdmin")
	//参数中使用@Header获取mesage
	@SendTo("my-mq-exchange1/queue_two_key")
	public org.springframework.messaging.Message data1(Message message)
	{
		System.out.println("headers:" + message.getMessageProperties().toString());
		String data = new String(message.getBody());
		System.out.println("queue_one data:" + data);
		
		return org.springframework.messaging.support.MessageBuilder.withPayload(data).build();
	}
}

 注意:

@SendTo的value填入的值应该是“exchange/routingKey”格式。

  • foo/bar - the replyTo exchange and routingKey.
  • foo/ - the replyTo exchange and default (empty) routingKey.
  • bar or /bar - the replyTo routingKey and default (empty) exchange.
  • / or empty - the replyTo default exchange and default routingKey.

   参考地址:http://docs.spring.io/spring-amqp/reference/htmlsingle/#async-annotation-driven-reply

 

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 队列监听器
 * 
 * @author zhongqing.lin
 * @version 0.0.0.1
 * @since 2015年3月30日 下午7:02:59
 */
@Component
public class QueueTwoLitener
{

	@RabbitListener(queues = "queue_two", exclusive = false)
	//参数中使用@Header获取mesage
	public void onMessage(Message message)
	{
		System.out.println("queue_two data:" + new String(message.getBody()));
	}
}

 

package com.test.rabbit.retry;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.Address;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.RejectAndDontRequeueRecoverer;

import com.test.utils.MessageUtil;
import com.test.utils.PropKeys;

/**
 * 拒绝消息,并回复
 * 
 * @version 0.0.0.1
 * @since 2015年4月21日 下午5:05:35
 */
public class ZkRejectAndRplyToRequeueRecoverer extends RejectAndDontRequeueRecoverer
{
	/** 用于发送拒绝消息状态给请求者 */
	RabbitTemplate replyToTemplate;

	@Override
	public void recover(Message message, Throwable cause)
	{
		MessageProperties mp = message.getMessageProperties();
		if (mp != null && StringUtils.isNotBlank(mp.getReplyTo()) && replyToTemplate != null)
		{
			Map headers = mp.getHeaders();
			System.err.println(headers.toString());
			Object vLang = headers.get(PropKeys.LANG);
			String lang = "en";
			if (vLang != null)
			{
				lang = (String) vLang;
			}
			com.test.utils.Message rejectRespMsg = new com.test.utils.Message(false);
			rejectRespMsg.setPayload(null);
			MessageUtil.changeResult(rejectRespMsg, "test.rabbit.replyto.interceptor.illegal.request", lang);
			Address address = new Address(mp.getReplyTo());
			replyToTemplate.convertAndSend(address.getExchangeName(), address.getRoutingKey(), rejectRespMsg);
		}
		super.recover(message, cause);
	}

	public void setReplyToTemplate(RabbitTemplate replyToTemplate)
	{
		this.replyToTemplate = replyToTemplate;
	}

}

 

你可能感兴趣的:(Java/Spring,Spring,MVC,MQ/RabbitMQ)