Search in sources :

Example 11 with ShutdownSignalException

use of com.rabbitmq.client.ShutdownSignalException in project rabbitmq-java-client by rabbitmq.

the class ShutdownNotifierComponent method addShutdownListener.

@Override
public void addShutdownListener(ShutdownListener listener) {
    ShutdownSignalException sse = null;
    synchronized (this.monitor) {
        sse = this.shutdownCause;
        this.shutdownListeners.add(listener);
    }
    if (// closed
    sse != null)
        listener.shutdownCompleted(sse);
}
Also used : ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException)

Example 12 with ShutdownSignalException

use of com.rabbitmq.client.ShutdownSignalException in project druid by druid-io.

the class RabbitMQFirehoseFactory method connect.

@Override
public Firehose connect(final ByteBufferInputRowParser firehoseParser) throws IOException {
    ConnectionOptions lyraOptions = new ConnectionOptions(this.connectionFactory);
    Config lyraConfig = new Config().withRecoveryPolicy(new RetryPolicy().withMaxRetries(config.getMaxRetries()).withRetryInterval(Duration.seconds(config.getRetryIntervalSeconds())).withMaxDuration(Duration.seconds(config.getMaxDurationSeconds())));
    String queue = config.getQueue();
    String exchange = config.getExchange();
    String routingKey = config.getRoutingKey();
    boolean durable = config.isDurable();
    boolean exclusive = config.isExclusive();
    boolean autoDelete = config.isAutoDelete();
    final Connection connection = Connections.create(lyraOptions, lyraConfig);
    connection.addShutdownListener(new ShutdownListener() {

        @Override
        public void shutdownCompleted(ShutdownSignalException cause) {
            log.warn(cause, "Connection closed!");
        }
    });
    final Channel channel = connection.createChannel();
    channel.queueDeclare(queue, durable, exclusive, autoDelete, null);
    channel.queueBind(queue, exchange, routingKey);
    channel.addShutdownListener(new ShutdownListener() {

        @Override
        public void shutdownCompleted(ShutdownSignalException cause) {
            log.warn(cause, "Channel closed!");
        }
    });
    // We create a QueueingConsumer that will not auto-acknowledge messages since that
    // happens on commit().
    final QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(queue, false, consumer);
    return new Firehose() {

        /**
       * Storing the latest delivery as a member variable should be safe since this will only be run
       * by a single thread.
       */
        private Delivery delivery;

        /**
       * Store the latest delivery tag to be able to commit (acknowledge) the message delivery up to
       * and including this tag. See commit() for more detail.
       */
        private long lastDeliveryTag;

        @Override
        public boolean hasMore() {
            delivery = null;
            try {
                // Wait for the next delivery. This will block until something is available.
                delivery = consumer.nextDelivery();
                if (delivery != null) {
                    lastDeliveryTag = delivery.getEnvelope().getDeliveryTag();
                    // If delivery is non-null, we report that there is something more to process.
                    return true;
                }
            } catch (InterruptedException e) {
                // A little unclear on how we should handle this.
                // At any rate, we're in an unknown state now so let's log something and return false.
                log.wtf(e, "Got interrupted while waiting for next delivery. Doubt this should ever happen.");
            }
            // nothing more to process.
            return false;
        }

        @Override
        public InputRow nextRow() {
            if (delivery == null) {
                //Just making sure.
                log.wtf("I have nothing in delivery. Method hasMore() should have returned false.");
                return null;
            }
            return firehoseParser.parse(ByteBuffer.wrap(delivery.getBody()));
        }

        @Override
        public Runnable commit() {
            // acknowledge values up to and including that value.
            return new Runnable() {

                // Store (copy) the last delivery tag to "become" thread safe.
                final long deliveryTag = lastDeliveryTag;

                @Override
                public void run() {
                    try {
                        log.info("Acknowledging delivery of messages up to tag: " + deliveryTag);
                        // Acknowledge all messages up to and including the stored delivery tag.
                        channel.basicAck(deliveryTag, true);
                    } catch (IOException e) {
                        log.error(e, "Unable to acknowledge message reception to message queue.");
                    }
                }
            };
        }

        @Override
        public void close() throws IOException {
            log.info("Closing connection to RabbitMQ");
            channel.close();
            connection.close();
        }
    };
}
Also used : Config(net.jodah.lyra.config.Config) Firehose(io.druid.data.input.Firehose) Channel(com.rabbitmq.client.Channel) Connection(com.rabbitmq.client.Connection) IOException(java.io.IOException) ShutdownListener(com.rabbitmq.client.ShutdownListener) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) ConnectionOptions(net.jodah.lyra.ConnectionOptions) Delivery(com.rabbitmq.client.QueueingConsumer.Delivery) RetryPolicy(net.jodah.lyra.retry.RetryPolicy)

Example 13 with ShutdownSignalException

use of com.rabbitmq.client.ShutdownSignalException in project wso2-synapse by wso2.

the class RabbitMQConsumer method receive.

