栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

RabbitMQ订阅模式

RabbitMQ订阅模式

工具类:RabbitMQUtils

package com.example.ExchangeRabbit;

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

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

public class RabbitMQUtils {

    public static Connection getConnection() throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("localhost");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");
        connectionFactory.setUsername("chocoMoss");
        connectionFactory.setPassword("123456");
        Connection connection = connectionFactory.newConnection();
        return connection;
    }
}

提供者:PublishSubscribeProducer

获取连接
声明交换机:channel.exchangeDeclare()
声明队列:channel.queueDeclare()
队列绑定交换机:channel.queueBind()
消息体
发送:channel.basicPublish()

package com.example.ExchangeRabbit;

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

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

public class PublishSubscribeProducer {

    

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();

        
        channel.exchangeDeclare("fanout_exchange", BuiltinExchangeType.FANOUT);

        
        channel.queueDeclare("fanout_queue_1",true,false,false,null);
        channel.queueDeclare("fanout_queue_2",true,false,false,null);

        //队列绑定交换机
        channel.queueBind("fanout_queue_1","fanout_exchange","");
        channel.queueBind("fanout_queue_2","fanout_exchange","");

        //消息
        String message = "我就是来试试";

        
        channel.basicPublish("fanout_exchange","",null,message.getBytes());

        //关闭资源
        channel.close();
        connection.close();
    }
}

消费者:PublishSubscribeConsumerOne

创建连接
声明队列:channel.queueDeclare()
创建消费者:DefaultConsumer
处理消息:handleDelivery
获取路由key:envelope.getRoutingKey()
获取交换机:envelope.getExchange()
获取消息ID:envelope.getDeliveryTag()
获取消息
消息监听:channel.basicConsume();

package com.example.ExchangeRabbit;

import com.rabbitmq.client.*;

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

public class PublishSubscribeConsumerOne {

    
    public static void main(String[] args) throws IOException, TimeoutException {
        //创建链接对象
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare("fanout_queue_1",true,false,false,null);

        //创建消费者,并设置消息处理
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //路由key
                String routingKey = envelope.getRoutingKey();
                //交换机
                String exchange = envelope.getExchange();
                //消息ID
                long deliveryTag = envelope.getDeliveryTag();
                //收到消息
                String message = new String(body,"UTF-8");

                System.out.println("routingKey:"+routingKey+",exchange:"+exchange+",deliveryTag:"+deliveryTag+",message:"+message);
            }
        };
        //消息监听
        channel.basicConsume("fanout_queue_1",true,defaultConsumer);

        //关闭资源
//        channel.close();
//        connection.close();
    }
}

消费者:PublishSubscribeConsumerTwo

package com.example.ExchangeRabbit;

import com.rabbitmq.client.*;

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

public class PublishSubscribeConsumerTwo {

    
    public static void main(String[] args) throws IOException, TimeoutException {
        //创建链接对象
        Connection connection = RabbitMQUtils.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare("fanout_queue_2",true,false,false,null);

        //创建消费者,并设置消息处理
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //路由key
                String routingKey = envelope.getRoutingKey();
                //交换机
                String exchange = envelope.getExchange();
                //消息ID
                long deliveryTag = envelope.getDeliveryTag();
                //收到消息
                String message = new String(body,"UTF-8");

                System.out.println("routingKey:"+routingKey+",exchange:"+exchange+",deliveryTag:"+deliveryTag+",message:"+message);
            }
        };
        //消息监听
        channel.basicConsume("fanout_queue_2",true,defaultConsumer);

        //关闭资源
//        channel.close();
//        connection.close();
    }
}

提供者提供消息

消费者获取消息

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/613206.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号