增加rabbitMQ 用例
This commit is contained in:
		@@ -31,9 +31,9 @@ spring-cloud:Finchley.SR2
 | 
				
			|||||||
| [spring-jdbc](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring/spring-jdbc)<br/>[spring-jdbc-annotation](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring/spring-jdbc-annotation) | spring jdbc-template 的使用                                  | [Using JdbcTemplat](https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/data-access.html#jdbc-JdbcTemplate) |
 | 
					| [spring-jdbc](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring/spring-jdbc)<br/>[spring-jdbc-annotation](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring/spring-jdbc-annotation) | spring jdbc-template 的使用                                  | [Using JdbcTemplat](https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/data-access.html#jdbc-JdbcTemplate) |
 | 
				
			||||||
| [spring-mybatis](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring/spring-mybatis)<br/>[spring-mybatis-annotation](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring/spring-mybatis-annotation) | spring 整合 mybatis                                          | [mybatis-spring中文文档](http://www.mybatis.org/spring/zh/index.html) |
 | 
					| [spring-mybatis](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring/spring-mybatis)<br/>[spring-mybatis-annotation](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring/spring-mybatis-annotation) | spring 整合 mybatis                                          | [mybatis-spring中文文档](http://www.mybatis.org/spring/zh/index.html) |
 | 
				
			||||||
| [spring-druid-mybatis](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring/spring-druid-mybatis)<br/>[spring-druid-mybatis-annotation](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring/spring-druid-mybatis-annotation) | spring 整合druid、mybatis                                    | [alibaba druid](https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98) |
 | 
					| [spring-druid-mybatis](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring/spring-druid-mybatis)<br/>[spring-druid-mybatis-annotation](https://github.com/heibaiying/spring-samples-for-all/tree/master/spring/spring-druid-mybatis-annotation) | spring 整合druid、mybatis                                    | [alibaba druid](https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98) |
 | 
				
			||||||
| spring-redis<br/>spring-redis-annotation                     | spring 整合 redis 单机、集群(jedis)<br/>spring 整合 redis 单机、集群(redisson) | [redisson中文文档](https://github.com/redisson/redisson/wiki/%E7%9B%AE%E5%BD%95) |
 | 
					| spring-redis<br/>spring-redis-annotation                     | spring 整合 redis 单机、集群(jedis客户端)<br/>spring 整合 redis 单机、集群(redisson客户端) | [redisson中文文档](https://github.com/redisson/redisson/wiki/%E7%9B%AE%E5%BD%95) |
 | 
				
			||||||
| spring-mongodb<br/>spring-mongodb-annotation                 | spring 整合 mongodb                                          |                                                              |
 | 
					| spring-mongodb<br/>spring-mongodb-annotation                 | spring 整合 mongodb                                          | [Spring Data MongoDB](https://docs.spring.io/spring-data/mongodb/docs/2.1.3.RELEASE/reference/html/#mongo.mongo-db-factory-java) |
 | 
				
			||||||
| spring-memcached<br/>spring-memcached-annotation             | spring 整合 memcached                                        | [Xmemcached中文文档](https://github.com/killme2008/xmemcached/wiki/Xmemcached%20%E4%B8%AD%E6%96%87%E7%94%A8%E6%88%B7%E6%8C%87%E5%8D%97) |
 | 
					| spring-memcached<br/>spring-memcached-annotation             | spring 整合 memcached(单机、集群)                            | [Xmemcached中文文档](https://github.com/killme2008/xmemcached/wiki/Xmemcached%20%E4%B8%AD%E6%96%87%E7%94%A8%E6%88%B7%E6%8C%87%E5%8D%97) |
 | 
				
			||||||
| spring-rabbitmq<br/>spring-rabbitmq-annotation               | spring 整合 rabbitmq                                         |                                                              |
 | 
					| spring-rabbitmq<br/>spring-rabbitmq-annotation               | spring 整合 rabbitmq                                         |                                                              |
 | 
				
			||||||
| spring-kafka<br/>spring-kafka-annotation                     | spring 整合 kafka                                            |                                                              |
 | 
					| spring-kafka<br/>spring-kafka-annotation                     | spring 整合 kafka                                            |                                                              |
 | 
				
			||||||
| spring-dubbo<br/>spring-dubbo-annotation                     | spring 整合 dubbo                                            |                                                              |
 | 
					| spring-dubbo<br/>spring-dubbo-annotation                     | spring 整合 dubbo                                            |                                                              |
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										88
									
								
								spring/spring-rabbitmq-annotation/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										88
									
								
								spring/spring-rabbitmq-annotation/pom.xml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,88 @@
 | 
				
			|||||||
 | 
					<?xml version="1.0" encoding="UTF-8"?>
 | 
				
			||||||
 | 
					<project xmlns="http://maven.apache.org/POM/4.0.0"
 | 
				
			||||||
 | 
					         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 | 
				
			||||||
 | 
					         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 | 
				
			||||||
 | 
					    <modelVersion>4.0.0</modelVersion>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <groupId>com.heibaiying</groupId>
 | 
				
			||||||
 | 
					    <artifactId>spring-rabbitmq</artifactId>
 | 
				
			||||||
 | 
					    <version>1.0-SNAPSHOT</version>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <properties>
 | 
				
			||||||
 | 
					        <spring-base-version>5.1.3.RELEASE</spring-base-version>
 | 
				
			||||||
 | 
					        <maven.compiler.source>1.8</maven.compiler.source>
 | 
				
			||||||
 | 
					        <maven.compiler.target>1.8</maven.compiler.target>
 | 
				
			||||||
 | 
					    </properties>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <dependencies>
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>org.springframework</groupId>
 | 
				
			||||||
 | 
					            <artifactId>spring-context</artifactId>
 | 
				
			||||||
 | 
					            <version>${spring-base-version}</version>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>org.springframework</groupId>
 | 
				
			||||||
 | 
					            <artifactId>spring-beans</artifactId>
 | 
				
			||||||
 | 
					            <version>${spring-base-version}</version>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>org.springframework</groupId>
 | 
				
			||||||
 | 
					            <artifactId>spring-core</artifactId>
 | 
				
			||||||
 | 
					            <version>${spring-base-version}</version>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>org.springframework</groupId>
 | 
				
			||||||
 | 
					            <artifactId>spring-web</artifactId>
 | 
				
			||||||
 | 
					            <version>${spring-base-version}</version>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>org.springframework</groupId>
 | 
				
			||||||
 | 
					            <artifactId>spring-webmvc</artifactId>
 | 
				
			||||||
 | 
					            <version>${spring-base-version}</version>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					        <!--spring rabbitmq 整合依赖-->
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>org.springframework.amqp</groupId>
 | 
				
			||||||
 | 
					            <artifactId>spring-rabbit</artifactId>
 | 
				
			||||||
 | 
					            <version>2.1.2.RELEASE</version>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					        <!--rabbitmq 传输对象序列化依赖了这个包-->
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>com.fasterxml.jackson.core</groupId>
 | 
				
			||||||
 | 
					            <artifactId>jackson-databind</artifactId>
 | 
				
			||||||
 | 
					            <version>2.9.8</version>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					        <!--单元测试相关包-->
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>junit</groupId>
 | 
				
			||||||
 | 
					            <artifactId>junit</artifactId>
 | 
				
			||||||
 | 
					            <version>4.12</version>
 | 
				
			||||||
 | 
					            <scope>test</scope>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>org.springframework</groupId>
 | 
				
			||||||
 | 
					            <artifactId>spring-test</artifactId>
 | 
				
			||||||
 | 
					            <version>${spring-base-version}</version>
 | 
				
			||||||
 | 
					            <scope>test</scope>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>org.projectlombok</groupId>
 | 
				
			||||||
 | 
					            <artifactId>lombok</artifactId>
 | 
				
			||||||
 | 
					            <version>1.18.4</version>
 | 
				
			||||||
 | 
					            <scope>provided</scope>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					    </dependencies>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <build>
 | 
				
			||||||
 | 
					        <finalName>spring-rabbitmq</finalName>
 | 
				
			||||||
 | 
					        <resources>
 | 
				
			||||||
 | 
					            <resource>
 | 
				
			||||||
 | 
					                <directory>src/main/resources</directory>
 | 
				
			||||||
 | 
					            </resource>
 | 
				
			||||||
 | 
					            <resource>
 | 
				
			||||||
 | 
					                <directory>src/main/java</directory>
 | 
				
			||||||
 | 
					            </resource>
 | 
				
			||||||
 | 
					        </resources>
 | 
				
			||||||
 | 
					    </build>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					</project>
 | 
				
			||||||
@@ -0,0 +1,23 @@
 | 
				
			|||||||
 | 
					package com.heibaiying.bean;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import lombok.AllArgsConstructor;
 | 
				
			||||||
 | 
					import lombok.Data;
 | 
				
			||||||
 | 
					import lombok.NoArgsConstructor;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.Date;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author : heibaiying
 | 
				
			||||||
 | 
					 * @description :
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					@Data
 | 
				
			||||||
 | 
					@AllArgsConstructor
 | 
				
			||||||
 | 
					@NoArgsConstructor
 | 
				
			||||||
 | 
					public class ProductManager {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private String name;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private int age;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private Date birthday;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -0,0 +1,25 @@
 | 
				
			|||||||
 | 
					package com.heibaiying.bean;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import lombok.AllArgsConstructor;
 | 
				
			||||||
 | 
					import lombok.Data;
 | 
				
			||||||
 | 
					import lombok.NoArgsConstructor;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.Date;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author : heibaiying
 | 
				
			||||||
 | 
					 * @description :
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					@Data
 | 
				
			||||||
 | 
					@NoArgsConstructor
 | 
				
			||||||
 | 
					@AllArgsConstructor
 | 
				
			||||||
 | 
					public class Programmer {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private String name;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private int age;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private float salary;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private Date birthday;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -0,0 +1,12 @@
 | 
				
			|||||||
 | 
					package com.heibaiying.constant;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author : heibaiying
 | 
				
			||||||
 | 
					 * @description :
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					public interface Type {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    String MANAGER="manager";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    String PROGRAMMER="programmer";
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -0,0 +1,57 @@
 | 
				
			|||||||
 | 
					package com.heibaiying.rabbit;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.heibaiying.rabbit.config.RabbitProperty;
 | 
				
			||||||
 | 
					import com.rabbitmq.client.Channel;
 | 
				
			||||||
 | 
					import org.springframework.amqp.core.*;
 | 
				
			||||||
 | 
					import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
 | 
				
			||||||
 | 
					import org.springframework.amqp.rabbit.connection.ConnectionFactory;
 | 
				
			||||||
 | 
					import org.springframework.amqp.rabbit.core.RabbitAdmin;
 | 
				
			||||||
 | 
					import org.springframework.amqp.rabbit.core.RabbitTemplate;
 | 
				
			||||||
 | 
					import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
 | 
				
			||||||
 | 
					import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
 | 
				
			||||||
 | 
					import org.springframework.context.annotation.Bean;
 | 
				
			||||||
 | 
					import org.springframework.context.annotation.ComponentScan;
 | 
				
			||||||
 | 
					import org.springframework.context.annotation.ComponentScans;
 | 
				
			||||||
 | 
					import org.springframework.context.annotation.Configuration;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author : heibaiying
 | 
				
			||||||
 | 
					 * @description : 声明队列、交换机、绑定关系、和队列消息监听
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Configuration
 | 
				
			||||||
 | 
					@ComponentScan("com.heibaiying.rabbit.config")
 | 
				
			||||||
 | 
					public class RabbitBaseConfig {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 声明连接工厂
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public ConnectionFactory connectionFactory(RabbitProperty property) {
 | 
				
			||||||
 | 
					        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
 | 
				
			||||||
 | 
					        connectionFactory.setAddresses(property.getAddresses());
 | 
				
			||||||
 | 
					        connectionFactory.setUsername(property.getUsername());
 | 
				
			||||||
 | 
					        connectionFactory.setPassword(property.getPassword());
 | 
				
			||||||
 | 
					        connectionFactory.setVirtualHost(property.getVirtualhost());
 | 
				
			||||||
 | 
					        return connectionFactory;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 创建一个管理器(org.springframework.amqp.rabbit.core.RabbitAdmin),用于管理交换,队列和绑定。
 | 
				
			||||||
 | 
					     *  auto-startup 指定是否自动声明上下文中的队列,交换和绑定, 默认值为true。
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
 | 
				
			||||||
 | 
					        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
 | 
				
			||||||
 | 
					        rabbitAdmin.setAutoStartup(true);
 | 
				
			||||||
 | 
					        return rabbitAdmin;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
 | 
				
			||||||
 | 
					        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
 | 
				
			||||||
 | 
					        return rabbitTemplate;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -0,0 +1,20 @@
 | 
				
			|||||||
 | 
					package com.heibaiying.rabbit.config;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.heibaiying.bean.ProductManager;
 | 
				
			||||||
 | 
					import com.heibaiying.bean.Programmer;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author : heibaiying
 | 
				
			||||||
 | 
					 * @description :消息委派处理类
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					public class MessageDelegate {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void handleMessage(ProductManager manager) {
 | 
				
			||||||
 | 
					        System.out.println("收到一个产品经理" + manager);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void handleMessage(Programmer programmer) {
 | 
				
			||||||
 | 
					        System.out.println("收到一个程序员" + programmer);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -0,0 +1,106 @@
 | 
				
			|||||||
 | 
					package com.heibaiying.rabbit.config;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.rabbitmq.client.Channel;
 | 
				
			||||||
 | 
					import org.springframework.amqp.core.*;
 | 
				
			||||||
 | 
					import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
 | 
				
			||||||
 | 
					import org.springframework.amqp.rabbit.connection.ConnectionFactory;
 | 
				
			||||||
 | 
					import org.springframework.amqp.rabbit.core.RabbitAdmin;
 | 
				
			||||||
 | 
					import org.springframework.amqp.rabbit.core.RabbitTemplate;
 | 
				
			||||||
 | 
					import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
 | 
				
			||||||
 | 
					import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
 | 
				
			||||||
 | 
					import org.springframework.context.annotation.Bean;
 | 
				
			||||||
 | 
					import org.springframework.context.annotation.ComponentScan;
 | 
				
			||||||
 | 
					import org.springframework.context.annotation.Configuration;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author : heibaiying
 | 
				
			||||||
 | 
					 * @description : 声明队列、交换机、绑定关系、和队列消息监听
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Configuration
 | 
				
			||||||
 | 
					public class RabbitBaseAnnotation {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public TopicExchange exchange() {
 | 
				
			||||||
 | 
					        // 创建一个持久化的交换机
 | 
				
			||||||
 | 
					        return new TopicExchange("topic01", true, false);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public Queue firstQueue() {
 | 
				
			||||||
 | 
					        // 创建一个持久化的队列1
 | 
				
			||||||
 | 
					        return new Queue("FirstQueue", true);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public Queue secondQueue() {
 | 
				
			||||||
 | 
					        // 创建一个持久化的队列2
 | 
				
			||||||
 | 
					        return new Queue("SecondQueue", true);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * BindingKey 中可以存在两种特殊的字符串“#”和“*”,其中“*”用于匹配一个单词,“#”用于匹配零个或者多个单词
 | 
				
			||||||
 | 
					     * 这里我们声明三个绑定关系用于测试topic这种类型交换器
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public Binding orange() {
 | 
				
			||||||
 | 
					        return BindingBuilder.bind(firstQueue()).to(exchange()).with("*.orange.*");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public Binding rabbit() {
 | 
				
			||||||
 | 
					        return BindingBuilder.bind(secondQueue()).to(exchange()).with("*.*.rabbit");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public Binding lazy() {
 | 
				
			||||||
 | 
					        return BindingBuilder.bind(secondQueue()).to(exchange()).with("lazy.#");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /*创建队列1消费者监听*/
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public SimpleMessageListenerContainer firstQueueLister(ConnectionFactory connectionFactory) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
 | 
				
			||||||
 | 
					        // 设置监听的队列
 | 
				
			||||||
 | 
					        container.setQueues(firstQueue());
 | 
				
			||||||
 | 
					        // 指定要创建的并发使用者数。
 | 
				
			||||||
 | 
					        container.setConcurrentConsumers(1);
 | 
				
			||||||
 | 
					        // 设置消费者数量的上限
 | 
				
			||||||
 | 
					        container.setMaxConcurrentConsumers(5);
 | 
				
			||||||
 | 
					        // 设置是否自动签收消费 为保证消费被成功消费,建议手工签收
 | 
				
			||||||
 | 
					        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
 | 
				
			||||||
 | 
					        container.setMessageListener(new ChannelAwareMessageListener() {
 | 
				
			||||||
 | 
					            @Override
 | 
				
			||||||
 | 
					            public void onMessage(Message message, Channel channel) throws Exception {
 | 
				
			||||||
 | 
					                // 可以在这个地方得到消息额外属性
 | 
				
			||||||
 | 
					                MessageProperties properties = message.getMessageProperties();
 | 
				
			||||||
 | 
					                //得到消息体内容
 | 
				
			||||||
 | 
					                byte[] body = message.getBody();
 | 
				
			||||||
 | 
					                System.out.println(firstQueue().getName() + "收到消息:" + new String(body));
 | 
				
			||||||
 | 
					                //第二个参数 代表是否一次签收多条
 | 
				
			||||||
 | 
					                channel.basicAck(properties.getDeliveryTag(), false);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					        return container;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /*创建队列2消费者监听*/
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public SimpleMessageListenerContainer secondQueueLister(ConnectionFactory connectionFactory) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
 | 
				
			||||||
 | 
					        container.setQueues(secondQueue());
 | 
				
			||||||
 | 
					        container.setMessageListener(new ChannelAwareMessageListener() {
 | 
				
			||||||
 | 
					            @Override
 | 
				
			||||||
 | 
					            public void onMessage(Message message, Channel channel) throws Exception {
 | 
				
			||||||
 | 
					                byte[] body = message.getBody();
 | 
				
			||||||
 | 
					                System.out.println(secondQueue().getName() + "收到消息:" + new String(body));
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					        return container;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -0,0 +1,75 @@
 | 
				
			|||||||
 | 
					package com.heibaiying.rabbit.config;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.heibaiying.constant.Type;
 | 
				
			||||||
 | 
					import org.springframework.amqp.core.Binding;
 | 
				
			||||||
 | 
					import org.springframework.amqp.core.BindingBuilder;
 | 
				
			||||||
 | 
					import org.springframework.amqp.core.DirectExchange;
 | 
				
			||||||
 | 
					import org.springframework.amqp.core.Queue;
 | 
				
			||||||
 | 
					import org.springframework.amqp.rabbit.connection.ConnectionFactory;
 | 
				
			||||||
 | 
					import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
 | 
				
			||||||
 | 
					import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
 | 
				
			||||||
 | 
					import org.springframework.amqp.support.converter.DefaultJackson2JavaTypeMapper;
 | 
				
			||||||
 | 
					import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
 | 
				
			||||||
 | 
					import org.springframework.context.annotation.Bean;
 | 
				
			||||||
 | 
					import org.springframework.context.annotation.Configuration;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.HashMap;
 | 
				
			||||||
 | 
					import java.util.Map;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author : heibaiying
 | 
				
			||||||
 | 
					 * @description : 声明队列、交换机、绑定关系、用于测试对象的消息传递
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Configuration
 | 
				
			||||||
 | 
					public class RabbitObjectAnnotation {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public DirectExchange objectTopic() {
 | 
				
			||||||
 | 
					        // 创建一个持久化的交换机
 | 
				
			||||||
 | 
					        return new DirectExchange("objectTopic", true, false);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public Queue objectQueue() {
 | 
				
			||||||
 | 
					        // 创建一个持久化的队列
 | 
				
			||||||
 | 
					        return new Queue("objectQueue", true);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public Binding binding() {
 | 
				
			||||||
 | 
					        return BindingBuilder.bind(objectQueue()).to(objectTopic()).with("object");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /*创建队列消费者监听*/
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public SimpleMessageListenerContainer objectQueueLister(ConnectionFactory connectionFactory) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
 | 
				
			||||||
 | 
					        // 设置监听的队列
 | 
				
			||||||
 | 
					        container.setQueues(objectQueue());
 | 
				
			||||||
 | 
					        // 将监听到的消息委派给实际的处理类
 | 
				
			||||||
 | 
					        MessageListenerAdapter adapter = new MessageListenerAdapter(new MessageDelegate());
 | 
				
			||||||
 | 
					        // 指定由哪个方法来处理消息 默认就是handleMessage
 | 
				
			||||||
 | 
					        adapter.setDefaultListenerMethod("handleMessage");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // 消息转换
 | 
				
			||||||
 | 
					        Jackson2JsonMessageConverter jackson2JsonMessageConverter = new Jackson2JsonMessageConverter();
 | 
				
			||||||
 | 
					        DefaultJackson2JavaTypeMapper javaTypeMapper = new DefaultJackson2JavaTypeMapper();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Map<String, Class<?>> idClassMapping = new HashMap<>();
 | 
				
			||||||
 | 
					        // 针对不同的消息体调用不同的重载方法
 | 
				
			||||||
 | 
					        idClassMapping.put(Type.MANAGER, com.heibaiying.bean.ProductManager.class);
 | 
				
			||||||
 | 
					        idClassMapping.put(Type.PROGRAMMER, com.heibaiying.bean.Programmer.class);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        javaTypeMapper.setIdClassMapping(idClassMapping);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        jackson2JsonMessageConverter.setJavaTypeMapper(javaTypeMapper);
 | 
				
			||||||
 | 
					        adapter.setMessageConverter(jackson2JsonMessageConverter);
 | 
				
			||||||
 | 
					        container.setMessageListener(adapter);
 | 
				
			||||||
 | 
					        return container;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -0,0 +1,29 @@
 | 
				
			|||||||
 | 
					package com.heibaiying.rabbit.config;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import lombok.Data;
 | 
				
			||||||
 | 
					import org.springframework.beans.factory.annotation.Value;
 | 
				
			||||||
 | 
					import org.springframework.context.annotation.Configuration;
 | 
				
			||||||
 | 
					import org.springframework.context.annotation.PropertySource;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author : heibaiying
 | 
				
			||||||
 | 
					 * @description : rabbit 属性配置
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					@Data
 | 
				
			||||||
 | 
					@PropertySource(value = "classpath:rabbitmq.properties")
 | 
				
			||||||
 | 
					@Configuration
 | 
				
			||||||
 | 
					public class RabbitProperty {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Value("${rabbitmq.addresses}")
 | 
				
			||||||
 | 
					    private String addresses;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Value("${rabbitmq.username}")
 | 
				
			||||||
 | 
					    private String username;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Value("${rabbitmq.password}")
 | 
				
			||||||
 | 
					    private String password;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Value("${rabbitmq.virtualhost}")
 | 
				
			||||||
 | 
					    private String virtualhost;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -0,0 +1,5 @@
 | 
				
			|||||||
 | 
					rabbitmq.addresses=localhost:5672
 | 
				
			||||||
 | 
					rabbitmq.username=guest
 | 
				
			||||||
 | 
					rabbitmq.password=guest
 | 
				
			||||||
 | 
					# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD>ռ<EFBFBD> Ĭ<><C4AC>Ϊ/  <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD>ν<EFBFBD><CEBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߹ܿ<DFB9>̨<EFBFBD><CCA8><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><F2B2BBBB>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>׳<EFBFBD><D7B3>쳣
 | 
				
			||||||
 | 
					rabbitmq.virtualhost=/
 | 
				
			||||||
@@ -0,0 +1,58 @@
 | 
				
			|||||||
 | 
					package com.heibaiying.rabbit;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.fasterxml.jackson.core.JsonProcessingException;
 | 
				
			||||||
 | 
					import com.fasterxml.jackson.databind.ObjectMapper;
 | 
				
			||||||
 | 
					import com.heibaiying.bean.ProductManager;
 | 
				
			||||||
 | 
					import com.heibaiying.bean.Programmer;
 | 
				
			||||||
 | 
					import com.heibaiying.constant.Type;
 | 
				
			||||||
 | 
					import org.junit.Test;
 | 
				
			||||||
 | 
					import org.junit.runner.RunWith;
 | 
				
			||||||
 | 
					import org.springframework.amqp.core.Message;
 | 
				
			||||||
 | 
					import org.springframework.amqp.core.MessageProperties;
 | 
				
			||||||
 | 
					import org.springframework.amqp.rabbit.core.RabbitTemplate;
 | 
				
			||||||
 | 
					import org.springframework.beans.factory.annotation.Autowired;
 | 
				
			||||||
 | 
					import org.springframework.test.context.ContextConfiguration;
 | 
				
			||||||
 | 
					import org.springframework.test.context.junit4.SpringRunner;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.Date;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author : heibaiying
 | 
				
			||||||
 | 
					 * @description : 传输对象
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@RunWith(SpringRunner.class)
 | 
				
			||||||
 | 
					@ContextConfiguration(classes = RabbitBaseConfig.class)
 | 
				
			||||||
 | 
					public class RabbitSendObjectTest {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Autowired
 | 
				
			||||||
 | 
					    private RabbitTemplate rabbitTemplate;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    public void sendProgrammer() throws JsonProcessingException {
 | 
				
			||||||
 | 
					        MessageProperties messageProperties = new MessageProperties();
 | 
				
			||||||
 | 
					        //必须设置 contentType为 application/json
 | 
				
			||||||
 | 
					        messageProperties.setContentType("application/json");
 | 
				
			||||||
 | 
					        // 必须指定类型
 | 
				
			||||||
 | 
					        messageProperties.getHeaders().put("__TypeId__", Type.PROGRAMMER);
 | 
				
			||||||
 | 
					        Programmer programmer = new Programmer("xiaoming", 34, 52200.21f, new Date());
 | 
				
			||||||
 | 
					        // 序列化与反序列化都使用的Jackson
 | 
				
			||||||
 | 
					        ObjectMapper mapper = new ObjectMapper();
 | 
				
			||||||
 | 
					        String programmerJson = mapper.writeValueAsString(programmer);
 | 
				
			||||||
 | 
					        Message message = new Message(programmerJson.getBytes(), messageProperties);
 | 
				
			||||||
 | 
					        rabbitTemplate.send("objectTopic", "object", message);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    public void sendProductManager() throws JsonProcessingException {
 | 
				
			||||||
 | 
					        MessageProperties messageProperties = new MessageProperties();
 | 
				
			||||||
 | 
					        messageProperties.setContentType("application/json");
 | 
				
			||||||
 | 
					        messageProperties.getHeaders().put("__TypeId__", Type.MANAGER);
 | 
				
			||||||
 | 
					        ProductManager manager = new ProductManager("xiaohong", 21, new Date());
 | 
				
			||||||
 | 
					        ObjectMapper mapper = new ObjectMapper();
 | 
				
			||||||
 | 
					        String managerJson = mapper.writeValueAsString(manager);
 | 
				
			||||||
 | 
					        Message message = new Message(managerJson.getBytes(), messageProperties);
 | 
				
			||||||
 | 
					        rabbitTemplate.send("objectTopic", "object", message);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -0,0 +1,51 @@
 | 
				
			|||||||
 | 
					package com.heibaiying.rabbit;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.junit.Test;
 | 
				
			||||||
 | 
					import org.junit.runner.RunWith;
 | 
				
			||||||
 | 
					import org.springframework.amqp.core.Message;
 | 
				
			||||||
 | 
					import org.springframework.amqp.core.MessageProperties;
 | 
				
			||||||
 | 
					import org.springframework.amqp.rabbit.core.RabbitTemplate;
 | 
				
			||||||
 | 
					import org.springframework.beans.factory.annotation.Autowired;
 | 
				
			||||||
 | 
					import org.springframework.test.context.ContextConfiguration;
 | 
				
			||||||
 | 
					import org.springframework.test.context.junit4.SpringRunner;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author : heibaiying
 | 
				
			||||||
 | 
					 * @description : 传输简单字符串
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@RunWith(SpringRunner.class)
 | 
				
			||||||
 | 
					@ContextConfiguration(classes = RabbitBaseConfig.class)
 | 
				
			||||||
 | 
					public class RabbitTest {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Autowired
 | 
				
			||||||
 | 
					    private RabbitTemplate rabbitTemplate;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    public void sendMessage() {
 | 
				
			||||||
 | 
					        MessageProperties properties = new MessageProperties();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        String allReceived = "我的路由键 quick.orange.rabbit 符合queue1 和 queue2 的要求,我应该被两个监听器接收到";
 | 
				
			||||||
 | 
					        Message message1 = new Message(allReceived.getBytes(), properties);
 | 
				
			||||||
 | 
					        rabbitTemplate.send("topic01", "quick.orange.rabbit", message1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        String firstReceived = "我的路由键 quick.orange.fox 只符合queue1 的要求,只能被queue 1 接收到";
 | 
				
			||||||
 | 
					        Message message2 = new Message(firstReceived.getBytes(), properties);
 | 
				
			||||||
 | 
					        rabbitTemplate.send("topic01", "quick.orange.fox", message2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        String secondReceived = "我的路由键 lazy.brown.fox 只符合queue2 的要求,只能被queue 2 接收到";
 | 
				
			||||||
 | 
					        Message message3 = new Message(secondReceived.getBytes(), properties);
 | 
				
			||||||
 | 
					        rabbitTemplate.send("topic01", "lazy.brown.fox", message3);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        String notReceived = "我的路由键 quick.brown.fox 不符合 topic1 任何绑定队列的要求,你将看不到我";
 | 
				
			||||||
 | 
					        Message message4 = new Message(notReceived.getBytes(), properties);
 | 
				
			||||||
 | 
					        rabbitTemplate.send("topic01", "quick.brown.fox", message4);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /*
 | 
				
			||||||
 | 
					         * SecondQueue收到消息:我的路由键 quick.orange.rabbit 符合queue1 和 queue2 的要求,我应该被两个监听器接收到
 | 
				
			||||||
 | 
					         * FirstQueue收到消息:我的路由键 quick.orange.rabbit 符合queue1 和 queue2 的要求,我应该被两个监听器接收到
 | 
				
			||||||
 | 
					         * FirstQueue收到消息:我的路由键 quick.orange.fox 只符合queue1 的要求,只能被queue 1 接收到
 | 
				
			||||||
 | 
					         * SecondQueue收到消息:我的路由键 lazy.brown.fox 只符合queue2 的要求,只能被queue 2 接收到
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										88
									
								
								spring/spring-rabbitmq/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										88
									
								
								spring/spring-rabbitmq/pom.xml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,88 @@
 | 
				
			|||||||
 | 
					<?xml version="1.0" encoding="UTF-8"?>
 | 
				
			||||||
 | 
					<project xmlns="http://maven.apache.org/POM/4.0.0"
 | 
				
			||||||
 | 
					         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 | 
				
			||||||
 | 
					         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 | 
				
			||||||
 | 
					    <modelVersion>4.0.0</modelVersion>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <groupId>com.heibaiying</groupId>
 | 
				
			||||||
 | 
					    <artifactId>spring-rabbitmq</artifactId>
 | 
				
			||||||
 | 
					    <version>1.0-SNAPSHOT</version>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <properties>
 | 
				
			||||||
 | 
					        <spring-base-version>5.1.3.RELEASE</spring-base-version>
 | 
				
			||||||
 | 
					        <maven.compiler.source>1.8</maven.compiler.source>
 | 
				
			||||||
 | 
					        <maven.compiler.target>1.8</maven.compiler.target>
 | 
				
			||||||
 | 
					    </properties>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <dependencies>
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>org.springframework</groupId>
 | 
				
			||||||
 | 
					            <artifactId>spring-context</artifactId>
 | 
				
			||||||
 | 
					            <version>${spring-base-version}</version>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>org.springframework</groupId>
 | 
				
			||||||
 | 
					            <artifactId>spring-beans</artifactId>
 | 
				
			||||||
 | 
					            <version>${spring-base-version}</version>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>org.springframework</groupId>
 | 
				
			||||||
 | 
					            <artifactId>spring-core</artifactId>
 | 
				
			||||||
 | 
					            <version>${spring-base-version}</version>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>org.springframework</groupId>
 | 
				
			||||||
 | 
					            <artifactId>spring-web</artifactId>
 | 
				
			||||||
 | 
					            <version>${spring-base-version}</version>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>org.springframework</groupId>
 | 
				
			||||||
 | 
					            <artifactId>spring-webmvc</artifactId>
 | 
				
			||||||
 | 
					            <version>${spring-base-version}</version>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					        <!--spring rabbitmq 整合依赖-->
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>org.springframework.amqp</groupId>
 | 
				
			||||||
 | 
					            <artifactId>spring-rabbit</artifactId>
 | 
				
			||||||
 | 
					            <version>2.1.2.RELEASE</version>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					        <!--rabbitmq 传输对象序列化依赖了这个包-->
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>com.fasterxml.jackson.core</groupId>
 | 
				
			||||||
 | 
					            <artifactId>jackson-databind</artifactId>
 | 
				
			||||||
 | 
					            <version>2.9.8</version>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					        <!--单元测试相关包-->
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>junit</groupId>
 | 
				
			||||||
 | 
					            <artifactId>junit</artifactId>
 | 
				
			||||||
 | 
					            <version>4.12</version>
 | 
				
			||||||
 | 
					            <scope>test</scope>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>org.springframework</groupId>
 | 
				
			||||||
 | 
					            <artifactId>spring-test</artifactId>
 | 
				
			||||||
 | 
					            <version>${spring-base-version}</version>
 | 
				
			||||||
 | 
					            <scope>test</scope>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>org.projectlombok</groupId>
 | 
				
			||||||
 | 
					            <artifactId>lombok</artifactId>
 | 
				
			||||||
 | 
					            <version>1.18.4</version>
 | 
				
			||||||
 | 
					            <scope>provided</scope>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					    </dependencies>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <build>
 | 
				
			||||||
 | 
					        <finalName>spring-rabbitmq</finalName>
 | 
				
			||||||
 | 
					        <resources>
 | 
				
			||||||
 | 
					            <resource>
 | 
				
			||||||
 | 
					                <directory>src/main/resources</directory>
 | 
				
			||||||
 | 
					            </resource>
 | 
				
			||||||
 | 
					            <resource>
 | 
				
			||||||
 | 
					                <directory>src/main/java</directory>
 | 
				
			||||||
 | 
					            </resource>
 | 
				
			||||||
 | 
					        </resources>
 | 
				
			||||||
 | 
					    </build>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					</project>
 | 
				
			||||||
@@ -0,0 +1,23 @@
 | 
				
			|||||||
 | 
					package com.heibaiying.bean;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import lombok.AllArgsConstructor;
 | 
				
			||||||
 | 
					import lombok.Data;
 | 
				
			||||||
 | 
					import lombok.NoArgsConstructor;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.Date;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author : heibaiying
 | 
				
			||||||
 | 
					 * @description :
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					@Data
 | 
				
			||||||
 | 
					@AllArgsConstructor
 | 
				
			||||||
 | 
					@NoArgsConstructor
 | 
				
			||||||
 | 
					public class ProductManager {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private String name;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private int age;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private Date birthday;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -0,0 +1,25 @@
 | 
				
			|||||||
 | 
					package com.heibaiying.bean;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import lombok.AllArgsConstructor;
 | 
				
			||||||
 | 
					import lombok.Data;
 | 
				
			||||||
 | 
					import lombok.NoArgsConstructor;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.Date;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author : heibaiying
 | 
				
			||||||
 | 
					 * @description :
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					@Data
 | 
				
			||||||
 | 
					@NoArgsConstructor
 | 
				
			||||||
 | 
					@AllArgsConstructor
 | 
				
			||||||
 | 
					public class Programmer {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private String name;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private int age;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private float salary;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private Date birthday;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -0,0 +1,12 @@
 | 
				
			|||||||
 | 
					package com.heibaiying.constant;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author : heibaiying
 | 
				
			||||||
 | 
					 * @description :
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					public interface Type {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    String MANAGER="manager";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    String PROGRAMMER="programmer";
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -0,0 +1,20 @@
 | 
				
			|||||||
 | 
					package com.heibaiying.rabbit;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.heibaiying.bean.ProductManager;
 | 
				
			||||||
 | 
					import com.heibaiying.bean.Programmer;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author : heibaiying
 | 
				
			||||||
 | 
					 * @description :消息委派处理类
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					public class MessageDelegate {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void handleMessage(ProductManager manager) {
 | 
				
			||||||
 | 
					        System.out.println("收到一个产品经理" + manager);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public void handleMessage(Programmer programmer) {
 | 
				
			||||||
 | 
					        System.out.println("收到一个程序员" + programmer);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -0,0 +1,102 @@
 | 
				
			|||||||
 | 
					package com.heibaiying.rabbit;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.rabbitmq.client.Channel;
 | 
				
			||||||
 | 
					import org.springframework.amqp.core.*;
 | 
				
			||||||
 | 
					import org.springframework.amqp.rabbit.connection.ConnectionFactory;
 | 
				
			||||||
 | 
					import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
 | 
				
			||||||
 | 
					import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
 | 
				
			||||||
 | 
					import org.springframework.context.annotation.Bean;
 | 
				
			||||||
 | 
					import org.springframework.context.annotation.Configuration;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author : heibaiying
 | 
				
			||||||
 | 
					 * @description : 声明队列、交换机、绑定关系、和队列消息监听
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Configuration
 | 
				
			||||||
 | 
					public class RabbitBaseAnnotation {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public TopicExchange exchange() {
 | 
				
			||||||
 | 
					        // 创建一个持久化的交换机
 | 
				
			||||||
 | 
					        return new TopicExchange("topic01", true, false);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public Queue firstQueue() {
 | 
				
			||||||
 | 
					        // 创建一个持久化的队列1
 | 
				
			||||||
 | 
					        return new Queue("FirstQueue", true);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public Queue secondQueue() {
 | 
				
			||||||
 | 
					        // 创建一个持久化的队列2
 | 
				
			||||||
 | 
					        return new Queue("SecondQueue", true);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * BindingKey 中可以存在两种特殊的字符串“#”和“*”,其中“*”用于匹配一个单词,“#”用于匹配零个或者多个单词
 | 
				
			||||||
 | 
					     * 这里我们声明三个绑定关系用于测试topic这种类型交换器
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public Binding orange() {
 | 
				
			||||||
 | 
					        return BindingBuilder.bind(firstQueue()).to(exchange()).with("*.orange.*");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public Binding rabbit() {
 | 
				
			||||||
 | 
					        return BindingBuilder.bind(secondQueue()).to(exchange()).with("*.*.rabbit");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public Binding lazy() {
 | 
				
			||||||
 | 
					        return BindingBuilder.bind(secondQueue()).to(exchange()).with("lazy.#");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /*创建队列1消费者监听*/
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public SimpleMessageListenerContainer firstQueueLister(ConnectionFactory connectionFactory) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
 | 
				
			||||||
 | 
					        // 设置监听的队列
 | 
				
			||||||
 | 
					        container.setQueues(firstQueue());
 | 
				
			||||||
 | 
					        // 指定要创建的并发使用者数。
 | 
				
			||||||
 | 
					        container.setConcurrentConsumers(1);
 | 
				
			||||||
 | 
					        // 设置消费者数量的上限
 | 
				
			||||||
 | 
					        container.setMaxConcurrentConsumers(5);
 | 
				
			||||||
 | 
					        // 设置是否自动签收消费 为保证消费被成功消费,建议手工签收
 | 
				
			||||||
 | 
					        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
 | 
				
			||||||
 | 
					        container.setMessageListener(new ChannelAwareMessageListener() {
 | 
				
			||||||
 | 
					            @Override
 | 
				
			||||||
 | 
					            public void onMessage(Message message, Channel channel) throws Exception {
 | 
				
			||||||
 | 
					                // 可以在这个地方得到消息额外属性
 | 
				
			||||||
 | 
					                MessageProperties properties = message.getMessageProperties();
 | 
				
			||||||
 | 
					                //得到消息体内容
 | 
				
			||||||
 | 
					                byte[] body = message.getBody();
 | 
				
			||||||
 | 
					                System.out.println(firstQueue().getName() + "收到消息:" + new String(body));
 | 
				
			||||||
 | 
					                //第二个参数 代表是否一次签收多条
 | 
				
			||||||
 | 
					                channel.basicAck(properties.getDeliveryTag(), false);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					        return container;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /*创建队列2消费者监听*/
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public SimpleMessageListenerContainer secondQueueLister(ConnectionFactory connectionFactory) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
 | 
				
			||||||
 | 
					        container.setQueues(secondQueue());
 | 
				
			||||||
 | 
					        container.setMessageListener(new ChannelAwareMessageListener() {
 | 
				
			||||||
 | 
					            @Override
 | 
				
			||||||
 | 
					            public void onMessage(Message message, Channel channel) throws Exception {
 | 
				
			||||||
 | 
					                byte[] body = message.getBody();
 | 
				
			||||||
 | 
					                System.out.println(secondQueue().getName() + "收到消息:" + new String(body));
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					        return container;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -0,0 +1,73 @@
 | 
				
			|||||||
 | 
					package com.heibaiying.rabbit;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.heibaiying.constant.Type;
 | 
				
			||||||
 | 
					import com.rabbitmq.client.Channel;
 | 
				
			||||||
 | 
					import org.springframework.amqp.core.*;
 | 
				
			||||||
 | 
					import org.springframework.amqp.rabbit.connection.ConnectionFactory;
 | 
				
			||||||
 | 
					import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
 | 
				
			||||||
 | 
					import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
 | 
				
			||||||
 | 
					import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
 | 
				
			||||||
 | 
					import org.springframework.amqp.support.converter.DefaultJackson2JavaTypeMapper;
 | 
				
			||||||
 | 
					import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
 | 
				
			||||||
 | 
					import org.springframework.context.annotation.Bean;
 | 
				
			||||||
 | 
					import org.springframework.context.annotation.Configuration;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.HashMap;
 | 
				
			||||||
 | 
					import java.util.Map;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author : heibaiying
 | 
				
			||||||
 | 
					 * @description : 声明队列、交换机、绑定关系、用于测试对象的消息传递
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Configuration
 | 
				
			||||||
 | 
					public class RabbitObjectAnnotation {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public DirectExchange objectTopic() {
 | 
				
			||||||
 | 
					        // 创建一个持久化的交换机
 | 
				
			||||||
 | 
					        return new DirectExchange("objectTopic", true, false);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public Queue objectQueue() {
 | 
				
			||||||
 | 
					        // 创建一个持久化的队列
 | 
				
			||||||
 | 
					        return new Queue("objectQueue", true);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public Binding binding() {
 | 
				
			||||||
 | 
					        return BindingBuilder.bind(objectQueue()).to(objectTopic()).with("object");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /*创建队列消费者监听*/
 | 
				
			||||||
 | 
					    @Bean
 | 
				
			||||||
 | 
					    public SimpleMessageListenerContainer objectQueueLister(ConnectionFactory connectionFactory) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
 | 
				
			||||||
 | 
					        // 设置监听的队列
 | 
				
			||||||
 | 
					        container.setQueues(objectQueue());
 | 
				
			||||||
 | 
					        // 将监听到的消息委派给实际的处理类
 | 
				
			||||||
 | 
					        MessageListenerAdapter adapter = new MessageListenerAdapter(new MessageDelegate());
 | 
				
			||||||
 | 
					        // 指定由哪个方法来处理消息 默认就是handleMessage
 | 
				
			||||||
 | 
					        adapter.setDefaultListenerMethod("handleMessage");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // 消息转换
 | 
				
			||||||
 | 
					        Jackson2JsonMessageConverter jackson2JsonMessageConverter = new Jackson2JsonMessageConverter();
 | 
				
			||||||
 | 
					        DefaultJackson2JavaTypeMapper javaTypeMapper = new DefaultJackson2JavaTypeMapper();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        Map<String, Class<?>> idClassMapping = new HashMap<>();
 | 
				
			||||||
 | 
					        // 针对不同的消息体调用不同的重载方法
 | 
				
			||||||
 | 
					        idClassMapping.put(Type.MANAGER, com.heibaiying.bean.ProductManager.class);
 | 
				
			||||||
 | 
					        idClassMapping.put(Type.PROGRAMMER, com.heibaiying.bean.Programmer.class);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        javaTypeMapper.setIdClassMapping(idClassMapping);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        jackson2JsonMessageConverter.setJavaTypeMapper(javaTypeMapper);
 | 
				
			||||||
 | 
					        adapter.setMessageConverter(jackson2JsonMessageConverter);
 | 
				
			||||||
 | 
					        container.setMessageListener(adapter);
 | 
				
			||||||
 | 
					        return container;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -0,0 +1,5 @@
 | 
				
			|||||||
 | 
					rabbitmq.addresses=localhost:5672
 | 
				
			||||||
 | 
					rabbitmq.username=guest
 | 
				
			||||||
 | 
					rabbitmq.password=guest
 | 
				
			||||||
 | 
					# <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD>ռ<EFBFBD> Ĭ<><C4AC>Ϊ/  <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD>ν<EFBFBD><CEBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߹ܿ<DFB9>̨<EFBFBD><CCA8><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><F2B2BBBB>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>׳<EFBFBD><D7B3>쳣
 | 
				
			||||||
 | 
					rabbitmq.virtualhost=/
 | 
				
			||||||
							
								
								
									
										43
									
								
								spring/spring-rabbitmq/src/main/resources/rabbitmq.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								spring/spring-rabbitmq/src/main/resources/rabbitmq.xml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,43 @@
 | 
				
			|||||||
 | 
					<?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/context
 | 
				
			||||||
 | 
					          http://www.springframework.org/schema/context/spring-context.xsd
 | 
				
			||||||
 | 
					          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">
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <context:property-placeholder location="rabbitmq.properties"/>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <!--声明连接工厂-->
 | 
				
			||||||
 | 
					    <rabbit:connection-factory id="connectionFactory"
 | 
				
			||||||
 | 
					                               addresses="${rabbitmq.addresses}"
 | 
				
			||||||
 | 
					                               username="${rabbitmq.username}"
 | 
				
			||||||
 | 
					                               password="${rabbitmq.password}"
 | 
				
			||||||
 | 
					                               virtual-host="${rabbitmq.virtualhost}"/>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <!--创建一个管理器(org.springframework.amqp.rabbit.core.RabbitAdmin),用于管理交换,队列和绑定。
 | 
				
			||||||
 | 
					    auto-startup 指定是否自动声明上下文中的队列,交换和绑定, 默认值为true。-->
 | 
				
			||||||
 | 
					    <rabbit:admin connection-factory="connectionFactory" auto-startup="true"/>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <!--声明 template 的时候需要声明id 不然会抛出异常-->
 | 
				
			||||||
 | 
					    <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <!--可以在xml采用如下方式声明交换机、队列、绑定管理 但是建议使用代码方式声明 方法更加灵活且可以采用链调用-->
 | 
				
			||||||
 | 
					    <rabbit:queue name="remoting.queue"/>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <rabbit:direct-exchange name="remoting.exchange">
 | 
				
			||||||
 | 
					        <rabbit:bindings>
 | 
				
			||||||
 | 
					            <rabbit:binding queue="remoting.queue" key="remoting.binding"/>
 | 
				
			||||||
 | 
					        </rabbit:bindings>
 | 
				
			||||||
 | 
					    </rabbit:direct-exchange>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    <!--扫描rabbit包 自动声明交换器、队列、绑定关系-->
 | 
				
			||||||
 | 
					    <context:component-scan base-package="com.heibaiying.rabbit"/>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					</beans>
 | 
				
			||||||
@@ -0,0 +1,58 @@
 | 
				
			|||||||
 | 
					package com.heibaiying.rabbit;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.fasterxml.jackson.core.JsonProcessingException;
 | 
				
			||||||
 | 
					import com.fasterxml.jackson.databind.ObjectMapper;
 | 
				
			||||||
 | 
					import com.heibaiying.bean.ProductManager;
 | 
				
			||||||
 | 
					import com.heibaiying.bean.Programmer;
 | 
				
			||||||
 | 
					import com.heibaiying.constant.Type;
 | 
				
			||||||
 | 
					import org.junit.Test;
 | 
				
			||||||
 | 
					import org.junit.runner.RunWith;
 | 
				
			||||||
 | 
					import org.springframework.amqp.core.Message;
 | 
				
			||||||
 | 
					import org.springframework.amqp.core.MessageProperties;
 | 
				
			||||||
 | 
					import org.springframework.amqp.rabbit.core.RabbitTemplate;
 | 
				
			||||||
 | 
					import org.springframework.beans.factory.annotation.Autowired;
 | 
				
			||||||
 | 
					import org.springframework.test.context.ContextConfiguration;
 | 
				
			||||||
 | 
					import org.springframework.test.context.junit4.SpringRunner;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.Date;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author : heibaiying
 | 
				
			||||||
 | 
					 * @description : 传输对象
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@RunWith(SpringRunner.class)
 | 
				
			||||||
 | 
					@ContextConfiguration(locations = "classpath:rabbitmq.xml")
 | 
				
			||||||
 | 
					public class RabbitSendObjectTest {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Autowired
 | 
				
			||||||
 | 
					    private RabbitTemplate rabbitTemplate;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    public void sendProgrammer() throws JsonProcessingException {
 | 
				
			||||||
 | 
					        MessageProperties messageProperties = new MessageProperties();
 | 
				
			||||||
 | 
					        //必须设置 contentType为 application/json
 | 
				
			||||||
 | 
					        messageProperties.setContentType("application/json");
 | 
				
			||||||
 | 
					        // 必须指定类型
 | 
				
			||||||
 | 
					        messageProperties.getHeaders().put("__TypeId__", Type.PROGRAMMER);
 | 
				
			||||||
 | 
					        Programmer programmer = new Programmer("xiaoming", 34, 52200.21f, new Date());
 | 
				
			||||||
 | 
					        // 序列化与反序列化都使用的Jackson
 | 
				
			||||||
 | 
					        ObjectMapper mapper = new ObjectMapper();
 | 
				
			||||||
 | 
					        String programmerJson = mapper.writeValueAsString(programmer);
 | 
				
			||||||
 | 
					        Message message = new Message(programmerJson.getBytes(), messageProperties);
 | 
				
			||||||
 | 
					        rabbitTemplate.send("objectTopic", "object", message);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    public void sendProductManager() throws JsonProcessingException {
 | 
				
			||||||
 | 
					        MessageProperties messageProperties = new MessageProperties();
 | 
				
			||||||
 | 
					        messageProperties.setContentType("application/json");
 | 
				
			||||||
 | 
					        messageProperties.getHeaders().put("__TypeId__", Type.MANAGER);
 | 
				
			||||||
 | 
					        ProductManager manager = new ProductManager("xiaohong", 21, new Date());
 | 
				
			||||||
 | 
					        ObjectMapper mapper = new ObjectMapper();
 | 
				
			||||||
 | 
					        String managerJson = mapper.writeValueAsString(manager);
 | 
				
			||||||
 | 
					        Message message = new Message(managerJson.getBytes(), messageProperties);
 | 
				
			||||||
 | 
					        rabbitTemplate.send("objectTopic", "object", message);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -0,0 +1,51 @@
 | 
				
			|||||||
 | 
					package com.heibaiying.rabbit;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.junit.Test;
 | 
				
			||||||
 | 
					import org.junit.runner.RunWith;
 | 
				
			||||||
 | 
					import org.springframework.amqp.core.Message;
 | 
				
			||||||
 | 
					import org.springframework.amqp.core.MessageProperties;
 | 
				
			||||||
 | 
					import org.springframework.amqp.rabbit.core.RabbitTemplate;
 | 
				
			||||||
 | 
					import org.springframework.beans.factory.annotation.Autowired;
 | 
				
			||||||
 | 
					import org.springframework.test.context.ContextConfiguration;
 | 
				
			||||||
 | 
					import org.springframework.test.context.junit4.SpringRunner;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * @author : heibaiying
 | 
				
			||||||
 | 
					 * @description : 传输简单字符串
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@RunWith(SpringRunner.class)
 | 
				
			||||||
 | 
					@ContextConfiguration(locations = "classpath:rabbitmq.xml")
 | 
				
			||||||
 | 
					public class RabbitTest {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Autowired
 | 
				
			||||||
 | 
					    private RabbitTemplate rabbitTemplate;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    public void sendMessage() {
 | 
				
			||||||
 | 
					        MessageProperties properties = new MessageProperties();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        String allReceived = "我的路由键 quick.orange.rabbit 符合queue1 和 queue2 的要求,我应该被两个监听器接收到";
 | 
				
			||||||
 | 
					        Message message1 = new Message(allReceived.getBytes(), properties);
 | 
				
			||||||
 | 
					        rabbitTemplate.send("topic01", "quick.orange.rabbit", message1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        String firstReceived = "我的路由键 quick.orange.fox 只符合queue1 的要求,只能被queue 1 接收到";
 | 
				
			||||||
 | 
					        Message message2 = new Message(firstReceived.getBytes(), properties);
 | 
				
			||||||
 | 
					        rabbitTemplate.send("topic01", "quick.orange.fox", message2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        String secondReceived = "我的路由键 lazy.brown.fox 只符合queue2 的要求,只能被queue 2 接收到";
 | 
				
			||||||
 | 
					        Message message3 = new Message(secondReceived.getBytes(), properties);
 | 
				
			||||||
 | 
					        rabbitTemplate.send("topic01", "lazy.brown.fox", message3);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        String notReceived = "我的路由键 quick.brown.fox 不符合 topic1 任何绑定队列的要求,你将看不到我";
 | 
				
			||||||
 | 
					        Message message4 = new Message(notReceived.getBytes(), properties);
 | 
				
			||||||
 | 
					        rabbitTemplate.send("topic01", "quick.brown.fox", message4);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /*
 | 
				
			||||||
 | 
					         * SecondQueue收到消息:我的路由键 quick.orange.rabbit 符合queue1 和 queue2 的要求,我应该被两个监听器接收到
 | 
				
			||||||
 | 
					         * FirstQueue收到消息:我的路由键 quick.orange.rabbit 符合queue1 和 queue2 的要求,我应该被两个监听器接收到
 | 
				
			||||||
 | 
					         * FirstQueue收到消息:我的路由键 quick.orange.fox 只符合queue1 的要求,只能被queue 1 接收到
 | 
				
			||||||
 | 
					         * SecondQueue收到消息:我的路由键 lazy.brown.fox 只符合queue2 的要求,只能被queue 2 接收到
 | 
				
			||||||
 | 
					         */
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user