`
yodg2gg
  • 浏览: 10272 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

(转)Spring AMQP-快速入门

阅读更多
  • Spring AMQP项目介绍
Spring AMQP项目将Spring的核心思想应用于基于AMQP的消息解决方案的开发上。它提供了“template”这个高度抽象来发送和接收信息。它同样提供了消息驱动的实体,这些实体存在于“listener container”容器中。这些库不但使得AMQP资源的管理变得容易,与此同时促进了依赖注入和声明式配置的使用。在所有的情况下,你将看到许多Spring框架提供的类似于JMS的便利。
这个项目包含两部分:
1、spring-amqp的基础抽象;
2、spring-rabbit这个RabbitMQ的实现
特点:
1、异步处理没有绑定消息的监听容器;
2、RabbitTemplate发送和接收消息;
3、RabbitAdmin自动声明队列,交换和绑定。
 
  • Spring AMQP快速入门
推荐开始在你的项目中使用spring-amqp的快捷方式是使用依赖管理系统,例如Maven和Gradle,配置片段如下:
  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.amqp</groupId>
  4. <artifactId>spring-rabbit</artifactId>
  5. <version>1.4.0.RELEASE</version>
  6. </dependency>
  7. </dependencies>
使用Java编码的方式:
  1. publicstaticvoid main(finalString... args)throwsException{
  2. ConnectionFactory cf =newCachingConnectionFactory();
  3. // set up the queue, exchange, binding on the broker
  4. RabbitAdmin admin =newRabbitAdmin(cf);
  5. Queue queue =newQueue("myQueue");
  6. admin.declareQueue(queue);
  7. TopicExchange exchange =newTopicExchange("myExchange");
  8. admin.declareExchange(exchange);
  9. admin.declareBinding(
  10. BindingBuilder.bind(queue).to(exchange).with("foo.*"));
  11. // set up the listener and container
  12. SimpleMessageListenerContainer container =
  13. newSimpleMessageListenerContainer(cf);
  14. Object listener =newObject(){
  15. publicvoid handleMessage(String foo){
  16. System.out.println(foo);
  17. }
  18. };
  19. MessageListenerAdapter adapter =newMessageListenerAdapter(listener);
  20. container.setMessageListener(adapter);
  21. container.setQueueNames("myQueue");
  22. container.start();
  23. // send something
  24. RabbitTemplate template =newRabbitTemplate(cf);
  25. template.convertAndSend("myExchange","foo.bar","Hello, world!");
  26. Thread.sleep(1000);
  27. container.stop();
  28. }
 
使用Spring的方式:
 
  1. publicstaticvoid main(finalString... args)throwsException{
  2. AbstractApplicationContext ctx =
  3. newClassPathXmlApplicationContext("context.xml");
  4. RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
  5. template.convertAndSend("Hello, world!");
  6. Thread.sleep(1000);
  7. ctx.destroy();
  8. }
 
  1. publicclassFoo{
  2. publicvoid listen(String foo){
  3. System.out.println(foo);
  4. }
  5. }
 

 

  1. <!--context.xml -->
  2. <rabbit:connection-factoryid="connectionFactory"/>
  3. <rabbit:templateid="amqpTemplate"connection-factory="connectionFactory"
  4. exchange="myExchange"routing-key="foo.bar"/>
  5. <rabbit:adminconnection-factory="connectionFactory"/>
  6. <rabbit:queuename="myQueue"/>
  7. <rabbit:topic-exchangename="myExchange">
  8. <rabbit:bindings>
  9. <rabbit:bindingqueue="myQueue"pattern="foo.*"/>
  10. </rabbit:bindings>
  11. </rabbit:topic-exchange>
  12. <rabbit:listener-containerconnection-factory="connectionFactory">
  13. <rabbit:listenerref="foo"method="listen"queue-names="myQueue"/>
  14. </rabbit:listener-container>
  15. <beanid="foo"class="foo.Foo"/>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics