Example usage for com.rabbitmq.client ConnectionFactory useNio

List of usage examples for com.rabbitmq.client ConnectionFactory useNio

Introduction

In this page you can find the example usage for com.rabbitmq.client ConnectionFactory useNio.

Prototype

public void useNio() 

Source Link

Document

Use non-blocking IO (NIO) for communication with the server.

Usage

From source file:reactor.rabbitmq.docs.ApiGuideSender.java

License:Open Source License

void optionsSimple() {
    // tag::options-simple[]
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.useNio();

    SenderOptions senderOptions = new SenderOptions().connectionFactory(connectionFactory) // <1>
            .resourceManagementScheduler(Schedulers.elastic()); // <2>
    // end::options-simple[]
    // tag::instanciation[]
    Sender sender = RabbitFlux.createSender(senderOptions);
    // end::instanciation[]
    // tag::outbound-message-flux[]
    Flux<OutboundMessage> outboundFlux = Flux.range(1, 10)
            .map(i -> new OutboundMessage("amq.direct", "routing.key", ("Message " + i).getBytes()));
    // end::outbound-message-flux[]
    Logger log = LoggerFactory.getLogger(ApiGuideSender.class);
    // tag::send-flux[]
    sender.send(outboundFlux) // <1>
            .doOnError(e -> log.error("Send failed", e)) // <2>
            .subscribe(); // <3>
    // end::send-flux[]
    // tag::resource-declaration[]
    Mono<AMQP.Exchange.DeclareOk> exchange = sender
            .declareExchange(ExchangeSpecification.exchange("my.exchange"));
    Mono<AMQP.Queue.DeclareOk> queue = sender.declareQueue(QueueSpecification.queue("my.queue"));
    Mono<AMQP.Queue.BindOk> binding = sender
            .bind(BindingSpecification.binding().exchange("my.exchange").queue("my.queue").routingKey("a.b"));
    // end::resource-declaration[]
    // tag::resource-declaration-static-import[]
    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"));
    // end::resource-declaration-static-import[]
    // tag::resource-deletion[]
    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"));
    // end::resource-deletion[]
    // tag::closing[]
    sender.close();//ww  w.  j  ava  2  s .  c  o m
    // end::closing[]
}

From source file:reactor.rabbitmq.docs.ApiGuideSender.java

License:Open Source License

void publisherConfirms() {
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.useNio();

    SenderOptions senderOptions = new SenderOptions().connectionFactory(connectionFactory)
            .resourceManagementScheduler(Schedulers.elastic());
    Sender sender = RabbitFlux.createSender(senderOptions);
    // tag::publisher-confirms[]
    Flux<OutboundMessage> outboundFlux = Flux.range(1, 10)
            .map(i -> new OutboundMessage("amq.direct", "routing.key", "hello".getBytes()));
    sender.sendWithPublishConfirms(outboundFlux).subscribe(outboundMessageResult -> {
        // outbound message has reached the broker
    });//w  w  w  . j av a2s .c  o  m
    // end::publisher-confirms[]
}

From source file:reactor.rabbitmq.RabbitFluxTests.java

License:Open Source License

@BeforeEach
public void init() throws Exception {
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.useNio();
    connection = connectionFactory.newConnection();
    Channel channel = connection.createChannel();
    String queueName = UUID.randomUUID().toString();
    queue = channel.queueDeclare(queueName, false, false, false, null).getQueue();
    channel.close();//from  ww  w . j a  v  a  2s  .c  om
    receiver = null;
    sender = null;
}

From source file:reactor.rabbitmq.RabbitFluxTests.java

License:Open Source License

@ParameterizedTest
@MethodSource("noAckAndManualAckFluxArguments")
public void receiverFluxDisposedOnConnectionClose(
        BiFunction<Receiver, String, Flux<? extends Delivery>> fluxFactory) throws Exception {
    Channel channel = connection.createChannel();
    int nbMessages = 10;
    Mono<Connection> connectionMono = Mono.fromCallable(() -> {
        ConnectionFactory cf = new ConnectionFactory();
        cf.useNio();
        return cf.newConnection();
    }).cache();/*from ww  w.j  ava2  s  .co m*/
    receiver = RabbitFlux.createReceiver(new ReceiverOptions().connectionMono(connectionMono));

    Flux<? extends Delivery> flux = fluxFactory.apply(receiver, queue);
    for (int $$ : IntStream.range(0, nbMessages).toArray()) {
        channel.basicPublish("", queue, null, "Hello".getBytes());
    }

    CountDownLatch messageReceivedLatch = new CountDownLatch(nbMessages);
    CountDownLatch completedLatch = new CountDownLatch(1);
    AtomicInteger counter = new AtomicInteger();
    Disposable subscription = flux.subscribe(msg -> {
        counter.incrementAndGet();
        messageReceivedLatch.countDown();
    }, error -> {
    }, () -> completedLatch.countDown());

    assertTrue(messageReceivedLatch.await(1, TimeUnit.SECONDS));
    assertEquals(nbMessages, counter.get());
    assertEquals(1, completedLatch.getCount());
    connectionMono.block().close();
    assertTrue(completedLatch.await(1, TimeUnit.SECONDS));
    subscription.dispose();
}