public MessageContext receive() {
    if (!checkConnection()) {
        if (!reconnect()) {
            if (logger.isDebugEnabled()) {
                logger.debug(getId() + " cannot receive message from store. Can not reconnect.");
            }
            return null;
        } else {
            logger.info(getId() + " reconnected to store.");
            isReceiveError = false;
        }
    }
    // setting channel
    if (channel != null) {
        if (!channel.isOpen()) {
            if (!setChannel()) {
                logger.info(getId() + " unable to create the channel.");
                return null;
            }
        }
    } else {
        if (!setChannel()) {
            logger.info(getId() + " unable to create the channel.");
            return null;
        }
    }
    // receive messages
    try {
        GetResponse delivery = null;
        delivery = channel.basicGet(queueName, false);
        if (delivery != null) {
            // deserilizing message
            StorableMessage storableMessage = null;
            ByteArrayInputStream bis = new ByteArrayInputStream(delivery.getBody());
            ObjectInput in = new ObjectInputStream(bis);
            try {
                storableMessage = (StorableMessage) in.readObject();
            } catch (ClassNotFoundException e) {
                logger.error(getId() + "unable to read the stored message" + e);
                channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
            }
            bis.close();
            in.close();
            org.apache.axis2.context.MessageContext axis2Mc = store.newAxis2Mc();
            MessageContext synapseMc = store.newSynapseMc(axis2Mc);
            synapseMc = MessageConverter.toMessageContext(storableMessage, axis2Mc, synapseMc);
            updateCache(delivery, synapseMc, null, false);
            if (logger.isDebugEnabled()) {
                logger.debug(getId() + " Received MessageId:" + delivery.getProps().getMessageId());
            }
            return synapseMc;
        }
    } catch (ShutdownSignalException sse) {
        logger.error(getId() + " connection error when receiving messages" + sse);
    } catch (IOException ioe) {
        logger.error(getId() + " connection error when receiving messages" + ioe);
    }
    return null;
}
Also used : ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) StorableMessage(org.apache.synapse.message.store.impl.commons.StorableMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(java.io.ObjectInput) MessageContext(org.apache.synapse.MessageContext) IOException(java.io.IOException) GetResponse(com.rabbitmq.client.GetResponse) ObjectInputStream(java.io.ObjectInputStream)

Example 14 with ShutdownSignalException

use of com.rabbitmq.client.ShutdownSignalException in project rabbitmq-jms-client by rabbitmq.

the class RMQSession method closeRabbitChannels.

private void closeRabbitChannels() throws JMSException {
    // does not throw exception
    this.clearBrowsingChannels();
    if (this.channel == null)
        return;
    try {
        this.channel.close();
    } catch (ShutdownSignalException x) {
    // nothing to do
    } catch (Exception x) {
        if (x instanceof IOException) {
            IOException ioe = (IOException) x;
            if (!(ioe.getCause() instanceof ShutdownSignalException)) {
                this.logger.warn("RabbitMQ channel({}) failed to close on session {}", this.channel, this, ioe);
                throw new RMQJMSException(ioe);
            }
        } else if (x instanceof TimeoutException) {
            TimeoutException te = (TimeoutException) x;
            this.logger.warn("RabbitMQ channel({}) timed out trying to close session {}", this.channel, this, te);
            throw new RMQJMSException(te);
        } else {
            throw new RMQJMSException("Unexpected exception from channel.close()", x);
        }
    }
}
Also used : RMQJMSException(com.rabbitmq.jms.util.RMQJMSException) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException) RMQJMSSelectorException(com.rabbitmq.jms.util.RMQJMSSelectorException) IllegalStateException(javax.jms.IllegalStateException) JMSException(javax.jms.JMSException) RMQJMSException(com.rabbitmq.jms.util.RMQJMSException) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) IOException(java.io.IOException) InvalidSelectorException(javax.jms.InvalidSelectorException) TimeoutException(java.util.concurrent.TimeoutException)

Example 15 with ShutdownSignalException

use of com.rabbitmq.client.ShutdownSignalException in project rabbitmq-jms-client by rabbitmq.

the class RMQConnection method close.

/**
 * From the JMS Spec:
 * <blockquote>
 * <p>This call blocks until a
 * receive or message listener in progress has completed. A blocked message consumer receive call returns null when
 * this message consumer is closed.</p>
 * </blockquote>
 * {@inheritDoc}
 */
@Override
public void close() throws JMSException {
    logger.trace("closing connection ({})", this);
    if (this.closed)
        return;
    this.closed = true;
    removeClientID();
    // We null any exception listener since we don't want it driven during close().
    this.exceptionListener.set(null);
    closeAllSessions();
    try {
        this.rabbitConnection.close();
    } catch (ShutdownSignalException x) {
    // nothing to do
    } catch (IOException x) {
        if (!(x.getCause() instanceof ShutdownSignalException)) {
            throw new RMQJMSException(x);
        }
    }
}
Also used : RMQJMSException(com.rabbitmq.jms.util.RMQJMSException) ShutdownSignalException(com.rabbitmq.client.ShutdownSignalException) IOException(java.io.IOException)

Aggregations

ShutdownSignalException (com.rabbitmq.client.ShutdownSignalException)24 IOException (java.io.IOException)20 ConsumerCancelledException (com.rabbitmq.client.ConsumerCancelledException)12 QueueingConsumer (com.rabbitmq.client.QueueingConsumer)12 Channel (com.rabbitmq.client.Channel)5 Connection (com.rabbitmq.client.Connection)4 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)4 ShutdownListener (com.rabbitmq.client.ShutdownListener)4 TimeoutException (java.util.concurrent.TimeoutException)4 Test (org.junit.Test)4 RMQJMSException (com.rabbitmq.jms.util.RMQJMSException)3 AMQP (com.rabbitmq.client.AMQP)2 RMQJMSSelectorException (com.rabbitmq.jms.util.RMQJMSSelectorException)2 IllegalStateException (javax.jms.IllegalStateException)2 InvalidSelectorException (javax.jms.InvalidSelectorException)2 JMSException (javax.jms.JMSException)2 BasicProperties (com.rabbitmq.client.AMQP.BasicProperties)1 Consumer (com.rabbitmq.client.Consumer)1 DefaultConsumer (com.rabbitmq.client.DefaultConsumer)1 GetResponse (com.rabbitmq.client.GetResponse)1