rabbitMQ的工作模式

本文详细介绍了RabbitMQ的四种工作模式:工作者模式,其中生产者发送消息,多个消费者通过竞争接收;发布订阅模式,一个生产者与多个消费者和队列配合,交换机按模式转发;路由模式,通过路由键将消息发送到匹配的队列;以及主题模式,使用通配符进行队列绑定,允许更灵活的消息路由。
摘要由CSDN通过智能技术生成

RabbitMQ的工作模式

1. 工作者模式:(一个生产者,多个消费者,消费者之间存在竞争关系)

生产者

package com.aaa.zmj;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class Product {
    public static void main(String[] args)throws  Exception {
        //创建连接工厂 --配置连接信息
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("192.168.81.166");
        //创建连接对象Connection
        Connection connection=factory.newConnection();
        //创建信道
        Channel channel = connection.createChannel();

        //创建队列
        /**
         * String queue, 队列的名称
         * boolean durable, 是否该队列持久化 rabbitMQ服务重启后该存放是否存在。
         * boolean exclusive, 是否独占 false
         * boolean autoDelete, 是否自动删除  如果长时间没有发生消息 则自动删除
         * Map<String, Object> arguments 额外参数  先给null
         */
        channel.queueDeclare("queue",true,false,false,null);
        //发生消息
        /**
         * String exchange: 交换机的名称 如果没有则使用“” 它回自动采用默认
         * String routingKey, 路由key  如果没有交换机的绑定 使用队列的名称
         * BasicProperties props, 消息的一些额外配置 目前先不加 null
         * byte[] body 消息的内容
         */
        for(int i=0;i<10;i++) {
            String msg = "仲梦君真帅!!!!!!!!!!!"+i;
            channel.basicPublish("", "queue", null, msg.getBytes());
        }
        //生产者这里可以管理资源  消费者不能关闭资源。
        channel.close();
        connection.close();

    }
}

消费者01:

package com.aaa.zmj;

import com.rabbitmq.client.*;

import java.io.IOException;


public class Consumer01 {
    public static void main(String[] args) throws Exception{
        //创建连接工厂 --配置连接信息
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("192.168.81.166");
        //创建连接对象Connection
        Connection connection=factory.newConnection();
        //创建信道
        Channel channel = connection.createChannel();

        //接受消息
        /**
         * (String queue, 队列的名称
         *  boolean autoAck, 是否自动确认
         *  Consumer callback: 回调方法 当队列中存在信息后 会自动触发回调函数。
         */
        DefaultConsumer callback=new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //body 接受的信息
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("消费者01:"+new String(body));
            }
        };
        channel.basicConsume("queue",true,callback);

    }
}

消费者02:

package com.aaa.zmj;

import com.rabbitmq.client.*;

import java.io.IOException;


public class Consumer02 {
    public static void main(String[] args) throws Exception{
        //创建连接工厂 --配置连接信息
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("192.168.81.166");
        //创建连接对象Connection
        Connection connection=factory.newConnection();
        //创建信道
        Channel channel = connection.createChannel();

        //接受消息
        /**
         * (String queue, 队列的名称
         *  boolean autoAck, 是否自动确认
         *  Consumer callback: 回调方法 当队列中存在信息后 会自动触发回调函数。
         */
        DefaultConsumer callback=new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //body 接受的信息
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("消费者02:"+new String(body));
            }
        };
        channel.basicConsume("queue",true,callback);

    }
}

2. 发布订阅模式

1. 特点:
    1.一个生产者
    2.多个消费者
    3.多个队列。
    4.交换机 转发消息。

生产者:

package com.aaa.zmj.fanout;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;


