隨筆 - 41  文章 - 7  trackbacks - 0
          <2016年8月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          原文:http://docs.spring.io/spring-amqp/docs/1.6.0.RELEASE/reference/html/

          1. 前言

          Spring AMQP項(xiàng)目將其核心Spring概念應(yīng)用于基于AMQP消息解決方案的開(kāi)發(fā)中.我們提供了一個(gè)發(fā)送和接收消息的高級(jí)抽象模板.同時(shí),我們也提供了消息驅(qū)動(dòng)POJO的支持.這些包有助于AMQP資源的管理,從而提升依賴注入和聲明式配置的使用. 在所有這些情況中,你會(huì)發(fā)現(xiàn)與Spring Framework對(duì)JMS支持是相似的. 對(duì)于其它項(xiàng)目相關(guān)的信息,可訪問(wèn)Spring AMQP項(xiàng)目主頁(yè)

          2. 介紹

          本參考文檔的第一部分是對(duì)Spring AMQP的高層次以及底層的概念的講述,還有一些可使你盡快上手運(yùn)行的代碼片斷.

          2.1 快速瀏覽

          2.1.1 介紹

          5分鐘開(kāi)啟Spring AMQP的旅行.

          前提:

          安裝和運(yùn)行RabbitMQ broker (http://www.rabbitmq.com/download.html). 然后獲取spring-rabbit JAR 以及其依賴包 - 最簡(jiǎn)單的方式是在你的構(gòu)建工具中聲明依賴,如. 對(duì)于Maven來(lái)說(shuō):

          <dependency>
          <groupId>org.springframework.amqp</groupId>
          <artifactId>spring-rabbit</artifactId>
          <version>1.6.0.RELEASE</version></dependency>

          對(duì)于gradle:

          compile 'org.springframework.amqp:spring-rabbit:1.6.0.RELEASE'

          兼容性

          由于默認(rèn)的Spring Framework 版本依賴是4.2.x, Spring AMQP 一般來(lái)說(shuō)可兼容早期版本的Spring Framework.但基于注解的監(jiān)聽(tīng)器和RabbitMessagingTemplate 需要Spring Framework 4.1+.

          類似地,默認(rèn)的amqp-client 版本是3.6.x,但framework 兼容3.4.0+.但依賴于新客戶端版本的功能將無(wú)法使用.注意,這里指的是java client library; 一般來(lái)說(shuō),對(duì)于老版本的broker也可以工作.

          非常非??焖?br />
          使用純java來(lái)發(fā)送和接收消息:
          ConnectionFactory connectionFactory = new CachingConnectionFactory(); 
          AmqpAdmin admin = new RabbitAdmin(connectionFactory);
          admin.declareQueue(new Queue("myqueue"));
          AmqpTemplate template = new RabbitTemplate(connectionFactory);
          template.convertAndSend("myqueue", "foo");
          String foo = (String) template.receiveAndConvert("myqueue");

          注意,在原生的Java Rabbit client中也有一個(gè)ConnectionFactory. 在上面的代碼中,我們使用的是Spring 抽象. 我們依賴的是broker中的默認(rèn)交換器 (因?yàn)樵诎l(fā)送操作中沒(méi)有指定交換器),以及默認(rèn)綁定(通過(guò)其名稱作為路由鍵將所有隊(duì)列綁定到默認(rèn)交換器中).那些行為是由AMQP規(guī)范來(lái)定義的.

          使用 XML配置

          同上面的例子一樣,只不過(guò)將外部資源配置到了XML:

          ApplicationContext context =new GenericXmlApplicationContext("classpath:/rabbit-context.xml"); 
          AmqpTemplate template = context.getBean(AmqpTemplate.class);
          template.convertAndSend("myqueue", "foo");
          String foo = (String) template.receiveAndConvert("myqueue");

          <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/rabbit            
          http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd"
          >
          <rabbit:connection-factory id="connectionFactory"/>
          <rabbit:template id="amqpTemplate"connection-factory="connectionFactory"/>
          <rabbit:admin connection-factory="connectionFactory"/>
          <rabbit:queue name="myqueue"/>
          </beans>

          <rabbit:admin/> 聲明會(huì)默認(rèn)自動(dòng)查找類型為QueueExchange 和Binding的bean, 并宣稱他們代表的broker的用戶, 因此在簡(jiǎn)單的Java driver中沒(méi)有必要明確的使用那個(gè)bean.
          有大量的選項(xiàng)來(lái)配置XML schema 中的組件屬性- 你可以使用xml編輯的自動(dòng)完成功能來(lái)探索它們并查看它們的相關(guān)文檔.

          使用 Java 配置

          同樣的例子可使用java來(lái)外部配置:
          ApplicationContext context = new AnnotationConfigApplicationContext(RabbitConfiguration.class);
          AmqpTemplate template = context.getBean(AmqpTemplate.class);
          template.convertAndSend("myqueue", "foo");
          String foo = (String) template.receiveAndConvert("myqueue");
          
          ........
          
          @Configuration
          public class RabbitConfiguration {
          
              @Bean
           public ConnectionFactory connectionFactory() {
                  returnnew CachingConnectionFactory("localhost");
              }
          
              @Bean
           public AmqpAdmin amqpAdmin() {
                  returnnew RabbitAdmin(connectionFactory());
           }
          
              @Bean
           public RabbitTemplate rabbitTemplate() {
                  returnnew RabbitTemplate(connectionFactory());
              }
          
              @Bean
           public Queue myQueue() {
                 returnnew Queue("myqueue");
              }
          }

          2.2 新特性

          2.2.1從1.5到1.6的變化

          測(cè)試支持

          提供了一個(gè)新的測(cè)試支持包. 參考Section 3.4, “Testing Support” 來(lái)了解更多信息.

          Builder

          現(xiàn)在Builders提供了更方便的API來(lái)配置Queue 和 Exchange 對(duì)象. 參考the section called “Builder API for Queues and Exchanges” 來(lái)了解更多信息.

          命名空間變化

          連接工廠

          為連接工廠bean聲明增加了thread-factory, 例如,以amqp-client創(chuàng)建的線程命名參考Section 3.1.2, “Connection and Resource Management” 來(lái)了解更多信息.

          當(dāng)使用CacheMode.CONNECTION時(shí),現(xiàn)在你可以限制允許連接的總數(shù)了. 參考Section 3.1.2, “Connection and Resource Management” 來(lái)了解更多信息.

          Queue 定義

          現(xiàn)在為匿名隊(duì)列提供了命名策略; 參考 the section called “AnonymousQueue” 來(lái)了解更多信息.

          監(jiān)聽(tīng)器容器變化

          空閑消息監(jiān)聽(tīng)器檢測(cè)

          當(dāng)空閑時(shí),現(xiàn)在可以配置監(jiān)聽(tīng)器容器來(lái)發(fā)布ApplicationEvent 事件了. 參考the section called “Detecting Idle Asynchronous Consumers”來(lái)了解更多信息.

          不匹配的隊(duì)列檢測(cè)

          默認(rèn)情況下,當(dāng)監(jiān)聽(tīng)器容器啟動(dòng)時(shí),如果探測(cè)到了隊(duì)列的mismatched屬性或參數(shù),容器會(huì)記錄異常但會(huì)繼續(xù)監(jiān)聽(tīng).現(xiàn)在容器有了一個(gè)mismatchedQueuesFatal 屬性,當(dāng)啟動(dòng)時(shí)存在問(wèn)題的話,這可以阻止容器(以及上下文)啟動(dòng).如果隨后檢測(cè)到問(wèn)題,它也會(huì)停止容器,如從連接故障中恢復(fù). 參考Section 3.1.15, “Message Listener Container Configuration” 來(lái)了解更多信息.

          監(jiān)聽(tīng)器容器日志

          現(xiàn)在監(jiān)聽(tīng)器容器提供了它的beanName 給內(nèi)部 SimpleAsyncTaskExecutor 作為threadNamePrefix.對(duì)于日志分析非常有用.

          默認(rèn)錯(cuò)誤處理器

          默認(rèn)錯(cuò)誤處理器(ConditionalRejectingErrorHandler) 現(xiàn)在認(rèn)為無(wú)可挽救的@RabbitListener 異常是致命的. 參考Section 3.1.13, “Exception Handling” 來(lái)了解更多信息.

          AutoDeclare 與 RabbitAdmins

          參考 Section 3.1.15, “Message Listener Container Configuration” (autoDeclare) 來(lái)查看在應(yīng)用程序上下文使用RabbitAdmin s的語(yǔ)法變化.

          AmqpTemplate: receive 與 timeout

          AmqpTemplate 和它的RabbitTemplate實(shí)現(xiàn)引入了許多新的帶有timeout的receive() 方法. 參考the section called “Polling Consumer”  來(lái)了解更多信息.

          AsyncRabbitTemplate

          引入了新的AsyncRabbitTemplate. 此模塊提供了許多發(fā)送和接收的方法,其返回值為ListenableFuture,此后可通過(guò)它來(lái)同步或異步地獲取結(jié)果. 參考 the section called “AsyncRabbitTemplate” 來(lái)了解更多信息

          RabbitTemplate 變化

          1.4.1 在broker支持時(shí),引入了Direct reply-to 的能力;它比使用臨時(shí)隊(duì)列來(lái)回應(yīng)更高效. 這個(gè)版本允許你覆蓋這種默認(rèn)行為,通常設(shè)置useTemporaryReplyQueues屬性為true來(lái)使用臨時(shí)隊(duì)列. 參考 the section called “RabbitMQ Direct reply-to” 來(lái)了解更多信息.

          RabbitTemplate 現(xiàn)在支持user-id-expression (userIdExpression 當(dāng)使用Java配置時(shí)). 參考See Validated User-ID RabbitMQ documentationand the section called “Validated User Id” 來(lái)了解更多信息.

          消息屬性

          CorrelationId

          correlationId 消息屬性現(xiàn)在可以是字符串了.參考the section called “Message Properties Converters” 來(lái)了解更多信息.

          長(zhǎng)字符串頭

          以前,DefaultMessagePropertiesConverter 會(huì)將頭轉(zhuǎn)換成(其頭長(zhǎng)度不超過(guò)長(zhǎng)字符串限制(默認(rèn)為1024) )成一個(gè)DataInputStream (實(shí)際上它只是引用LongString DataInputStream).
          在輸出時(shí),這個(gè)頭不會(huì)進(jìn)行轉(zhuǎn)換(除了字符串, 例如. 在java.io.DataInputStream@1d057a39 上調(diào)用toString()方法).

          在這個(gè)版本中, Long LongString 默認(rèn)作為 LongString s ;你可以通過(guò)其getBytes[]toString(), 或 getStream()方法來(lái)訪問(wèn)它的內(nèi)容. 更大的輸入LongString 現(xiàn)在也會(huì)在輸出上正確轉(zhuǎn)換了.

          參考 the section called “Message Properties Converters” 來(lái)了解更多信息.

          Inbound Delivery Mode

          deliveryMode 屬性不再映射MessageProperties.deliveryMode;這是為了避免意外傳播,如果同一個(gè)MessageProperties 對(duì)象用來(lái)發(fā)送出站消息.
          相反, 入站deliveryMode 頭映射為MessageProperties.receivedDeliveryMode.

          參考 the section called “Message Properties Converters” 來(lái)了解更多信息.

          當(dāng)使用注解endpoints時(shí),頭將在名為AmqpHeaders.RECEIVED_DELIVERY_MODE的頭中提供.

          參考the section called “Annotated Endpoint Method Signature” 來(lái)了解更多信息.

          Inbound User ID

          user_id 屬性不再映射MessageProperties.userId; 這是為了避免意外傳播,如果同一個(gè)MessageProperties 對(duì)象用來(lái)發(fā)送出站消息.相反, 入站userId 頭會(huì)映射到MessageProperties.receivedUserId.

          參考 the section called “Message Properties Converters” 來(lái)了解更多信息.

          當(dāng)使用注解endpoints時(shí),頭將在名為AmqpHeaders.RECEIVED_USER_ID的頭中提供.

          參考 the section called “Annotated Endpoint Method Signature”  來(lái)了解更多信息.

          RabbitAdmin 變化

          Declaration Failures

          之間,ignoreDeclarationFailures 標(biāo)志只在channel上發(fā)生IOException 才會(huì)生效(如未匹配參數(shù)). 現(xiàn)在它對(duì)于任何異常都可以生效(如:TimeoutException).
          此外,無(wú)論何時(shí)聲明失敗,都會(huì)發(fā)布DeclarationExceptionEvent.RabbitAdmin 最后聲明事件將作為屬性lastDeclarationExceptionEvent也是可用的

          參考 Section 3.1.10, “Configuring the broker” 來(lái)了解更多信息.

          @RabbitListener Changes

          Multiple Containers per Bean

          當(dāng)使用Java 8+,可以添加多個(gè)@RabbitListener 注解到 @Bean 類或它們的方法上.當(dāng)使用Java 7-,你可以使用 @RabbitListeners 容器注解來(lái)提供同樣的功能.
          參考the section called “@Repeatable @RabbitListener” 來(lái)了解更多信息.

          @SendTo SpEL Expressions

          @SendTo for routing replies with no replyTo property can now be SpEL expressions evaluated against the request/reply. 

          參考 the section called “Reply Management”來(lái)了解更多信息.

          @QueueBinding Improvements

          現(xiàn)在你可以在@QueueBinding 注解中為queues, exchanges 和bindings 指定參數(shù). 

          Header交換器可通過(guò)@QueueBinding來(lái)支持

          參考the section called “Annotation-driven Listener Endpoints” 來(lái)了解更多信息.

          延遲消息交換器(Delayed Message Exchange)

          Spring AMQP 現(xiàn)在有了第一個(gè)支持RabbitMQ Delayed Message Exchange 插件. 參考Section 3.1.11, “Delayed Message Exchange” 來(lái)了解更多信息.

          交換器內(nèi)部標(biāo)志(Exchange internal flag)

          任何Exchange 定義現(xiàn)在都可標(biāo)記為internal ,當(dāng)聲明交換器時(shí),RabbitAdmin 會(huì)將值傳遞給broker. 

          參考 Section 3.1.10, “Configuring the broker”來(lái)了解更多信息.

          CachingConnectionFactory 變化

          CachingConnectionFactory Cache Statistics

           CachingConnectionFactory 現(xiàn)在通過(guò)運(yùn)行時(shí)和JMX提供了cache屬性.參考the section called “Runtime Cache Properties”  來(lái)了解更多信息.

          訪問(wèn)底層RabbitMQ連接工廠

          添加了一個(gè)新的getter方法來(lái)獲取底層factory.這是很有用的,例如,可以添加自定義屬性. 參考 Section 3.1.3, “Adding Custom Client Connection Properties” 來(lái)了解更多信息.

          Channel Cache

          默認(rèn)的channel 緩存大小已從1增加到了25. 參考Section 3.1.2, “Connection and Resource Management”來(lái)了解更多信息.

          此外, SimpleMessageListenerContainer 不再設(shè)置緩存大小至少要與concurrentConsumers的數(shù)目一樣大 - 這是多余的,因?yàn)槿萜飨M(fèi)者channels是不會(huì)緩存的.

          RabbitConnectionFactoryBean

          factory bean現(xiàn)在暴露了一個(gè)屬性來(lái)將client連接屬性添加到由工廠產(chǎn)生的連接中.

          Java 反序列化(Deserialization)

          當(dāng)使用Java反序列化時(shí),現(xiàn)在允許配置類的白名單. 如果你從不可信來(lái)源接收消息,考慮創(chuàng)建白名單是很重要的. 參考 the section called “Java Deserialization” 來(lái)了解更多信息.

          JSON MessageConverter

          改善了JSON消息轉(zhuǎn)換器,現(xiàn)在允許消費(fèi)消息在消息頭中可以沒(méi)有類型(type)信息. 參考the section called “Message Conversion for Annotated Methods” 和 the section called “Jackson2JsonMessageConverter”來(lái)了解更多信息.

          Logging Appenders

          Log4j2

          加入了log4j2 appender,此appenders現(xiàn)在可配置addresses 屬性來(lái)連接broker集群.

          Client 連接屬性

          現(xiàn)在你可以向RabbitMQ連接中加入自定義連接屬性了.

          參考Section 3.2, “Logging Subsystem AMQP Appenders” 來(lái)了解更多信息.

          2.2.2 早期版本

          參考Section A.2, “Previous Releases” 來(lái)了解早期版本的變化.

          posted on 2016-08-13 12:03 胡小軍 閱讀(5929) 評(píng)論(0)  編輯  收藏 所屬分類: RabbitMQ
          主站蜘蛛池模板: 鹤壁市| 成安县| 广河县| 旅游| 巫溪县| 嘉祥县| 鄂伦春自治旗| 成都市| 融水| 白玉县| 黄大仙区| 九龙县| 读书| 汽车| 江津市| 凤城市| 阜康市| 永济市| 荣成市| 灌云县| 卫辉市| 虎林市| 澎湖县| 双流县| 繁峙县| 阳春市| 盖州市| 奈曼旗| 寿光市| 北票市| 通化县| 泸西县| 榆社县| 宿州市| 承德市| 额尔古纳市| 扎囊县| 乌兰浩特市| 兴隆县| 荆州市| 碌曲县|