RabbitMQ配置生产者和消费者

默认已经安装好RabbitMQ服务,且在RabbitMQ管理端页面(默认port:15672)上需创建exchange、queue及exchange与queue绑定关系等

下面进行项目中生产者和消费者的配置

一、添加依赖

在项目文件pom.xml中添加依赖

<!-- RabbitMQ集成spring -->
<!-- https://mvnrepository.com/artifact/org.springframework.amqp/spring-rabbit -->

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

二、生产者

1.配置

<?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.0.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/rabbit
        http://www.springframework.org/schema/rabbit/spring-rabbit-2.2.xsd">

    <!--   其他配置  -->


    <!-- rabbit连接 -->
    <rabbit:connection-factory id="connectionFactory"
                                username="guest"
                                password="guest"
                                host="192.168.8.130"
                                port="5672"
                                channel-cache-size="50"
                                publisher-returns="true"
                                publisher-confirms="true"
                                connection-timeout="5000"
                                />

    <!-- 通过指定下面的admin信息,当前producer中的exchange和queue会在rabbitmq服务器上自动生成 -->
    <rabbit:admin connection-factory="connectionFactory"/>

    <!-- 消息格式转化 -->
    <bean id="jsonMessageConverter"  class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter"/>

    <!-- 设置交换器 用于数据发送-->
    <rabbit:template id="rabbitProviderName" exchange="exchange-name"  connection-factory="connectionFactory" message-converter="jsonMessageConverter" />

    <!-- 设置队列 -->
    <rabbit:queue id="queue-id" durable="true" auto-delete="false" exclusive="false" name="queues-name" />
    <!-- durable:是否持久化; exclusive: 仅创建者可以使用的私有队列,断开后自动删除; auto_delete: 当所有消费客户端连接断开后,是否自动删除队列 -->    

</beans>

2.代码-生产者生产消息

package com.api;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
//其他 import

public class ConsumerHandlerClass 
{
    @Autowired  
    private RabbitTemplate rabbitProviderName; //rabbitProviderName 需要和配置中template的id对应

    public void sendMessage(){
        Message message = new Message(("{\"key\":1}").getBytes(), new MessageProperties());
        rabbitProviderName.send(message);
    }
}

三、消费者

1.配置

<?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.0.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/rabbit
        http://www.springframework.org/schema/rabbit/spring-rabbit-2.2.xsd">
    

    <!--  其他配置  -->

    <!-- rabbit连接 -->
    <rabbit:connection-factory id="connectionFactory"
                                username="guest"
                                password="guest"
                                host="192.168.8.130"
                                port="5672"
                                channel-cache-size="50"
                                publisher-returns="true"
                                publisher-confirms="true"
                                connection-timeout="5000"
                                />

    <!-- 通过指定下面的admin信息,当前producer中的exchange和queue会在rabbitmq服务器上自动生成 -->
    <rabbit:admin connection-factory="connectionFactory"/>

    <!-- 消息格式转化 生产者生产的数据转换为json存入消息队列-->
    <bean id="jsonMessageConverter"  class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter"/>

    <!-- 设置交换器 用于数据接口-->
    <rabbit:template id="consumerTemplate" connection-factory="connectionFactory" exchange="exchange-name" message-converter="jsonMessageConverter"/>

    <!-- topic -->
    <rabbit:topic-exchange name="exchange-name" durable="true" auto-delete="false">
        <rabbit:bindings>
            <rabbit:binding queue="queue-id" pattern="queues-name.*"/>
        </rabbit:bindings>
    </rabbit:topic-exchange>

    <!-- 接收消息类 自定义接口类-->
    <bean id="consumerHandler" class="com.api.ConsumerHandlerClass"/>

    <!-- 消息的监听的代理类 支付回调-->
    <bean id="receiveListenerAdapter" class="org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter">
        <constructor-arg ref="consumerHandler" />
        <property name="defaultListenerMethod" value="handlerFunc"></property> <!-- 接收消息类中的方法,自定义接口类的默认方法 -->
        <property name="messageConverter" ref="jsonMessageConverter"></property>
    </bean>

    <!-- 监听容器 -->
    <rabbit:listener-container connection-factory="connectionFactory">
        <rabbit:listener queues="queue-id" ref="receiveListenerAdapter"/>
        <!-- 此处可以继续监听多队列 <rabbit:listener queues="queue-id2" ref="receiveListenerAdapter2"/>-->
    </rabbit:listener-container>

</beans>

2.代码-消费者处理消息

package com.api;

//其他 import

public class ConsumerHandlerClass 
{
    public void handlerFunc(HashMap arguments) {
        //arguments 是生产者产生的消息
    }
}

你可能感兴趣的:(Java,RabbitMQ,生产者,消费者,MQ)