Spring integration the RabbitMQ
Create two projects
Add the dependent
Consumers and producers are equally dependent
</dependency> <! <dependency> <groupId>org.springframework.amqp</groupId> <artifactId> Spring-rabbit </artifactId> < version > 2.1.8. RELEASE < / version > < / dependency > <! -- Test --> <dependency> <groupId>junit</ artifactId> <version>4.13</version> </dependency> <! --> <dependency> <groupId>org.springframework</groupId> <artifactId> Spring-test </artifactId> <version>5.0.5.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> . < groupId > org, apache maven plugins < / groupId > < artifactId > maven - compiler - plugin < / artifactId > < version > 3.8.0 < / version > <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugins> </build>Copy the code
Integration of configuration
1. RabbitMQ connection parameter configuration file
rabbitmq.properties
The rabbitmq. Host = 192.168.12.135 rabbitmq. Port = 5672 the rabbitmq. Username = heima rabbitmq. Password = heima rabbitmq.virtual-host=/itcastCopy the code
2.Spring integrates rabbitMQ configuration files
The producer side
Spring-rabbitmq. XML configuration file
<? The 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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd"> <! - loading configuration file - > < context: the property - placeholder location = "classpath: the rabbitmq. Properties" / > <! Rabbitmq connectionFactory --> <rabbit: connectionFactory id="connectionFactory" host="${rabbitmq.host}" port="${rabbitmq.port}" username="${rabbitmq.username}" password="${rabbitmq.password}" virtual-host="${rabbitmq.virtual-host}"/> <! <rabbit:admin connection-factory="connectionFactory"/> <! -- Define a persistent queue. If it does not exist, it will be created automatically. If it is not bound to a switch, it is bound to the default switch. The default switch type is direct, the name is "", and the routing key is the queue name. -- ID: bean name name: queue name Auto-declare: automatic creation auto-delete: automatic deletion. The queue is automatically deleted after the last consumer disconnects from the queue. Exclusive: Whether the queue is durable: --> <rabbit:queue id="spring_queue" name="spring_queue" auto-declare="true"/> <! - broadcast; All queues can receive messages --> <! Define persistent queues in broadcast switches, --> <rabbit:queue ID =" spring_FANout_queue_1 "name=" spring_FANout_queue_1" auto-declare="true"/> <! Define persistent queues in broadcast switches, --> <rabbit:queue ID =" spring_FANout_queue_2 "name=" spring_FANout_queue_2" auto-declare="true"/> <! Define the broadcast type switch; --> <rabbit: FANout-exchange ID ="spring_fanout_exchange" name="spring_fanout_exchange" auto-declare="true"> <rabbit:bindings> <rabbit:binding queue="spring_fanout_queue_1"/> <rabbit:binding queue="spring_fanout_queue_2"/> </rabbit:bindings> </rabbit:fanout-exchange> <! -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ wildcard; * matches a word, # match multiple words ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ -- -- > <! Define persistent queues in broadcast switches, --> <rabbit:queue ID ="spring_topic_queue_star" name="spring_topic_queue_star" auto-declare="true"/> <! Define persistent queues in broadcast switches, --> <rabbit:queue ID ="spring_topic_queue_well" name="spring_topic_queue_well" auto-declare="true"/> <! Define persistent queues in broadcast switches, --> <rabbit:queue ID ="spring_topic_queue_well2" name="spring_topic_queue_well2" auto-declare="true"/> <rabbit:topic-exchange id="spring_topic_exchange" name="spring_topic_exchange" auto-declare="true"> <rabbit:bindings> <rabbit:binding pattern="heima.*" queue="spring_topic_queue_star"/> <rabbit:binding pattern="heima.#" queue="spring_topic_queue_well"/> <rabbit:binding pattern="itcast.#" queue="spring_topic_queue_well2"/> </rabbit:bindings> </rabbit:topic-exchange> <! <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/> </beans>Copy the code
The consumer
<? The 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:context="http://www.springframework.org/schema/context" 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/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd"> <! - loading configuration file - > < context: the property - placeholder location = "classpath: the rabbitmq. Properties" / > <! Rabbitmq connectionFactory --> <rabbit: connectionFactory id="connectionFactory" host="${rabbitmq.host}" port="${rabbitmq.port}" username="${rabbitmq.username}" password="${rabbitmq.password}" virtual-host="${rabbitmq.virtual-host}"/> <bean id="springQueueListener" class="com.itheima.rabbitmq.listener.SpringQueueListener"/> <! --<bean id="fanoutListener1" class="com.itheima.rabbitmq.listener.FanoutListener1"/> <bean id="fanoutListener2" class="com.itheima.rabbitmq.listener.FanoutListener2"/> <bean id="topicListenerStar" class="com.itheima.rabbitmq.listener.TopicListenerStar"/> <bean id="topicListenerWell" class="com.itheima.rabbitmq.listener.TopicListenerWell"/> <bean id="topicListenerWell2" class="com.itheima.rabbitmq.listener.TopicListenerWell2"/>--> <rabbit:listener-container connection-factory="connectionFactory" auto-declare="true"> <rabbit:listener ref="springQueueListener" queue-names="spring_queue"/> <! --<rabbit:listener ref="fanoutListener1" queue-names="spring_fanout_queue_1"/> <rabbit:listener ref="fanoutListener2" queue-names="spring_fanout_queue_2"/> <rabbit:listener ref="topicListenerStar" queue-names="spring_topic_queue_star"/> <rabbit:listener ref="topicListenerWell" queue-names="spring_topic_queue_well"/> <rabbit:listener ref="topicListenerWell2" queue-names="spring_topic_queue_well2"/>--> </rabbit:listener-container> </beans>Copy the code
The consumer side writes the implementation class of the MessageListener interface
package com.itheima.rabbitmq.listener; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageListener; public class SpringQueueListener implements MessageListener { @Override public void onMessage(Message message) { System.out.println(new String(message.getBody())); }}Copy the code
The producer sends the message
Use Autowired to inject rabbitTemplate automatically and then use convertAndSend to send the message
@Autowired private RabbitTemplate rabbitTemplate; @ Test public void test1 () {rabbitTemplate. ConvertAndSend (" spring_queue ", "Hello word!!!!!" ); }Copy the code
3. Configuration items
rabbit:queue/
Id: indicates the bean name. Name: indicates the queue name. Auto-declare: indicates an automatic creation. The queue is automatically deleted after the last consumer disconnects from the queue. Exclusive: Whether to be exclusive and durable: Whether to be persistent
The sample * *
<rabbit:queue id="spring_fanout_queue_1" name="spring_fanout_queue_1" auto-declare="true"/>
Copy the code
<rabbit:template />
Id: bean name connection-factory: factory BEA name
The sample
<! <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>Copy the code
rabbit:fanout-exchange/
Id: indicates the bean name. Name: indicates the switch name. Auto-declare: indicates that the bean is automatically deleted. When the last queue disconnects from the switch, the switch is automatically deleted
Switch bound queue
<rabbit:bindings>
<rabbit:binding queue="spring_fanout_queue_1" />
<rabbit:binding queue="spring_fanout_queue_2"/>
</rabbit:bindings>
Copy the code
The sample * *
<! Define the broadcast type switch; --> <rabbit: FANout-exchange ID ="spring_fanout_exchange" name="spring_fanout_exchange" auto-declare="true"> <rabbit:bindings> <rabbit:binding queue="spring_fanout_queue_1"/> <rabbit:binding queue="spring_fanout_queue_2"/> </rabbit:bindings> </rabbit:fanout-exchange>Copy the code