RabbitMQ > Spring AMQP

本文介绍了如何使用Spring AMQP项目与RabbitMQ集成,重点讲解了RabbitTemplate、Listener Container、RabbitAdmin的用法,以及如何配置连接、声明Queues、Exchanges和Bindings,并展示了异步消费者和发送接收消息的示例。
摘要由CSDN通过智能技术生成

Spring AMQP Project将Spring的核心观念应用于基于AMQP协议的消息解决方案,它包括两部分:spring-amqp是基础抽象,spring-rabbit是基于RabbitMQ的实现。

特征:
1)提供了一个”模板”RabbitTemplate来发送和接收消息
2)使用Listener Container用于异步处理进来的消息
3)使用RabbitAdmin自动声明Queues、Exchanges和Bindings

下面结合Spring amqp官方的Quick Start(http://projects.spring.io/spring-amqp/#quick-start)解释其用法。

1.在pom.xml (maven)中引入spring-rabbit dependency

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

2.在对应project的src/main/resources/目录下添加spring-rabbit.xml(我本机目录是src/main/resources/spring/spring-rabbitmq.xml)

<?xml version="1.0" encoding="UTF-8"?>
<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/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
    http://www.springframework.org/schema/rabbit
    http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">

    <rabbit:connection-factory id="connectionFactory" host="192.168.73.128" port="5672"
        username="tony" password="123" channel-cache-size="25"/>

    <rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
        exchange="myExchange" routing-key="foo.bar"/>

    <rabbit:admin connection-factory="connectionFactory" />
    <rabbit:queue name="myQueue"/>
    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值