Search in sources :

Example 1 with Delivery

use of com.rabbitmq.client.Delivery in project wso2-axis2-transports by wso2.

the class RabbitMQMessageSender method sendRPC.

/**
 * Publish message in RPC style and wait for the response received replyTo queue
 *
 * @param exchangeName    the exchange to publish the message to
 * @param routingKey      the routing key
 * @param basicProperties other properties for the message
 * @param messageBody     the message body
 * @param timeout         waiting timeout until response receive
 * @return response message received to the replyTo queue
 * @throws IOException
 */
private Delivery sendRPC(String exchangeName, String routingKey, AMQP.BasicProperties basicProperties, byte[] messageBody, long timeout) throws IOException {
    Delivery response = null;
    String replyTo = basicProperties.getReplyTo();
    final BlockingQueue<Delivery> responses = new ArrayBlockingQueue<>(1);
    publishMessage(exchangeName, routingKey, basicProperties, messageBody);
    String replyConsumerTag = channel.basicConsume(replyTo, true, (consumerTag, delivery) -> responses.offer(delivery), consumerTag -> {
    });
    try {
        response = responses.poll(timeout, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    } finally {
        channel.basicCancel(replyConsumerTag);
    }
    if (response == null) {
        throw new AxisFault("Did not receive a response within " + timeout + "ms to the replyTo queue " + replyTo);
    }
    return response;
}
Also used : AxisFault(org.apache.axis2.AxisFault) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) Delivery(com.rabbitmq.client.Delivery)

Example 2 with Delivery

use of com.rabbitmq.client.Delivery in project wso2-axis2-transports by wso2.

the class RabbitMQSender method sendMessage.

/**
 * Performs the sending of the AMQP message
 *
 * @param msgCtx           the axis2 message context
 * @param targetEPR        the RabbitMQ endpoint
 * @param outTransportInfo the {@link OutTransportInfo} object
 * @throws AxisFault
 */
@Override
public void sendMessage(MessageContext msgCtx, String targetEPR, OutTransportInfo outTransportInfo) throws AxisFault {
    if (targetEPR != null) {
        // execute when publishing a message to queue in standard flow
        RabbitMQOutTransportInfo transportOutInfo = new RabbitMQOutTransportInfo(targetEPR);
        String factoryName = RabbitMQUtils.resolveTransportDescriptionFromTargetEPR(transportOutInfo.getProperties(), rabbitMQConnectionFactory);
        Channel channel = null;
        Delivery response = null;
        SenderType senderType = null;
        try {
            // get rabbitmq properties from the EPR
            Map<String, String> epProperties = BaseUtils.getEPRProperties(targetEPR);
            // set the routing key
            String queueName = epProperties.get(RabbitMQConstants.QUEUE_NAME);
            String routingKey = epProperties.get(RabbitMQConstants.QUEUE_ROUTING_KEY);
            if (StringUtils.isNotEmpty(queueName) && StringUtils.isEmpty(routingKey)) {
                // support the backward compatibility
                routingKey = queueName;
            } else if (StringUtils.isEmpty(queueName) && StringUtils.isEmpty(routingKey)) {
                routingKey = targetEPR.substring(targetEPR.indexOf("/") + 1, targetEPR.indexOf("?"));
            }
            // send the message
            senderType = getSenderType(msgCtx, epProperties);
            if (senderType == SenderType.PUBLISHER_CONFIRMS) {
                channel = rabbitMQConfirmChannelPool.borrowObject(factoryName);
            } else {
                channel = rabbitMQChannelPool.borrowObject(factoryName);
            }
            RabbitMQMessageSender sender = new RabbitMQMessageSender(channel, factoryName, senderType, rabbitMQChannelPool, rabbitMQConfirmChannelPool);
            response = sender.send(routingKey, msgCtx, epProperties);
        } catch (Exception e) {
            channel = null;
            handleException("Error occurred while sending message out.", e);
        } finally {
            returnToPool(factoryName, channel, senderType);
        }
        // inject message to the axis engine if a response received
        if (response != null) {
            MessageContext responseMsgCtx = createResponseMessageContext(msgCtx);
            String contentType = RabbitMQUtils.buildMessage(response.getProperties(), response.getBody(), responseMsgCtx);
            handleIncomingMessage(responseMsgCtx, RabbitMQUtils.getTransportHeaders(response.getProperties()), RabbitMQUtils.getSoapAction(response.getProperties()), contentType);
        }
    } else if (outTransportInfo instanceof RabbitMQOutTransportInfo) {
        // execute when publishing a message to the replyTo queue in request-response flow
        RabbitMQOutTransportInfo transportOutInfo = (RabbitMQOutTransportInfo) outTransportInfo;
        String factoryName = transportOutInfo.getConnectionFactoryName();
        Channel channel = null;
        try {
            // get the correlationId and replyTo queue from the transport headers
            Map<String, Object> transportHeaders = (Map<String, Object>) msgCtx.getProperty(msgCtx.TRANSPORT_HEADERS);
            msgCtx.setProperty(RabbitMQConstants.CORRELATION_ID, transportHeaders.get(RabbitMQConstants.CORRELATION_ID));
            String replyTo = (String) transportHeaders.get(RabbitMQConstants.RABBITMQ_REPLY_TO);
            // set rabbitmq properties
            Map<String, String> rabbitMQProperties = new HashMap<>();
            rabbitMQProperties.put(RabbitMQConstants.QUEUE_AUTODECLARE, "false");
            rabbitMQProperties.put(RabbitMQConstants.EXCHANGE_AUTODECLARE, "false");
            // send the message to the replyTo queue
            channel = rabbitMQChannelPool.borrowObject(factoryName);
            RabbitMQMessageSender sender = new RabbitMQMessageSender(channel, factoryName, SenderType.DEFAULT, rabbitMQChannelPool, rabbitMQConfirmChannelPool);
            sender.send(replyTo, msgCtx, rabbitMQProperties);
        } catch (Exception e) {
            log.error("Error occurred while sending message out.", e);
            channel = null;
        } finally {
            returnToPool(factoryName, channel, SenderType.DEFAULT);
        }
    }
}
Also used : Channel(com.rabbitmq.client.Channel) Delivery(com.rabbitmq.client.Delivery) MessageContext(org.apache.axis2.context.MessageContext) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with Delivery

