List of usage examples for com.rabbitmq.client DefaultConsumer DefaultConsumer
public DefaultConsumer(Channel channel)
From source file:rabbitsend.RabbitSend.java
public void turnOn(String host, int port) throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(host);// w w w. j a va 2s .c o m factory.setPort(port); connection = factory.newConnection(); channel = connection.createChannel(); //deklarasi exchange channel.exchangeDeclare(EXCHANGE_NAME, "direct"); //deklarasi queue messageQueue = channel.queueDeclare().getQueue(); //channel.queueBind(messageQueue, EXCHANGE_NAME, ""); //deklarasi listener Consumer receiver = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws UnsupportedEncodingException { String message = new String(body, "UTF-8"); System.out.println(message); } }; channel.basicConsume(messageQueue, true, receiver); }
From source file:reactor.rabbitmq.ConnectionRecoveryTests.java
License:Open Source License
@Test public void sendRetryOnFailureAllFluxMessagesShouldBeSentAndConsumed() throws Exception { int nbMessages = 10; CountDownLatch latch = new CountDownLatch(nbMessages); AtomicInteger counter = new AtomicInteger(); Channel channel = connection.createChannel(); channel.basicConsume(queue, true, new DefaultConsumer(channel) { @Override/*from www.j a v a 2 s . co m*/ public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) { counter.incrementAndGet(); latch.countDown(); } }); Flux<OutboundMessage> msgFlux = Flux.range(0, nbMessages) .map(i -> new OutboundMessage("", queue, "".getBytes())).delayElements(ofMillis(200)); sender = createSender(new SenderOptions().connectionMono(connectionMono)); sender.send(msgFlux, new SendOptions().exceptionHandler(new ExceptionHandlers.RetrySendingExceptionHandler(ofSeconds(5), ofMillis(100), ExceptionHandlers.CONNECTION_RECOVERY_PREDICATE))) .subscribe(); closeAndWaitForRecovery((RecoverableConnection) connectionMono.block()); assertTrue(latch.await(10, TimeUnit.SECONDS)); assertEquals(nbMessages, counter.get()); }
From source file:reactor.rabbitmq.RabbitFluxTests.java
License:Open Source License
@Test public void sender() throws Exception { int nbMessages = 10; CountDownLatch latch = new CountDownLatch(nbMessages); AtomicInteger counter = new AtomicInteger(); Channel channel = connection.createChannel(); channel.basicConsume(queue, true, new DefaultConsumer(channel) { @Override/*from w w w .jav a2 s .c o m*/ public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { counter.incrementAndGet(); latch.countDown(); } }); Flux<OutboundMessage> msgFlux = Flux.range(0, nbMessages) .map(i -> new OutboundMessage("", queue, "".getBytes())); sender = createSender(); sender.send(msgFlux).subscribe(); assertTrue(latch.await(1, TimeUnit.SECONDS)); assertEquals(nbMessages, counter.get()); }
From source file:reactor.rabbitmq.ReactorRabbitMqTests.java
License:Open Source License
@Test public void sender() throws Exception { int nbMessages = 10; CountDownLatch latch = new CountDownLatch(nbMessages); AtomicInteger counter = new AtomicInteger(); Channel channel = connection.createChannel(); channel.basicConsume(queue, true, new DefaultConsumer(channel) { @Override/*from w w w .j av a 2 s . co m*/ public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { counter.incrementAndGet(); latch.countDown(); } }); Flux<OutboundMessage> msgFlux = Flux.range(0, nbMessages) .map(i -> new OutboundMessage("", queue, "".getBytes())); sender = ReactorRabbitMq.createSender(); sender.send(msgFlux).subscribe(); assertTrue(latch.await(1, TimeUnit.SECONDS)); assertEquals(nbMessages, counter.get()); }
From source file:reactor.rabbitmq.ReactorRabbitMqTests.java
License:Open Source License
@Test public void publishConfirms() throws Exception { int nbMessages = 10; CountDownLatch consumedLatch = new CountDownLatch(nbMessages); CountDownLatch confirmedLatch = new CountDownLatch(nbMessages); AtomicInteger counter = new AtomicInteger(); Channel channel = connection.createChannel(); channel.basicConsume(queue, true, new DefaultConsumer(channel) { @Override//from w w w .j a v a2 s . co m public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { counter.incrementAndGet(); consumedLatch.countDown(); } }); Flux<OutboundMessage> msgFlux = Flux.range(0, nbMessages) .map(i -> new OutboundMessage("", queue, "".getBytes())); sender = ReactorRabbitMq.createSender(); sender.sendWithPublishConfirms(msgFlux).subscribe(outboundMessageResult -> { confirmedLatch.countDown(); }); assertTrue(consumedLatch.await(1, TimeUnit.SECONDS)); assertTrue(confirmedLatch.await(1, TimeUnit.SECONDS)); assertEquals(nbMessages, counter.get()); }
From source file:reactor.rabbitmq.Receiver.java
License:Open Source License
public Flux<Delivery> consumeNoAck(final String queue, ReceiverOptions options) { // TODO track flux so it can be disposed when the sender is closed? // could be also developer responsibility return Flux.create(emitter -> { connectionMono.subscribe(connection -> { try { // TODO handle exception Channel channel = connection.createChannel(); final DefaultConsumer consumer = new DefaultConsumer(channel) { @Override//from w ww . j a v a 2 s. co m public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { emitter.next(new Delivery(envelope, properties, body)); } @Override public void handleCancel(String consumerTag) throws IOException { LOGGER.warn("Flux consumer {} has been cancelled", consumerTag); } }; final String consumerTag = channel.basicConsume(queue, true, consumer); emitter.onDispose(() -> { try { if (channel.isOpen() && channel.getConnection().isOpen()) { channel.basicCancel(consumerTag); channel.close(); } } catch (TimeoutException | IOException e) { throw new ReactorRabbitMqException(e); } }); } catch (IOException e) { throw new ReactorRabbitMqException(e); } }); }, options.getOverflowStrategy()); }
From source file:reactor.rabbitmq.Receiver.java
License:Open Source License
public Flux<AcknowledgableDelivery> consumeManuelAck(final String queue, ReceiverOptions options) { // TODO track flux so it can be disposed when the sender is closed? // could be also developer responsibility return Flux.create(emitter -> { connectionMono.subscribe(connection -> { try { Channel channel = connection.createChannel(); if (options.getQos() != 0) { channel.basicQos(options.getQos()); }/* www. ja v a 2 s . c om*/ final DefaultConsumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { AcknowledgableDelivery message = new AcknowledgableDelivery(envelope, properties, body, getChannel()); if (options.getHookBeforeEmit().apply(emitter, message)) { emitter.next(message); } } }; final String consumerTag = channel.basicConsume(queue, false, consumer); emitter.onDispose(() -> { try { if (channel.isOpen() && channel.getConnection().isOpen()) { channel.basicCancel(consumerTag); channel.close(); } } catch (TimeoutException | IOException e) { throw new ReactorRabbitMqException(e); } }); } catch (IOException e) { throw new ReactorRabbitMqException(e); } }); }, options.getOverflowStrategy()); }
From source file:reactor.rabbitmq.SenderTests.java
License:Open Source License
@Test void createExchangeBeforePublishing() throws Exception { int nbMessages = 10; CountDownLatch latch = new CountDownLatch(nbMessages); AtomicInteger counter = new AtomicInteger(); Channel channel = connection.createChannel(); channel.basicConsume(queue, true, new DefaultConsumer(channel) { @Override/*from w ww . j a va 2 s . co m*/ public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { counter.incrementAndGet(); latch.countDown(); } }); String exchange = UUID.randomUUID().toString(); Flux<OutboundMessage> msgFlux = Flux.range(0, nbMessages) .map(i -> new OutboundMessage(exchange, queue, "".getBytes())); sender = createSender(); sender.declare(ExchangeSpecification.exchange(exchange).type("direct").autoDelete(true)) .then(sender.bind(BindingSpecification.binding(exchange, queue, queue))).then(sender.send(msgFlux)) .subscribe(); assertTrue(latch.await(1, TimeUnit.SECONDS)); assertEquals(nbMessages, counter.get()); }
From source file:Release1.Controllers.AlarmController.java
/** * mtodo para recibir mensajes de los sensores de alarmas de ventana * dependiendo del mensaje recibido se envia la respuesta a los sensores * @throws IOException//from ww w . j a v a 2s .c o m * @throws TimeoutException */ private synchronized void receiveAlamrWindowSensorMessage() throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(HOST); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(ID_CHANNEL_AWINDOW_SENSOR, "fanout"); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, ID_CHANNEL_AWINDOW_SENSOR, ""); Consumer consumer; consumer = new DefaultConsumer(channel) { @Override public synchronized void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { setMessage(new String(body, "UTF-8")); logger.info("Class ALARM WINDOW Controller --- RECEIVED from Sensor --- Value: " + new String(body, "UTF-8")); if (Integer.parseInt(getMessage()) > MAX_VALUE_ALARMS) {//si el valor obtenido es mayor al permitido para simular las alarmas se activa la alarma try { sendMessage(ID_CHANNEL_AWINDOW_CONTROLLER, ID_AWINDOW_ON);//envio de mensaje para encender alarma la alarma de ventnaa logger.info("Class: ALARM CONTROLLER --- SEND --- ACTIVE ALARM WINDOW"); } catch (TimeoutException ex) { logger.error(ex); } } else { try { sendMessage(ID_CHANNEL_AWINDOW_CONTROLLER, ID_AWINDOW_OFF);//envio de mensaje apra apagar la alarma de ventana } catch (TimeoutException ex) { logger.error(ex); } } } }; channel.basicConsume(queueName, true, consumer); }
From source file:Release1.Controllers.AlarmController.java
/** * mtodo para recibir mensajes de los sensores de alarmas de puerta * dependiendo del mensaje recibido se envia la respuesta a los sensores * @throws IOException// w ww . j a v a2 s.c o m * @throws TimeoutException */ private void receiveAlarmDoorMessage() throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(HOST); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(ID_CHANNEL_ADOOR_SENSOR, "fanout"); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, ID_CHANNEL_ADOOR_SENSOR, ""); Consumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { setMessage(new String(body, "UTF-8")); logger.info("Class Alarm DOOR Controller --- RECEIVED from Sensor --- Value: " + new String(body, "UTF-8")); if (Integer.parseInt(getMessage()) > MAX_VALUE_ALARMS) { try { sendMessage(ID_CHANNEL_ADOOR_CONTROLLER, ID_ADOOR_ON);//envio de mensaje para encender la alarma } catch (TimeoutException ex) { logger.error(ex); } } else { try { sendMessage(ID_CHANNEL_ADOOR_CONTROLLER, ID_ADOOR_OFF);//envio de mensajes para apagar la alarma } catch (TimeoutException ex) { logger.error(ex); } } } }; channel.basicConsume(queueName, true, consumer); }