reactor rabbitmq

Reactive API for RabbitMQ

参考:Reactor RabbitMQ Reference Guide

Reactor RabbitMQ is a reactive API for RabbitMQ based on Reactor and RabbitMQ Java Client. Reactor RabbitMQ API enables messages to be published to RabbitMQ and consumed from RabbitMQ using functional APIs with non-blocking back-pressure and very low overheads. This enables applications using Reactor to use RabbitMQ as a message bus or streaming platform and integrate with other systems to provide an end-to-end reactive pipeline.

sender

参考:sender api

//define connection factory
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.useNio();

//define sender options 
SenderOptions senderOptions = new SenderOptions()
	//Specify connection factory
    .connectionFactory(connectionFactory)
    //Specify array of addresses and connection name
    .connectionSupplier(cf -> cf.newConnection(                                  
        new Address[] {new Address("192.168.0.1"), new Address("192.168.0.2")},
        "reactive-sender"))
     //Specify scheduler for resource management
    .resourceManagementScheduler(Schedulers.boundedElastic());

//create rabbit sender
Sender sender = RabbitFlux.createSender(senderOptions);

//define outboundMessage flux 
Flux<OutboundMessage> outboundFlux  =
    Flux.range(1, 10)
        .map(i -> new OutboundMessage(
            "amq.direct",
            "routing.key", ("Message " + i).getBytes()
        ));
//send msg
sender.send(outboundFlux)                         
	//If the sending fails, log an error
    .doOnError(e -> log.error("Send failed", e))  
    //Subscribe to trigger the actual flow of records from outboundFlux to RabbitMQ
    .subscribe();

//import static reactor.rabbitmq.ResourcesSpecification.*;
//Managing resources (exchanges, queues, and bindings)
sender.declare(exchange("my.exchange"))
    .then(sender.declare(queue("my.queue")))
    .then(sender.bind(binding("my.exchange", "a.b", "my.queue")))
    .subscribe(r -> System.out.println("Exchange and queue declared and bound"));
//unbind or delete resources
sender.unbind(binding("my.exchange", "a.b", "my.queue"))
    .then(sender.delete(exchange("my.exchange")))
    .then(sender.delete(queue("my.queue")))
    .subscribe(r -> System.out.println("Exchange and queue unbound and deleted"));

//confirm callback
sender.sendWithPublishConfirms(outboundFlux)
    .subscribe(outboundMessageResult -> {
        if (outboundMessageResult.isAck()) {
                                                                    
        }
    });
//return callback
sender.sendWithPublishConfirms(outboundFlux, new SendOptions().trackReturned(true))  
        .subscribe(outboundMessageResult -> {
            if (outboundMessageResult.isReturned()) {
                                                                                     
            }
        });

//close sender
sender.close();

receiver

参考:receiver - api

//define connection factory
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.useNio();

//define receiver options
ReceiverOptions receiverOptions =  new ReceiverOptions()
    .connectionFactory(connectionFactory)
    .connectionSupplier(cf -> cf.newConnection(                                  
        new Address[] {new Address("192.168.0.1"), new Address("192.168.0.2")},
        "reactive-sender"))                      
    .connectionSubscriptionScheduler(Schedulers.boundedElastic()); 

//create receiver and generate msg flux from queue
Flux<Delivery> inboundFlux = RabbitFlux.createReceiver(receiverOptions)
		//subscribe msg from queue(consumeNoAck|consumeAutoAck|consumeManualAck)
        .consumeAutoAck("reactive.queue");

//consume msg from flux
inboundFlux.map(delivery -> convertMsgDto(delivery))
.filter(msgDto -> ...)
.subscribe(msg -> {
	...
}); 

//close receiver
receiver.close();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

罗小爬EX

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值