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();

猜你喜欢

转载自blog.csdn.net/luo15242208310/article/details/112002583