RabbitMQ入门教程 For Java【8】 - 与Spring集成

来源:互联网 发布:fm2016巨星数据库 编辑:程序博客网 时间:2024/06/10 07:14
<rabbit:connection-factory id="connectionFactory" host="192.168.5.198" publisher-confirms="true" virtual-host="test" username="test" password="1234"><rabbit:queue name="red" durable="true"><rabbit:queue name="blue" durable="true"><bean id="redQueueListener" class="com.aitongyi.customer.RedQueueListener"><bean id="blueQueueListener" class="com.aitongyi.customer.BlueQueueListener"><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-size: 24px;">RabbitMQ入门教程 For Java【8】 - 与Spring集成</span></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-size: 24px;">简介:</span></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">        RabbitMQ在与Spring集成我做了两个项目,一个项目是消息生产者,负责发送消息,另外一个是消息消费者,负责监听消息。大致的流程图如下:</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; text-align: center;"><img src="http://img.blog.csdn.net/20160125174129872?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" style="border: none; max-width: 100%;" /></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">项目环境:</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">Windows7 64bit</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">Eclipse Kepler SR2</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">JDK 1.7</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">Tomcat 7</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">RabbitMQ 3.6.0</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">项目源码地址:</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">生产者:https://github.com/chwshuang/spring-rabbitmq-producer</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">消费者:https://github.com/chwshuang/spring-rabbitmq-customer</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"></p><h1 style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;"><a target=_blank name="t0" style="color: rgb(51, 102, 153);"></a>生产者:</h1><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">        与Spring集成的项目使用的是Maven,只需要一个依赖配置就搞定:</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"></p><div class="dp-highlighter bg_plain" style="font-family: Consolas, 'Courier New', Courier, mono, serif; width: 700.90625px; overflow-y: hidden; overflow-x: auto; padding-top: 1px; opacity: 0.6; position: relative; color: rgb(51, 51, 51); line-height: 26px; margin: 18px 0px !important; background: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/black/plain.gif) 100% 0% no-repeat rgb(51, 51, 51);"><div class="bar" style="position: relative; padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-stretch: normal; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153);"><strong>[plain]</strong> <a target=_blank href="http://blog.csdn.net/chwshuang/article/details/50580718#" class="ViewSource" title="view plain" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">view plain</a><span data-mod="popu_168"> <a target=_blank href="http://blog.csdn.net/chwshuang/article/details/50580718#" class="CopyToClipboard" title="copy" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">copy</a></span><div style="position: absolute; left: 626px; top: 1422px; width: 29px; height: 14px; z-index: 99;"></div><span data-mod="popu_169"> <a target=_blank href="http://blog.csdn.net/chwshuang/article/details/50580718#" class="PrintSource" title="print" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">print</a></span><a target=_blank href="http://blog.csdn.net/chwshuang/article/details/50580718#" class="About" title="?" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">?</a><span class="tracking-ad" data-mod="popu_167"><a target=_blank href="https://code.csdn.net/snippets/1565282" target="_blank" title="在CODE上查看代码片" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><img src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代码片" style="border: none; max-width: 100%; position: relative; top: 1px; left: 2px;" /></a></span><span class="tracking-ad" data-mod="popu_170"><a target=_blank href="https://code.csdn.net/snippets/1565282/fork" target="_blank" title="派生到我的代码片" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><img src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到我的代码片" style="border: none; max-width: 100%; position: relative; top: 2px; left: 2px;" /></a></span></div></div><ol start="1" style="padding: 0px; border: none; position: relative; list-style-position: initial; list-style-image: initial; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important;"><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">             <!-- RabbitMQ -->  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">lt;dependency>  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"><groupId>org.springframework.amqp</groupId>  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"><artifactId>spring-rabbit</artifactId>  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"><version>1.3.5.RELEASE</version>  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">lt;/dependency>  </span></li></ol></div><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">        消息生产者在Spring的配置也比较简单,只需要一个连接工厂和一个连接模版类就搞定了。</span><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"></span><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"></p><div class="dp-highlighter bg_plain" style="font-family: Consolas, 'Courier New', Courier, mono, serif; width: 700.90625px; overflow-y: hidden; overflow-x: auto; padding-top: 1px; opacity: 0.6; position: relative; color: rgb(51, 51, 51); line-height: 26px; margin: 18px 0px !important; background: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/black/plain.gif) 100% 0% no-repeat rgb(51, 51, 51);"><div class="bar" style="position: relative; padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-stretch: normal; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153);"><strong>[plain]</strong> <a target=_blank href="http://blog.csdn.net/chwshuang/article/details/50580718#" class="ViewSource" title="view plain" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">view plain</a><span data-mod="popu_168"> <a target=_blank href="http://blog.csdn.net/chwshuang/article/details/50580718#" class="CopyToClipboard" title="copy" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">copy</a></span><div style="position: absolute; left: 626px; top: 1647px; width: 29px; height: 14px; z-index: 99;"></div><span data-mod="popu_169"> <a target=_blank href="http://blog.csdn.net/chwshuang/article/details/50580718#" class="PrintSource" title="print" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">print</a></span><a target=_blank href="http://blog.csdn.net/chwshuang/article/details/50580718#" class="About" title="?" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">?</a><span class="tracking-ad" data-mod="popu_167"><a target=_blank href="https://code.csdn.net/snippets/1565282" target="_blank" title="在CODE上查看代码片" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><img src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代码片" style="border: none; max-width: 100%; position: relative; top: 1px; left: 2px;" /></a></span><span class="tracking-ad" data-mod="popu_170"><a target=_blank href="https://code.csdn.net/snippets/1565282/fork" target="_blank" title="派生到我的代码片" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><img src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到我的代码片" style="border: none; max-width: 100%; position: relative; top: 2px; left: 2px;" /></a></span></div></div><ol start="1" style="padding: 0px; border: none; position: relative; list-style-position: initial; list-style-image: initial; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important;"><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">       <!-- ========================================RabbitMQ========================================= -->  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"><rabbit:connection-factory id="connectionFactory" host="localhost" publisher-confirms="true" virtual-host="test" username="test" password="1234" />  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"><rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>  </span></li></ol></div><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">        生产者在Spring中使用时,只需要定义一个服务接口实现类即可。</span><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"></span><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"></p><div class="dp-highlighter bg_plain" style="font-family: Consolas, 'Courier New', Courier, mono, serif; width: 700.90625px; overflow-y: hidden; overflow-x: auto; padding-top: 1px; opacity: 0.6; position: relative; color: rgb(51, 51, 51); line-height: 26px; margin: 18px 0px !important; background: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/black/plain.gif) 100% 0% no-repeat rgb(51, 51, 51);"><div class="bar" style="position: relative; padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-stretch: normal; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153);"><strong>[plain]</strong> <a target=_blank href="http://blog.csdn.net/chwshuang/article/details/50580718#" class="ViewSource" title="view plain" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">view plain</a><span data-mod="popu_168"> <a target=_blank href="http://blog.csdn.net/chwshuang/article/details/50580718#" class="CopyToClipboard" title="copy" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">copy</a></span><div style="position: absolute; left: 626px; top: 1907px; width: 29px; height: 14px; z-index: 99;"></div><span data-mod="popu_169"> <a target=_blank href="http://blog.csdn.net/chwshuang/article/details/50580718#" class="PrintSource" title="print" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">print</a></span><a target=_blank href="http://blog.csdn.net/chwshuang/article/details/50580718#" class="About" title="?" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">?</a><span class="tracking-ad" data-mod="popu_167"><a target=_blank href="https://code.csdn.net/snippets/1565282" target="_blank" title="在CODE上查看代码片" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><img src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代码片" style="border: none; max-width: 100%; position: relative; top: 1px; left: 2px;" /></a></span><span class="tracking-ad" data-mod="popu_170"><a target=_blank href="https://code.csdn.net/snippets/1565282/fork" target="_blank" title="派生到我的代码片" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><img src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到我的代码片" style="border: none; max-width: 100%; position: relative; top: 2px; left: 2px;" /></a></span></div></div><ol start="1" style="padding: 0px; border: none; position: relative; list-style-position: initial; list-style-image: initial; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important;"><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">@Service("rabbitService")  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">public class RabbitServiceImpl {  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">    private static Logger log = LoggerFactory.getLogger(RabbitServiceImpl.class);  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">    @Autowired  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">    private RabbitTemplate rabbitTemplate;  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">    /**  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">     * 发送消息  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">     * @param msg 消息内容  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">     * @param routingKey 路由关键字  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">     * void  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">     */  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">    public void setMessage(String msg, String routingKey) {  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">        rabbitTemplate.convertAndSend(routingKey, msg);  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">        log.info("rabbitmq--发送消息完成: routingKey[{}]-msg[{}]", routingKey, msg);  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">    }  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">}  </span></li></ol></div><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">生产者端还需要创建一个测试页面,通过Ajax技术发送消息到生产者端的控制层,由控制层调用消息服务层发送消息。</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"></p><div class="dp-highlighter bg_html" style="font-family: Consolas, 'Courier New', Courier, mono, serif; width: 700.90625px; overflow-y: hidden; overflow-x: auto; padding-top: 1px; opacity: 0.6; position: relative; color: rgb(51, 51, 51); line-height: 26px; margin: 18px 0px !important; background: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/black/html.gif) 100% 0% no-repeat rgb(51, 51, 51);"><div class="bar" style="position: relative; padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-stretch: normal; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153);"><strong>[html]</strong> <a target=_blank href="http://blog.csdn.net/chwshuang/article/details/50580718#" class="ViewSource" title="view plain" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">view plain</a><span data-mod="popu_168"> <a target=_blank href="http://blog.csdn.net/chwshuang/article/details/50580718#" class="CopyToClipboard" title="copy" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">copy</a></span><div style="position: absolute; left: 623px; top: 2370px; width: 29px; height: 14px; z-index: 99;"></div><span data-mod="popu_169"> <a target=_blank href="http://blog.csdn.net/chwshuang/article/details/50580718#" class="PrintSource" title="print" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">print</a></span><a target=_blank href="http://blog.csdn.net/chwshuang/article/details/50580718#" class="About" title="?" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">?</a><span class="tracking-ad" data-mod="popu_167"><a target=_blank href="https://code.csdn.net/snippets/1565282" target="_blank" title="在CODE上查看代码片" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><img src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代码片" style="border: none; max-width: 100%; position: relative; top: 1px; left: 2px;" /></a></span><span class="tracking-ad" data-mod="popu_170"><a target=_blank href="https://code.csdn.net/snippets/1565282/fork" target="_blank" title="派生到我的代码片" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><img src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到我的代码片" style="border: none; max-width: 100%; position: relative; top: 2px; left: 2px;" /></a></span></div></div><ol start="1" class="dp-xml" style="padding: 0px; border: none; position: relative; list-style-position: initial; list-style-image: initial; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important;"><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;"><</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">%@ page </span><span class="attribute" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">contentType</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">=</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">"text/html;charset=UTF-8"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="attribute" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">language</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">=</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">"java"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">%</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"><!DOCTYPE html<span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;"><</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">html</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;"><</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">head</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;"><</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">meta</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="attribute" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">charset</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">=</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">"UTF-8"</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;"><</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">script</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="attribute" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">src</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">=</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">"http://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">></span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;"></</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">script</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;"><</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">title</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">测试</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;"></</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">title</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;"><</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">script</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="attribute" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">type</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">=</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">"text/javascript"</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"> function sendMsg(){  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">     var <span class="attribute" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">t</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> = $("#type").val();  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">     //alert(t);  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">     $.post('/producer/rabbit/setMessage',{msg:'hello world '+t+' rabbit!',type:t}, function(result){  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">         if(result.success){  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">             //alert("发送成功");  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">         }else{  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">             alert("发送失败");  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">         }  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">     },'json');  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"> }  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;"></</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">script</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;"></</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">head</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;"><</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">body</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;"><</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">h1</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">发送消息</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;"></</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">h1</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">></span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;"><</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">hr</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;"><</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">select</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="attribute" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">id</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">=</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">"type"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">   </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;"><</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">option</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="attribute" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">selected</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">=</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">"selected"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="attribute" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">value</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">=</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">"red"</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">red</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;"></</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">option</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;"><</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">option</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="attribute" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">value</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">=</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">"blue"</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">blue</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;"></</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">option</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;"></</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">select</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;"><</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">button</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="attribute" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">id</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">=</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">"send"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="attribute" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">onclick</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">=</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">"sendMsg()"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="attribute" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">value</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">=</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">"发送消息"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="attribute" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">type</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">=</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">"button"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="attribute" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">title</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">=</span><span class="attribute-value" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">"send"</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">发送</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;"></</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">button</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;"></</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">body</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;"></</span><span class="tag-name" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">html</span><span class="tag" style="margin: 0px; padding: 0px; border: none; color: rgb(102, 204, 255); font-weight: bold; background-color: inherit;">></span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li></ol></div><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"></p><h1 style="margin: 0px; padding: 0px; color: rgb(51, 51, 51); font-family: Arial; line-height: 26px;"><a target=_blank name="t1" style="color: rgb(51, 102, 153);"></a>消费者:</h1><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">        RabbitMQ的消息消费者需要监听消息,以及处理收到的消息,所以需要配置一个监听器,声明一个需要监听的队列,和一个消息处理器。消费者端与spring集成的侵入较少。</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"></p><div class="dp-highlighter bg_plain" style="font-family: Consolas, 'Courier New', Courier, mono, serif; width: 700.90625px; overflow-y: hidden; overflow-x: auto; padding-top: 1px; opacity: 0.6; position: relative; color: rgb(51, 51, 51); line-height: 26px; margin: 18px 0px !important; background: url(http://static.blog.csdn.net/scripts/SyntaxHighlighter/styles/images/black/plain.gif) 100% 0% no-repeat rgb(51, 51, 51);"><div class="bar" style="position: relative; padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-stretch: normal; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153);"><strong>[plain]</strong> <a target=_blank href="http://blog.csdn.net/chwshuang/article/details/50580718#" class="ViewSource" title="view plain" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">view plain</a><span data-mod="popu_168"> <a target=_blank href="http://blog.csdn.net/chwshuang/article/details/50580718#" class="CopyToClipboard" title="copy" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">copy</a></span><div style="position: absolute; left: 626px; top: 3227px; width: 29px; height: 14px; z-index: 99;"></div><span data-mod="popu_169"> <a target=_blank href="http://blog.csdn.net/chwshuang/article/details/50580718#" class="PrintSource" title="print" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">print</a></span><a target=_blank href="http://blog.csdn.net/chwshuang/article/details/50580718#" class="About" title="?" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">?</a><span class="tracking-ad" data-mod="popu_167"><a target=_blank href="https://code.csdn.net/snippets/1565282" target="_blank" title="在CODE上查看代码片" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><img src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代码片" style="border: none; max-width: 100%; position: relative; top: 1px; left: 2px;" /></a></span><span class="tracking-ad" data-mod="popu_170"><a target=_blank href="https://code.csdn.net/snippets/1565282/fork" target="_blank" title="派生到我的代码片" style="color: rgb(204, 204, 204); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><img src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到我的代码片" style="border: none; max-width: 100%; position: relative; top: 2px; left: 2px;" /></a></span></div></div><ol start="1" style="padding: 0px; border: none; position: relative; list-style-position: initial; list-style-image: initial; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important;"><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;"><pre name="code" class="plain">   <!-- ========================================RabbitMQ========================================= -->  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">    <!-- 连接工厂 -->  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">    <rabbit:connection-factory id="connectionFactory" host="192.168.5.198" publisher-confirms="true" virtual-host="test" username="test" password="1234" />  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">    <!-- 监听器 -->  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">    <rabbit:listener-container connection-factory="connectionFactory">  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">        <!-- queues是队列名称,可填多个,用逗号隔开, method是ref指定的Bean调用Invoke方法执行的方法名称 -->  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">        <rabbit:listener queues="red" method="onMessage" ref="redQueueListener" />  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">        <rabbit:listener queues="blue" method="onMessage" ref="blueQueueListener" />  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">    </rabbit:listener-container>  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">    <!-- 队列声明 -->  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">    <rabbit:queue name="red" durable="true" />  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">    <!-- 队列声明 -->  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">    <rabbit:queue name="blue" durable="true" />  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">    <!-- 红色监听处理器 -->  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">    <bean id="redQueueListener" class="com.aitongyi.customer.RedQueueListener" />  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">    <!-- 蓝色监听处理器 -->  </span></li><li class="alt" style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(238, 238, 238); line-height: 18px;"><span style="margin: 0px; padding: 0px; border: none; color: rgb(255, 255, 255); background-color: inherit;">    <bean id="blueQueueListener" class="com.aitongyi.customer.BlueQueueListener" />  </span></li></ol></div><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><br style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;" /><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"></p><pre style="white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; line-height: 26px; background-color: rgb(255, 255, 255);">
        消息处理器可以只是一个通过spring管理的普通Bean对象,需要有一个在xml中定义method同名的方法

