rabbitMQ常用的五种模式

rabbitMQ常用的五种模式

1、简单模式

在这里插入图片描述
生产者代码:

package com.demo.my.hello;

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

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

public class Product {
    public static void main(String[] args) {
//        创建一个连接工厂
        ConnectionFactory factory = new ConnectionFactory();
//        连接主机
        factory.setHost("192.168.31.38");
//        创建一个连接对象
        try {
            Connection connection = factory.newConnection();
//            创建信道
            Channel channel = connection.createChannel();
//            创建队列
//            String queue, 队列名称
//            boolean durable, 是否持久化
//            boolean exclusive, 是否独占
//            boolean autoDelete,是否删除
//            Map<String, Object> arguments额外参数
            channel.queueDeclare("ban129_queue",true,false,false,null);
//            发送信息
            String msg="你们好啊";
//            String exchange, 交换机名称
//            String routingKey, 路由key 如果路由key不存在 则使用队列名称
//            AMQP.BasicProperties props, 其他的额外参数
//            byte[] body消息的内容
            channel.basicPublish("","ban129_queue",null,msg.getBytes());

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

消费者代码:

package com.demo.my.hello;

import com.rabbitmq.client.*;


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

public class Consumer1 {
    public static void main(String[] args) {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.31.38");
        try {
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();

//      接受消息
            DefaultConsumer concult =new DefaultConsumer(channel){
                @Override
                public void handleDelivery(java.lang.String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//                    super.handleDelivery(consumerTag, envelope, properties, body);
                    System.out.println("消息的内容:"+new String(body));
                }
            };
//            String queue, 队列名称
//            Consumer callback 信息内容
            channel.basicConsume("ban129_queue",concult);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }
}

2、工作者模式

在这里插入图片描述

生产者代码:

package com.demo.my.work;


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

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

public class Product {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.31.38");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
//        String queue,
//        boolean durable,
//        boolean exclusive,
//        boolean autoDelete,
//        Map<String, Object> arguments
//        创建队列
        channel.queueDeclare("ban129_queue_work",true,false,false,null);

        for (int i = 0; i < 10; i++) {
            String msg="真好"+i;
            channel.basicPublish("","ban129_queue_work",null,msg.getBytes());
        }
//        String exchange, 交换机名称
//        String routingKey, 路由key
//        AMQP.BasicProperties props,额外参数
//        byte[] body 消息的内容

    }
}

消费者1号代码:

package com.demo.my.work;

import com.rabbitmq.client.*;

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

public class Consumer01 {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.31.38");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("接受的消息01"+new String(body));
            }
        };
        channel.basicConsume("ban129_queue_work",consumer);
    }
}

消费者2号代码:

package com.demo.my.work;

import com.rabbitmq.client.*;

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

public class Consumer02 {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.31.38");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("接受的消息02"+new String(body));
            }
        };
        channel.basicConsume("ban129_queue_work",consumer);
    }
}

3、发布/订阅模式

在这里插入图片描述
生产者代码:

package com.demo.my.fanout;

import com.rabbitmq.client.*;

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

public class Product {
    public static void main(String[] args) throws IOException, TimeoutException {
        //        创建一个连接工厂
        ConnectionFactory factory = new ConnectionFactory();
//        连接主机
        factory.setHost("192.168.31.38");
//        创建一个连接对象

            Connection connection = factory.newConnection();
//            创建信道
            Channel channel = connection.createChannel();
//            创建队列
//        String queue, 队列名称
//        boolean durable, 持久化
//        boolean exclusive, 是否独占
//        boolean autoDelete,是否自动删除
//        Map<String, Object> arguments 额外的参数
        channel.queueDeclare("ban129_queue_fanout01",true,false,false,null);
        channel.queueDeclare("ban129_queue_fanout02",true,false,false,null);
        channel.queueDeclare("ban129_queue_fanout03",true,false,false,null);
//        创建交换机
//        String exchange,
//        BuiltinExchangeType type,
//        boolean durable
//        创建交换机
        channel.exchangeDeclare("ban129_exchange", BuiltinExchangeType.FANOUT,true);
//        绑定交换机
//        String queue,
//        String exchange,
//        String routingKey
        channel.queueBind("ban129_queue_fanout01","ban129_exchange","");
        channel.queueBind("ban129_queue_fanout02","ban129_exchange","");
        channel.queueBind("ban129_queue_fanout03","ban129_exchange","");
//        发送消息
        String msg="我们是发布模式";
//        String exchange,
//        String routingKey,
//        AMQP.BasicProperties props,
//        byte[] body
        channel.basicPublish("ban129_exchange","",null,msg.getBytes());


        }
}

