SpringMVC集成RebbitMq(含源码)
安装RebbitMq可以参考:https://blog.csdn.net/qq_47588845/article/details/107986373
1.添加jar
2.添加生产者配置文件:application-rabbitProduce.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.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd"> <rabbit:connection-factory id="mqProduceConnectionFactory" username="guest" password="guest" host="127.0.0.1" port="5672"/> <rabbit:admin connection-factory="mqProduceConnectionFactory"/> <rabbit:template id="amqpTemplate" connection-factory="mqProduceConnectionFactory" exchange="hjexchange" routing-key="wzh"/> <rabbit:queue name="com.lll.employee" auto-delete="false" durable="true" exclusive="false" auto-declare="true"/> <rabbit:direct-exchange name="hjexchange" durable="true" auto-delete="false"> <rabbit:bindings> <rabbit:binding key="com.lll.employee" queue="com.lll.employee">rabbit:binding> rabbit:bindings> rabbit:direct-exchange> beans>
3.添加消费者配置文件:application-rabbitConsume.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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd"> <rabbit:connection-factory id="mqConsumerConnectionFactory" username="guest" password="guest" host="127.0.0.1" port="5672"/> <rabbit:admin connection-factory="mqConsumerConnectionFactory"/> <rabbit:queue name="com.lll.employee" auto-delete="false" durable="true" exclusive="false" auto-declare="true"/> <rabbit:direct-exchange name="hjexchange" durable="true" auto-delete="false"> <rabbit:bindings> <rabbit:binding key="com.lll.employee" queue="com.lll.employee">rabbit:binding> rabbit:bindings> rabbit:direct-exchange> <bean id="directConsumer" class="com.lll.rebbitmq.DirectConsumer">bean> <rabbit:listener-container connection-factory="mqConsumerConnectionFactory"> <rabbit:listener ref="directConsumer" queues="com.lll.employee"/> rabbit:listener-container> beans>
3.在applicationContext.xml中引入对应配置
<import resource="application-rabbitProduce.xml"/>
<import resource="application-rabbitConsume.xml"/>
**注意:这里如果配置了其他的 connection-factory 命名必须不一样,否则不能识别会报错。
4.添加生产者服务
package com.lll.rebbitmq; import javax.annotation.Resource; import com.lll.utils.JacksonUtil; import org.apache.log4j.Logger; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.stereotype.Service; /** * 通过调用rabbitmq AmqpTemplate对象发送消息 * @author lll * */ @Service("messageProducer") public class MessageProducer { private Logger log = Logger.getLogger(MessageProducer.class); @Resource private AmqpTemplate amqpTemplate; public void send(Object message) { log.info("发送消息为 : " + JacksonUtil.toJsonString(message)); amqpTemplate.convertAndSend("com.lll.employee", JacksonUtil.toJsonString(message)); } }
5.添加消费者服务
package com.lll.rebbitmq; import org.apache.log4j.Logger; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageListener; import org.springframework.stereotype.Service; /** * rabbitmq接收推送的数据 * @author lll * */ @Service("directConsumer") public class DirectConsumer implements MessageListener { private Logger log = Logger.getLogger(DirectConsumer.class); @Override public void onMessage(Message message) { log.info("------> 消息队列 接收到的消息为 : " + message); } }
6.在接口中调用
/** * @time 2018年7月31日下午4:45:41 * @author lll * @describe 通过消息对接异步添加 */ @PostMapping("/addByRebbitmq") public String addByRebbitmq(@RequestBody Employee employee) { try{ String message = JacksonUtil.toJsonString(employee); messageProducer.send(message); return "success"; }catch (Exception e){ return e.getMessage(); } }
7.实际效果:
8.资源
源码地址:https://github.com/CodingPandaLLL/springlll.git
源码压缩包地址:https://codeload.github.com/CodingPandaLLL/springlll/zip/refs/tags/1.0.3