reactor-rabbitmq小试牛刀

本文主要研究一下如何使用reactor-rabbitmq

maven

        <dependency>
            <groupId>io.projectreactor.rabbitmq</groupId>
            <artifactId>reactor-rabbitmq</artifactId>
            <version>1.0.0.M2</version>
        </dependency>
复制代码

rabbitmq

  • 参考docker搭建rabbitmq集群
  • 当前使用的镜像是bijukunjummen/rabbitmq-server:3.7.0,docker-compose文件配置的账号密码为myuser/mypass
  • 访问http://192.168.99.100:15672可以查看界面

实例

    @Test
    public void testProducer() throws InterruptedException {
        int count = 100;
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.useNio();
        connectionFactory.setUsername("myuser");
        connectionFactory.setPassword("mypass");
        SenderOptions senderOptions =  new SenderOptions()
                .connectionFactory(connectionFactory)
                .connectionSupplier(cf -> cf.newConnection(
                        new Address[] {new Address("192.168.99.100",5672), new Address("192.168.99.100",5673), new Address("192.168.99.100",5674)},
                        "reactive-sender"))
                .resourceCreationScheduler(Schedulers.elastic());
        Sender sender = ReactorRabbitMq.createSender(senderOptions);
        Flux<OutboundMessageResult> confirmations = sender.sendWithPublishConfirms(Flux.range(1, count)
                .map(i -> new OutboundMessage("", QUEUE, ("Message_" + i).getBytes())));

        CountDownLatch latch = new CountDownLatch(count);

        sender.declareQueue(QueueSpecification.queue(QUEUE))
                .thenMany(confirmations)
                .doOnError(e -> LOGGER.error("Send failed", e))
                .subscribe(r -> {
                    if (r.isAck()) {
                        LOGGER.info("Message {} sent successfully", new String(r.getOutboundMessage().getBody()));
                        latch.countDown();
                    }
                });

        latch.await(10, TimeUnit.SECONDS);
        sender.close();
    }

    @Test
    public void testConsumer() throws InterruptedException {
        int count = 100;
        CountDownLatch latch = new CountDownLatch(count);

        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.useNio();
        connectionFactory.setUsername("myuser");
        connectionFactory.setPassword("mypass");
        SenderOptions senderOptions =  new SenderOptions()
                .connectionFactory(connectionFactory)
                .connectionSupplier(cf -> cf.newConnection(
                        new Address[] {new Address("192.168.99.100",5672), new Address("192.168.99.100",5673), new Address("192.168.99.100",5674)},
                        "reactive-sender"))
                .resourceCreationScheduler(Schedulers.elastic());

        Sender sender = ReactorRabbitMq.createSender(senderOptions);
        Mono<AMQP.Queue.DeclareOk> queueDeclaration = sender.declareQueue(QueueSpecification.queue(QUEUE));

        ReceiverOptions receiverOptions = new ReceiverOptions()
                .connectionFactory(connectionFactory)
                .connectionSupplier(cf -> cf.newConnection(
                        new Address[] {new Address("192.168.99.100",5672), new Address("192.168.99.100",5673), new Address("192.168.99.100",5674)},
                        "reactive-receiver"))
                .connectionSubscriptionScheduler(Schedulers.elastic());
        Receiver receiver = ReactorRabbitMq.createReceiver(receiverOptions);
        Flux<Delivery> messages = receiver.consumeAutoAck(QUEUE);
        Disposable disposable = queueDeclaration.thenMany(messages).subscribe(m -> {
            LOGGER.info("Received message {}", new String(m.getBody()));
            latch.countDown();
        });

        latch.await(10, TimeUnit.SECONDS);

        disposable.dispose();
        sender.close();
        receiver.close();
    }
复制代码
  • 由于设置了账号密码,因而需要在ConnectionFactory那里指定账号密码
  • 另外由于使用了rabbitmq集群,因而通过connectionSupplier指定要连接的多个rabbitmq地址
  • 这里不管是producer还是consumer,都通过queueDeclaration进行操作

小结

reactor-rabbitmq对rabbitmq的api进行封装,改造为reactive streams模式,提供了Non-blocking Back-pressure以及End-to-end Reactive Pipeline特性。

doc

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值