return “网络中断,请稍后再试~”;
}
}
}
复制代码
2.2.2 消息接收者
接收者需要配置以下内容
-
交换器:直接new对应的交换器类型
-
队列:只有Queue类型,通过名称区分
-
交换器和队列的绑定:通过BindingBuilder.bind(队列).to(交换器).with(路由键);
-
只声明交换器和队列绑定,并不会马上创建,而是在发送消息或者监听队列时才会创建
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AmqpSubscriberConfig {
/**
- 直连交换器
*/
@Bean
public DirectExchange emailDirectExchange() {
// 声明方式一
// return new DirectExchange(“exchange.direct.springboot.email”);
// 声明方式二
return ExchangeBuilder.directExchange(“exchange.direct.springboot.email”).build();
}
/**
- 声明队列
*/
@Bean
public Queue emailQueue() {
// 声明方式一
// return new Queue(“queue.direct.springboot.email”);
// 声明方式二
return QueueBuilder.durable(“queue.direct.springboot.email”).build();
}
/**
- 交换器和队列绑定
*/
@Bean
@Resource
public Binding emailBiding(Queue emailQueue, DirectExchange emailDirectExchange) {
// 将路由使用路由键绑定到交换器上
return BindingBuilder.bind(emailQueue).to(emailDirectExchange).with(“springboot.email.routing.key”);
}
}
复制代码
监听队列
-
监听的队列必须存在,否则将会报错
-
监听的队列消费完成会自动确认消息
-
如果多个队列同时监听一个队列,则消息会轮训地由不同方法处理
-
可以在参数中指定接收类型,消息将会自动转为对应类型
-
也可以指定Message参数获取对应消息信息
-
org.springframework.amqp.core.Message
-
获取消息属性:message.getMessageProperties()
-
获取消息内容:message.getBody()
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
- 消息订阅监听
*/
@Component
public class SubscriberListener {
/**
- direct监听,相同监听队列消息将会轮流处理
*/
@RabbitListener(queues = “queue.direct.springboot.email”)
public void receiver01(String msg) {
System.out.println("receiver01 message = " + msg);
}
@RabbitListener(queues = “queue.direct.springboot.email”)
public void receiver02(String msg) {
System.out.println("receiver02 message = " + msg);
}
}
复制代码
2.1.3 消息发布订阅
1.先启动订阅者,可以看到队列声明
2. 启动发布者,然后发布消息
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class PublishController {
@Resource
private RabbitTemplate rabbitTemplate;
@RequestMapping(“/direct”)
public Object direct(String message) {
try {
// 指定发送的交换器和路由键
rabbitTemplate.convertAndSend(“exchange.direct.springboot.email”, “springboot.email.routing.key”, message);
return message;
} catch (AmqpException e) {
System.out.println(e.getMessage());
return “网络中断,请稍后再试~”;
}
}
}
复制代码
3.订阅者会轮流收到信息
receiver01 message = direct
receiver02 message = direct
receiver01 message = direct
receiver02 message = direct
receiver01 message = direct
receiver02 message = direct
复制代码
2.2 topic - 主题交换器
2.2.1 消息发送者
声明topic交换器
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BlogPublisherConfig {
@Bean
public Exchange blogTopicExchange() {
return ExchangeBuilder.topicExchange(“exchange.topic.springboot.blog”).build();
}
}
复制代码
声明controller
@RequestMapping(“/topic”)
public Object topic(String routingKey, String message) {
rabbitTemplate.convertAndSend(“exchange.topic.springboot.blog”, routingKey, message);
return routingKey + " : " + message;
}
复制代码
2.2.2 消息接收者
声明交换器、三个队列、队列的绑定
-
*:匹配一个串
-
#:匹配一个或者多个串
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
@Configuration
public class BlogSubscriberConfig {
/**
- 主题交换器
*/
@Bean
public TopicExchange blogTopicExchange() {
return ExchangeBuilder.topicExchange(“exchange.topic.springboot.blog”).build();
}
@Bean
public Queue blogJavaQueue() {
return QueueBuilder.durable(“queue.topic.springboot.blog.java”).build();
}
@Bean
public Queue blogMqQueue() {
return QueueBuilder.durable(“queue.topic.springboot.blog.mq”).bu