Example usage for com.rabbitmq.client Envelope getDeliveryTag

List of usage examples for com.rabbitmq.client Envelope getDeliveryTag

Introduction

In this page you can find the example usage for com.rabbitmq.client Envelope getDeliveryTag.

Prototype

public long getDeliveryTag() 

Source Link

Document

Get the delivery tag included in this parameter envelope

Usage

From source file:org.adslabs.adsfulltext.Callback.java

License:Open Source License

@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
        throws java.io.IOException {

    // Constant strings
    //// w  w  w.j  a va2 s  . co m
    String exchangeName = "FulltextExtractionExchange";
    String routingKey = "WriteMetaFileRoute";

    String contentType = properties.getContentType();
    long deliveryTag = envelope.getDeliveryTag();
    // (process the message components here ...)

    // This line does not work:
    //this.ch.basicPublish(exchangeName, routingKey, null, body);
    System.out.println("Going to ACK");
    this.ch.basicAck(deliveryTag, false);
}

From source file:org.apache.airavata.messaging.core.impl.ExperimentConsumer.java

License:Apache License

@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
        throws IOException {

    Message message = new Message();

    try {/*from www.jav a 2 s.co m*/
        ThriftUtils.createThriftFromBytes(body, message);
        long deliveryTag = envelope.getDeliveryTag();
        if (message.getMessageType() == MessageType.EXPERIMENT
                || message.getMessageType() == MessageType.EXPERIMENT_CANCEL) {
            TBase event = null;
            String gatewayId = null;
            ExperimentSubmitEvent experimentEvent = new ExperimentSubmitEvent();
            ThriftUtils.createThriftFromBytes(message.getEvent(), experimentEvent);
            log.info(" Message Received with message id '" + message.getMessageId()
                    + "' and with message type '" + message.getMessageType() + "'  for experimentId:" + " "
                    + experimentEvent.getExperimentId());
            event = experimentEvent;
            gatewayId = experimentEvent.getGatewayId();
            MessageContext messageContext = new MessageContext(event, message.getMessageType(),
                    message.getMessageId(), gatewayId, deliveryTag);
            messageContext.setUpdatedTime(AiravataUtils.getTime(message.getUpdatedTime()));
            messageContext.setIsRedeliver(envelope.isRedeliver());
            handler.onMessage(messageContext);
        } else {
            log.error("{} message type is not handle in ProcessLaunch Subscriber. Sending ack for "
                    + "delivery tag {} ", message.getMessageType().name(), deliveryTag);
            sendAck(deliveryTag);
        }
    } catch (TException e) {
        String msg = "Failed to de-serialize the thrift message, from routing keys:" + envelope.getRoutingKey();
        log.warn(msg, e);
    }

}

From source file:org.apache.airavata.messaging.core.impl.MessageConsumer.java

License:Apache License

@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
        throws IOException {

    Message message = new Message();

    try {//from w w w.j a  v a  2 s .  c o  m
        logger.info("handleDelivery() -> Handling message delivery. Consumer Tag : " + consumerTag);
        ThriftUtils.createThriftFromBytes(body, message);

        DBEventMessage dBEventMessage = new DBEventMessage();
        ThriftUtils.createThriftFromBytes(message.getEvent(), dBEventMessage);

        MessageContext messageContext = new MessageContext((TBase) dBEventMessage, message.getMessageType(),
                message.getMessageId(), "gatewayId", envelope.getDeliveryTag());
        handler.onMessage(messageContext);
        //sendAck(deliveryTag);

    } catch (TException e) {
        logger.error("handleDelivery() -> Error handling delivery. Consumer Tag : " + consumerTag, e);
    }

}

From source file:org.apache.airavata.messaging.core.impl.ProcessConsumer.java

License:Apache License

