日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Spring RabbitMQ使用

發布時間:2025/3/20 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring RabbitMQ使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

項目地址
http://spring.io/projects

提供了對AMOP的支持
提供了RabbitMQ的實現
http://spring.io/projects/spring-amqp

配置文件

第一步,定義連接工廠
第二步,定義模板,可以指定交換機或隊列
第三步,定義隊列、交換機、以及完成隊列和交換機的綁定
第四步,定義監聽
第五步,定義管理,用于管理隊列、交換機等

Durable
表示對持久化的配置
True,表示持久化
False,表示非持久化

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"xsi:schemaLocation="http://www.springframework.org/schema/rabbithttp://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsd"><!-- 定義RabbitMQ的連接工廠 --><rabbit:connection-factory id="connectionFactory"host="127.0.0.1" port="5672" username="taotao" password="taotao"virtual-host="/taotao" /><!-- 定義Rabbit模板,指定連接工廠以及定義exchange --><rabbit:template id="amqpTemplate" connection-factory="connectionFactory"exchange="fanoutExchange" /><!-- <rabbit:template id="amqpTemplate" connection-factory="connectionFactory" exchange="fanoutExchange" routing-key="foo.bar" /> --><!-- MQ的管理,包括隊列、交換器等 --><rabbit:admin connection-factory="connectionFactory" /><!-- 定義隊列,自動聲明 --><rabbit:queue name="myQueue" auto-declare="true" /><!-- 定義交換器,自動聲明 --><rabbit:fanout-exchange name="fanoutExchange"auto-declare="true" durable="true"><rabbit:bindings><rabbit:binding queue="myQueue" /></rabbit:bindings></rabbit:fanout-exchange><!-- <rabbit:topic-exchange name="myExchange"> <rabbit:bindings> <rabbit:binding queue="myQueue" pattern="foo.*" /> </rabbit:bindings> </rabbit:topic-exchange> --><!-- 隊列監聽 --><rabbit:listener-container connection-factory="connectionFactory"><rabbit:listener ref="foo" method="listen"queue-names="myQueue" /></rabbit:listener-container><bean id="foo" class="cn.itcast.rabbitmq.spring.Foo" /></beans>

生產者

package cn.itcast.rabbitmq.spring;import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringMain {public static void main(final String... args) throws Exception {AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring/rabbitmq-context.xml");//RabbitMQ模板RabbitTemplate template = ctx.getBean(RabbitTemplate.class);//發送消息template.convertAndSend("Hello, world!");Thread.sleep(1000);// 休眠1秒ctx.destroy(); //容器銷毀} }

消費者

package cn.itcast.rabbitmq.spring;/*** 消費者* @author zhijun**/ public class Foo {//具體執行業務的方法public void listen(String foo) {System.out.println("消費者: " + foo);} }

運行

總結

以上是生活随笔為你收集整理的Spring RabbitMQ使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。