05_RabbitMQ入门案例—fanout模式&Direct模式&Topic模式

RabbitMQ入门案例—fanout模式

package com.tian.rabbitmq.routing;

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

public class Producer {
    public static void main(String[] args) {
        //所有的中间件技术都是基于tcp/ip协议基础之上构建新型的协议规范,只不过rabbitmq遵循的是amcp
        // ip port


        //1:创建连接工程
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.1.150");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");
        connectionFactory.setVirtualHost("/");
        Connection connection = null;
        Channel channel = null;
        try {
            //2:创建连接connection
            connection = connectionFactory.newConnection("生产者");

            //3:通过连接获取通道Channel
            channel = connection.createChannel();

            //4:通过创建交换机,声明队列,绑定关系,路由key, 发送消息,和接收消息
            String queueName = "queue1";
            /**
             * @params1 队列的名称
             * @params2 是否持久化
             * @params3 排他性
             * @params4 是否自动删除
             * @params5 携带附属参数
             */
            channel.queueDeclare(queueName, false, false, false, null);
            //5:准备消息内容
            String message = "hello rabbitmq";

            // 准备交换机
            String exchangeName="faout-exchange";
            //定义路由key
            String routeKey="";
            //指定交换机的类型
            String type="fanout";


            //6:发送消息给队列queue
            //@params1:交换机
            channel.basicPublish(exchangeName, routeKey, null, message.getBytes());
            System.out.println("消息发送成功");

        } catch (Exception ex) {
            ex.printStackTrace();
        }finally {
            //7:关闭连接
            if(channel!=null&&channel.isOpen()){
                try {
                    channel.close();
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
            //8:关闭通道
            if(connection!=null&&connection.isOpen()){
                try {
                    connection.close();
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        }

    }
}

已经是绑定上了

运行后 queue11 queue2 queue3 都会多一条消息

package com.tian.rabbitmq.routing;

import com.rabbitmq.client.*;

import java.io.IOException;

public class Consumer {
    private static Runnable runnable=new Runnable() {
        public void run() {
            //1:创建连接工程
            ConnectionFactory connectionFactory = new ConnectionFactory();
            connectionFactory.setHost("192.168.1.150");
            connectionFactory.setPort(5672);
            connectionFactory.setUsername("admin");
            connectionFactory.setPassword("admin");
            connectionFactory.setVirtualHost("/");
            final String queryName = Thread.currentThread().getName();
            Connection connection = null;
            Channel channel = null;
            try {
                //2:创建连接connection
                connection = connectionFactory.newConnection("生产者");

                //3:通过连接获取通道Channel
                channel = connection.createChannel();

                channel.basicConsume(queryName, true, new DeliverCallback() {
                    public void handle(String s, Delivery delivery) throws IOException {
                        System.out.println(queryName+"收到的消息是" + new String(delivery.getBody(), "UTF-8"));
                    }
                }, new CancelCallback() {
                    public void handle(String s) throws IOException {
                        System.out.println("接收失败了");
                    }
                });

                System.out.println("开始接收消息");
                //     System.in.read();

            } catch (Exception ex) {
                ex.printStackTrace();
            }finally {
                //7:关闭连接
                if(channel!=null&&channel.isOpen()){
                    try {
                        channel.close();
                    }catch (Exception ex){
                        ex.printStackTrace();
                    }
                }
                //8:关闭通道
                if(connection!=null&&connection.isOpen()){
                    try {
                        connection.close();
                    }catch (Exception ex){
                        ex.printStackTrace();
                    }
                }
            }

        }
    };

    public static void main(String[] args) {

        new Thread(runnable,"queue11").start();
        new Thread(runnable,"queue2").start();
        new Thread(runnable,"queue3").start();

    }
}

运行之后 消息都被消费了

Direct模式

producer代码改这几个即可

在这里插入图片描述

在这里插入图片描述

Topic模式

producer代码改这几个即可

在这里插入图片描述

绑定关系

在这里插入图片描述

完整的声明创建方式

producer

package com.tian.rabbitmq.all;

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

public class Producer {
    public static void main(String[] args) {
        //所有的中间件技术都是基于tcp/ip协议基础之上构建新型的协议规范,只不过rabbitmq遵循的是amcp
        // ip port


        //1:创建连接工程
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.1.150");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");
        connectionFactory.setVirtualHost("/");
        Connection connection = null;
        Channel channel = null;
        try {
            //2:创建连接connection
            connection = connectionFactory.newConnection("生产者");

            //3:通过连接获取通道Channel
            channel = connection.createChannel();

            //4:通过创建交换机,声明队列,绑定关系,路由key, 发送消息,和接收消息
            String queueName = "queue1";

            channel.queueDeclare(queueName, false, false, false, null);
            //5:准备消息内容
            String message = "你好 hello rabbitmq";

            // 准备交换机
            String exchangeName="direct_message_exchange";
            //指定交换机的类型
            String exchangeType="direct";
            channel.exchangeDeclare(exchangeName,exchangeType,true);
            //声明队列
            channel.queueDeclare("queue5",true,false,false,null);
            channel.queueDeclare("queue6",true,false,false,null);
            channel.queueDeclare("queue7",true,false,false,null);
            //绑定队列和交换机的关系
            channel.queueBind("queue5",exchangeName,"order");
            channel.queueBind("queue6",exchangeName,"order");
            channel.queueBind("queue7",exchangeName,"course");


            channel.basicPublish(exchangeName, "course", null, message.getBytes());
            System.out.println("消息发送成功");

        } catch (Exception ex) {
            ex.printStackTrace();
        }finally {
            //7:关闭连接
            if(channel!=null&&channel.isOpen()){
                try {
                    channel.close();
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
            //8:关闭通道
            if(connection!=null&&connection.isOpen()){
                try {
                    connection.close();
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        }



    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值