@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties basicProperties,
        byte[] body) throws IOException {

    Message message = new Message();

    try {/*  w w  w. j  ava 2 s .  co m*/
        ThriftUtils.createThriftFromBytes(body, message);
        TBase event = null;
        String gatewayId = null;
        long deliveryTag = envelope.getDeliveryTag();
        if (message.getMessageType().equals(MessageType.LAUNCHPROCESS)) {
            ProcessSubmitEvent processSubmitEvent = new ProcessSubmitEvent();
            ThriftUtils.createThriftFromBytes(message.getEvent(), processSubmitEvent);
            log.info(" Message Received with message id '" + message.getMessageId() + " and with message type:"
                    + message.getMessageType() + ", for processId:" + processSubmitEvent.getProcessId()
                    + ", expId:" + processSubmitEvent.getExperimentId());
            event = processSubmitEvent;
            gatewayId = processSubmitEvent.getGatewayId();
            MessageContext messageContext = new MessageContext(event, message.getMessageType(),
                    message.getMessageId(), gatewayId, deliveryTag);
            messageContext.setUpdatedTime(AiravataUtils.getTime(message.getUpdatedTime()));
            messageContext.setIsRedeliver(envelope.isRedeliver());
            handler.onMessage(messageContext);
        } else {
            log.error("{} message type is not handle in ProcessLaunch Subscriber. Sending ack for "
                    + "delivery tag {} ", message.getMessageType().name(), deliveryTag);
            sendAck(deliveryTag);
        }
    } catch (TException e) {
        String msg = "Failed to de-serialize the thrift message, from routing keys:" + envelope.getRoutingKey();
        log.warn(msg, e);
    }

}

From source file:org.apache.airavata.messaging.core.impl.RabbitMQProcessLaunchConsumer.java

License:Apache License

public String listen(final MessageHandler handler) throws AiravataException {
    try {/*from  w  w w .j a v a 2  s. c om*/
        messageHandler = handler;
        Map<String, Object> props = handler.getProperties();
        final Object routing = props.get(MessagingConstants.RABBIT_ROUTING_KEY);
        if (routing == null) {
            throw new IllegalArgumentException("The routing key must be present");
        }
        List<String> keys = new ArrayList<String>();
        if (routing instanceof List) {
            for (Object o : (List) routing) {
                keys.add(o.toString());
            }
        } else if (routing instanceof String) {
            keys.add((String) routing);
        }

        String queueName = (String) props.get(MessagingConstants.RABBIT_QUEUE);
        String consumerTag = (String) props.get(MessagingConstants.RABBIT_CONSUMER_TAG);
        if (queueName == null) {
            if (!channel.isOpen()) {
                channel = connection.createChannel();
                channel.basicQos(prefetchCount);
                //                    channel.exchangeDeclare(taskLaunchExchangeName, "fanout");
            }
            queueName = channel.queueDeclare().getQueue();
        } else {

            channel.queueDeclare(queueName, durableQueue, false, false, null);
        }

        final String id = getId(keys, queueName);
        if (queueDetailsMap.containsKey(id)) {
            throw new IllegalStateException("This subscriber is already defined for this Consumer, "
                    + "cannot define the same subscriber twice");
        }

        if (consumerTag == null) {
            consumerTag = "default";
        }

        // bind all the routing keys
        //            for (String routingKey : keys) {
        //                channel.queueBind(queueName, taskLaunchExchangeName, routingKey);
        //            }
        // autoAck=false, we will ack after task is done
        channel.basicConsume(queueName, false, consumerTag, new QueueingConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                    byte[] body) {
                Message message = new Message();

                try {
                    ThriftUtils.createThriftFromBytes(body, message);
                    TBase event = null;
                    String gatewayId = null;
                    long deliveryTag = envelope.getDeliveryTag();
                    if (message.getMessageType().equals(MessageType.LAUNCHPROCESS)) {
                        ProcessSubmitEvent processSubmitEvent = new ProcessSubmitEvent();
                        ThriftUtils.createThriftFromBytes(message.getEvent(), processSubmitEvent);
                        log.debug(" Message Received with message id '" + message.getMessageId()
                                + "' and with message type '" + message.getMessageType()
                                + "'  for experimentId:" + " " + processSubmitEvent.getProcessId());
                        event = processSubmitEvent;
                        gatewayId = processSubmitEvent.getGatewayId();
                        MessageContext messageContext = new MessageContext(event, message.getMessageType(),
                                message.getMessageId(), gatewayId, deliveryTag);
                        messageContext.setUpdatedTime(AiravataUtils.getTime(message.getUpdatedTime()));
                        messageContext.setIsRedeliver(envelope.isRedeliver());
                        handler.onMessage(messageContext);
                    } else {
                        log.error("{} message type is not handle in ProcessLaunch Consumer. Sending ack for "
                                + "delivery tag {} ", message.getMessageType().name(), deliveryTag);
                        sendAck(deliveryTag);
                    }
                } catch (TException e) {
                    String msg = "Failed to de-serialize the thrift message, from routing keys and queueName "
                            + id;
                    log.warn(msg, e);
                }
            }

            @Override
            public void handleCancel(String consumerTag) throws IOException {
                super.handleCancel(consumerTag);
                log.info("Consumer cancelled : " + consumerTag);
            }
        });

        // save the name for deleting the queue
        queueDetailsMap.put(id, new QueueDetails(queueName, keys));
        return id;
    } catch (Exception e) {
        String msg = "could not open channel for exchange " + taskLaunchExchangeName;
        log.error(msg);
        throw new AiravataException(msg, e);
    }
}

