rabbitMq(一)内部消息分发

pom文件

    <dependencies>
        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>5.1.2</version>
        </dependency>
    </dependencies>

测试

public static void main(String[] args) {
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();

        // 设置连接属性
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");

        Connection connection = null;
        Channel channel = null;

        try{
            // 3、 从链接工厂获取连接
            connection = factory.newConnection("生产者");

            // 4、从链接中创建通道
            channel = connection.createChannel();

            /**
             * 5、声明队列
             * @param queue 队列名称
             * @param durable 是否持久化
             * @param exclusive 是否排他
             * @param autoDelete 是否自动删除
             * @param arguments 队列参数
             */
            //channel.queueDeclare("qeueu1", false, false, null)
            channel.queueDeclare("queue1", false, false,false, null);

            //消息内容
            String message = "hellow world";

            // 6、发起消息
            channel.basicPublish("","queue1",null,message.getBytes());
            System.out.println("消息已发送。");


        }catch (IOException e){
            e.printStackTrace();
        }catch (TimeoutException e1){
            e1.printStackTrace();
        }
    }

 rabbitmq界面

 

消费者

 public static void main(String[] args) {
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();

        // 设置连接属性
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");

        Connection connection = null;
        Channel channel = null;

        try{
            // 3、 从链接工厂获取连接
            connection = factory.newConnection("消费者");

            // 4、从链接中创建通道
            channel = connection.createChannel();

            /**
             * 5、声明队列
             * @param queue 队列名称
             * @param durable 是否持久化
             * @param exclusive 是否排他
             * @param autoDelete 是否自动删除
             * @param arguments 队列参数
             */
            //channel.queueDeclare("qeueu1", false, false, null)
            channel.queueDeclare("queue1", false, false,false, null);

            // 6、发起消息
            DeliverCallback callback = new DeliverCallback() {
                public void handle(String consumerTag, Delivery delivery) throws IOException {
                    System.out.println("收到消息: " + new String(delivery.getBody(),"UTF-8"));
                }
            };

            //7、监听队列
            channel.basicConsume("queue1", true, callback, new CancelCallback() {
                public void handle(String s) throws IOException {

                }
            });

        System.out.println("收到消息。");
        System.in.read();


        }catch (IOException e){
            e.printStackTrace();
        }catch (TimeoutException e1){
            e1.printStackTrace();
        }
    }

  rabbitmq界面 消费消息

原文地址:https://www.cnblogs.com/Jomini/p/13700777.html