4、路由模式

在这里插入图片描述

生产者代码:

package com.demo.my.direct;

import com.rabbitmq.client.*;

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

public class Product {
    public static void main(String[] args) throws IOException, TimeoutException {
        //        创建一个连接工厂
        ConnectionFactory factory = new ConnectionFactory();
//        连接主机
        factory.setHost("192.168.31.38");
//        创建一个连接对象

        Connection connection = factory.newConnection();
//            创建信道
        Channel channel = connection.createChannel();
//        String queue,
//        boolean durable,
//        boolean exclusive,
//        boolean autoDelete,
//        Map<String, Object> arguments
//        创建队列
        channel.queueDeclare("ban129_queue_direct01",true,false,false,null);
        channel.queueDeclare("ban129_queue_direct02",true,false,false,null);
        channel.queueDeclare("ban129_queue_direct03",true,false,false,null);
//        创建交换机
//        String exchange,
//        BuiltinExchangeType type,
//        boolean durable
        channel.exchangeDeclare("ban129_exchange_direct", BuiltinExchangeType.DIRECT,true);
//        队列绑定
//        String queue,
//        String exchange,
//        String routingKey
        channel.queueBind("ban129_queue_direct01","ban129_exchange_direct","error");
        channel.queueBind("ban129_queue_direct02","ban129_exchange_direct","info");
        channel.queueBind("ban129_queue_direct02","ban129_exchange_direct","error");
        channel.queueBind("ban129_queue_direct02","ban129_exchange_direct","warning");
        channel.queueBind("ban129_queue_direct03","ban129_exchange_direct","error");
//        发布消息
        for (int i = 0; i <10 ; i++) {
            String msg="路由模式";
//            String exchange,
//            String routingKey,
//            AMQP.BasicProperties props,
//            byte[] body
            channel.basicPublish("ban129_exchange_direct","error",null,msg.getBytes());
        }
    }
}

5、主题模式

在这里插入图片描述
生产者代码:

package com.demo.my.topic;

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

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

public class Product {
    public static void main(String[] args) throws IOException, TimeoutException {
        //        创建一个连接工厂
        ConnectionFactory factory = new ConnectionFactory();
//        连接主机
        factory.setHost("192.168.31.38");
//        创建一个连接对象

        Connection connection = factory.newConnection();
//            创建信道
        Channel channel = connection.createChannel();
//            创建队列
//        String queue, 队列名称
//        boolean durable, 持久化
//        boolean exclusive, 是否独占
//        boolean autoDelete,是否自动删除
//        Map<String, Object> arguments 额外的参数
        channel.queueDeclare("ban129_queue_topic01",true,false,false,null);
        channel.queueDeclare("ban129_queue_topic02",true,false,false,null);
        channel.queueDeclare("ban129_queue_topic03",true,false,false,null);
//        创建交换机
//        String exchange,
//        BuiltinExchangeType type,
//        boolean durable
        channel.exchangeDeclare("ban129_exchange_topic", BuiltinExchangeType.TOPIC,true);
//        绑定
//        String queue,
//        String exchange,
//        String routingKey
        channel.queueBind("ban129_queue_topic01","ban129_exchange_topic","*.man.*");
        channel.queueBind("ban129_queue_topic01","ban129_exchange_topic","*..qcx");
        channel.queueBind("ban129_queue_topic01","ban129_exchange_topic","lazy.#");
//      发送消息
        String msg="晚上好 qy129全体同学们";
        channel.basicPublish("ban129_exchange_topic","qcx.orange.qcx",null,msg.getBytes());
        channel.basicPublish("ban129_exchange_topic","qcx.orange.qcx",null,msg.getBytes());
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值