RabbitMQ_Quick_Start - TFdream/blog GitHub Wiki

依赖

<!-- rabbitmq -->
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit</artifactId>
            <version>1.7.8.RELEASE</version>
        </dependency>

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:context="http://www.springframework.org/schema/context"
       xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xmlns:util="http://www.springframework.org/schema/util"
       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
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.mindflow.demo.rabbitmq" />

    <context:property-placeholder location="classpath:rabbitmq.properties" />

    <!-- 配置connection-factory -->
    <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}" port="${rabbitmq.port}" username="${rabbitmq.username}" password="${rabbitmq.password}" />

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

    <!--定义queue-->
    <rabbit:queue id="rabbitmq-demo" name="rabbitmq-demo"/>

    <!-- 定义direct exchange,绑定com.mj.test queue -->
    <rabbit:direct-exchange name="demo-direct-exchange" durable="true" >
        <rabbit:bindings>
            <rabbit:binding queue="rabbitmq-demo" key="hello"></rabbit:binding>
        </rabbit:bindings>
    </rabbit:direct-exchange>

    <!--定义rabbit template用于数据的接收和发送-->
    <rabbit:template id="amqpTemplate" connection-factory="connectionFactory" exchange="demo-direct-exchange" message-converter="jsonMessageConverter"/>

    <bean id="jsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" />

    <!-- 接受 -->
    <bean id="messageReceiver" class="com.mindflow.demo.rabbitmq.listener.MessageReceiver"></bean>

    <rabbit:listener-container connection-factory="connectionFactory">
        <rabbit:listener queues="rabbitmq-demo" ref="messageReceiver"/>
    </rabbit:listener-container>

</beans>

接受者

package com.mindflow.demo.rabbitmq.listener;

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

/**
 * @author Ricky Fung
 */
public class MessageReceiver implements MessageListener {

    @Override
    public void onMessage(Message message) {
        System.out.println("receive:"+new String(message.getBody()));
    }
}

发送者

package com.mindflow.demo.rabbitmq;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Ricky Fung
 */
@Service
public class MessageSender {

    @Resource(name = "amqpTemplate")
    private AmqpTemplate amqpTemplate;

    public void send() {
        List<String> submobileList = new ArrayList<>();
        submobileList.add("1");
        submobileList.add("2");
        submobileList.add("3");
        Map<String, Object> bodyMap = new HashMap<>();
        bodyMap.put("batchNo", "递四方速递");
        bodyMap.put("item", submobileList);

        amqpTemplate.convertAndSend("hello", bodyMap);
        System.out.println("success");
    }
}

⚠️ **GitHub.com Fallback** ⚠️