导入RabbitMQ.xml
在Spring.xml中写
<import resource="classpath*:spring-rabbitMQ.xml" />
RabbitMQ.xml配置信息
rabbitmq_username等信息从jdbc文件中读取
<?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.2.xsd">
<!--配置connection-factory,指定连接rabbit server参数 -->
<rabbit:connection-factory id="connectionFactory"
username="${rabbitmq_username}" password="${rabbitmq_pwd}"
host="${rabbitmq_host}" port="${rabbitmq_port}" />
<!--通过指定下面的admin信息,当前producer中的exchange和queue会在rabbitmq服务器上自动生成 -->
<rabbit:admin id="connectAdmin" connection-factory="connectionFactory" />
<!--定义queue -->
<rabbit:queue name="mqtt-subscription-paho79728512782700qos1" durable="true" auto-delete="false"
exclusive="false" declared-by="connectAdmin" />
<!-- 定义direct exchange,绑定queueTest -->
<rabbit:direct-exchange name="amq.topic"
durable="true" auto-delete="false" declared-by="connectAdmin">
<!-- <rabbit:bindings>
<rabbit:binding queue="mqtt-subscription-paho79728512782700qos1" key="111111"></rabbit:binding>
</rabbit:bindings> -->
</rabbit:direct-exchange>
<!--定义rabbit template用于数据的接收和发送 -->
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
exchange="amq.topic" />
<!-- 消息接收者 -->
<bean id="messageReceiver" class="org.cboard.util.RabbitMQConsumer"></bean>
<!-- queue litener 观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象 -->
<rabbit:listener-container
connection-factory="connectionFactory">
<rabbit:listener queues="mqtt-subscription-paho79728512782700qos1" ref="messageReceiver" />
</rabbit:listener-container>
</beans>
生产者
package org.cboard.util;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class SendMsgToRabbitMQ {
@Autowired
private AmqpTemplate rabbitTemplate;
/**
*
* @Title: sendMQ
* @Description: 将消息发送给RabbitMQ
* @param @param exchange 指定交换器
* @param @param routingKey 指定路由
* @param @param data 传送的数据
* @return void 返回类型
* @throws
* @author _只一个
* @date 2020年9月10日
* @param exchange
* @param routingKey
* @param data
*/
public void sendMQ(String exchange, String routingKey,String data) {
rabbitTemplate.convertAndSend(exchange,routingKey,data);
}
}
消费者
package org.cboard.util;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import com.rabbitmq.client.Channel;
public class RabbitMQConsumer implements MessageListener{
private Logger logger = LoggerFactory.getLogger(RabbitMQConsumer.class);
@Override
public void onMessage(Message message) {
try {
String messages = new String(message.getBody(), "UTF-8");
String[] str=messages.split(",");
logger.info("消费者收到消息---> receive message------->:{}", message);
} catch (Exception e) {
logger.error("MQ消息处理异常!", e);
} finally {
}
}
}