RabbitMQ與AMQP協議詳解能夠看看這個 http://www.cnblogs.com/franky... html
下面是rabbitMQ和spring集成的配置,我配置了二種ExCahange: topicExchange和directExChangejava
<?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:context="http://www.springframework.org/schema/context" 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/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd"> <description>Spring公共配置 </description> <!--配置connection-factory,指定鏈接rabbit server參數 --> <rabbit:connection-factory id="connectionFactory" host="${rabbit.connect.host}" port="${rabbit.connect.port}" username="${rabbit.connect.username}" password="${rabbit.connect.password}" channel-cache-size="${rabbit.connect.channelCacheSize}" publisher-returns="true" publisher-confirms="true" /> <!--經過指定下面的admin信息,當前producer中的exchange和queue會在rabbitmq服務器上自動生成 --> <rabbit:admin connection-factory="connectionFactory" /> <rabbit:template id="amqpTemplate" connection-factory="connectionFactory" /> <!--定義queue 說明:durable:是否持久化 exclusive: 僅建立者能夠使用的私有隊列,斷開後自動刪除 auto_delete: 當全部消費客戶端鏈接斷開後,是否自動刪除隊列--> <rabbit:queue name="mq.asdf" durable="true" auto-delete="false" exclusive="false" /> <rabbit:queue name="mq.asdf2" durable="true" auto-delete="false" exclusive="false" /> <rabbit:queue name="mq.qwer" durable="true" auto-delete="false" exclusive="false" /> <!--定義topic-exchange --> <rabbit:topic-exchange name="mq.asdfExChange" durable="true" auto-delete="false"> <rabbit:bindings> <rabbit:binding queue="mq.asdf" pattern="mq.asdf.send"></rabbit:binding> <rabbit:binding queue="mq.asdf2" pattern="mq.asdf2.send"></rabbit:binding> <rabbit:binding queue="mq.asdf2" pattern="mq.asdf.send"></rabbit:binding> </rabbit:bindings> </rabbit:topic-exchange> <!--定義direct-exchange --> <rabbit:direct-exchange name="mq.qwerExChange" durable="true" auto-delete="false"> <rabbit:bindings> <rabbit:binding queue="mq.qwer" key="mq.qwer.send" ></rabbit:binding> </rabbit:bindings> </rabbit:direct-exchange> <!-- 消息接收者 --> <bean id="asdfConsumer" class="com.demo.action.AsdfConsumer"></bean> <bean id="asdfConsumer2" class="com.demo.action.AsdfConsumer2"></bean> <bean id="qwerConsumer" class="com.demo.action.QwerConsumer"></bean> <!-- queue litener 觀察 監聽模式 當有消息到達時會通知監聽在對應的隊列上的監聽對象 --> <rabbit:listener-container connection-factory="connectionFactory" > <rabbit:listener queues="mq.asdf" ref="asdfConsumer"/> </rabbit:listener-container> <rabbit:listener-container connection-factory="connectionFactory" > <rabbit:listener queues="mq.asdf2" ref="asdfConsumer2"/> </rabbit:listener-container> <rabbit:listener-container connection-factory="connectionFactory" > <rabbit:listener queues="mq.qwer" ref="qwerConsumer"/> </rabbit:listener-container> </beans>
生成者demo例子 :spring
@Controller @RequestMapping("/amqpTest") public class AmqpTestController { @Autowired private AmqpTemplate amqpTemplate; @RequestMapping("/sendMsg") @ResponseBody public String sendAmqbMsg(Model model,@RequestParam(value="msg",defaultValue="hello world!!!")String msg){ if(model!=null&&!"".equals(msg)){ amqpTemplate.convertAndSend("mq.asdfExChange", "mq.asdf.send", msg); }else{ amqpTemplate.convertAndSend("mq.asdfExChange", "mq.asdf.send", "hello world"); } return "success"; } @RequestMapping("/sendMsg2") @ResponseBody public String sendAmqbMsg2(Model model,@RequestParam(value="msg",defaultValue="hello world!!!")String msg){ if(model!=null&&!"".equals(msg)){ amqpTemplate.convertAndSend("mq.asdfExChange", "mq.asdf2.send", "這個世界很奇妙!!!"); }else{ amqpTemplate.convertAndSend("mq.asdfExChange", "mq.asdf2.send", "這個世界很奇妙"); } return "success"; } @RequestMapping("/sendMsg3") @ResponseBody public String sendAmqbMsg3(Model model,@RequestParam(value="msg",defaultValue="hello world!!!")String msg){ if(model!=null&&!"".equals(msg)){ amqpTemplate.convertAndSend("mq.qwerExChange", "mq.qwer.send", "神奇的世界!!!"); }else{ amqpTemplate.convertAndSend("mq.qwerExChange", "mq.qwer.send", "神奇的世界"); } return "success"; } }
消費者: 服務器
這裏我按照配置列出來一個Demoapp
<bean id="asdfConsumer" class="com.demo.action.AsdfConsumer"> </bean>
消費者code
路徑匹配上就沒有什麼問題.public class AsdfConsumer implements MessageListener{server
static{ System.out.println("已經依賴成功 "); } public void onMessage(Message message) { MessageProperties m=message.getMessageProperties(); // System.out.println(m); String msg= new String (message.getBody()); System.out.println("消費掉了:"+msg+"------->>>>>"); } }