8RabbitMQ-消息的确认机制(生产者)
Posted mrchengs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了8RabbitMQ-消息的确认机制(生产者)相关的知识,希望对你有一定的参考价值。
RabbitMQ 之消息确认机制(事务+Confirm)
https://blog.csdn.net/u013256816/article/details/55515234
概述:
生产者:
import java.io.IOException; import java.util.concurrent.TimeoutException; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.util.ConnectionUtils; public class TXsend { private static final String QUEUE_NAMW = "test_tx_queue"; public static void main(String[] args) throws IOException, TimeoutException { Connection conn = ConnectionUtils.getConnection(); Channel channel = conn.createChannel(); channel.queueDeclare(QUEUE_NAMW, false, false, false, null); String msg = "tx"; try { //开启事务模式、 channel.txSelect(); channel.basicPublish("", QUEUE_NAMW, null, msg.getBytes()); //模拟事故 int i = 1/0; //提交 channel.txCommit(); } catch (Exception e) {
//进行事务回滚 channel.txRollback(); System.out.println("TxRollback..."); } channel.close(); conn.close(); } }
消费者:
import java.io.IOException; import java.util.concurrent.TimeoutException; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.Consumer; import com.rabbitmq.client.DefaultConsumer; import com.rabbitmq.client.Envelope; import com.rabbitmq.client.AMQP.BasicProperties; import com.rabbitmq.util.ConnectionUtils; public class TxReceive { private static final String QUEUE_NAMW = "test_tx_queue"; public static void main(String[] args) throws IOException, TimeoutException { Connection conn = ConnectionUtils.getConnection(); Channel channel = conn.createChannel(); //队列声明 channel.queueDeclare(QUEUE_NAMW, false, false, false, null); channel.basicQos(1); //绑定队列到交换机转发器 //channel.queueBind(QUEUE_NAMW, "", ""); //定义一个消费者 Consumer consumer = new DefaultConsumer(channel){ //收到消息就会触发这个方法 @Override public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException { String msg = new String(body,"utf-8"); System.out.println("消费者1接收到的消息" + msg); try { Thread.sleep(1500); } catch (InterruptedException e) { e.printStackTrace(); }finally{ System.out.println("消费者1处理完成!"); //手动回执 channel.basicAck(envelope.getDeliveryTag(), false); } } }; //监听队列 //自动应答false boolean autoAck = false; channel.basicConsume(QUEUE_NAMW, autoAck, consumer); } }
此时消费者不会接收到消息
此种模式还是很耗时的,采用这种方式 降低了 Rabbitmq 的消息吞吐量
Confirm模式
概述
producer 端 confirm 模式的实现原理
开启 confirm 模式的方法 已经在 transaction 事务模式的 channel 是不能再设置成 confirm 模式的,即这两种模式是不能共存的。 生产者通过调用 channel 的 confirmSelect 方法将 channel 设置为 confirm 模式 核心代码:
编程模式
普通模式:
import java.io.IOException; import java.util.concurrent.TimeoutException; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.util.ConnectionUtils; public class confirm{ private static final String QUEUE_NAMW = "test_tx_confirm1"; public static void main(String[] args) throws IOException, TimeoutException, InterruptedException { Connection conn = ConnectionUtils.getConnection(); Channel channel = conn.createChannel(); channel.queueDeclare(QUEUE_NAMW, false, false, false, null); //生产者调用confirmSelect,将channel设置为confirm模式 channel.confirmSelect(); String msg = "confirm"; channel.basicPublish("", QUEUE_NAMW, null, msg.getBytes()); if(!channel.waitForConfirms()){ System.out.println("send failed"); }else{ System.out.println("send ok"); } channel.close(); conn.close(); } }
import java.io.IOException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.util.ConnectionUtils;
public class TXsend {
private static final String QUEUE_NAMW = "test_tx_confirm1";
public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
Connection conn = ConnectionUtils.getConnection();
Channel channel = conn.createChannel();
channel.queueDeclare(QUEUE_NAMW, false, false, false, null);
//1
//生产者调用confirmSelect,将channel设置为confirm模式
channel.confirmSelect();
//2
String msg = "confirm";
//批量发送
for(int i=1;i<=10;i++){
channel.basicPublish("", QUEUE_NAMW, null, msg.getBytes());
}
//3
//确认
if(!channel.waitForConfirms()){
System.out.println("send failed");
}else{
System.out.println("send ok");
}
channel.close();
conn.close();
}
}
import java.io.IOException; import java.util.Collection; import java.util.Collections; import java.util.SortedSet; import java.util.TreeSet; import java.util.concurrent.TimeoutException; import com.rabbitmq.client.Channel; import com.rabbitmq.client.ConfirmListener; import com.rabbitmq.client.Connection; import com.rabbitmq.util.ConnectionUtils; public class TXsend { private static final String QUEUE_NAMW = "test_tx_confirm3"; public static void main(String[] args) throws IOException, TimeoutException, InterruptedException { Connection conn = ConnectionUtils.getConnection(); Channel channel = conn.createChannel(); channel.queueDeclare(QUEUE_NAMW, false, false, false, null); //生产者调用confirmSelect,将channel设置为confirm模式 channel.confirmSelect(); //未确认的消息标识 final SortedSet<Long> confirmSet = Collections.synchronizedSortedSet(new TreeSet<Long>()); //频道加一个监听 channel.addConfirmListener(new ConfirmListener() { //回调/重发重试 可以1s之后再发 10s之后再发 @Override public void handleNack(long deliveryTag, boolean multiple) throws IOException { if(multiple){ System.out.println("handleNack-----multiple =1"); confirmSet.headSet(deliveryTag+1).clear();; }else{ System.out.println("handleNack-----multiple =0"); confirmSet.remove(deliveryTag); } } //没问题的handleAck @Override public void handleAck(long deliveryTag, boolean multiple) throws IOException { if(multiple){ System.out.println("handleAck-----multiple =1"); confirmSet.headSet(deliveryTag+1).clear();; }else{ System.out.println("handleAck-----multiple =0"); confirmSet.remove(deliveryTag); } } }); String msg = "confirm"; //模拟插入数据 while(true){ long seqNo = channel.getNextPublishSeqNo(); channel.basicPublish("", QUEUE_NAMW, null, msg.getBytes()); confirmSet.add(seqNo); } } }
以上是关于8RabbitMQ-消息的确认机制(生产者)的主要内容,如果未能解决你的问题,请参考以下文章