消息确认机制
生产者将消息发送出去后,是否到达rabbitmq服务器默认不知道,通过两种方式,AMQP实现了事务机制和Confirm模式。
事务机制
使用协议,但是降低了rabbitmq的消息吞吐量
- txSelect:当前channel设置成transaction模式
- txCommit:提交事务
- txRollback:回滚事务
public class TxProduct {
private static final String QUEUE_NAME = "test_queue_tx";
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = ConnectionUtils.getConnection();
Channel channel = connection.createChannel();
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String msg = "tx msg";
//开启
try {
channel.txSelect();
for (int i = 0; i < 10; i++) {
channel.basicPublish("", QUEUE_NAME, null, msg.getBytes());
if (i == 4) {
int n = 1 / 0;
}
}
channel.txCommit()