[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. public class RedQueueListener{  
  2.     private static Logger log = LoggerFactory.getLogger(RedQueueListener.class);  
  3.     /**  
  4.      * 处理消息  
  5.      * @param message  
  6.      * void  
  7.      */  
  8.     public void onMessage(String message) {  
  9.         log.info("RedQueueListener--receved:"  + message);  
  10.     }  
  11. }  


RabbitMQ创建消息环境


        在项目开发完成后,我们需要在RabbitMQ中创建vhost,user、queues,这样,消费者在启动的时候,登录、声明虚拟机、队列就不会报错。

1. 创建test虚拟机,然后点击Name标签,进入虚拟机,添加权限




2. 进入【Admin】菜单【Users】标签创建用户,然后点击用户名称进入详情设置权限




3. 进入【Queues】菜单,创建队列




执行测试:

将生产者和消费者项目添加到Tomcat中,然后启动,在浏览器输入【http://localhost:8080/producer/test.jsp】,进入测试页面,分别发送红色、蓝色的消息到队列,消费者端会显示接收日志



生产者端日志

[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. 2016-01-25 18:09:26 722 [INFO] c.a.p.c.RabbitController - rabbitmq--收到待发送消息: type[blue]-msg[hello world blue rabbit!]  
  2. 2016-01-25 18:09:26 723 [INFO] c.a.p.s.RabbitServiceImpl - rabbitmq--发送消息完成: routingKey[blue]-msg[hello world blue rabbit!]  
  3. 2016-01-25 18:09:28 715 [INFO] c.a.p.c.RabbitController - rabbitmq--收到待发送消息: type[red]-msg[hello world red rabbit!]  
  4. 2016-01-25 18:09:28 716 [INFO] c.a.p.s.RabbitServiceImpl - rabbitmq--发送消息完成: routingKey[red]-msg[hello world red rabbit!]  

消费者端日志

[plain] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. 2016-01-25 18:09:26 727 [INFO] c.a.c.BlueQueueListener - BlueQueueListener Receved:hello world blue rabbit!  
  2. 2016-01-25 18:09:28 719 [INFO] c.a.c.RedQueueListener - RedQueueListener Receved:hello world red r
                                             
0 0
原创粉丝点击