From source file:reactor.rabbitmq.RabbitFluxTests.java

License:Open Source License

@Test
public void connectionMonoSharedConnection() throws Exception {
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.useNio();
    Mono<? extends Connection> connectionMono = Utils.singleConnectionMono(connectionFactory,
            cf -> cf.newConnection());

    sender = createSender(new SenderOptions().connectionMono(connectionMono));
    receiver = createReceiver(new ReceiverOptions().connectionMono(connectionMono));

    String connectionQueue = sender
            .declare(QueueSpecification.queue().durable(false).autoDelete(true).exclusive(true)).block()
            .getQueue();//from   ww w .  ja  va 2 s  .c  om

    sendAndReceiveMessages(connectionQueue);
}

From source file:reactor.rabbitmq.RabbitFluxTests.java

License:Open Source License

@Test
public void creatConnectionWithConnectionSupplier() throws Exception {
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.useNio();

    sender = createSender(new SenderOptions().connectionFactory(connectionFactory)
            .connectionSupplier(cf -> cf.newConnection("reactive-sendRetryOnFailure")));

    receiver = createReceiver(new ReceiverOptions().connectionFactory(connectionFactory)
            .connectionSupplier(cf -> cf.newConnection("reactive-receiver")));

    sendAndReceiveMessages(queue);//w  w w  . ja  v a2s .c  o m
}

From source file:reactor.rabbitmq.RabbitFluxTests.java

License:Open Source License

@Test
public void createConnectionWithConnectionMono() throws Exception {
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.useNio();

    sender = createSender(new SenderOptions().connectionMono(
            Mono.fromCallable(() -> connectionFactory.newConnection("reactive-sendRetryOnFailure"))));

    receiver = createReceiver(new ReceiverOptions()
            .connectionMono(Mono.fromCallable(() -> connectionFactory.newConnection("reactive-receiver"))));

    sendAndReceiveMessages(queue);/*from  w  w w.  j  a  v a 2  s  .  c  o m*/
}

From source file:reactor.rabbitmq.RabbitFluxTests.java

License:Open Source License

@Test
public void creatingNonExistentPassiveChannelResultsInError() {
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.useNio();

    sender = createSender(new SenderOptions().connectionMono(
            Mono.fromCallable(() -> connectionFactory.newConnection("non-existing-passive-queue"))));

    StepVerifier.create(sender.declareQueue(QueueSpecification.queue("non-existing-queue").passive(true)))
            .expectError(ShutdownSignalException.class).verify();
}

From source file:reactor.rabbitmq.RabbitFluxTests.java

License:Open Source License

@Test
public void creatingNonExistentPassiveExchangeResultsInError() {
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.useNio();

    sender = createSender(new SenderOptions().connectionMono(
            Mono.fromCallable(() -> connectionFactory.newConnection("non-existing-passive-exchange"))));

    StepVerifier//from ww  w.  j  av a 2 s.  c o  m
            .create(sender
                    .declareExchange(ExchangeSpecification.exchange("non-existing-exchange").passive(true)))
            .expectError(ShutdownSignalException.class).verify();
}

From source file:reactor.rabbitmq.ReactorRabbitMqTests.java

License:Open Source License

@Before
public void init() throws Exception {
    ConnectionFactory connectionFactory = new ConnectionFactory();
    connectionFactory.useNio();
    connection = connectionFactory.newConnection();
    Channel channel = connection.createChannel();
    String queueName = UUID.randomUUID().toString();
    queue = channel.queueDeclare(queueName, false, false, false, null).getQueue();
    channel.close();// www . j  a  v a  2  s.  c  o  m
    receiver = null;
    sender = null;
}