1. 程式人生 > >Spring Boot中引入RabbitMQ

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