Example usage for com.rabbitmq.client ShutdownSignalException isHardError

List of usage examples for com.rabbitmq.client ShutdownSignalException isHardError

Introduction

In this page you can find the example usage for com.rabbitmq.client ShutdownSignalException isHardError.

Prototype

public boolean isHardError() 

Source Link

Usage

From source file:com.coderdojo.libretalk.MainActivity.java

License:Apache License

@Override
public void onPause() {
    super.onPause(); // Always call the superclass method first
    datasource.close();// www .  ja  va 2s  .  c  o  m
    //XXX NETWORKING CODE BEGIN

    if (this.connection.getStatus() == LibretalkConnection.ConnectionStatus.CONNECTED) {
        try {
            this.connection.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ShutdownSignalException sig) {
            Log.w("libretalk::LibretalkConnection", "Caught shutdownSignal while attempting to close Channel:");
            Log.w("libretalk::LibretalkConnection", "\t[== " + sig.getMessage() + "==]");
            Log.w("libretalk::LibretalkConnection", "\tHard Error : " + sig.isHardError());
            Log.w("libretalk::LibretalkConnection", "\tReason     : " + sig.getReason());
        }
    }

    //XXX NETWORKING CODE END
}

From source file:com.netcore.hsmart.dlrconsumers.DlrConsumer1000.java

/**
 * @param args the command line arguments
 * @throws java.lang.Exception//from   ww  w  . j a  v  a  2  s.co m
 */
public static void main(String[] args) throws Exception {

    /**
     * Set properties at runtime
     */
    System.setProperty("dlrconsumer_logfile", "557_dlr_consumer.log");
    AppConstants.loadAppConfig();

    final String queueName = AppConstants.getDlrReceiverQueuePrefix() + GATEWAY_ID;
    final String exchangeName = AppConstants.getDlrReceiverExchangeName();
    final String routingKey = GATEWAY_ID;

    Logger logger = LoggerFactory.getLogger(DlrConsumer1000.class);

    try {

        Connection connection = AppConstants.getRabbitMqObject().newConnection();

        connection.addShutdownListener(new ShutdownListener() {
            @Override
            public void shutdownCompleted(ShutdownSignalException cause) {
                //throw new UnsupportedOperationException("Not supported yet.");
                if (cause.isHardError()) {
                    Connection conn;
                    conn = (Connection) cause.getReference();
                    if (!cause.isInitiatedByApplication()) {
                        Method reason = cause.getReason();
                        logger.info("REASON:" + reason.toString());
                    }

                } else {
                    Channel ch = (Channel) cause.getReference();
                    logger.info("NO HARDSHUTDOWN");
                }
            }
        });

        Channel channel = connection.createChannel();

        channel.exchangeDeclare(exchangeName, "direct");
        channel.queueDeclare(queueName, true, false, false, null);
        channel.queueBind(queueName, exchangeName, routingKey);
        channel.basicQos(1000);
        //channel.addShutdownListener(listener);
        logger.info(" [*] Waiting for messages from gateway " + GATEWAY_ID + ". To exit press CTRL+C");

        Consumer consumer;

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

                Map<String, String> dataToPost = new HashMap<>();
                String payload = new String(body, "UTF-8");

                logger.info("REF_ID:" + dataToPost.get("ref_id") + "|START+++++++++++++++");
                logger.info("REF_ID:" + dataToPost.get("ref_id") + "|PAYLOAD:" + payload);

                String payloadParts[] = payload.split(AppConstants.getPayloadGroupSeparator());

                for (String payloadPart : payloadParts) {
                    String s[] = payloadPart.split(AppConstants.getPayloadKVSeparator());

                    //check if any parameter is empty
                    if (s.length == 2) {
                        dataToPost.put(s[0], s[1]);
                    } else {
                        logger.info("REF_ID:" + dataToPost.get("ref_id") + "|EMPTY_PARAM:" + s[0]);
                        dataToPost.put(s[0], null);
                    }
                }

                long deliveryTag = envelope.getDeliveryTag();

                if (invokeApiCall(dataToPost)) {
                    channel.basicAck(deliveryTag, false);

                } else {
                    channel.basicNack(deliveryTag, false, true);
                }

                /**
                 * release memory
                 */
                logger.info("REF_ID:" + dataToPost.get("ref_id") + "|END-----------------");
                dataToPost.clear();
                payloadParts = null;

            }
        };

        String cTag = channel.basicConsume(queueName, false, consumer);
        logger.info("CONSUMER TAG : " + cTag);

    } catch (IOException | TimeoutException e) {
        logger.error("RMQ_ERROR:" + e.getMessage());
    }
}

