【生产者端】
一,添加依赖
<依赖性>
<的groupId> org.springframework.amqp </的groupId>
<artifactId的>弹簧兔</ artifactId的>
</依赖性>
二,配置的applicationContext-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.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd“ > <! - 连接工厂 - > < :connection-factory id =“connectionFactory” host =“$ {rabbitmq.host}” port =“$ {rabbitmq.port}” username =“$ {rabbitmq.username}” password =“$ {rabbitmq.password}” virtual -host =“$ {rabbitmq.vhost}” /> <! - Rabbit的管理,自动管理队列和交换机 - > < rabbit :admin connection-factory =“connectionFactory” /> <! - 交换机这里使用topic类型 - > < rabbit :topic-exchange name =“JESSES_ITEM_EXCHANGE” /> <! - Rabbit的模板,提供了操作消息的API,需要指定连接工厂和交换机 - > <兔子:模板ID =“rabbitTemplate” connection-factory =“connectionFactory” exchange =“JESSES_ITEM_EXCHANGE” /> </ beans >
三,写方法实现发送消息。
private void sendMessage (Item item ,String type ){ try { Map < String ,Object > msg = new HashMap <>(); 味精。把(“数据” ,项目。的getId ()); 味精。put (“type” ,type ); 味精。把(“时间戳” ,系统。的currentTimeMillis ()); 这个。rabbitTemplate 。convertAndSend (“项目” + 型,JsonUtils 。的toString (MSG )); } 捕获(JsonProcessingException ë ){ Ë 。printStackTrace (); / *服务层的异常一般是抛,如果尝试会无法事务回滚。 但这里用试试。就算消息发送失败,也不应该回滚事务让修改失败。 应该在这里进行处理。写代码重新发送。* / } }
一,导入弹簧兔的罐包
二,配置的applicationContext-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.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd"> <!--连接工厂--> <rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}" port="${rabbitmq.port}" username="${rabbitmq.username}" password="${rabbitmq.password}" virtual-host="${rabbitmq.vhost}" /> <!--Rabbit的管理,自动管理队列和交换机--> <rabbit:admin connection-factory="connectionFactory" /> <!--定义队列--> <rabbit:queue name="JESSES_WEB_ITEM_QUEUE" /> <!--消息监听者--> <bean id="itemMessageListener" class="com.jesses.web.mq.listener.ItemMessageListener" /> <rabbit:listener-container connection-factory="connectionFactory"> <rabbit:listener ref="itemMessageListener" method="consume" queue-names="JESSES_WEB_ITEM_QUEUE"/> </rabbit:listener-container> </beans>
三、写消费者实现消费消息。
package com.jesses.web.mq.listener; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.jesses.web.service.ItemService; import org.springframework.beans.factory.annotation.Autowired; import java.io.IOException; /** * 监听商品信息的一个消费者 */ public class ItemMessageListener { @Autowired private ItemService itemService; private static final ObjectMapper MAPPER=new ObjectMapper(); //消费方法 public void consume(String msg){ Long itemId=0L; try { JsonNode jsonNode = MAPPER.readTree(msg);//读取传过来的字符串读成树节点 itemId = jsonNode.get("data").asLong();//用保存时的key获取指定节点。 } catch (IOException e) { e.printStackTrace(); 返回; } //删除缓存 this 。itemService 。deleteCache (itemId ); } }
PS:队列和交换机的绑定可以在配置中定义,也可以在控制面板手动去做。
手动去做可以避免日后再改代码,利于解耦合。