From source file:org.apache.airavata.messaging.core.impl.RabbitMQTaskLaunchConsumer.java

License:Apache License

public String listen(final MessageHandler handler) throws AiravataException {
    try {// w w w .  j  a va 2s . co m
        messageHandler = handler;
        Map<String, Object> props = handler.getProperties();
        final Object routing = props.get(MessagingConstants.RABBIT_ROUTING_KEY);
        if (routing == null) {
            throw new IllegalArgumentException("The routing key must be present");
        }
        List<String> keys = new ArrayList<String>();
        if (routing instanceof List) {
            for (Object o : (List) routing) {
                keys.add(o.toString());
            }
        } else if (routing instanceof String) {
            keys.add((String) routing);
        }

        String queueName = (String) props.get(MessagingConstants.RABBIT_QUEUE);
        String consumerTag = (String) props.get(MessagingConstants.RABBIT_CONSUMER_TAG);
        if (queueName == null) {
            if (!channel.isOpen()) {
                channel = connection.createChannel();
                //                    channel.exchangeDeclare(taskLaunchExchangeName, "fanout");
            }
            queueName = channel.queueDeclare().getQueue();
        } else {

            channel.queueDeclare(queueName, durableQueue, false, false, null);
        }

        final String id = getId(keys, queueName);
        if (queueDetailsMap.containsKey(id)) {
            throw new IllegalStateException("This subscriber is already defined for this Consumer, "
                    + "cannot define the same subscriber twice");
        }

        if (consumerTag == null) {
            consumerTag = "default";
        }

        // bind all the routing keys
        //            for (String routingKey : keys) {
        //                channel.queueBind(queueName, taskLaunchExchangeName, routingKey);
        //            }
        // autoAck=false, we will ack after task is done
        channel.basicConsume(queueName, false, consumerTag, new QueueingConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                    byte[] body) {
                Message message = new Message();

                try {
                    ThriftUtils.createThriftFromBytes(body, message);
                    TBase event = null;
                    String gatewayId = null;
                    long deliveryTag = envelope.getDeliveryTag(); //todo store this in zookeeper, once job is done we can ack
                    if (message.getMessageType().equals(MessageType.LAUNCHTASK)) {
                        TaskSubmitEvent taskSubmitEvent = new TaskSubmitEvent();
                        ThriftUtils.createThriftFromBytes(message.getEvent(), taskSubmitEvent);
                        log.debug(" Message Received with message id '" + message.getMessageId()
                                + "' and with message type '" + message.getMessageType()
                                + "'  for experimentId: " + taskSubmitEvent.getExperimentId() + "and taskId: "
                                + taskSubmitEvent.getTaskId());
                        event = taskSubmitEvent;
                        gatewayId = taskSubmitEvent.getGatewayId();
                    } else if (message.getMessageType().equals(MessageType.TERMINATETASK)) {
                        TaskTerminateEvent taskTerminateEvent = new TaskTerminateEvent();
                        ThriftUtils.createThriftFromBytes(message.getEvent(), taskTerminateEvent);
                        log.debug(" Message Received with message id '" + message.getMessageId()
                                + "' and with message type '" + message.getMessageType()
                                + "'  for experimentId: " + taskTerminateEvent.getExperimentId()
                                + "and taskId: " + taskTerminateEvent.getTaskId());
                        event = taskTerminateEvent;
                        gatewayId = taskTerminateEvent.getGatewayId();
                    }
                    System.out.println("*deliveryTag:" + deliveryTag);
                    MessageContext messageContext = new MessageContext(event, message.getMessageType(),
                            message.getMessageId(), gatewayId, deliveryTag);
                    messageContext.setUpdatedTime(AiravataUtils.getTime(message.getUpdatedTime()));
                    handler.onMessage(messageContext);
                    /*try {
                    channel.basicAck(deliveryTag,false); //todo move this logic to monitoring component to ack when the job is done
                    } catch (IOException e) {
                    logger.error(e.getMessage(), e);
                    }*/
                } catch (TException e) {
                    String msg = "Failed to de-serialize the thrift message, from routing keys and queueName "
                            + id;
                    log.warn(msg, e);
                }
            }
        });
        // save the name for deleting the queue
        queueDetailsMap.put(id, new QueueDetails(queueName, keys));
        return id;
    } catch (Exception e) {
        String msg = "could not open channel for exchange " + taskLaunchExchangeName;
        log.error(msg);
        throw new AiravataException(msg, e);
    }
}

