作者:鱼儿玩玩 | 来源:互联网 | 2023-08-18 18:59
Rabbit整合spring(ssm)开发生产者本文做RabbitMQ和spring(ssm)整合是在原有的ssm项目框架的基础之上进行的,相关ssm的整合在这里我就不做详细的讲解
Rabbit整合spring(ssm)开发生产者
本文做RabbitMQ和spring(ssm)整合是在原有的ssm项目框架的基础之上进行的,相关ssm的整合在这里我就不做详细的讲解,直接整合RabbitMQ
1.收先加入整合RabbitMQ所需要的jar包,如果使用maven管理项目则添加jar包依赖
<dependency>
<groupId>org.springframework.amqpgroupId>
<artifactId>spring-rabbitartifactId>
<version>1.3.5.RELEASEversion>
dependency>
2.开发rabbitMQ.properties和rabbitmq.xml并将rabbitmq.xml引入到spring主配置文件中去
rabbitMQ.properties
mq.host=10.199.203.161
mq.username=crs
mq.password=crs
mq.port=5672
#\u4e0b\u5355\u961f\u5217
mq.insertOrderQueueName=insertOrderQueue
mq.insertOrderQueueKey=551B3D4A88C47EDAE0530100007FC2EA
#\u652f\u4ed8\u8ba2\u5355\u961f\u5217
mq.orderPayQueueName=orderPayQueue
mq.orderPayQueueKey=f6da59e9b9434f25b4800b3363dec3e4
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.0.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd" >
<rabbit:connection-factory id="connectionFactory" host="${mq.host}" username="${mq.username}" password="${mq.password}" port="${mq.port}"/>
<rabbit:admin id="connectAdmin" connection-factory="connectionFactory"/>
<bean id="ackManual"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="staticField"
value="org.springframework.amqp.core.AcknowledgeMode.MANUAL" />
bean>
<rabbit:direct-exchange name="amqpExchange" durable="true" auto-delete="false">
<rabbit:bindings>
rabbit:bindings>
rabbit:direct-exchange>
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
exchange="amqpExchange"/>
beans>
spring-config.xml
<import resource="rabbitmq.xml" />
3.发送消息
//MQ发送消息
PropertyUtil propertyUtil = new PropertyUtil();
String queueKey=propertyUtil.getProperty("/rabbitMQ.properties", "mq.queueKey");
mqProducer.sendDataToQueue(queueKey, JSONObject.toJSONString(order.getMqOrderInfo()));
MqProducterImpl.java
package com.wanda.crs.standard.service.impl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wanda.crs.common.BaseException;
import com.wanda.crs.standard.service.MQProducer;
import com.wanda.crs.utils.Contants;
@Service("mqProducer")
public class MQProducerImpl implements MQProducer {
private static final Logger logger = LoggerFactory.getLogger(MQProducerImpl.class);
@Autowired
private AmqpTemplate amqpTemplate;
@Override
public void sendDataToQueue(String queueKey, Object object) throws Exception {
try {
amqpTemplate.convertAndSend(queueKey, object);
} catch (Exception e) {
logger.info(e.getLocalizedMessage());
throw new Exception(Contants.MSG_MQ_SEND_ERROR);
}
}
}
MessageProducer.java文件(两种都可以)
package com.crs.ticket.utils.rubbitmq;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* 功能概要:消息产生,提交到队列中去
*
* @author pypua
* @since 2017年7月16日
*/
@Service
public class MessageProducer {
private Logger logger = LoggerFactory.getLogger(MessageProducer.class);
@Autowired
private RabbitTemplate rabbitTemplate;
public void sendMessage(Object message){
logger.info("to send message:{}",message);
rabbitTemplate.convertAndSend("queuemqKey",message);
}
}