Spring集成RabbitMQ

前言

  • Spring项目中引入RabbitMQ,使得分布式应用通过消息机制进行通信
  • MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法
  • RabbitMQ is the most widely deployed open source message broker

源代码

所需JAR包

  • 图片

这里写图片描述

  • POM
<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
    <version>1.2.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.1</version>
</dependency>
<dependency>
    <groupId>org.springframework.amqp</groupId>
    <artifactId>spring-amqp</artifactId>
    <version>1.7.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.amqp</groupId>
    <artifactId>spring-rabbit</artifactId>
    <version>1.7.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>
    <version>4.0.3</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.7</version>
</dependency>
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
</dependency>

RabbitMQ连接信息

#rabbitmq testing environment
spring.rabbitmq.addresses=192.168.247.131:5672
spring.rabbitmq.username=star
spring.rabbitmq.password=star

RabbitMQ配置信息

<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-2.5.xsd
    http://www.springframework.org/schema/rabbit
    http://www.springframework.org/schema/rabbit/spring-rabbit-1.6.xsd">

    <!-- 连接服务配置 --> <!-- 开启发送确认机制 --> <!-- 设置结果返回监听方法 -->  <!-- 缓存中要维护的通道数 --> 
    <rabbit:connection-factory id="rabbitConnectionFactory"
                               addresses="${spring.rabbitmq.addresses}"
                               username="${spring.rabbitmq.username}"
                               password="${spring.rabbitmq.password}"
                               publisher-confirms="true"
                               publisher-returns="true"
                               channel-cache-size="5" 
                               /> 
                               
	<!-- 消息确认监听器 -->
    <rabbit:admin connection-factory="rabbitConnectionFactory" id="rabbitAdmin"/>

	
	<bean id="jsonMessageConverter" class="com.sto.rookie.util.Gson2JsonMessageConverter"/>

	<!-- 生产者 -->
    <!-- exchange queue binging 绑定 -->
    <rabbit:topic-exchange name="workorder_topic_exchange" durable="true" auto-delete="false" id="workorder_topic_exchange">
        <rabbit:bindings>
            <rabbit:binding queue="workorder_queue" pattern="queue.distribute.workorder.*"/>
            <rabbit:binding queue="workorder_queue_ebay_trace" pattern="queue.distribute.workorder.*"/>
            <rabbit:binding queue="workorder_queue_ebay_oms" pattern="queue.distribute.workorder.*"/>
        </rabbit:bindings>
    </rabbit:topic-exchange>
    
    <!-- spring rabbitTemplate声明 -->
    <rabbit:template exchange="workorder_topic_exchange" id="rabbitTemplate" connection-factory="rabbitConnectionFactory"
          	 confirm-callback="confirmCallBackListener" return-callback="returnCallBackListener" message-converter="jsonMessageConverter" />

	<!-- 定义队列 -->
    <rabbit:queue id="workorder_queue" durable="true" auto-delete="false" exclusive="false" name="workorder_queue"/>
    <rabbit:queue id="workorder_queue_ebay_trace" durable="true" auto-delete="false" exclusive="false" name="workorder_queue_ebay_trace"/>
    <rabbit:queue id="workorder_queue_ebay_oms" durable="true" auto-delete="false" exclusive="false" name="workorder_queue_ebay_oms"/>

</beans>

监听器ConfirmCallBackListener源码

package com.sto.rookie.timer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.CorrelationData;

public class ConfirmCallBackListener implements RabbitTemplate.ConfirmCallback{
	private static final Log logger = LogFactory.getLog(ConfirmCallBackListener.class);
	
	@Override
	public void confirm(CorrelationData correlationData, boolean ack, String cause) {
		// TODO Auto-generated method stub
		logger.info("确认ID:" + correlationData);
		if (ack) {
			logger.info("消息发送成功");
		} else {
			logger.info("消息发送失败:" + cause);
		}
	}
}

监听器ReturnCallBackListener源码

package com.sto.rookie.timer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

