• RabbitDemo —— Fanout


    SendLog:

    public class SendLog {
        private static final String ex_log="ex_log";
        
        public static void main(String[] args) throws Exception {
            ConnectionFactory factory = Common.getFactory();
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
            /** 
             * 声明转发器和类型 可用的转发器类型Direct Topic Headers Fanout 
             * Direct Exchange – 处理路由键。需要将一个队列绑定到交换机上,要求该消息与一个特定的路由键完全匹配。 
             * Fanout Exchange – 不处理路由键。你只需要简单的将队列绑定到交换机上。一个发送到交换机的消息都会被转发到与该交换机绑定的所有队列上。 
             * 很像子网广播,每台子网内的主机都获得了一份复制的消息。Fanout交换机转发消息是最快的。  
             * Topic Exchange – 将路由键和某模式进行匹配。此时队列需要绑定要一个模式上。符号“#”匹配一个或多个词,符号“*”匹配不多不少一个词。 
             * 因此“audit.#”能够匹配到“audit.irs.corporate”,但是“audit.*” 只会匹配到“audit.irs”。 
             */  
            channel.exchangeDeclare(ex_log, "fanout");
            
            String msg = new Date().toString();
            /**
             * 事务一致性1
             */
            /*channel.txSelect();
            try {
                //将消息发送到转发器上,routingKey为"",第三个参数表明消息不是持久化的
                channel.basicPublish(ex_log, "", true, true, MessageProperties.PERSISTENT_TEXT_PLAIN, msg.getBytes());
                channel.txCommit();
            }catch (Exception e) {
                channel.txRollback();
            }*/
            /**
             * 事务一致性2 
             */
            channel.confirmSelect();
            channel.addConfirmListener(new ConfirmListener() {
                public void handleNack(long deliveryTag, boolean multiple) throws IOException {
                    //失败重发
                    channel.basicPublish(ex_log, "", true, true, MessageProperties.PERSISTENT_TEXT_PLAIN, msg.getBytes());
                }
                public void handleAck(long deliveryTag, boolean multiple) throws IOException {
                    //确认OK
                    System.out.println("成功发送到对应的QUEUE中");
                }
            });
            channel.confirmSelect();
            channel.basicPublish(ex_log, "", true, false, MessageProperties.PERSISTENT_TEXT_PLAIN, msg.getBytes());
            System.out.println("send:"+msg);
            
            channel.close();
            connection.close();
        }
    }
    View Code

    ReceiveLogsToFile:

    public class ReceiveLogsToFile {
        private static final String ex_log="ex_log";
        
        public static void main(String[] args) throws Exception {
            ConnectionFactory factory = Common.getFactory();
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
            
            //1. 声明转发器和类型
            channel.exchangeDeclare(ex_log, "fanout");
            //2. 声明消费队列(名称为系统自动生成,非持久、排外、自动删除的队列)
            String queueName = channel.queueDeclare().getQueue();
            //3. 将队列和转发器进行绑定,routingKey为""
            channel.queueBind(queueName, ex_log, "");
            
            System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
            
            Consumer consumer = new DefaultConsumer(channel) {
                public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
                        throws IOException {
                    print2File(new String(body));
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    channel.basicAck(envelope.getDeliveryTag(), false);
                }
            };
            channel.basicConsume(queueName, false, consumer);
        }
        private static void print2File(String msg) {  
            try {  
                String dir = "C:/Users/Luke/Desktop";  
                String logFileName = new SimpleDateFormat("yyyy-MM-dd").format(new Date());  
                File file = new File(dir, logFileName + ".txt");  
                FileOutputStream fos = new FileOutputStream(file, true);  
                fos.write((msg + "
    ").getBytes());  
                fos.flush();  
                fos.close(); 
            } catch (Exception e) {  
                e.printStackTrace();  
            }
            System.out.println("ok");
        }  
    }
    View Code

    ReceiveLogsToConsole:

    public class ReceiveLogsToConsole {
        private static final String ex_log="ex_log";
        
        public static void main(String[] args) throws Exception {
            ConnectionFactory factory = Common.getFactory();
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
            
            //1. 声明转发器及类型
            channel.exchangeDeclare(ex_log, "fanout");
            //2. 声明队列(名称为系统自动生成,非持久、排外、自动删除的队列)
            String queueName = channel.queueDeclare().getQueue();
            //3. 将队列和转发器进行绑定,routingKey为""
            channel.queueBind(queueName, ex_log, "");
            
            System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
            
            Consumer consumer = new DefaultConsumer(channel) {
                public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
                        throws IOException {
                    System.out.println(" [x] Received '" + new String(body));
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    channel.basicAck(envelope.getDeliveryTag(), false);
                }
            };
            channel.basicConsume(queueName, false, consumer);
        }
    }
    View Code
  • 相关阅读:
    最长连续子序列(dp,分而治之递归)
    判断线段是否相交
    1840: Jack Straws
    5065: 最长连续子序列
    TZOJ 4493: Remove Digits
    TZOJ 5271: 质因数的个数
    2019年天梯赛
    PTA 树的遍历
    TZOJ:3660: 家庭关系
    PTA 复数四则运算
  • 原文地址:https://www.cnblogs.com/yifanSJ/p/9022341.html
Copyright © 2020-2023  润新知