<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.6.xsd" >spring
<context:component-scan base-package="com.cana.message.server.test.query" /> <!--若是不指定這個admin,只要隊列不存在,spring容器啓動失敗,也就是說隊列的自動聲明須要admin --> <rabbit:admin id="admin" connection-factory="connectionFactory" /> <rabbit:connection-factory id="connectionFactory" host="192.168.1.7" username="admin" password="admin" port="5672" virtual-host="vbam" channel-cache-size="10"/> <bean id="jsonMessageConverter" class="org.springframework.amqp.support.converter.JsonMessageConverter"/> <rabbit:queue name="template1Queue" id="template1Queue" durable="true" auto-delete="false" exclusive="false" declared-by="admin"/> <!--exchange看做一條線,queue看做一個點 --> <rabbit:direct-exchange name="template1Exchange"> <rabbit:bindings> <rabbit:binding queue="template1Queue" key="template1Queue" ></rabbit:binding><!--綁定隊列--> <!--<rabbit:binding queue="test2" key='test2'></rabbit:binding>--> </rabbit:bindings> </rabbit:direct-exchange> <rabbit:template id="template1" exchange="template1Exchange" connection-factory="connectionFactory" message-converter="jsonMessageConverter" />
<!--自定義消息接受者-->
<bean id="template1Listener" class="com.cana.message.server.test.query.Template1Listener"/>
<!-- queue監聽器,觀察監聽模式。當有消息到達本應用時會通知監聽在對應隊列queue上的監聽對象 -->
<rabbit:listener-container connection-factory="connectionFactory" acknowledge="manual" >
<rabbit:listener queues="template1Queue" ref="template1Listener" />
</rabbit:listener-container> json
</beans>spring-mvc
監聽類(消費者類)br/>@Component
public class Template1Listener implements ChannelAwareMessageListener {
private static final Logger log = LoggerFactory.getLogger(Template1Listener.class);br/>@Override
public void onMessage(Message message,Channel channel) {
System.out.println("監聽到一個消息");
try {
//channel.basicAck(1L, false);
} catch (Exception e) {
e.printStackTrace();
}
}
}mvc
生產者類(測試類)br/>@RunWith(SpringJUnit4Cla***unner.class)
@ContextConfiguration(locations = { "classpath:spring/*.xml" })
public class MessageTest {ide
@Autowired private Template1Producer template1Producer; @Test public void m1() { while(true) { try { Thread.sleep(2000L); } catch (InterruptedException e) { e.printStackTrace(); } template1Producer.send(new Date()); } }
}測試