use of com.rabbitmq.client.Delivery in project wso2-axis2-transports by wso2.

the class RabbitMQMessageSender method send.

/**
 * Publish message to the exchange with the routing key. Execute relevant logic based on the sender type.
 *
 * @param routingKey         the routing key to publish the message
 * @param msgContext         the axis2 message context
 * @param rabbitMQProperties the rabbitmq endpoint parameters
 * @return {@link Delivery} in RPC messaging style
 * @throws AxisRabbitMQException
 * @throws IOException
 * @throws InterruptedException
 */
// TODO: handle x-consistent-hash
public Delivery send(String routingKey, MessageContext msgContext, Map<String, String> rabbitMQProperties) throws Exception {
    Delivery response = null;
    // declaring queue if given
    String queueName = rabbitMQProperties.get(RabbitMQConstants.QUEUE_NAME);
    String exchangeName = rabbitMQProperties.get(RabbitMQConstants.EXCHANGE_NAME);
    try {
        RabbitMQUtils.declareQueue(channel, queueName, rabbitMQProperties);
    } catch (IOException ex) {
        channel = checkAndIgnoreInEquivalentParamException(ex, RabbitMQConstants.QUEUE, queueName);
    }
    try {
        RabbitMQUtils.declareExchange(channel, exchangeName, rabbitMQProperties);
    } catch (IOException ex) {
        channel = checkAndIgnoreInEquivalentParamException(ex, RabbitMQConstants.EXCHANGE, exchangeName);
    }
    RabbitMQUtils.bindQueueToExchange(channel, queueName, exchangeName, rabbitMQProperties);
    AMQP.BasicProperties.Builder builder = buildBasicProperties(msgContext);
    int deliveryMode = NumberUtils.toInt(rabbitMQProperties.get(RabbitMQConstants.QUEUE_DELIVERY_MODE), RabbitMQConstants.DEFAULT_DELIVERY_MODE);
    builder.deliveryMode(deliveryMode);
    long replyTimeout = NumberUtils.toLong((String) msgContext.getProperty(RabbitMQConstants.RABBITMQ_WAIT_REPLY), RabbitMQConstants.DEFAULT_RABBITMQ_TIMEOUT);
    long confirmTimeout = NumberUtils.toLong((String) msgContext.getProperty(RabbitMQConstants.RABBITMQ_WAIT_CONFIRMS), RabbitMQConstants.DEFAULT_RABBITMQ_TIMEOUT);
    AMQP.BasicProperties basicProperties = builder.build();
    byte[] messageBody = RabbitMQUtils.getMessageBody(msgContext);
    switch(senderType) {
        case RPC:
            response = sendRPC(exchangeName, routingKey, basicProperties, messageBody, replyTimeout);
            break;
        case PUBLISHER_CONFIRMS:
            sendPublisherConfirms(exchangeName, routingKey, basicProperties, messageBody, confirmTimeout);
            break;
        default:
            publishMessage(exchangeName, routingKey, basicProperties, messageBody);
            break;
    }
    return response;
}
Also used : AMQP(com.rabbitmq.client.AMQP) Delivery(com.rabbitmq.client.Delivery) IOException(java.io.IOException)

Example 4 with Delivery

use of com.rabbitmq.client.Delivery in project flink by apache.

the class RMQSource method run.

@Override
public void run(SourceContext<OUT> ctx) throws Exception {
    final RMQCollectorImpl collector = new RMQCollectorImpl(ctx);
    final long timeout = rmqConnectionConfig.getDeliveryTimeout();
    while (running) {
        Delivery delivery = consumer.nextDelivery(timeout);
        synchronized (ctx.getCheckpointLock()) {
            if (delivery != null) {
                processMessage(delivery, collector);
            }
            if (collector.isEndOfStreamSignalled()) {
                this.running = false;
                return;
            }
        }
    }
}
Also used : Delivery(com.rabbitmq.client.Delivery)

Example 5 with Delivery

use of com.rabbitmq.client.Delivery in project flink by apache.

the class QueueingConsumer method handleDelivery.

@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
    checkShutdown();
    this.queue.add(new Delivery(envelope, properties, body));
}
Also used : Delivery(com.rabbitmq.client.Delivery)

Aggregations

Delivery (com.rabbitmq.client.Delivery)5 AMQP (com.rabbitmq.client.AMQP)1 Channel (com.rabbitmq.client.Channel)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)1 AxisFault (org.apache.axis2.AxisFault)1 MessageContext (org.apache.axis2.context.MessageContext)1