首页 > 其他分享 >Routing(路由),有选择地接收消息

Routing(路由),有选择地接收消息

时间:2022-11-20 11:36:15浏览次数:79  
标签:String rabbitmq client 路由 Routing import com channel 选择地

image

生产者

package org.example.routing;

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

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

public class EmitLogDirect {
    private static final String EXCHANGE_NAME = "direct_logs";
    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.121.200");
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            channel.exchangeDeclare(EXCHANGE_NAME, "direct");

            List<String> severityList = Arrays.asList("error","info","warning");
            for (String severity : severityList) {
                channel.basicPublish(EXCHANGE_NAME, severity, null, (severity+"消息").getBytes(StandardCharsets.UTF_8));
                System.out.println(" [x] Sent '" + severity + "':'" + (severity+"消息") + "'");
            }
        }
    }
}

消费者1

package org.example.routing;

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

import java.nio.charset.StandardCharsets;

public class ReceiveLogsDirect1 {
    private static final String EXCHANGE_NAME = "direct_logs";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.121.200");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "direct");
        String queueName = channel.queueDeclare().getQueue();

        channel.queueBind(queueName, EXCHANGE_NAME, "error");

        System.out.println(" [error] Waiting for messages. To exit press CTRL+C");

        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
            System.out.println(" [error] Received '" +
                    delivery.getEnvelope().getRoutingKey() + "':'" + message + "'");
        };
        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
        });
    }
}

消费者2

package org.example.routing;

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

import java.nio.charset.StandardCharsets;

public class ReceiveLogsDirect2 {
    private static final String EXCHANGE_NAME = "direct_logs";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.121.200");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "direct");
        String queueName = channel.queueDeclare().getQueue();


        channel.queueBind(queueName, EXCHANGE_NAME, "info");

        System.out.println(" [info] Waiting for messages. To exit press CTRL+C");

        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
            System.out.println(" [info] Received '" +
                    delivery.getEnvelope().getRoutingKey() + "':'" + message + "'");
        };
        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
        });
    }
}

消费者3

package org.example.routing;

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

import java.nio.charset.StandardCharsets;

public class ReceiveLogsDirect3 {
    private static final String EXCHANGE_NAME = "direct_logs";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.121.200");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "direct");
        String queueName = channel.queueDeclare().getQueue();


        channel.queueBind(queueName, EXCHANGE_NAME, "error");
        channel.queueBind(queueName, EXCHANGE_NAME, "info");
        channel.queueBind(queueName, EXCHANGE_NAME, "warning");

        System.out.println(" [all] Waiting for messages. To exit press CTRL+C");

        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
            System.out.println(" [all] Received '" +
                    delivery.getEnvelope().getRoutingKey() + "':'" + message + "'");
        };
        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
        });
    }
}

标签:String,rabbitmq,client,路由,Routing,import,com,channel,选择地
From: https://www.cnblogs.com/xl4ng/p/16908097.html

相关文章

  • 学习ASP.NET Core Blazor编程系列十——路由(下)
    学习ASP.NETCoreBlazor编程系列一——综述学习ASP.NETCoreBlazor编程系列二——第一个Blazor应用程序(上)学习ASP.NETCoreBlazor编程系列二——第一个Blazor应......
  • django--路由系统
    django路由系统 --动态路由 #----urls.py----------urlpatterns=[path('admin/',admin.site.urls),path('index/',views.index),re_path(r'edit/(\w*)/',vi......
  • Vue跳转路由后回到顶部
    在使用Vue3写个人博客时,点击一篇博客之后我想要跳转到文章详情界面,但是跳转后该页面位置还停留在上一个路由时的位置,而实际上是需要跳转后回到顶部。解决方法为,为路由添......
  • 路由的嵌套
    home路径下配置一个news和message子路径importVuefrom'vue'importVueRouterfrom'vue-router'constHome=()=>import('../components/Home')constHomeNews......
  • 硬路由刷OpenWrt的网络拓扑
    硬路由看似有很多网口,但其实只有一张网卡(一个网口),所有的网口都是通过划分VLAN来实现不同的功能。如下图:硬路由有5个网口。其中Port0属于VLAN1,作为WAN口。Port1~Port4属......
  • 动态路由的使用
    :to绑定参数  /:参数名,进行拼接参数  $route.params.参数名,取参数值    ......
  • 华三和华为的交换机/路由器命名规则
    华三和华为的交换机/路由器命名规则华三交换机:H3CS5500-28C-EIH3C 是华三的品牌名称S指交换机,SR指路由器S5500 指子产品系列产品系列:9 核心机箱式交换机7 高端......
  • 解决SPA单页面应用(create-react-app,Vue-cli)路由跳转后404的问题。
    在Nginx中配置try_filesserver{listen80;server_namelocalhost;root"****";location/{ try_files$uri$uri/......
  • 路由原理hash和history
    hash和history都是运用于前后端项目分离的要清楚两者的区别以及两者各自的使用场景,还有各自的使用特点和优缺点hash是由#后面拼接的真实url路径history是h5新增的特性......
  • 通过代码跳转路由
    <template><divid="app"><button@click="toHome">首页</button><buttonto="/about"@click="toAbout">关于</button><!--相当于占位符--><r......