zoukankan      html  css  js  c++  java
  • rabbitMQ-helloWorld

      

    private static final String EXCHANGE_NAME = "direct_logs"; private static final String DELAY_EXCHANGE_NAME = "delay_exchange"; private static final String ROUTING_KEY = "error";
    // 生产者
    public static void main(String[] args) { try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("10.202.72.206"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME,"direct"); String msg = "talk is cheap ,show me the code "; for(int i = 0;i <10;i++){ channel.basicPublish(EXCHANGE_NAME,ROUTING_KEY,null,msg.getBytes()); } System.out.println(" [x] Sent '" + ROUTING_KEY + "':'" + msg + "'"); }catch (Exception e){ System.out.println("something wrong"); e.printStackTrace(); } }
     private static final String EXCHANGE_NAME = "direct_logs";
        private static final String DELAY_EXCHANGE_NAME = "delay_exchange";
        private static final String ROUTING_KEY = "error";
        private static final String QUEUE_NAME = "myqueue";
        //消费者
        public static void main(String[] argv) throws Exception {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("10.202.72.206");
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();
    
            channel.exchangeDeclare(EXCHANGE_NAME, "direct");
            String queueName = channel.queueDeclare().getQueue();
    
    
            channel.queueBind(queueName, EXCHANGE_NAME, ROUTING_KEY);
    
            System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    
            Consumer consumer = new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
    
                    String msg = new String(body, "utf-8");
                    System.out.println("[1] Recv msg:" + msg);
    
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        System.out.println("[1] done ");
                        channel.basicAck(envelope.getDeliveryTag(), false);
                    }
                }
            };
            //自动应答 false
            boolean autoAck=false;
            channel.basicConsume(queueName,autoAck, consumer);
        }
  • 相关阅读:
    题解 P5320
    Codeforces 1500F
    三个 AGC D(AGC037D、AGC043D、AGC050D)
    Atcoder Regular Contst 084 D
    DG-基础知识点整理
    MySQL-数据恢复场景实验
    MySQL-查看Galera集群状态
    MySQL-运行日志切割
    MySQL-生产环境删除大表或大量binlog策略
    MySQL-基于(MySQL 5.7)NDB中启用共享权限表
  • 原文地址:https://www.cnblogs.com/mmh760/p/13692306.html
Copyright © 2011-2022 走看看