List of usage examples for com.rabbitmq.client ConnectionFactory setUri
public void setUri(String uriString) throws URISyntaxException, NoSuchAlgorithmException, KeyManagementException
From source file:kieker.analysis.plugin.reader.amqp.AMQPReader.java
License:Apache License
private Connection createConnection() throws IOException, TimeoutException, KeyManagementException, NoSuchAlgorithmException, URISyntaxException { final ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setUri(this.uri); connectionFactory.setRequestedHeartbeat(this.heartbeat); return connectionFactory.newConnection(); }
From source file:kieker.monitoring.writer.amqp.AmqpWriter.java
License:Apache License
private Connection createConnection() throws KeyManagementException, NoSuchAlgorithmException, URISyntaxException, IOException, TimeoutException { final ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setUri(this.uri); connectionFactory.setRequestedHeartbeat(this.heartbeat); // Use only daemon threads for connections. Otherwise, all connections would have to be explicitly // closed for the JVM to terminate. connectionFactory.setThreadFactory(new DaemonThreadFactory()); return connectionFactory.newConnection(); }
From source file:localdomain.localhost.RabbitMQClient.java
License:Apache License
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try {/*from w w w .j a va 2s . c o m*/ ConnectionFactory factory = new ConnectionFactory(); String messages = new String(); String uri = System.getProperty("CLOUDAMQP_URL"); factory.setUri(uri); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, true, false, false, null); QueueingConsumer consumer = new QueueingConsumer(channel); boolean autoACK = false; channel.basicConsume(QUEUE_NAME, autoACK, consumer); System.out.println(" [*] Waiting 100ms for a message"); QueueingConsumer.Delivery delivery = consumer.nextDelivery(100); while (delivery != null) { String message = new String(delivery.getBody()); System.out.println(" [x] Received '" + message + "'"); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); messages = message + " <br/> " + messages; delivery = consumer.nextDelivery(100); } request.setAttribute("messages", messages); request.getRequestDispatcher("/index.jsp").forward(request, response); channel.close(); connection.close(); } catch (Exception e) { request.setAttribute("throwable", e); request.getRequestDispatcher("/index.jsp").forward(request, response); } }
From source file:localdomain.localhost.RabbitMQServer.java
License:Apache License
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try {//w w w . j av a 2s . co m String message = request.getParameter("message"); ConnectionFactory factory = new ConnectionFactory(); String uri = System.getProperty("CLOUDAMQP_URL"); factory.setUri(uri); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); boolean durable = true; channel.queueDeclare(QUEUE_NAME, durable, false, false, null); channel.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); channel.close(); connection.close(); response.sendRedirect(request.getContextPath() + "/index.jsp"); } catch (Exception e) { request.setAttribute("throwable", e); request.getRequestDispatcher("/index.jsp").forward(request, response); } }
From source file:net.nzcorp.hbase.tableevent_signaler.TableEventSignalerTest.java
License:Apache License
@Test public void prePutHappyCase() throws Exception { Map<String, String> kvs = configureHBase(primaryTableNameString, secondaryIdxTableNameString, "e", "eg", "a", amq_default_address, primaryTableNameString, "true", ""); setupHBase(kvs);/*from ww w. ja v a 2s.c o m*/ //simulate population of secondary index as a result of the above Put idxPut = new Put("EFG1".getBytes()); idxPut.addColumn("a".getBytes(), "EFB1".getBytes(), "".getBytes()); secondaryIdxTable.put(idxPut); // Add a column to the primary table, which should trigger a data ripple to the downstream table Put tablePut = new Put("EFG1".getBytes()); tablePut.addColumn("e".getBytes(), "some_key".getBytes(), "some_value".getBytes()); primaryTable.put(tablePut); //check that values made it to the queue com.rabbitmq.client.ConnectionFactory factory = new com.rabbitmq.client.ConnectionFactory(); factory.setUri(amq_default_address); com.rabbitmq.client.Connection conn = factory.newConnection(); com.rabbitmq.client.Channel channel = conn.createChannel(); System.out.println(String.format("Test: connecting to %s", primaryTableNameString)); while (true) { GetResponse response = channel.basicGet(primaryTableNameString, false); if (response == null)//busy-wait until the message has made it through the MQ { continue; } String routingKey = response.getEnvelope().getRoutingKey(); Assert.assertEquals("Routing key should be rowkey", "genome", routingKey); String contentType = response.getProps().getContentType(); Assert.assertEquals("Content type should be preserved", "application/json", contentType); Map<String, Object> headers = response.getProps().getHeaders(); Assert.assertEquals("An action should be set on the message", "put", headers.get("action").toString()); byte[] body = response.getBody(); JSONObject jo = new JSONObject(new String(body)); String column_family = (String) jo.get("column_family"); Assert.assertEquals("Column family should be preserved in the message body", "eg", column_family); String column_value = (String) jo.get("column_value"); Assert.assertEquals("Column value should be preserved in the message body", "some_value", column_value); long deliveryTag = response.getEnvelope().getDeliveryTag(); channel.basicAck(deliveryTag, false); break; } }
From source file:net.nzcorp.hbase.tableevent_signaler.TableEventSignalerTest.java
License:Apache License
@Test public void verifyValueNotSentByDefault() throws Exception { Map<String, String> kvs = configureHBase(primaryTableNameString, secondaryIdxTableNameString, "e", "eg", "a", amq_default_address, primaryTableNameString, "false", ""); setupHBase(kvs);/*w w w. j a va2s .co m*/ //simulate population of secondary index as a result of the above Put idxPut = new Put("EFG1".getBytes()); idxPut.addColumn("a".getBytes(), "EFB1".getBytes(), "".getBytes()); secondaryIdxTable.put(idxPut); // Add a column to the primary table, which should trigger a data ripple to the downstream table Put tablePut = new Put("EFG1".getBytes()); tablePut.addColumn("e".getBytes(), "some_key".getBytes(), "some_value".getBytes()); primaryTable.put(tablePut); //check that values made it to the queue com.rabbitmq.client.ConnectionFactory factory = new com.rabbitmq.client.ConnectionFactory(); factory.setUri(amq_default_address); com.rabbitmq.client.Connection conn = factory.newConnection(); com.rabbitmq.client.Channel channel = conn.createChannel(); System.out.println(String.format("Test: connecting to %s", primaryTableNameString)); while (true) { GetResponse response = channel.basicGet(primaryTableNameString, false); if (response == null)//busy-wait until the message has made it through the MQ { continue; } String routingKey = response.getEnvelope().getRoutingKey(); Assert.assertEquals("Routing key should be rowkey", "genome", routingKey); String contentType = response.getProps().getContentType(); Assert.assertEquals("Content type should be preserved", "application/json", contentType); Map<String, Object> headers = response.getProps().getHeaders(); Assert.assertEquals("An action should be set on the message", "put", headers.get("action").toString()); byte[] body = response.getBody(); JSONObject jo = new JSONObject(new String(body)); String column_family = (String) jo.get("column_family"); Assert.assertEquals("Column family should be preserved in the message body", "eg", column_family); String column_value = (String) jo.get("column_value"); Assert.assertEquals("Column value is not sent by default", "", column_value); long deliveryTag = response.getEnvelope().getDeliveryTag(); channel.basicAck(deliveryTag, false); break; } }
From source file:net.nzcorp.hbase.tableevent_signaler.TableEventSignalerTest.java
License:Apache License
@Test public void filterOnQualifiers() throws Exception { Map<String, String> kvs = configureHBase(primaryTableNameString, secondaryIdxTableNameString, "e", "eg", "a", amq_default_address, primaryTableNameString, "false", "one_key|some_key"); setupHBase(kvs);// w w w . j av a 2 s .c o m //simulate population of secondary index as a result of the above Put idxPut = new Put("EFG1".getBytes()); idxPut.addColumn("a".getBytes(), "EFB1".getBytes(), "".getBytes()); secondaryIdxTable.put(idxPut); // Add a column to the primary table, which should trigger a data ripple to the downstream table Put tablePut = new Put("EFG1".getBytes()); tablePut.addColumn("e".getBytes(), "some_key".getBytes(), "some_value".getBytes()); primaryTable.put(tablePut); //check that values made it to the queue com.rabbitmq.client.ConnectionFactory factory = new com.rabbitmq.client.ConnectionFactory(); factory.setUri(amq_default_address); com.rabbitmq.client.Connection conn = factory.newConnection(); com.rabbitmq.client.Channel channel = conn.createChannel(); System.out.println(String.format("Test: connecting to %s", primaryTableNameString)); while (true) { GetResponse response = channel.basicGet(primaryTableNameString, false); if (response == null)//busy-wait until the message has made it through the MQ { continue; } String routingKey = response.getEnvelope().getRoutingKey(); Assert.assertEquals("Routing key should be rowkey", "genome", routingKey); String contentType = response.getProps().getContentType(); Assert.assertEquals("Content type should be preserved", "application/json", contentType); Map<String, Object> headers = response.getProps().getHeaders(); Assert.assertEquals("An action should be set on the message", "put", headers.get("action").toString()); byte[] body = response.getBody(); JSONObject jo = new JSONObject(new String(body)); String column_family = (String) jo.get("column_family"); Assert.assertEquals("Column family should be preserved in the message body", "eg", column_family); String column_value = (String) jo.get("column_value"); Assert.assertEquals("Column value is not sent by default", "", column_value); long deliveryTag = response.getEnvelope().getDeliveryTag(); channel.basicAck(deliveryTag, false); break; } }
From source file:net.nzcorp.hbase.tableevent_signaler.TableEventSignalerTest.java
License:Apache License
@Test public void onlyPutWhenInQualifierFilter() throws Exception { Map<String, String> kvs = configureHBase(primaryTableNameString, secondaryIdxTableNameString, "e", "eg", "a", amq_default_address, primaryTableNameString, "false", "some_key"); setupHBase(kvs);/*ww w . j a v a 2 s . c om*/ //simulate population of secondary index as a result of the above Put idxPut = new Put("EFG1".getBytes()); idxPut.addColumn("a".getBytes(), "EFB1".getBytes(), "".getBytes()); secondaryIdxTable.put(idxPut); // Add a column to the primary table, which should trigger a data ripple to the downstream table Put tablePut = new Put("EFG1".getBytes()); tablePut.addColumn("e".getBytes(), "some_key".getBytes(), "some_value".getBytes()); tablePut.addColumn("e".getBytes(), "other_key".getBytes(), "some_value".getBytes()); tablePut.addColumn("e".getBytes(), "third_key".getBytes(), "some_value".getBytes()); primaryTable.put(tablePut); //check that values made it to the queue com.rabbitmq.client.ConnectionFactory factory = new com.rabbitmq.client.ConnectionFactory(); factory.setUri(amq_default_address); com.rabbitmq.client.Connection conn = factory.newConnection(); com.rabbitmq.client.Channel channel = conn.createChannel(); System.out.println(String.format("Test: connecting to %s", primaryTableNameString)); while (true) { int numMessages = channel.queueDeclarePassive(primaryTableNameString).getMessageCount(); GetResponse response = channel.basicGet(primaryTableNameString, false); if (response == null || numMessages == 0)//busy-wait until the message has made it through the MQ { continue; } Assert.assertEquals("There should be only a single key in the queue", 1, numMessages); String routingKey = response.getEnvelope().getRoutingKey(); Assert.assertEquals("Routing key should be rowkey", "genome", routingKey); String contentType = response.getProps().getContentType(); Assert.assertEquals("Content type should be preserved", "application/json", contentType); Map<String, Object> headers = response.getProps().getHeaders(); Assert.assertEquals("An action should be set on the message", "put", headers.get("action").toString()); byte[] body = response.getBody(); JSONObject jo = new JSONObject(new String(body)); String column_family = (String) jo.get("column_family"); Assert.assertEquals("Column family should be preserved in the message body", "eg", column_family); String column_value = (String) jo.get("column_value"); Assert.assertEquals("Column value is not sent by default", "", column_value); long deliveryTag = response.getEnvelope().getDeliveryTag(); channel.basicAck(deliveryTag, false); break; } }
From source file:net.nzcorp.hbase.tableevent_signaler.TableEventSignalerTest.java
License:Apache License
@Test(expected = IOException.class) public void filterOnQualifiersThatDoesNotExistSilencesEvents() throws Exception { Map<String, String> kvs = configureHBase(primaryTableNameString, secondaryIdxTableNameString, "e", "eg", "a", amq_default_address, primaryTableNameString, "false", "one|some"); setupHBase(kvs);/*from w w w . j a v a 2 s.co m*/ //simulate population of secondary index as a result of the above Put idxPut = new Put("EFG1".getBytes()); idxPut.addColumn("a".getBytes(), "EFB1".getBytes(), "".getBytes()); secondaryIdxTable.put(idxPut); // Add a column to the primary table, which should trigger a data ripple to the downstream table Put tablePut = new Put("EFG1".getBytes()); tablePut.addColumn("e".getBytes(), "some_key".getBytes(), "some_value".getBytes()); primaryTable.put(tablePut); //check that values made it to the queue com.rabbitmq.client.ConnectionFactory factory = new com.rabbitmq.client.ConnectionFactory(); factory.setUri(amq_default_address); com.rabbitmq.client.Connection conn = factory.newConnection(); com.rabbitmq.client.Channel channel = conn.createChannel(); System.out.println(String.format("is ok? %s", channel.queueDeclarePassive(primaryTableNameString))); }
From source file:net.nzcorp.hbase.tableevent_signaler.TableEventSignalerTest.java
License:Apache License
@Test public void preDeleteHappyCase() throws Exception { Map<String, String> kvs = configureHBase(primaryTableNameString, secondaryIdxTableNameString, "e", "eg", "a", amq_default_address, primaryTableNameString, "true", ""); setupHBase(kvs);//from ww w. j a va 2 s .co m com.rabbitmq.client.ConnectionFactory factory = new com.rabbitmq.client.ConnectionFactory(); factory.setUri(amq_default_address); com.rabbitmq.client.Connection conn = factory.newConnection(); com.rabbitmq.client.Channel channel = conn.createChannel(); //simulate population of secondary index for a put on the downstreamTable Put idxPut = new Put("EFG1".getBytes()); idxPut.addColumn("a".getBytes(), "EFB1".getBytes(), "".getBytes()); secondaryIdxTable.put(idxPut); //simulate a data rippling performed by the data_rippler service consuming off of the rabbitmq queue Put tablePut = new Put("EFB1".getBytes()); tablePut.addColumn("eg".getBytes(), "some_key".getBytes(), "some_value".getBytes()); downstreamTable.put(tablePut); // since we made no active put to the queue from the prePut, we need to declare it explicitly here channel.queueDeclare(primaryTableNameString, true, false, false, null); // finished with the setup, we now issue a delete which should be caught by the rabbitmq Delete d = new Delete("EFG1".getBytes()); primaryTable.delete(d); //check that values made it to the queue while (true) { GetResponse response = channel.basicGet(primaryTableNameString, false); if (response == null)//busy-wait until the message has made it through the MQ { continue; } String routingKey = response.getEnvelope().getRoutingKey(); Assert.assertEquals("Routing key should be rowkey", "genome", routingKey); String contentType = response.getProps().getContentType(); Assert.assertEquals("Content type should be preserved", "application/json", contentType); Map<String, Object> headers = response.getProps().getHeaders(); Assert.assertEquals("An action should be set on the message", "delete", headers.get("action").toString()); byte[] body = response.getBody(); JSONObject jo = new JSONObject(new String(body)); String column_qualifier = (String) jo.get("column_qualifier"); Assert.assertEquals("Column qualifier should be empty, signalling a row delete", "", column_qualifier); long deliveryTag = response.getEnvelope().getDeliveryTag(); channel.basicAck(deliveryTag, false); break; } }