producer发送消息的时候需要设置一个消息类型,然后consumer订阅的时候只订阅自己需要的消息类型即可
一对一
配置
@Configuration
public class OneConfig {
/* one2one */
final static String ONE2ONE = "one2one";
@Bean
public Queue queueOne2One() {
return new Queue(ONE2ONE);
}
}
生产者
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* 一个生产者对应一个消费者:生产者
*/
@Component
public class OneProducer {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send() {
String sendMsg = "hello one2one " + new Date();
System.out.println("one2one 生产者 sendMessage : " + sendMsg);
this.rabbitTemplate.convertAndSend("one2one", sendMsg);
}
}
消费者
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 一个生产者对应一个消费者:消费者
*/
@Component
@RabbitListener(queues = "one2one")
public class OneCustomer {
@RabbitHandler
public void process(String hello) {
System.out.println("one2one 消息消费者消费消息 : " + hello);
}
}
测试类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/***
* 一个生产者 对应 一个消费者
*/
@RestController
public class One2OneController {
@Autowired
private OneProducer oneProducer;
@RequestMapping("/one2one")
public String one2OneSend(){
oneProducer.send();
return "ok";
}
}
一对多
配置
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class One2manyConfig {
/* one2many */
final static String ONE2MANY = "one2many";
@Bean
public Queue queueOne2Many(){
return new Queue(ONE2MANY);
}
}
生产者
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* 一对多模式:单个消息生产者
*/
@Component
public class One2ManyProducer {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send(String msg) {
String sendMsg = msg + new Date();
System.out.println("one2many 生产者产生的消息 : " + sendMsg);
this.rabbitTemplate.convertAndSend("one2many", sendMsg);
}
}
消费者
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 一对多模式:多个消费者
*/
@Component
@RabbitListener(queues = "one2many")
public class One2ManyCustomer1 {
@RabbitHandler
public void process(String msg) {
System.out.println("one2may 消费者1号 : " + msg);
}
}
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 一对多模式:多个消费者
*/
@Component
@RabbitListener(queues = "one2many")
public class One2ManyCustomer2 {
@RabbitHandler
public void process(String msg) {
System.out.println("one2may 消费者2号 : " + msg);
}
}
测试方法
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class One2manyController {
@Autowired
private One2ManyProducer one2ManyProducer;
@RequestMapping("/one2many")
public String one2Many() {
for (int i = 0; i < 10; i++) {
one2ManyProducer.send("one2many 消费者你好 ,----" + i);
}
return "ok";
}
}
多对多
配置
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Many2manyConfig {
/* many2many */
final static String MANY2MANY = "many2many";
@Bean
public Queue queueMany2Many(){
return new Queue(MANY2MANY,true);
}
}
生产者
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* 一对多模式:单个消息生产者
*/
@Component
public class Mnay2ManyProducer1 {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send(String msg) {
String sendMsg = msg + new Date();
System.out.println("one2many 一号生产者产生的消息 : " + sendMsg);
this.rabbitTemplate.convertAndSend("many2many", sendMsg);
}
}
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* 一对多模式:单个消息生产者
*/
@Component
public class Mnay2ManyProducer2 {
@Autowired
private AmqpTemplate rabbitTemplate;
public void send(String msg) {
String sendMsg = msg + new Date();
System.out.println("one2many 二号生产者产生的消息 : " + sendMsg);
this.rabbitTemplate.convertAndSend("many2many", sendMsg);
}
}
消费者
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 一对多模式:多个消费者
*/
@Component
@RabbitListener(queues = "many2many")
public class Mnay2ManyCustomer1 {
@RabbitHandler
public void process(String msg) {
System.out.println("many2many 消费者1号 : " + msg);
}
}
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 一对多模式:多个消费者
*/
@Component
@RabbitListener(queues = "many2many")
public class Mnay2ManyCustomer2 {
@RabbitHandler
public void process(String msg) {
System.out.println("many2many 消费者2号 : " + msg);
}
}
测试方法
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Many2manyController {
@Autowired
private Mnay2ManyProducer1 mnay2ManyProducer1;
@Autowired
private Mnay2ManyProducer2 mnay2ManyProducer2;
@RequestMapping("/many2many")
public String many2many() {
for (int i = 0; i < 10; i++) {
mnay2ManyProducer1.send("many2many 消费者你好(一号生产者说) ,----" + i);
mnay2ManyProducer2.send("many2many 消费者你好(二号生产者说) ,----" + i);
}
return "ok";
}
}
发送对象
对象
public class User {
private int id;
private String name;
private String age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
}
配置
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ObjConfig {
final static String OBJ = "obj";
@Bean
public Queue queue(){
return new Queue(OBJ);
}
}
生产者
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 对象传输,生产者
*/
@Component
public class ObjProducter {
@Autowired
private RabbitTemplate rabbitTemplate;
public void send() {
User us = new User();
us.setAge("测试");
us.setName("aaa");
us.setId(123);
rabbitTemplate.convertAndSend("obj", us);
System.out.println("发送对象数据: " );
}
}
消费者
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* 对象的传输,消费者
*/
@Component
@RabbitListener(queues = "obj")
public class ObjCustomer {
@RabbitHandler
public void process(User user) {
System.out.println("消费者 : " + user.toString());
}
}
测试方法
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/***
* 发送对象
*/
@RestController
public class ObjController {
@Autowired
private ObjProducter objProducter;
@RequestMapping("/sendobj")
public String sendobj() {
objProducter.send();
return "ok";
}
}