RabbitMQ integration with Spring

Spring-Rabbit


 

 import dependencies

<dependency>
	<groupId>org.springframework.amqp</groupId>
	 <artifactId>spring-rabbit</artifactId>
</dependency>

rabbitmq configuration file

rabbitmq.host=127.0.0.1
rabbitmq.port=5672
rabbitmq.username=mytest
rabbitmq.passwd=mytest
rabbitmq.vhost=/mytest

 

Producer spring-rabbitmq configuration file

<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/rabbit
	http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd
	http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
	
	<!-- connection factory -->
	<rabbit:connection-factory id="connectionFactory"
		host="${rabbitmq.host}" port="${rabbitmq.port}" username="${rabbitmq.username}"
		password="${rabbitmq.passwd}" virtual-host="${rabbitmq.vhost}" />
	
	<!-- MQ management, including queues, exchanges, etc. -->
	<rabbit:admin connection-factory="connectionFactory" />
	
	<!-- durable: Persistence. Whether the queue or switch is to be stored to disk can also be stored to the content -->
	<!-- define switch -->
	<rabbit:topic-exchange name="KAOLA-EXCHANGE-ITEM" auto-declare="true" durable="true" >
	</rabbit:topic-exchange>
	
	<!-- define template -->
	<rabbit:template id="template" connection-factory="connectionFactory" exchange="KAOLA-EXCHANGE-ITEM" >
	</rabbit:template>
	
</beans>

 

Send message content to switch

import com.fasterxml.jackson.databind.ObjectMapper;
@Autowired
private RabbitTemplate rabbitTemplate;
private static final ObjectMapper MAPPER = new ObjectMapper();
private void sendMsg(Long itemId,String type){
		try{
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("itemId", itemId);
			map.put("type", type);
			this.rabbitTemplate.convertAndSend("item."+type,MAPPER.writeValueAsString(map));
		}catch(Exception e){
			e.printStackTrace ();
		}
}
Method call:
//Send a message to the switch to notify other systems that the item has been updated
sendMsg(item.getId(),"update");
//Send a message to the switch to notify other systems that the item has been added
sendMsg(item.getId(),"insert");
//Send a message to the switch to notify other systems that the item has been deleted
sendMsg(item.getId(),"delete");

 

 The consumer receives the message:

 

import dependencies

 

<dependency>
	<groupId>org.springframework.amqp</groupId>
	 <artifactId>spring-rabbit</artifactId>
</dependency>

rabbitmq configuration file

rabbitmq.host=127.0.0.1
rabbitmq.port=5672
rabbitmq.username=mytest
rabbitmq.passwd=mytest
rabbitmq.vhost=/mytest

 

 consumer spring-rabbitmq configuration file

<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/rabbit
	http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd
	http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
	
	<!-- connection factory -->
	<rabbit:connection-factory id="connectionFactory"
		host="${rabbitmq.host}" port="${rabbitmq.port}" username="${rabbitmq.username}"
		password="${rabbitmq.passwd}" virtual-host="${rabbitmq.vhost}" />
	
	<!-- MQ management, including queues, exchanges, etc. -->
	<rabbit:admin connection-factory="connectionFactory" />
	
	<!-- define queue -->
	<rabbit:queue name="KAOLA-WEB-ITEM" auto-declare="true" durable="true" />
	
	<bean id="itemHandler" class="com.kaola.portal.mq.handler.ItemHandler" />
	
	<!-- define listener -->
	<rabbit:listener-container connection-factory="connectionFactory">
		<rabbit:listener ref="itemHandler" method="execute" queue-names="KAOLA-WEB-ITEM"/>
	</rabbit:listener-container>
	
</beans>

 

Specific processing logic:

 

import org.apache.commons.lang3.StringUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ItemHandler {
	private static final ObjectMapper MAPPER = new ObjectMapper();
	public void execute(String msg){
		try {
			JsonNode jsonNode = MAPPER.readTree(msg);
			Long itemId = jsonNode.get("itemId").asLong();
			String type = jsonNode.get("type").asText();
			if(StringUtils.equals(type, "update")||StringUtils.equals("insert", type)){
				//TODO  根据itemId操作redis中的数据
			}else if(StringUtils.equals(type, "delete")){
				//TODO  根据itemId操作redis中的数据
			}
		} catch (Exception e) {
		}
	}
}

 

队列和交换机的绑定关系

 

实现:

1、 在配置文件中将队列和交换机完成绑定

<!-- 定义队列,自动声明 -->
<rabbit:queue name="KAOLA-WEB-ITEM" auto-declare="true"></rabbit:queue>
<!-- 定义队列、交换机、以及完成队列和交换机的绑定 -->
<rabbit:topic-exchange  name="KAOLA-EXCHANGE-ITEM" auto-declare="true" durable="true">
	<rabbit:bindings>
		<!--<rabbit:binding queue="KAOLA-WEB-ITEM" pattern="item.*" />-->
		<rabbit:binding queue="KAOLA-WEB-ITEM" pattern="item.update" />
		<rabbit:binding queue="KAOLA-WEB-ITEM" pattern="item.delete" />
	</rabbit:bindings>
</rabbit:topic-exchange>

 

 

 

2、 可以在管理界面中完成绑定

2.1)绑定关系如果发生变化,需要修改配置文件,并且服务需要重启

2.2)管理更加灵活

2.3)更容易对绑定关系的权限管理,流程管理

 

在界面管理工具中完成绑定关系

从 从交换机KAOLA-EXCHANGE-ITEM中完成绑定,如下图



 
 
 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326621190&siteId=291194637