From source file:org.apache.airavata.sga.messaging.service.core.impl.MessageConsumer.java

License:Apache License

@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
        throws IOException {

    Message message = new Message();

    try {//from w  ww  .  j ava 2s .c o m
        logger.info("handleDelivery() -> Handling message delivery. Consumer Tag : " + consumerTag);
        ThriftUtils.createThriftFromBytes(body, message);

        //            TaskContext experimentEvent = new TaskContext();
        //            ThriftUtils.createThriftFromBytes(message.getEvent(), experimentEvent);

        MessageContext messageContext = new MessageContext(message, message.getMessageId(),
                envelope.getDeliveryTag());
        handler.onMessage(messageContext);
        //sendAck(deliveryTag);

    } catch (TException e) {
        logger.error("handleDelivery() -> Error handling delivery. Consumer Tag : " + consumerTag, e);
    }

}

From source file:org.apache.camel.component.rabbitmq.RabbitMQEndpoint.java

License:Apache License

private void setEnvelopeHeaders(Envelope envelope, Message message) {
    message.setHeader(RabbitMQConstants.DELIVERY_TAG, envelope.getDeliveryTag());
    message.setHeader(RabbitMQConstants.EXCHANGE, envelope.getExchange());
    message.setHeader(RabbitMQConstants.ROUTING_KEY, envelope.getRoutingKey());
}

From source file:org.apache.flume.amqp.BaseAmqpTest.java

License:Apache License

protected QueueingConsumer.Delivery createDeliveryWithTag(long deliveryTag) {
    QueueingConsumer.Delivery delivery = mock(QueueingConsumer.Delivery.class);
    when(delivery.getBody()).thenReturn(BODY);

    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put(KEY_1, VALUE_1);/*from  w  w  w.  j  a  v  a 2 s  .  c om*/
    headers.put(KEY_2, VALUE_2);

    AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder().timestamp(MESSAGE_DATE).appId(APP_ID)
            .contentType(CONTENT_TYPE).contentEncoding(CONTENT_ENCODING).correlationId(CORRELATION_ID)
            .messageId(MESSAGE_ID).expiration(EXPIRATION).type(TYPE).userId(USER_ID).headers(headers).build();

    when(delivery.getProperties()).thenReturn(properties);
    Envelope envelope = mock(Envelope.class);
    when(envelope.getDeliveryTag()).thenReturn(deliveryTag);
    when(envelope.getRoutingKey()).thenReturn(ROUTING_KEY);
    when(delivery.getEnvelope()).thenReturn(envelope);

    return delivery;
}

From source file:org.ballerinalang.messaging.rabbitmq.nativeimpl.channel.listener.Start.java

License:Open Source License

/**
 * Receive messages from the RabbitMQ server.
 *
 * @param resource  Ballerina resource function.
 * @param channel   RabbitMQ Channel object.
 * @param queueName Name of the queue messages are consumed from.
 * @param autoAck   True if the server should consider messages acknowledged once delivered;
 *                  false if the server should expect explicit acknowledgements.
 *///  w  w  w.  j a va 2s .c  om
private void receiveMessages(Resource resource, Channel channel, String queueName, boolean autoAck) {
    try {
        channel.basicConsume(queueName, autoAck, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                    byte[] body) throws IOException {
                dispatchMessage(resource, body, channel, envelope.getDeliveryTag(), autoAck);
            }
        });
    } catch (IOException exception) {
        throw new BallerinaException(exception);
    }
}