List of usage examples for com.rabbitmq.client Channel basicAck
void basicAck(long deliveryTag, boolean multiple) throws IOException;
From source file:net.lshift.accent.AccentConsumer.java
License:Apache License
/** * Acknowledge a message with the given id. * * @param deliveryTag/*from www . j ava 2 s . co m*/ * @param batch * @throws StaleMessageException if the message could not be acked (for example, if the channel had been reset in the meantime). */ public void reliableAck(final long deliveryTag, final boolean batch) { final Long channelDeliveryTag = outstandingAcks.get(deliveryTag); if (channelDeliveryTag != null) { owner.executeIfChannelValid(new ChannelCallback() { public void runWithChannel(Channel c) throws IOException { c.basicAck(channelDeliveryTag, batch); outstandingAcks.remove(deliveryTag); } }); } else { throw new StaleMessageException(deliveryTag); } }
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 w w w . j a v a2 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()); 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);/*from w w w . j a va2 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("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);//from ww w . ja v a2s . 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);/*from ww w . j a v a2 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 public void preDeleteHappyCase() throws Exception { Map<String, String> kvs = configureHBase(primaryTableNameString, secondaryIdxTableNameString, "e", "eg", "a", amq_default_address, primaryTableNameString, "true", ""); setupHBase(kvs);/*from w ww.j a va2s.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; } }
From source file:net.nzcorp.hbase.tableevent_signaler.TableEventSignalerTest.java
License:Apache License
@Test public void discernNewPutFromUpdate() throws Exception { Map<String, String> kvs = configureHBase(primaryTableNameString, secondaryIdxTableNameString, "e", "eg", "a", amq_default_address, primaryTableNameString, "true", ""); setupHBase(kvs);/*from w w w .j a v a2s .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); /* The first put should be registered as a "put" action, while the second should be registered as an "update" action, thereby signalling different action to be taken by the consumers */ Put tablePut = new Put("EFG1".getBytes()); tablePut.addColumn("e".getBytes(), "some_key".getBytes(), "some_value".getBytes()); primaryTable.put(tablePut); tablePut = new Put("EFG1".getBytes()); tablePut.addColumn("e".getBytes(), "some_other_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(); int msgs_to_consume = 2; while (msgs_to_consume > 0) { System.out.println(String.format("Messages to get: %s", msgs_to_consume)); 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(); 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_qualifier"); if (headers.get("action").toString().equals("update")) { Assert.assertEquals("Column value should be preserved in the message body", "some_other_key", column_value); } else { Assert.assertEquals("Column value should be preserved in the message body", "some_key", column_value); } long deliveryTag = response.getEnvelope().getDeliveryTag(); channel.basicAck(deliveryTag, false); msgs_to_consume--; } }
From source file:org.apache.airavata.gfac.monitor.impl.push.amqp.SimpleJobFinishConsumer.java
License:Apache License
public void listen() { try {/*ww w . j a va 2 s . co m*/ String queueName = ServerSettings.getSetting(Constants.GFAC_SERVER_PORT, "8950"); String uri = "amqp://localhost"; ConnectionFactory connFactory = new ConnectionFactory(); connFactory.setUri(uri); Connection conn = connFactory.newConnection(); logger.info("--------Created the connection to Rabbitmq server successfully-------"); final Channel ch = conn.createChannel(); logger.info("--------Created the channel with Rabbitmq server successfully-------"); ch.queueDeclare(queueName, false, false, false, null); logger.info("--------Declare the queue " + queueName + " in Rabbitmq server successfully-------"); final QueueingConsumer consumer = new QueueingConsumer(ch); ch.basicConsume(queueName, consumer); (new Thread() { public void run() { try { while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); logger.info( "---------------- Job Finish message received:" + message + " --------------"); synchronized (completedJobsFromPush) { completedJobsFromPush.add(message); } ch.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } } catch (Exception ex) { logger.error("--------Cannot connect to a RabbitMQ Server--------", ex); } } }).start(); } catch (Exception ex) { logger.error("Cannot connect to a RabbitMQ Server: ", ex); logger.info("------------- Push monitoring for HPC jobs is disabled -------------"); } }
From source file:org.apache.axis2.transport.amqp.out.AMQPSender.java
License:Apache License
/** * Create a Consumer for the reply destination and wait for the response AMQP * message synchronously. If a message arrives within the specified time * interval, process it through Axis2// w w w. j av a2s . c o m * * @param chan * the session to use to listen for the response * @param replyDestination * the AMQP reply Destination * @param msgCtx * the outgoing message for which we are expecting the response * @param contentTypeProperty * the message property used to determine the content type of the * response message * @throws AxisFault * on error */ private void waitForResponseAndProcess(Channel chan, Destination replyDestination, MessageContext msgCtx, String correlationId, String contentTypeProperty) throws AxisFault { byte[] body = null; AMQP.BasicProperties props = null; long deliveryTag = 0; QueueingConsumer qi = new QueueingConsumer(chan); QueueingConsumer.Delivery delivery = null; String waitReply = null; long timeout = AMQPConstants.DEFAULT_AMQP_TIMEOUT; waitReply = (String) msgCtx.getProperty(AMQPConstants.AMQP_WAIT_REPLY); if (waitReply != null) timeout = Long.valueOf(waitReply).longValue(); log.debug("Waiting for a maximum of " + timeout + "ms for a response message to destination : " + replyDestination + " with AMQP correlation ID : " + correlationId); try { delivery = qi.nextDelivery(timeout); if (delivery == null) { // shut down your consumer here - no events arrived // before the timeout was reached log.warn("Did not receive a AMQP response within " + timeout + " ms to destination : " + replyDestination + " with AMQP correlation ID : " + correlationId); metrics.incrementTimeoutsReceiving(); return; } else { props = delivery.getProperties(); body = delivery.getBody(); deliveryTag = delivery.getEnvelope().getDeliveryTag(); AMQPMessage reply = new AMQPMessage("" + deliveryTag, delivery.getEnvelope(), props, body); if (correlationId.equals(props.getCorrelationId())) { chan.basicAck(deliveryTag, false); // acknowledge receipt of the message metrics.incrementMessagesReceived(); metrics.incrementBytesReceived(props.getBodySize()); processSyncResponse(msgCtx, reply, contentTypeProperty); metrics.incrementMessagesReceived(); } } } catch (AxisFault e) { metrics.incrementFaultsReceiving(); throw e; } catch (IOException e) { log.error("Exception while acking message: " + e); } catch (ShutdownSignalException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ConsumerCancelledException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:org.apache.axis2.transport.rabbitmq.RabbitMQSender.java
License:Open Source License
private void processResponse(RabbitMQConnectionFactory factory, MessageContext msgContext, String correlationID, String replyTo, Hashtable<String, String> eprProperties) throws IOException { Connection connection = factory.createConnection(); if (!RabbitMQUtils.isQueueAvailable(connection, replyTo)) { log.info("Reply-to queue : " + replyTo + " not available, hence creating a new one"); RabbitMQUtils.declareQueue(connection, replyTo, eprProperties); }/*from w w w .j a v a2 s . c o m*/ Channel channel = connection.createChannel(); QueueingConsumer consumer = new QueueingConsumer(channel); QueueingConsumer.Delivery delivery = null; RabbitMQMessage message = new RabbitMQMessage(); boolean responseFound = false; int timeout = RabbitMQConstants.DEFAULT_REPLY_TO_TIMEOUT; String timeoutStr = eprProperties.get(RabbitMQConstants.REPLY_TO_TIMEOUT); if (!StringUtils.isEmpty(timeoutStr)) { try { timeout = Integer.parseInt(timeoutStr); } catch (NumberFormatException e) { log.warn( "Number format error in reading replyto timeout value. Proceeding with default value (30000ms)", e); } } //start consuming without acknowledging String consumerTag = channel.basicConsume(replyTo, false, consumer); try { while (!responseFound) { log.debug("Waiting for next delivery from reply to queue " + replyTo); delivery = consumer.nextDelivery(timeout); if (delivery != null) { if (delivery.getProperties().getCorrelationId().equals(correlationID)) { responseFound = true; log.debug( "Found matching response with correlation ID : " + correlationID + ". Sending ack"); //acknowledge correct message channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } else { //not acknowledge wrong messages and re-queue log.debug("Found messages with wrong correlation ID. Re-queueing and sending nack"); channel.basicNack(delivery.getEnvelope().getDeliveryTag(), false, true); } } } } catch (ShutdownSignalException e) { log.error("Error receiving message from RabbitMQ broker " + e.getLocalizedMessage()); } catch (InterruptedException e) { log.error("Error receiving message from RabbitMQ broker " + e.getLocalizedMessage()); } catch (ConsumerCancelledException e) { log.error("Error receiving message from RabbitMQ broker" + e.getLocalizedMessage()); } finally { if (channel != null || channel.isOpen()) { //stop consuming channel.basicCancel(consumerTag); } } if (delivery != null) { log.debug("Processing response from reply-to queue"); AMQP.BasicProperties properties = delivery.getProperties(); Map<String, Object> headers = properties.getHeaders(); message.setBody(delivery.getBody()); message.setDeliveryTag(delivery.getEnvelope().getDeliveryTag()); message.setReplyTo(properties.getReplyTo()); message.setMessageId(properties.getMessageId()); //get content type from message String contentType = properties.getContentType(); if (contentType == null) { //if not get content type from transport parameter contentType = eprProperties.get(RabbitMQConstants.REPLY_TO_CONTENT_TYPE); if (contentType == null) { //if none is given, set to default content type log.warn("Setting default content type " + RabbitMQConstants.DEFAULT_CONTENT_TYPE); contentType = RabbitMQConstants.DEFAULT_CONTENT_TYPE; } } message.setContentType(contentType); message.setContentEncoding(properties.getContentEncoding()); message.setCorrelationId(properties.getCorrelationId()); if (headers != null) { message.setHeaders(headers); if (headers.get(RabbitMQConstants.SOAP_ACTION) != null) { message.setSoapAction(headers.get(RabbitMQConstants.SOAP_ACTION).toString()); } } MessageContext responseMsgCtx = createResponseMessageContext(msgContext); RabbitMQUtils.setSOAPEnvelope(message, responseMsgCtx, contentType); handleIncomingMessage(responseMsgCtx, RabbitMQUtils.getTransportHeaders(message), message.getSoapAction(), message.getContentType()); } }