From source file:com.netcore.hsmart.smsconsumers.SmsConsumer1000.java

/**
 * @param args the command line arguments
 * @throws java.lang.Exception/* w w w  . j a v  a 2s.  c  o  m*/
 */
public static void main(String[] args) throws Exception {

    /**
     * Set properties at runtime
     */
    System.setProperty("smsconsumer_logfile", GATEWAY_ID + "_sms_consumer.log");
    AppConstants.loadAppConfig();

    final String queueName = AppConstants.getSmsReceiverQueuePrefix() + GATEWAY_ID;
    final String exchangeName = AppConstants.getSmsReceiverExchangeName();
    final String routingKey = GATEWAY_ID;

    Logger logger = LoggerFactory.getLogger(SmsConsumer1000.class);

    try {

        Connection connection = AppConstants.getRabbitMqObject().newConnection();

        connection.addShutdownListener(new ShutdownListener() {
            @Override
            public void shutdownCompleted(ShutdownSignalException cause) {
                //throw new UnsupportedOperationException("Not supported yet.");
                if (cause.isHardError()) {
                    Connection conn;
                    conn = (Connection) cause.getReference();
                    if (!cause.isInitiatedByApplication()) {
                        Method reason = cause.getReason();
                        logger.info("REASON:" + reason.toString());
                    }

                } else {
                    Channel ch = (Channel) cause.getReference();
                    logger.info("NO HARDSHUTDOWN");
                }
            }
        });

        Channel channel = connection.createChannel();

        channel.exchangeDeclare(exchangeName, "direct");
        channel.queueDeclare(queueName, true, false, false, null);
        channel.queueBind(queueName, exchangeName, routingKey);
        channel.basicQos(1000);
        //channel.addShutdownListener(listener);
        logger.info(" [*] Waiting for messages from gateway " + GATEWAY_ID + ". To exit press CTRL+C");

        Consumer consumer;

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

                Map<String, String> dataToPost = new HashMap<>();
                String payload = new String(body, "UTF-8");

                String payloadParts[] = payload.split(AppConstants.getPayloadGroupSeparator());

                for (String payloadPart : payloadParts) {
                    String s[] = payloadPart.split(AppConstants.getPayloadKVSeparator());
                    dataToPost.put(s[0], s[1]);
                }
                logger.info("REF_ID:" + dataToPost.get("ref_id") + "|START+++++++++++++++");
                logger.info("REF_ID:" + dataToPost.get("ref_id") + "|PAYLOAD:" + payload);

                long deliveryTag = envelope.getDeliveryTag();

                if (invokeApiCall(dataToPost)) {
                    if (saveRequestData()) {

                        channel.basicAck(deliveryTag, false);
                    } else {
                        channel.basicNack(deliveryTag, false, true);
                    }

                } else {
                    channel.basicNack(deliveryTag, false, true);
                }

                /**
                 * release memory
                 */
                logger.info("REF_ID:" + dataToPost.get("ref_id") + "|END-----------------");
                API_CALL_STATS.clear();
                dataToPost.clear();
                payloadParts = null;

            }
        };

        String cTag = channel.basicConsume(queueName, false, consumer);
        logger.info("CONSUMER TAG : " + cTag);

    } catch (IOException | TimeoutException e) {
        logger.error("RMQ_ERROR:" + e.getMessage());
    }
}

From source file:com.sonymobile.jenkins.plugins.mq.mqnotifier.MQConnection.java

License:Open Source License

@Override
public void shutdownCompleted(ShutdownSignalException cause) {
    if (cause.isHardError()) {
        if (!cause.isInitiatedByApplication()) {
            LOGGER.warn("MQ connection was suddenly disconnected.");
            try {
                if (connection != null && connection.isOpen()) {
                    connection.close();/*from w ww .j ava 2s .  com*/
                }
                if (channel != null && channel.isOpen()) {
                    channel.close();
                }
            } catch (IOException e) {
                LOGGER.error("IOException: ", e);
            } catch (AlreadyClosedException e) {
                LOGGER.error("AlreadyClosedException: ", e);
            } finally {
                channel = null;
                connection = null;
            }
        }
    } else {
        LOGGER.warn("MQ channel was suddenly disconnected.");
    }
}

From source file:org.mule.transport.amqp.internal.endpoint.receiver.MessageReceiverConsumer.java

License:Open Source License