public class Product {
    public static void main(String[] args)throws  Exception {
        //创建连接工厂 --配置连接信息
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("192.168.81.166");
        //创建连接对象Connection
        Connection connection=factory.newConnection();
        //创建信道
        Channel channel = connection.createChannel();

        //创建队列
        /**
         * String queue, 队列的名称
         * boolean durable, 是否该队列持久化 rabbitMQ服务重启后该存放是否存在。
         * boolean exclusive, 是否独占 false
         * boolean autoDelete, 是否自动删除  如果长时间没有发生消息 则自动删除
         * Map<String, Object> arguments 额外参数  先给null
         */
        channel.queueDeclare("queue_fanout01",true,false,false,null);
        channel.queueDeclare("queue_fanout02",true,false,false,null);

        //创建交换机
        /**
         * String exchange,交换机的名称
         * BuiltinExchangeType type, 交换机的类型
         * boolean durable:是否持久化
         */
        channel.exchangeDeclare("fanout_exchange", BuiltinExchangeType.FANOUT,true);
        /**
         * String queue,  队列名
         * String exchange, 交换机的名称
         * String routingKey:路由key 如果交换机为fanout模式则不需要路由key
         */
        channel.queueBind("queue_fanout01","fanout_exchange","");
        channel.queueBind("queue_fanout02","fanout_exchange","");
        //发生消息
        /**
         * String exchange: 交换机的名称 如果没有则使用“” 它回自动采用默认
         * String routingKey, 路由key  如果没有交换机的绑定 使用队列的名称
         * BasicProperties props, 消息的一些额外配置 目前先不加 null
         * byte[] body 消息的内容
         */
        for(int i=0;i<10;i++) {
            String msg = "啊哈哈哈哈!!!!!!!!!!!"+i;
            channel.basicPublish("fanout_exchange", "", null, msg.getBytes());
        }
        //生产者这里可以管理资源  消费者不能关闭资源。
        channel.close();
        connection.close();

    }
}

消费者:

package com.aaa.zmj.fanout;

import com.rabbitmq.client.*;

import java.io.IOException;

public class Consumer01 {
    public static void main(String[] args) throws Exception{
        //创建连接工厂 --配置连接信息
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("192.168.81.166");
        //创建连接对象Connection
        Connection connection=factory.newConnection();
        //创建信道
        Channel channel = connection.createChannel();

        //接受消息
        /**
         * (String queue, 队列的名称
         *  boolean autoAck, 是否自动确认
         *  Consumer callback: 回调方法 当队列中存在信息后 会自动触发回调函数。
         */
        DefaultConsumer callback=new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //body 接受的信息
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("消费者01:"+new String(body));
            }
        };
        channel.basicConsume("queue_fanout01",true,callback);

    }
}

3.路由模式

特点:
    1.一个生产者
    2.多个消费者
    3.多个队列。
    4.交换机 转发消息。
    5.routekey:路由key 只要routekey匹配的消息可以到达对应队列。
package com.aaa.qy129.direct;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;


public class Product {
    public static void main(String[] args)throws  Exception {
        //创建连接工厂 --配置连接信息
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("192.168.81.166");
        //创建连接对象Connection
        Connection connection=factory.newConnection();
        //创建信道
        Channel channel = connection.createChannel();

        //创建队列
        /**
         * String queue, 队列的名称
         * boolean durable, 是否该队列持久化 rabbitMQ服务重启后该存放是否存在。
         * boolean exclusive, 是否独占 false
         * boolean autoDelete, 是否自动删除  如果长时间没有发生消息 则自动删除
         * Map<String, Object> arguments 额外参数  先给null
         */
        channel.queueDeclare("queue_direct01",true,false,false,null);
        channel.queueDeclare("queue_direct02",true,false,false,null);

        //创建交换机
        /**
         * String exchange,交换机的名称
         * BuiltinExchangeType type, 交换机的类型
         * boolean durable:是否持久化
         */
        channel.exchangeDeclare("exchange_direct", BuiltinExchangeType.DIRECT,true);
        /**
         * String queue,  队列名
         * String exchange, 交换机的名称
         * String routingKey:路由key 如果交换机为fanout模式则不需要路由key
         */
        channel.queueBind("queue_direct01","exchange_direct","error");


        channel.queueBind("queue_direct02","exchange_direct","info");
        channel.queueBind("queue_direct02","exchange_direct","error");
        channel.queueBind("queue_direct02","exchange_direct","warning");
        //发生消息
        /**
         * String exchange: 交换机的名称 如果没有则使用“” 它回自动采用默认
         * String routingKey, 路由key  如果没有交换机的绑定 使用队列的名称
         * BasicProperties props, 消息的一些额外配置 目前先不加 null
         * byte[] body 消息的内容
         */
        for(int i=0;i<10;i++) {
            String msg = "仲梦君真帅!!!!!!!!!!!"+i;
            channel.basicPublish("exchange_direct", "error", null, msg.getBytes());
        }
        //生产者这里可以管理资源  消费者不能关闭资源。
        channel.close();
        connection.close();

    }
}



