Spring 整合RabbitMQ

Spring 整合RabbitMQ

1.Spring AMQP
The Spring AMQP project applies core Spring concepts to the development of AMQP-based messaging solutions. 
It provides a "template" as a high-level abstraction for sending 
and receiving messages. It also provides support for Message-driven 
POJOs with a "listener container". 
These libraries facilitate management of AMQP resources while 
promoting the use of dependency injection
and declarative configuration. In all of these cases, you will see
similarities to the JMS support in the Spring Framework.
The project consists of two parts; spring-amqp is the base abstraction,
and spring-rabbit is the RabbitMQ implementation.


上述英文引自spring官网。
Spring AMQP项目提供了与sping核心概念结合且基于AMQP的消息队列解决方案。
他提供了一个抽象的模板用于发送和接收消息。他还提供了一个监听容器
支持消息驱动的实体。这些AMQP的库和实例通过依赖注入和声明式配置引入。
总的来说,你将在Spring Framework框架看到类似于对JMS(Java Message Service, 
与AMQP(Advance Message Queuing Prototal)不同的另一种消息队列协议)的支持。

2. spring-rabbit的主要API:
1.MessageListenerContainer:监听容器,为消息入队提供异步处理
2.RabbitTemplate:用于接收和发送消息
3.RabbitAmdin: 用来声明队列、交换器、绑定
3. 引入spring-rabbit依赖
<dependency>
  <groupId>org.springframework.amqp</groupId>
  <artifactId>spring-rabbit</artifactId>
  <version>2.0.2.RELEASE</version>
</dependency>
4.配置文件
使用idea搭建maven工程
在reasourse目录下创建spring-context.xml
spring-context: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.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">

    <bean id="fooMessageListener" class="com.zhy.com.zhy.mqstudy.FooMessageListener"/>

    <!--配置连接-->
    <rabbit:connection-factory id="connectionFactory" host="127.0.0.1" port="5672"
    username="guest" password="guest" virtual-host="/" requested-heartbeat="60"/>

    <!--配置RabbitTemplate-->
    <rabbit:template id="aqmpTemplate" connection-factory="connectionFactory" exchange="myExchange" routing-key="foo.bar"/>

    <!--配置RabbitAdmin-->
    <rabbit:admin connection-factory="connectionFactory"/>

    <!--配置队列名称-->
    <rabbit:queue name="myQueue"/>

    <!--配置topic类型的交换器-->
    <rabbit:topic-exchange name="myExchange">

        <rabbit:bindings>
            <rabbit:binding queue="myQueue" pattern="foo.*"></rabbit:binding>
        </rabbit:bindings>
    </rabbit:topic-exchange>

    <!--配置监听器-->
    <rabbit:listener-container connection-factory="connectionFactory">
        <rabbit:listener ref="fooMessageListener" queue-names="myQueue"/>
    </rabbit:listener-container>
</beans>


发送消息:FooMessageListener.java文件:

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

public class FooMessageListener implements MessageListener{


    @Override
    public void onMessage(Message message) {
        String messageBody = new String(message.getBody());
        System.out.println(messageBody);
    }
}


接收消息:SendMessage.java:

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
*
* @description: 发送消息
* @author: Karvins
* @Date: 2019/8/4 14:56
* @Modify By:
*
*/
public class SendMessage {

    public static void main(String[] args) {
        AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("spring-context.xml");
        RabbitTemplate template = ctx.getBean(RabbitTemplate.class);
        template.convertAndSend("Hello world");
        ctx.close();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值