spring amqp rabbitmq 学习(二) 接收消息

上一篇贴出了发送信息的配置,这一篇看看接受信息的配置

applicationContext-receive.xml配置如下:

上篇中将消息发送到了名称为simpleSend的队列上,所以这里amqpTemplate中的queue定义为simpleSend,另外不管是发送消息还是接受消息都需要声明队列<rabbit:queue name="simpleSend" />

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
	xsi:schemaLocation="http://www.springframework.org/schema/beans    
      http://www.springframework.org/schema/beans/spring-beans.xsd   
      http://www.springframework.org/schema/context   
      http://www.springframework.org/schema/context/spring-context.xsd   
      http://www.springframework.org/schema/aop   
      http://www.springframework.org/schema/aop/spring-aop.xsd   
      http://www.springframework.org/schema/tx   
      http://www.springframework.org/schema/tx/spring-tx.xsd
      http://www.springframework.org/schema/mvc  
      http://www.springframework.org/schema/mvc/spring-mvc.xsd
      http://www.springframework.org/schema/rabbit
	  http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd">

	<rabbit:connection-factory id="connectionFactory" host="192.168.1.175" port="5672" channel-cache-size="25" />

	<rabbit:template id="amqpTemplate" connection-factory="connectionFactory" queue="simpleSend"/>

	<rabbit:admin connection-factory="connectionFactory" />

	<rabbit:queue name="simpleSend" />

	<!-- <rabbit:listener-container connection-factory="connectionFactory">
		<rabbit:listener ref="foo" method="listen" queue-names="simpleSend" />
	</rabbit:listener-container>

	<bean id="foo" class="io.github.itnaxi.amqp.Foo" /> -->

</beans>

 接下来写测试案例HomeControllerReceiveTest:

import io.github.itnaxi.amqp.AbstractConfig;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.test.context.ContextConfiguration;
@ContextConfiguration(locations={"/springMVC.xml","/receive/applicationContext-receive.xml"})
public class HomeControllerReceiveTest extends AbstractConfig{
	
	private final Logger log=Logger.getLogger(HomeControllerReceiveTest.class);
	
	@Resource
	private RabbitTemplate rabbitTemplate;
	
	@Test
	public void testReceive() {
		log.info(rabbitTemplate.receiveAndConvert().toString());
	}

}
执行testReceive可以看到控制台输出如下:
 foo
表示接收成功

 

你可能感兴趣的:(spring,rabbitmq,AMQP)