public class ReturnCallBackListener implements RabbitTemplate.ReturnCallback {
	private static final Log logger = LogFactory.getLog(ReturnCallBackListener.class);

    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
    	logger.info("收到回调");
    	logger.info("return--message:" + new String(message.getBody()) + ",replyCode:" + replyCode + ",replyText:" + replyText + ",exchange:" + exchange + ",routingKey:" + routingKey);
    }

}

配置jsonMessageConverter

package com.sto.rookie.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.support.converter.AbstractJsonMessageConverter;
import org.springframework.amqp.support.converter.ClassMapper;
import org.springframework.amqp.support.converter.DefaultClassMapper;
import org.springframework.amqp.support.converter.MessageConversionException;

import com.google.gson.Gson;

public class Gson2JsonMessageConverter extends AbstractJsonMessageConverter {

	private static Log log = LogFactory.getLog(Gson2JsonMessageConverter.class);

	private static ClassMapper classMapper = new DefaultClassMapper();

	private static Gson gson = new Gson();

	public Gson2JsonMessageConverter() {
		super();
	}

	@Override
	protected Message createMessage(Object object, MessageProperties messageProperties) {
		byte[] bytes = null;
		try {
			String jsonString = gson.toJson(object);
			log.info("【发送到rabbitmq的JSON数据】"+jsonString);
			bytes = jsonString.getBytes(getDefaultCharset());
		} catch (IOException e) {
			throw new MessageConversionException("Failed to convert Message content", e);
		}
		messageProperties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
		messageProperties.setContentEncoding(getDefaultCharset());
		if (bytes != null) {
			messageProperties.setContentLength(bytes.length);
		}
		classMapper.fromClass(object.getClass(), messageProperties);
		return new Message(bytes, messageProperties);
	}

	@Override
	public Object fromMessage(Message message) throws MessageConversionException {
		Object content = null;
		MessageProperties properties = message.getMessageProperties();
		if (properties != null) {
			String contentType = properties.getContentType();
			if (contentType != null && contentType.contains("json")) {
				String encoding = properties.getContentEncoding();
				if (encoding == null) {
					encoding = getDefaultCharset();
				}
				try {
					Class<?> targetClass = getClassMapper().toClass(message.getMessageProperties());
					content = convertBytesToObject(message.getBody(), encoding, targetClass);
				} catch (IOException e) {
					throw new MessageConversionException("Failed to convert Message content", e);
				}
			} else {
				log.warn("Could not convert incoming message with content-type [" + contentType + "]");
			}
		}
		if (content == null) {
			content = message.getBody();
		}
		return content;
	}

	private Object convertBytesToObject(byte[] body, String encoding, Class<?> clazz)
			throws UnsupportedEncodingException {
		String contentAsString = new String(body, encoding);
		return gson.fromJson(contentAsString, clazz);
	}
}

实体类RaMQFailMessageDto

package com.sto.rookie.pojo;

public class RaMQFailMessageDto {
	private String workOrderId;
	private String failreason;
	
	public String getWorkOrderId() {
		return workOrderId;
	}
	public void setWorkOrderId(String workOrderId) {
		this.workOrderId = workOrderId;
	}
	public String getFailreason() {
		return failreason;
	}
	public void setFailreason(String failreason) {
		this.failreason = failreason;
	}
	
	

}

Handler中具体调用

logger.info("开始执行【任务操作上传】推送任务!");
RaMQFailMessageDto rMessageDto = new RaMQFailMessageDto();
rMessageDto.setWorkOrderId("12345678");
rMessageDto.setFailreason("Fail");
String uuid = UUID.randomUUID().toString();
CorrelationData correlationData = new CorrelationData(uuid);
// 方法调用时,多个参数, 需要换行时, 在逗号后进行
rabbitTemplate.convertAndSend("workorder_topic_exchange", "queue.distribute.workorder.movement",
		rMessageDto, correlationData);
logger.info("【任务操作上传】推送任务结束!");

Rabbitmq概念

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值