List of usage examples for com.rabbitmq.client Connection close
@Override void close() throws IOException;
From source file:org.apache.flume.RabbitMQUtil.java
License:Apache License
public static void close(Connection connection, com.rabbitmq.client.Channel channel) { if (null != channel) { try {//from w ww. j av a 2 s .co m channel.close(); } catch (Exception ex) { if (log.isErrorEnabled()) log.error("Exception thrown while closing channel", ex); } } if (null != connection) { try { connection.close(); } catch (Exception ex) { if (log.isErrorEnabled()) log.error("Exception thrown while closing connection", ex); } } }
From source file:org.apache.helix.recipes.rabbitmq.ConsumerThread.java
License:Apache License
@Override public void run() { Connection connection = null; try {/*from w w w .j a va2s. com*/ ConnectionFactory factory = new ConnectionFactory(); factory.setHost(_mqServer); connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "topic"); String queueName = channel.queueDeclare().getQueue(); String bindingKey = _partition.toString(); channel.queueBind(queueName, EXCHANGE_NAME, bindingKey); System.out.println( " [*] " + _consumerId + " Waiting for messages on " + bindingKey + ". To exit press CTRL+C"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); String routingKey = delivery.getEnvelope().getRoutingKey(); System.out.println(" [x] " + _consumerId + " Received '" + routingKey + "':'" + message + "'"); } } catch (InterruptedException e) { System.err.println(" [-] " + _consumerId + " on " + _partition + " is interrupted ..."); } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { try { connection.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
From source file:org.apache.helix.recipes.rabbitmq.Emitter.java
License:Apache License
public static void main(String[] args) throws Exception { if (args.length < 1) { System.err.println("USAGE: java Emitter rabbitmqServer (e.g. localhost) numberOfMessage (optional)"); System.exit(1);/* ww w . j av a 2 s . co m*/ } final String mqServer = args[0]; // "zzhang-ld"; int count = Integer.MAX_VALUE; if (args.length > 1) { try { count = Integer.parseInt(args[1]); } catch (Exception e) { // TODO: handle exception } } System.out.println("Sending " + count + " messages with random topic id"); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(mqServer); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "topic"); for (int i = 0; i < count; i++) { int rand = ((int) (Math.random() * 10000) % SetupConsumerCluster.DEFAULT_PARTITION_NUMBER); String routingKey = "topic_" + rand; String message = "message_" + rand; channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes()); System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'"); Thread.sleep(1000); } connection.close(); }
From source file:org.apache.james.transport.mailets.AmqpForwardAttribute.java
License:Apache License
private void sendContent(Map<String, byte[]> content) throws IOException, TimeoutException { Connection connection = null; Channel channel = null;//from w w w . j ava 2s . c om try { connection = connectionFactory.newConnection(); channel = connection.createChannel(); channel.exchangeDeclarePassive(exchange); sendContentOnChannel(channel, content); } finally { if (channel != null) { channel.close(); } if (connection != null) { connection.close(); } } }
From source file:org.apache.nifi.amqp.processors.AMQPPublisherTest.java
License:Apache License
@Test(expected = IllegalStateException.class) public void failPublishIfChannelClosed() throws Exception { Connection conn = new TestConnection(null, null); try (AMQPPublisher sender = new AMQPPublisher(conn, mock(ComponentLog.class))) { conn.close(); sender.publish("oleg".getBytes(), null, "foo", ""); }//from w w w . j a v a 2 s . c om }
From source file:org.apache.nifi.amqp.processors.AMQPPublisherTest.java
License:Apache License
@Test public void validateSuccessfullPublishingAndRouting() throws Exception { Map<String, List<String>> routingMap = new HashMap<>(); routingMap.put("key1", Arrays.asList("queue1", "queue2")); Map<String, String> exchangeToRoutingKeymap = new HashMap<>(); exchangeToRoutingKeymap.put("myExchange", "key1"); Connection connection = new TestConnection(exchangeToRoutingKeymap, routingMap); try (AMQPPublisher sender = new AMQPPublisher(connection, mock(ComponentLog.class))) { sender.publish("hello".getBytes(), null, "key1", "myExchange"); Thread.sleep(200);//from www . ja v a 2 s. c o m } assertNotNull(connection.createChannel().basicGet("queue1", true)); assertNotNull(connection.createChannel().basicGet("queue2", true)); connection.close(); }
From source file:org.apache.nifi.amqp.processors.AMQPPublisherTest.java
License:Apache License
@Test public void validateSuccessfullPublishingAndUndeliverableRoutingKey() throws Exception { Map<String, List<String>> routingMap = new HashMap<>(); routingMap.put("key1", Arrays.asList("queue1", "queue2")); Map<String, String> exchangeToRoutingKeymap = new HashMap<>(); exchangeToRoutingKeymap.put("myExchange", "key1"); Connection connection = new TestConnection(exchangeToRoutingKeymap, routingMap); ReturnListener retListener = mock(ReturnListener.class); connection.createChannel().addReturnListener(retListener); try (AMQPPublisher sender = new AMQPPublisher(connection, new MockComponentLog("foo", ""))) { sender.publish("hello".getBytes(), null, "key1", "myExchange"); Thread.sleep(1000);/*from www. j a v a2 s . c o m*/ } Thread.sleep(200); verify(retListener, atMost(1)).handleReturn(Mockito.anyInt(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(BasicProperties.class), (byte[]) Mockito.any()); connection.close(); }
From source file:org.apache.niolex.rabbit.basic.Send.java
License:Apache License
public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); for (int i = 0; i < 100; ++i) { String message = String.format("%02d %s", i, MockUtil.randString(5)); ThreadUtil.sleepAtLeast(1000);/* ww w. ja v a 2 s . com*/ channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8")); System.out.println(" [x] Sent '" + message + "'"); } channel.close(); connection.close(); }
From source file:org.apache.niolex.rabbit.log.EmitLog.java
License:Apache License
public static void main(String[] argv) throws java.io.IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "fanout"); for (int i = 0; i < 100; ++i) { String message = String.format("%02d %s", i, MockUtil.randString(5)); ThreadUtil.sleepAtLeast(1000);//from w ww .j av a 2 s . c o m channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); } channel.close(); connection.close(); }
From source file:org.apache.niolex.rabbit.log.EmitLogDirect.java
License:Apache License
public static void main(String[] argv) throws java.io.IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "direct"); for (int i = 0; i < 100; ++i) { String message = String.format("%02d %s", i, MockUtil.randString(5)); String severity = MockUtil.randInt(3) == 0 ? "error" : "info"; ThreadUtil.sleepAtLeast(1000);//from w w w .j a v a2 s . co m channel.basicPublish(EXCHANGE_NAME, severity, null, message.getBytes()); System.out.println(" [x] Sent '" + severity + "': '" + message + "'"); } channel.close(); connection.close(); }