RabbitMQ——事务

时光怂恿深爱的人放手 提交于 2019-12-22 00:04:37

事务模式

事务具有原子性,MQ发送消息也应当具有原子性,下面介绍一下事务模式:

send

public class Send {

    private static final String QUEUE_NAME = "simple_mq";

    public static void main(String[] args) throws IOException, TimeoutException {

        Connection connection = RabbitConnection.getConnection();

        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        String message = "hello simple_mq";

        try {
            channel.txSelect();
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
            channel.txCommit();
        } catch (IOException e) {
            channel.txRollback();
            System.out.println("回滚~~");
        }
        System.out.println(" [x] Sent.......... '" + message + "'");
        channel.close();
        connection.close();
    }
}

和我们最初写的简单队列相比,就加了try里面的内容,消费者代码还是不变。


confirm模式

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!