消费者:

package com.aaa.qy129.direct;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer01 {
    public static void main(String[] args) {
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //配置连接
        factory.setHost("192.168.81.166");
        //创建连接对象
        Connection connection = null;
        try {
            connection = factory.newConnection();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        //创建信道
        Channel channel = null;
        try {
            channel = connection.createChannel();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //接受消息
        /**
         * (String queue, 队列的名称
         *  boolean autoAck, 是否自动确认
         *  Consumer callback: 回调方法 当队列中存在信息后 会自动触发回调函数。
         */
        DefaultConsumer callback=new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //body接受消息
                System.out.println("01消息内容:"+new String(body));
            }
        };
        try {
            channel.basicConsume("direct_01",true,callback);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

4. topic主体模式

1. 绑定按照通配符的模式。
     *: 统配一个单词。
     #: 统配n个单词

hello.orange.rabbit

lazy.orange

生产者

package com.aaa.qy129.topic;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * @Author 闫克起
 * @Date 2021/4/19 17:49
 * @Version 1.0
 */
public class Product {
    public static void main(String[] args)throws  Exception {
        //创建连接工厂 --配置连接信息
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("192.168.81.166");
        //创建连接对象Connection
        Connection connection=factory.newConnection();
        //创建信道
        Channel channel = connection.createChannel();

        //创建队列
        /**
         * String queue, 队列的名称
         * boolean durable, 是否该队列持久化 rabbitMQ服务重启后该存放是否存在。
         * boolean exclusive, 是否独占 false
         * boolean autoDelete, 是否自动删除  如果长时间没有发生消息 则自动删除
         * Map<String, Object> arguments 额外参数  先给null
         */
        channel.queueDeclare("queue_topic01",true,false,false,null);
        channel.queueDeclare("queue_topic02",true,false,false,null);

        //创建交换机
        /**
         * String exchange,交换机的名称
         * BuiltinExchangeType type, 交换机的类型
         * boolean durable:是否持久化
         */
        channel.exchangeDeclare("exchange_topic", BuiltinExchangeType.TOPIC,true);
        /**
         * String queue,  队列名
         * String exchange, 交换机的名称
         * String routingKey:路由key 如果交换机为fanout模式则不需要路由key
         */
        channel.queueBind("queue_topic01","exchange_topic","*.orange.*");


        channel.queueBind("queue_topic02","exchange_topic","*.*.rabbit");
        channel.queueBind("queue_topic02","exchange_topic","lazy.#");


        //发生消息
        /**
         * String exchange: 交换机的名称 如果没有则使用“” 它回自动采用默认
         * String routingKey, 路由key  如果没有交换机的绑定 使用队列的名称
         * BasicProperties props, 消息的一些额外配置 目前先不加 null
         * byte[] body 消息的内容
         */
        for(int i=0;i<10;i++) {
            String msg = "ahahaha!!!!!!!!!!!"+i;
            channel.basicPublish("exchange_topic", "lazy.orange.rabbit", null, msg.getBytes());
        }
        //生产者这里可以管理资源  消费者不能关闭资源。
        channel.close();
        connection.close();

    }
}


消费者:

package com.aaa.qy129.topic;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer01 {
    public static void main(String[] args) {
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //配置连接
        factory.setHost("192.168.81.166");
        //创建连接对象
        Connection connection = null;
        try {
            connection = factory.newConnection();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        //创建信道
        Channel channel = null;
        try {
            channel = connection.createChannel();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //接受消息
        /**
         * (String queue, 队列的名称
         *  boolean autoAck, 是否自动确认
         *  Consumer callback: 回调方法 当队列中存在信息后 会自动触发回调函数。
         */
        DefaultConsumer callback=new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //body接受消息
                System.out.println("01消息内容:"+new String(body));
            }
        };
        try {
            channel.basicConsume("queue_topic01",true,callback);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

会自动触发回调函数。
         */
        DefaultConsumer callback=new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //body接受消息
                System.out.println("01消息内容:"+new String(body));
            }
        };
        try {
            channel.basicConsume("queue_topic01",true,callback);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值