RabbitMq发送接收消息方式

RabbitMq发送接收消息方式

RabbitMq不使用交换机

发送方

    public static void main(String[] args) {
        String messageData = "test message, hello!";
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("47.110.157.82");
        factory.setPort(5672);
        factory.setUsername("liu");
        factory.setPassword("123456");
        Connection connection=null;
        Channel channel=null;
        try {
            connection = factory.newConnection();
            channel = connection.createChannel();
            /**
             * 参数1:队列名
             * 参数2:持久化
             * 参数3:是否排外,排外只允许一个消费者监听
             * 参数4:是否自动删除,队列中没有消息并且没有消费者连接时会自动删除
             * 参数4:为队列设置属性,通常为null
             */
            channel.queueDeclare("myqueue",true,false,false,null);

            /**
             * 参数1:交换机名
             * 参数2:队列名或者routingkey,指定了交换机这里就是routingkey
             * 参数3:设置消息属性,通常为null
             * 参数4:消息数据
             */
            channel.basicPublish("","myqueue",null,messageData.getBytes());
            System.out.println("消息发送成功");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }finally {
            if(channel!=null){
                try {
                    channel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (TimeoutException e) {
                    e.printStackTrace();
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

接收方

    public static void main(String[] args) {
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("47.110.157.82");
        factory.setPort(5672);
        factory.setUsername("liu");
        factory.setPassword("123456");
        Connection connection=null;
        Channel channel=null;
        try {
            connection = factory.newConnection();
            channel = connection.createChannel();                     
            channel.queueDeclare("myqueue",true,false,false,null);
            /**
             * 参数1 监听队列名
             * 参数2 自动确认
             * 参数3 接受者标签
             * 参数4 消息接收回调方法
             */
            channel.basicConsume("myqueue",true,"",new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                   String message=new String(body,"utf-8");
                    System.out.println(message);
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }finally {
        }
    }

RabbitMq-direct

发送方

 public static void main(String[] args) {
        String messageData = "direct message, hello!";
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("47.110.157.82");
        factory.setPort(5672);
        factory.setUsername("liu");
        factory.setPassword("123456");
        Connection connection=null;
        Channel channel=null;
        try {
            connection = factory.newConnection();
            channel = connection.createChannel();
            /**
             * 队列
             * 参数1:队列名
             * 参数2:持久化
             * 参数3:是否排外,排外只允许一个消费者监听
             * 参数4:是否自动删除,队列中没有消息并且没有消费者连接时会自动删除
             * 参数4:为队列设置属性,通常为null
             */
            channel.queueDeclare("myqueue001",true,false,false,null);
            /**
             * 声明一个交换机
             * 参数1交换机名称
             * 参数2交换机类型
             * 参数3是否持久化
             */
            channel.exchangeDeclare("exchange001","direct",true);
            /**
             * 将队列绑定到交换机
             * 参数1队列名称
             * 参数2交换机名称
             * 参数3routingkey(Bindingkey)
             */
            channel.queueBind("myqueue001","exchange001","routingkey001");
            /**
             * 参数1:交换机名
             * 参数2:routingkey
             * 参数3:设置消息属性,通常为null
             * 参数4:消息数据
             */
            channel.basicPublish("exchange001","routingkey001",null,messageData.getBytes());
            System.out.println("消息发送成功");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }finally {
            if(channel!=null){
                try {
                    channel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (TimeoutException e) {
                    e.printStackTrace();
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

接收方

 public static void main(String[] args) {
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("47.110.157.82");
        factory.setPort(5672);
        factory.setUsername("liu");
        factory.setPassword("123456");
        Connection connection=null;
        Channel channel=null;

        try {
            connection = factory.newConnection();
            channel = connection.createChannel();
            channel.queueDeclare("myqueue001",true,false,false,null);
            channel.exchangeDeclare("exchange001","direct",true);
            channel.queueBind("myqueue001","exchange001","routingkey001");

            /**
             * 参数1 监听队列名
             * 参数2 自动确认
             * 参数3 接受者标签
             * 参数4 消息接收回调方法
             */
            channel.basicConsume("myqueue001",true,"",new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                   String message=new String(body,"utf-8");
                    System.out.println(message);
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }finally {
        }
    }

RabbitMq-fanout

发送方

 public static void main(String[] args) {
        String messageData = "fanout message, hello!";
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("47.110.157.82");
        factory.setPort(5672);
        factory.setUsername("liu");
        factory.setPassword("123456");
        Connection connection=null;
        Channel channel=null;
        try {
            connection = factory.newConnection();
            channel = connection.createChannel();
            channel.exchangeDeclare("exchange002","fanout",true);
            /**
             * 参数1:交换机名
             * 参数2:routingkey
             * 参数3:设置消息属性,通常为null
             * 参数4:消息数据
             */
            channel.basicPublish("exchange002","",null,messageData.getBytes());
            System.out.println("消息发送成功");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }finally {
            if(channel!=null){
                try {
                    channel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (TimeoutException e) {
                    e.printStackTrace();
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

接收方01

    public static void main(String[] args) {
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("47.110.157.82");
        factory.setPort(5672);
        factory.setUsername("liu");
        factory.setPassword("123456");
        Connection connection=null;
        Channel channel=null;

        try {
            connection = factory.newConnection();
            channel = connection.createChannel();
            //创建随机队列
            String queue = channel.queueDeclare().getQueue();
            channel.exchangeDeclare("exchange002","fanout",true);
            channel.queueBind(queue,"exchange002","");

            /**
             * 参数1 监听队列名
             * 参数2 自动确认
             * 参数3 接受者标签
             * 参数4 消息接收回调方法
             */
            channel.basicConsume(queue,true,"",new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                   String message=new String(body,"utf-8");
                    System.out.println(“消费者01+message);
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }finally {
        }
    }

接收方02

    public static void main(String[] args) {
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("47.110.157.82");
        factory.setPort(5672);
        factory.setUsername("liu");
        factory.setPassword("123456");
        Connection connection=null;
        Channel channel=null;

        try {
            connection = factory.newConnection();
            channel = connection.createChannel();
            //创建随机队列
            String queue = channel.queueDeclare().getQueue();
            channel.exchangeDeclare("exchange002","fanout",true);
            channel.queueBind(queue,"exchange002","");

            /**
             * 参数1 监听队列名
             * 参数2 自动确认
             * 参数3 接受者标签
             * 参数4 消息接收回调方法
             */
            channel.basicConsume(queue,true,"",new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                   String message=new String(body,"utf-8");
                    System.out.println(“消费者02+message);
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }finally {
        }
    }

RabbitMq-topic

消费者001

  public static void main(String[] args) {
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("47.110.157.82");
        factory.setPort(5672);
        factory.setUsername("liu");
        factory.setPassword("123456");
        Connection connection=null;
        Channel channel=null;

        try {
            connection = factory.newConnection();
            channel = connection.createChannel();
            channel.queueDeclare("topicmyqueue001",true,false,false,null);
            channel.exchangeDeclare("topicexchange001","topic",true);
            channel.queueBind("topicmyqueue001","topicexchange001","aa");
            /**
             * 参数1 监听队列名
             * 参数2 自动确认
             * 参数3 接受者标签
             * 参数4 消息接收回调方法
             */
            channel.basicConsume("topicmyqueue001",true,"",new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                   String message=new String(body,"utf-8");
                    System.out.println("消费者001"+message);
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }finally {
        }
    }

消费者002

  public static void main(String[] args) {
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("47.110.157.82");
        factory.setPort(5672);
        factory.setUsername("liu");
        factory.setPassword("123456");
        Connection connection=null;
        Channel channel=null;

        try {
            connection = factory.newConnection();
            channel = connection.createChannel();
            channel.queueDeclare("topicmyqueue002",true,false,false,null);
            channel.exchangeDeclare("topicexchange001","topic",true);
            channel.queueBind("topicmyqueue002","topicexchange001","aa.*");
            /**
             * 参数1 监听队列名
             * 参数2 自动确认
             * 参数3 接受者标签
             * 参数4 消息接收回调方法
             */
            channel.basicConsume("topicmyqueue002",true,"",new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                   String message=new String(body,"utf-8");
                    System.out.println("消费者002"+message);
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }finally {
        }
    }

消费者003

  public static void main(String[] args) {
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("47.110.157.82");
        factory.setPort(5672);
        factory.setUsername("liu");
        factory.setPassword("123456");
        Connection connection=null;
        Channel channel=null;

        try {
            connection = factory.newConnection();
            channel = connection.createChannel();
            channel.queueDeclare("topicmyqueue003",true,false,false,null);
            channel.exchangeDeclare("topicexchange001","topic",true);
            channel.queueBind("topicmyqueue003","topicexchange001","aa.#");
            /**
             * 参数1 监听队列名
             * 参数2 自动确认
             * 参数3 接受者标签
             * 参数4 消息接收回调方法
             */
            channel.basicConsume("topicmyqueue003",true,"",new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                   String message=new String(body,"utf-8");
                    System.out.println("消费者003"+message);
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }finally {
        }
    }

发送者

public static void main(String[] args) {
        String messageData = "topic message, hello!";
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("47.110.157.82");
        factory.setPort(5672);
        factory.setUsername("liu");
        factory.setPassword("123456");
        Connection connection=null;
        Channel channel=null;
        try {
            connection = factory.newConnection();
            channel = connection.createChannel();
            channel.exchangeDeclare("topicexchange001","topic",true);
            /**
             * 参数1:交换机名
             * 参数2:routingkey
             * 参数3:设置消息属性,通常为null
             * 参数4:消息数据
             */
          //消费者001,消费者003可以接收到消息
          //  channel.basicPublish("topicexchange001","aa",null,messageData.getBytes());
          //消费者002,消费者003可以接收到消息
          //  channel.basicPublish("topicexchange001","aa.bb",null,messageData.getBytes());
          //消费者003可以接收到消息 
              channel.basicPublish("topicexchange001","aa.bb.cc",null,messageData.getBytes());
            System.out.println("消息发送成功");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }finally {
            if(channel!=null){
                try {
                    channel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (TimeoutException e) {
                    e.printStackTrace();
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

桀骜浮沉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值