zoukankan      html  css  js  c++  java
  • Rabbitmq(7) confirm模式

    1.

    //将通道设置为comfirm模式
    channel.confirmSelect();
    // 消息确认
    if(!channel.waitForConfirms()){
    System.out.println(message+"fail");
    }else{
    System.out.println(message+"ok");
    }
    2. 发送者
    package com.aynu.bootamqp.service;
    
    import com.aynu.bootamqp.commons.utils.Amqp;
    import com.rabbitmq.client.Channel;
    import com.rabbitmq.client.Connection;
    
    
    import java.io.IOException;
    import java.util.concurrent.TimeoutException;
    
    public class Send {
    
        private final static String Exchange_NAME ="hello";
        public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
                Connection connection = Amqp.getConnection();
                Channel channel = connection.createChannel();
                //声明交换机
                channel.exchangeDeclare(Exchange_NAME,"topic");
                //在手动确认机制之前
                //一次只发送一条消息,给不同的消费者
                channel.basicQos(1);
                //将通道设置为comfirm模式
                channel.confirmSelect();
                String message = "hello ps";
                String routingKey ="goods.delete";
                for (int i = 0; i <10; i++) {
                    channel.basicPublish(Exchange_NAME,routingKey,null,message.getBytes("utf-8"));
                }
                // 消息确认
                if(!channel.waitForConfirms()){
                    System.out.println(message+"fail");
                }else{
                    System.out.println(message+"ok");
                }
                channel.close();
                connection.close();
        }
    }
    2.接收者
    package com.aynu.bootamqp.service;
    
    import com.aynu.bootamqp.commons.utils.Amqp;
    import com.rabbitmq.client.*;
    
    import java.io.IOException;
    import java.util.concurrent.TimeoutException;
    @SuppressWarnings("all")
    public class Receive2 {
    
        private final static String QUEUE_NAME ="hello1";
        private final static String Exchange_NAME ="hello";
        public static void main(String[] args) throws IOException, TimeoutException {
            Connection connection = Amqp.getConnection();
            Channel channel = connection.createChannel();
            channel.queueDeclare(QUEUE_NAME,false,false,false,null);
            channel.queueBind(QUEUE_NAME,Exchange_NAME,"goods.#");
            channel.basicQos(1);
            DefaultConsumer consumer = new DefaultConsumer(channel) {
    
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope,
                                           AMQP.BasicProperties properties, byte[] body) throws IOException {
                    super.handleDelivery(consumerTag, envelope, properties, body);
                    String msg = new String(body,"utf-8");
                    System.out.println("receive2222"+msg);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }finally {
                        // 手动发送消息确认机制
                        channel.basicAck(envelope.getDeliveryTag(),false);
                    }
                }
            };
            boolean autoAck = false;
            channel.basicConsume(QUEUE_NAME,autoAck,consumer);
        }
    }
     
  • 相关阅读:
    curl命令常见用法汇总 good
    Spring Boot flyway的启动时机比较早
    android 签名被篡改(Keystore was tampered with, or password was incorrect)
    android sdk 如何重新生成debug.keystore
    Android的debug.keystore拒绝访问导致的生成异常及解决方案
    Android生成keystore是报错拒绝访问
    android 高德地图出现【定位失败key鉴权失败】
    Android Studio开发入门-引用jar及so文件
    android 自定义AlertDialog(一段)
    Android自定义控件之日历控件
  • 原文地址:https://www.cnblogs.com/mm163/p/10704464.html
Copyright © 2011-2022 走看看