List of usage examples for com.rabbitmq.client Connection createChannel
Channel createChannel() throws IOException;
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);/* www . ja va2s .c om*/ } 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 ww w. 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);/*from w w w . j av a 2 s . c o m*/ 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 a va 2 s. c om*/ 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);/*w w w. ja v a 2 s . com*/ channel.basicPublish(EXCHANGE_NAME, severity, null, message.getBytes()); System.out.println(" [x] Sent '" + severity + "': '" + message + "'"); } channel.close(); connection.close(); }
From source file:org.apache.niolex.rabbit.log.EmitLogTopic.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.exchangeDeclare(EXCHANGE_NAME, "topic"); for (int i = 0; i < 100; ++i) { String message = String.format("%02d %s", i, MockUtil.randString(5)); ThreadUtil.sleepAtLeast(1000);/*from ww w. java 2 s . co m*/ String routingKey = getSeverity() + "." + getFacility(); channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes()); System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'"); } channel.close(); connection.close(); }
From source file:org.apache.niolex.rabbit.rpc.RPCServer.java
License:Apache License
public static void main(String[] argv) { Connection connection = null; Channel channel = null;//from w ww. j ava 2s.c om try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null); channel.basicQos(1); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(RPC_QUEUE_NAME, false, consumer); System.out.println(" [x] Awaiting RPC requests"); while (true) { String response = null; QueueingConsumer.Delivery delivery = consumer.nextDelivery(); BasicProperties props = delivery.getProperties(); BasicProperties replyProps = new BasicProperties.Builder().correlationId(props.getCorrelationId()) .build(); try { String message = new String(delivery.getBody(), "UTF-8"); int n = Integer.parseInt(message); System.out.println(" [.] fib(" + message + ")"); response = "" + fib(n); } catch (Exception e) { System.out.println(" [.] " + e.toString()); response = ""; } finally { channel.basicPublish("", props.getReplyTo(), replyProps, response.getBytes("UTF-8")); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } } } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { try { connection.close(); } catch (Exception ignore) { } } } }
From source file:org.apache.nutch.indexer.history.HistoryIndexer.java
License:Apache License
private void sendEvent(final String message) { ConnectionFactory factory = new ConnectionFactory(); final String host = conf.get("history.rabbitmq.host", "localhost"); factory.setHost(host);/*w ww .j av a 2s . c o m*/ final String user = conf.get("history.rabbitmq.user"); factory.setUsername(user); final String password = conf.get("history.rabbitmq.password"); factory.setPassword(password); Connection connection = null; Channel channel = null; try { connection = factory.newConnection(); channel = connection.createChannel(); } catch (IOException | TimeoutException e) { final String errMsg = "failed opening connection or channel"; LOG.error(errMsg, e); closeChannelAndConnection(channel, true); throw new RuntimeException(errMsg, e); } final String queueNamesStr = conf.get("history.rabbitmq.queue.names", "nlp,langdetector"); final String[] queueNames = queueNamesStr.split(","); for (final String singleQueueName : queueNames) { try { channel.queueDeclare(singleQueueName, true, false, false, null); channel.basicPublish("", singleQueueName, null, message.getBytes()); LOG.debug(" [x] Sent '" + message + "'"); } catch (IOException e) { final String errMsg = String.format("failed sending message [%s] to queue [%s]", message, singleQueueName); LOG.error(errMsg, e); } } closeChannelAndConnection(channel, false); }
From source file:org.apache.nutch.publisher.rabbitmq.RabbitMQPublisherImpl.java
License:Apache License
@Override public boolean setConfig(Configuration conf) { try {/* ww w. jav a 2s .c om*/ EXCHANGE_SERVER = conf.get("rabbitmq.exchange.server", "fetcher_log"); EXCHANGE_TYPE = conf.get("rabbitmq.exchange.type", "fanout"); HOST = conf.get("rabbitmq.host", "localhost"); PORT = conf.getInt("rabbitmq.port", 5672); VIRTUAL_HOST = conf.get("rabbitmq.virtualhost", null); USERNAME = conf.get("rabbitmq.username", null); PASSWORD = conf.get("rabbitmq.password", null); QUEUE_NAME = conf.get("rabbitmq.queue.name", "fanout.queue"); QUEUE_DURABLE = conf.getBoolean("rabbitmq.queue.durable", true); QUEUE_ROUTING_KEY = conf.get("rabbitmq.queue.routingkey", "fanout.key"); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(HOST); factory.setPort(PORT); if (VIRTUAL_HOST != null) { factory.setVirtualHost(VIRTUAL_HOST); } if (USERNAME != null) { factory.setUsername(USERNAME); factory.setPassword(PASSWORD); } Connection connection = factory.newConnection(); channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_SERVER, EXCHANGE_TYPE); channel.queueDeclare(QUEUE_NAME, QUEUE_DURABLE, false, false, null); channel.queueBind(QUEUE_NAME, EXCHANGE_SERVER, QUEUE_ROUTING_KEY); LOG.info("Configured RabbitMQ publisher"); return true; } catch (Exception e) { LOG.error("Could not initialize RabbitMQ publisher - {}", StringUtils.stringifyException(e)); return false; } }
From source file:org.apache.synapse.tranport.amqp.AMQPTwoWayProducerClient.java
License:Apache License
public static void main(String[] args) throws IOException, InterruptedException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); AMQPTwoWayProducerClient.produceAndConsume(MESSAGE2, channel, "consumer", "consumerReply"); channel.close();/*from w ww.j a va2s . c om*/ connection.close(); }