List of usage examples for com.rabbitmq.client Channel close
@Override void close() throws IOException, TimeoutException;
From source file:reactor.rabbitmq.ReactorRabbitMqTests.java
License:Open Source License
@Test public void createResourcesPublishConsume() throws Exception { final String queueName = UUID.randomUUID().toString(); final String exchangeName = UUID.randomUUID().toString(); final String routingKey = "a.b"; int nbMessages = 100; try {/* www . j av a 2 s . c o m*/ sender = ReactorRabbitMq.createSender(); MonoProcessor<Void> resourceSendingSub = sender .createExchange(ExchangeSpecification.exchange(exchangeName)) .then(sender.createQueue(QueueSpecification.queue(queueName))) .then(sender.bind(BindingSpecification.binding().queue(queueName).exchange(exchangeName) .routingKey(routingKey))) .then(sender.send(Flux.range(0, nbMessages) .map(i -> new OutboundMessage(exchangeName, routingKey, "".getBytes())))) .subscribe(); resourceSendingSub.dispose(); CountDownLatch latch = new CountDownLatch(nbMessages); AtomicInteger count = new AtomicInteger(); receiver = ReactorRabbitMq.createReceiver(); Disposable receiverSubscription = receiver.consumeNoAck(queueName).subscribe(msg -> { count.incrementAndGet(); latch.countDown(); }); assertTrue(latch.await(1, TimeUnit.SECONDS)); assertEquals(nbMessages, count.get()); receiverSubscription.dispose(); } finally { final Channel channel = connection.createChannel(); channel.exchangeDelete(exchangeName); channel.queueDelete(queueName); channel.close(); } }
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 ww w . j a v a 2 s . c o 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()); }/*from w w w. j av a 2 s. c o m*/ 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.SenderBenchmarkUtils.java
License:Open Source License
public static String declareQueue(Connection connection) throws Exception { String queueName = UUID.randomUUID().toString(); Channel channel = connection.createChannel(); String queue = channel.queueDeclare(queueName, false, false, false, null).getQueue(); channel.close(); return queue; }
From source file:reactor.rabbitmq.SenderBenchmarkUtils.java
License:Open Source License
public static void deleteQueue(Connection connection, String queue) throws Exception { Channel channel = connection.createChannel(); channel.queueDelete(queue);/*from w ww .java 2 s.c o m*/ channel.close(); }
From source file:reactor.rabbitmq.SenderTests.java
License:Open Source License
@BeforeEach public void init() throws Exception { ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.useNio();/* w ww. j a va2s. c om*/ connection = connectionFactory.newConnection(); Channel channel = connection.createChannel(); String queueName = UUID.randomUUID().toString(); queue = channel.queueDeclare(queueName, false, false, false, null).getQueue(); channel.close(); sender = null; }
From source file:reactor.rabbitmq.SenderTests.java
License:Open Source License
@AfterEach public void tearDown() throws Exception { if (connection != null) { Channel channel = connection.createChannel(); channel.queueDelete(queue);// w ww. jav a 2 s.com channel.close(); connection.close(); } if (sender != null) { sender.close(); } }
From source file:ru.kinomir.queue.QueueSender.java
public synchronized void sendToQueue(Object data, String queueName, String queueHost, String userName, String password, String port, String virtualHost) { Channel channel = null; Connection connection = null; try {// w w w .j a v a2 s .c o m logger.info("Send message to queue '" + queueName + "'"); ConnectionFactory factory = new ConnectionFactory(); if (!StringTools.isEmpty(userName)) { factory.setUsername(userName); } if (!StringTools.isEmpty(password)) { factory.setPassword(password); } if (!StringTools.isEmpty(port)) { try { factory.setPort(Integer.parseInt(port)); } catch (NumberFormatException ignore) { } } if (!StringTools.isEmpty(virtualHost)) { factory.setVirtualHost(virtualHost); } factory.setHost(queueHost); connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(queueName, true, false, false, null); String message = convertToString(data); logger.info("Message text: " + message); channel.basicPublish("", queueName, MessageProperties.MINIMAL_PERSISTENT_BASIC, message.getBytes()); logger.info("Message was sent"); } catch (Exception ex) { logger.error("Uneble send message: " + convertToString(data)); logger.debug(ex.getMessage(), ex); } finally { try { if (channel != null) { channel.close(); } if (connection != null) { connection.close(); } } catch (Exception ignore) { } } }
From source file:samples.userguide.RabbitMQAMQPClient.java
License:Apache License
public static void main(String[] args) { String queueName = System.getProperty("queueName"); String mode = System.getProperty("mode"); String routingKey = System.getProperty("routingKey"); String exchangeName = System.getProperty("exchangeName"); String quote = System.getProperty("payLoad"); if (quote == null) { quote = "IBM"; }// w w w. j a va 2 s. c o m String msg = "<m:placeOrder xmlns:m=\"http://services.samples\">\n" + " <m:order>\n" + " <m:price>" + getRandom(100, 0.9, true) + "</m:price>\n" + " <m:quantity>" + (int) getRandom(10000, 1.0, true) + "</m:quantity>\n" + " <m:symbol>" + quote + "</m:symbol>\n" + " </m:order>\n" + "</m:placeOrder>"; ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = null; Channel channel = null; try { connection = factory.newConnection(); channel = connection.createChannel(); if (mode == null) { mode = "producer"; } if ("producer".equals(mode)) { if (queueName != null) { channel.basicPublish("", queueName, null, msg.getBytes()); } else { if (routingKey != null) { if (exchangeName == null) { exchangeName = "topic-exchange"; } channel.basicPublish(exchangeName, routingKey, null, msg.getBytes()); } else { if (exchangeName == null) { exchangeName = "subscriber-exchange"; } channel.basicPublish(exchangeName, "", null, msg.getBytes()); } } } else { if (queueName == null) { queueName = "ConsumerProxy"; } QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println("[x] received '" + message + "'"); } channel.close(); connection.close(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } finally { if (channel != null && channel.isOpen()) { try { channel.close(); } catch (IOException e) { System.err.println("Error occurred while closing the channel:" + e.getMessage()); } } if (connection != null && connection.isOpen()) { try { connection.close(); } catch (IOException e) { System.err.println("Error occurred while closing the connection:" + e.getMessage()); } } } }
From source file:sd_aula06.Send.java
public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("reindeer.rmq.cloudamqp.com"); factory.setUsername("jmodzuaw"); factory.setPassword("Kwuy7kd81ED1fIj9gxEti1J4FTPBj2Jz"); factory.setVirtualHost("jmodzuaw"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); String message = "RafaelReis: VSF!"; channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8")); System.out.println(" [x] Sent '" + message + "'"); channel.close(); connection.close();/*from w ww. j ava2 s . c o m*/ }