本文主要是介绍RabbitMQ 发送接受确认与mandatory参数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 前言
- 一、 配置
- 一、发送确认
- 三、上节的修改
- 三、消费者确认
前言
代码什么的在我的仓库里都有https://gitee.com/song_mengyu/Example/tree/master/Project/rabbit-ack-test
一、 配置
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>
一、发送确认
首先是 yml的配置
server:port: 8081
spring:application:name: rabbit-serverrabbitmq:port: 5673host: 192.168.136.128username: adminpassword: adminpublisher-confirms: truepublisher-returns: true
对于这个发送确认只是做个简单的演示,所以我我们就只设置个队列即可
@Configuration
public class Config {@Beanpublic Queue MyQueue(){return new Queue("myQueue", true);}
}
然后是发送的逻辑
package com.mymy;import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Sender {@AutowiredRabbitTemplate rabbitTemplate;public void send(){rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {@Overridepublic void confirm(CorrelationData correlationData, boolean ack, String s) {if(ack){System.out.println("发送成功");}else {System.out.println("发送失败");}}});rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {@Overridepublic void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {System.out.println("消息内容是" + message.getBody().toString());}});rabbitTemplate.convertAndSend("myQueue", "hello!");}
}
最后是Controller
package com.mymy;import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class Controller {@Autowiredprivate RabbitTemplate rabbitTemplate;@Autowiredprivate Sender sender;@GetMapping("/send")public String send(){sender.send();return "okk";}
}
上边的代码我们在测试时会发现当我们在发送一个请求时,就会报错
下面就是对代码的一些修改,同时再加上mandatory参数
三、上节的修改
首先先对mandatory参数介绍。当此参数为true时,交换器无法根据自身的类型和路由键找到一个符合的队列时将会把消息返回给生产者,如果为false,则直接丢弃掉。下面的代码参考了这个博主的文章https://blog.csdn.net/weixin_44929059/article/details/116303723
配置类如下
package com.mymy;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.core.RabbitTemplate;
import org.springframework.amqp.support.converter.SerializerMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;@Configuration
public class Config {@Beanpublic Queue MyQueue(){return new Queue("myQueue", true);}@Beanpublic DirectExchange directExchange(){return new DirectExchange("myExchange");}@Beanpublic Binding binding(){return BindingBuilder.bind(MyQueue()).to(directExchange()).with("myRouting");}@Bean@Scope("prototype")public RabbitTemplate crabbitTemplate(ConnectionFactory connectionFactory) {RabbitTemplate template = new RabbitTemplate(connectionFactory);template.setMandatory(true);template.setMessageConverter(new SerializerMessageConverter());return template;}
}
Controller
package com.mymy;import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;@RestController
public class Controller {@ResourceRabbitTemplate rabbitTemplate;//为该方法中的rabbitTemplate单独设置回调方法@PostConstructpublic void init() {rabbitTemplate.setMandatory(true);rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {@Overridepublic void confirm(CorrelationData correlationData, boolean ack, String cause) {if (ack) {System.out.println("数据发送成功");}else{System.out.println("数据发送失败");}}});rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {@Overridepublic void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {System.out.println("接受失败");}});}@GetMapping("/send")public void send() {rabbitTemplate.convertAndSend("myExchange","myRouting", "hello!");}
}
三、消费者确认
yml配置
server:port: 8080
spring:application:name: consumerrabbitmq:password: adminusername: adminhost: 192.168.136.128port: 5673listener:simple:acknowledge-mode: manual
Lisenner类的编写
package com.mymy;import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Date;
import java.util.Map;@Component
@RabbitListener(queues = {"myQueue"})
public class Listener {@RabbitHandlerpublic void process(Map hello, Channel channel, Message message) throws IOException {System.out.println("HelloReceiver收到 : " + hello.toString() +"收到时间"+new Date());try {//告诉服务器收到这条消息 已经被我消费了 可以在队列删掉 这样以后就不会再发了 否则消息服务器以为这条消息没处理掉 后续还会在发channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);System.out.println("receiver success");} catch (IOException e) {e.printStackTrace();//丢弃这条消息//channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,false);System.out.println("receiver fail");}}
}
这篇关于RabbitMQ 发送接受确认与mandatory参数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!