Spring Boot中引入RabbitMQ

简单示例,演示发送、消费。
RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,最初起源于金融系统,AMQP,即Advanced Message Queuing Protocol,高级消息队列协议。

1、RabbitMQ配置

RabbitMQ使用amqp协议,在spring boot中需要引入spring-boot-starter-amqp
pom.xml

<!-- RabbitMQ amqp -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

application.properties

spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
#spring.rabbitmq.username=guest
#spring.rabbitmq.password=guest
spring.rabbitmq.username=admin
spring.rabbitmq.password=123456

RabbitConfig
配置两个队列

@Configuration
public class RabbitConfig {	
	@Bean
    public Queue UserQueue() {
        //the name of the queue
        return new Queue("userinfoRK");
    }
	@Bean
    public Queue Queue() {
		//Simple container collecting information to describe a queue. 
		//Used in conjunction with AmqpAdmin. conjunction 互相协调
        return new Queue("MyRabbitQueue");
    }
}

2、业务模拟

User.java实体类

public class User  implements Serializable {
	private static final long serialVersionUID = -1288839839888856613L;
	String name;
	int age;
	List<String> books;
	get,set...
	
	public String toString() {//输出使用
		String bkList = "";
		if (books != null) {
			for (String book : books) {
				bkList += book+", ";
			}
		}
		return "The Entity is name:" + name + ",age:" + age + ",Books:" + bkList;
	}

}

MySender.java消息发送

@Component
public class MySender {
	@Autowired
	//Specifies a basic set of AMQP operations.
	//Provides synchronous send and receive methods.
	private AmqpTemplate rabbitTemplate;

	/**
	 * The method send POJO objects.
	 */
	public void sendPOJO() {		
		//POJO
		User user = new User();
		user.setName("Tom");
		user.setAge(35);
		//list
		List<String> books = new ArrayList<String>();
		books.add("the book");
		books.add("that book");
		user.setBooks(books);		

		//Convert a Java object to an Amqp Message
		// and send it to a default exchange with a specific routing key.
		this.rabbitTemplate.convertAndSend("userinfoRK", user);
	}	
	/**
	 * send message in circle mode
	 */
	public void send() {
		String context = "hi, tom " + new Date();
		for (int i = 0; i < 10; i++) {
			context = context +" - "+ i;
			System.out.println(context);
			this.rabbitTemplate.convertAndSend("MyRabbitQueue", context);
		}
	}
}

MyReceiver.java消息接收,需要注意监听的queues名称

@Component
public class MyReceiver {
	@RabbitHandler
	@RabbitListener(queues = "MyRabbitQueue")
	public void process(String messages) {
		System.out.println(" ----------- ");
		System.out.println("Receiver  : " + messages);
		System.out.println("");
	}
	
	@RabbitListener(queues = "userinfoRK")
	public void process(User user) {
		System.out.println(" ----------- ");
		System.out.println("Receiver  : " + user);
		System.out.println("");
	}
}

3、测试

RabbitMQTest.java测试类

@RunWith(SpringRunner.class)
@SpringBootTest
public class RabbitMQTest {
	@Autowired
    private MySender mySender;
	
	@Test
    public void test() throws Exception {
		mySender.sendPOJO();
		mySender.send();
    }
}

result
参考资料:
https://www.cnblogs.com/ityouknow/p/6120544.html

猜你喜欢

转载自blog.csdn.net/weixin_44153121/article/details/86307300