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. public static void main(final String... args) throws Exception {
  2. ConnectionFactory cf = new CachingConnectionFactory();
  3. // set up the queue, exchange, binding on the broker
  4. RabbitAdmin admin = new RabbitAdmin(cf);
  5. Queue queue = new Queue("myQueue");
  6. admin.declareQueue(queue);
  7. TopicExchange exchange = new TopicExchange("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. new SimpleMessageListenerContainer(cf);
  14. Object listener = new Object() {
  15. public void handleMessage(String foo) {
  16. System.out.println(foo);
  17. }
  18. };
  19. MessageListenerAdapter adapter = new MessageListenerAdapter(listener);
  20. container.setMessageListener(adapter);
  21. container.setQueueNames("myQueue");
  22. container.start();
  23. // send something
  24. RabbitTemplate template = new RabbitTemplate(cf);
  25. template.convertAndSend("myExchange", "foo.bar", "Hello, world!");
  26. Thread.sleep(1000);
  27. container.stop();
  28. }

使用Spring的方式:

   
   
  1. public static void main(final String... args) throws Exception {
  2. AbstractApplicationContext ctx =
  3. new ClassPathXmlApplicationContext("context.xml");
  4. RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
  5. template.convertAndSend("Hello, world!");
  6. Thread.sleep(1000);
  7. ctx.destroy();
  8. }

   
   
  1. public class Foo {
  2. public void listen(String foo) {
  3. System.out.println(foo);
  4. }
  5. }

   
   
  1. <!--context.xml -->
  2. <rabbit:connection-factory id="connectionFactory" />
  3. <rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
  4. exchange="myExchange" routing-key="foo.bar"/>
  5. <rabbit:admin connection-factory="connectionFactory" />
  6. <rabbit:queue name="myQueue" />
  7. <rabbit:topic-exchange name="myExchange">
  8. <rabbit:bindings>
  9. <rabbit:binding queue="myQueue" pattern="foo.*" />
  10. </rabbit:bindings>
  11. </rabbit:topic-exchange>
  12. <rabbit:listener-container connection-factory="connectionFactory">
  13. <rabbit:listener ref="foo" method="listen" queue-names="myQueue" />
  14. </rabbit:listener-container>
  15. <bean id="foo" class="foo.Foo" />


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值