@Override
public void handleShutdownSignal(final String consumerTag, final ShutdownSignalException sig) {
    // Only restart the consumer in case this is not a connection problem. If that is the
    // case the connector's reconnection strategy will handle it.
    if (!sig.isHardError()) {
        logger.warn("Received shutdown signal for consumer tag: " + consumerTag
                + ", the message receiver will try to restart.", sig);
        messageReceiver.restart(false);//w w w  .j a v  a2s. c  o m
    }
}

From source file:org.teksme.server.common.messaging.AMQPBrokerManager.java

License:Apache License

public void shutdownCompleted(ShutdownSignalException sse) {

    Connection conn = (Connection) sse.getReference();
    Command closeCommand = (Command) sse.getReason();
    if (sse.isHardError()) {
        if (!sse.isInitiatedByApplication()) {
            AMQP.Connection.Close closeMethod = (AMQP.Connection.Close) closeCommand.getMethod();
            logger.log(Level.SEVERE, "Connection host [" + conn.getHost() + "] closed. Shutdown reason: "
                    + closeMethod.getReplyCode());
        }//from  w  w  w  .j  a  va  2  s . com
    } else {
        Channel ch = (Channel) sse.getReference();
        logger.info("Connection host [" + conn.getHost() + "] closed. Shutdown reason: " + ch.getCloseReason());
    }

    logger.info("RabbitMQ AMQP broker shutdown completed!");

}

From source file:ss.udapi.sdk.services.RabbitMqConsumer.java

License:Apache License

@Override
public void handleShutdownSignal(String cTag, ShutdownSignalException signal) {

    if (connectShutDownLogged == false) {
        connectShutDownLogged = true; // so we don't end up logging it for
        // each cTag

        String hardError = "";
        String applInit = "";
        String reason = "";

        if (signal.isHardError()) {
            hardError = "connection";
        } else {/*from   w  ww .  j a va  2  s . c  o  m*/
            hardError = "channel";
        }

        if (signal.isInitiatedByApplication()) {
            applInit = "application";
        } else {
            applInit = "broker";
        }

        reason = signal.getReason().toString();
        logger.error("Connectivity to MQ has failed.  It was caused by " + applInit + " at the " + hardError
                + " level.  Reason received " + reason);
    }
    String resourceId = CtagResourceMap.getResource(cTag);
    ResourceImpl resource = (ResourceImpl) ResourceWorkerMap.getResourceImpl(resourceId);
    resource.mqDisconnectEvent();
    MQListener.removeMapping(cTag);
}

From source file:uk.ac.soton.itinnovation.experimedia.arch.ecc.amqpAPI.impl.amqp.AMQPBasicChannel.java

private void notifyChannelClosed(ShutdownSignalException sse) {
    if (!isClosingDown && sse != null) {
        boolean connectionOK = !sse.isHardError();

        if (channelListener != null)
            channelListener.onChannelShutdown(connectionOK, sse.getMessage());
    }//  ww w .ja  v  a2 s.c  o  m
}

From source file:uk.trainwatch.rabbitmq.RabbitRPCClient.java

License:Apache License

private byte[] primitiveCall(AMQP.BasicProperties props, byte[] message)
        throws IOException, ShutdownSignalException, TimeoutException {
    checkConsumer();// w  ww  .ja v  a  2  s  .c o  m
    BlockingCell<Object> k = new BlockingCell<>();

    String replyId = RabbitMQ.newCorrelationId();
    props = ((props == null) ? new AMQP.BasicProperties.Builder() : props.builder()).correlationId(replyId)
            .replyTo(replyQueue).build();
    continuationMap.put(replyId, k);

    publish(props, message);

    Object reply = k.uninterruptibleGet(timeout);
    if (reply instanceof ShutdownSignalException) {
        ShutdownSignalException sig = (ShutdownSignalException) reply;
        ShutdownSignalException wrapper = new ShutdownSignalException(sig.isHardError(),
                sig.isInitiatedByApplication(), sig.getReason(), sig.getReference());
        wrapper.initCause(sig);
        throw wrapper;
    } else {
        return (byte[]) reply;
    }
}

From source file:vn.com.uet.performance.rabbitmq.MulticastParams.java

License:Open Source License

private static boolean exists(Connection connection, Checker checker) throws IOException {
    try {/*from   w w w. j av  a2 s .  co m*/
        Channel ch = connection.createChannel();
        checker.check(ch);
        ch.abort();
        return true;
    } catch (IOException e) {
        ShutdownSignalException sse = (ShutdownSignalException) e.getCause();
        if (!sse.isHardError()) {
            AMQP.Channel.Close closeMethod = (AMQP.Channel.Close) sse.getReason();
            if (closeMethod.getReplyCode() == AMQP.NOT_FOUND) {
                return false;
            }
        }
        throw e;
    }
}