Class RabbitUtils


  • public abstract class RabbitUtils
    extends java.lang.Object
    Author:
    Mark Fisher, Mark Pollack, Gary Russell, Artem Bilan
    • Constructor Summary

      Constructors 
      Constructor Description
      RabbitUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void cancel​(com.rabbitmq.client.Channel channel, java.lang.String consumerTag)  
      static void clearPhysicalCloseRequired()
      Clear the physicalCloseRequired flag.
      static void closeChannel​(com.rabbitmq.client.Channel channel)
      Close the given RabbitMQ Channel and ignore any thrown exception.
      static void closeConnection​(Connection connection)
      Close the given RabbitMQ Connection and ignore any thrown exception.
      static void closeMessageConsumer​(com.rabbitmq.client.Channel channel, java.util.Collection<java.lang.String> consumerTags, boolean transactional)  
      static void commitIfNecessary​(com.rabbitmq.client.Channel channel)
      Commit the Channel if not within a JTA transaction.
      static void declareTransactional​(com.rabbitmq.client.Channel channel)
      Declare to that broker that a channel is going to be used transactionally, and convert exceptions that arise.
      static int getMaxFrame​(ConnectionFactory connectionFactory)
      Return the negotiated frame_max.
      static boolean isExchangeDeclarationFailure​(java.lang.Exception e)
      Return true if there is a ShutdownSignalException in the cause tree and its reason is "COMMAND_INVALID" and the operation being performed was exchangeDeclare.
      static boolean isExclusiveUseChannelClose​(com.rabbitmq.client.ShutdownSignalException sig)
      Return true if the ShutdownSignalException reason is AMQP.Channel.Close and the operation that failed was basicConsumer and the failure text contains "exclusive".
      static boolean isMismatchedQueueArgs​(java.lang.Exception e)
      Return true if there is a ShutdownSignalException in the cause tree and its reason is "PRECONDITION_FAILED" and the operation being performed was queueDeclare.
      static boolean isNormalChannelClose​(com.rabbitmq.client.ShutdownSignalException sig)
      Return true if the ShutdownSignalException reason is AMQP.Channel.Close and the reply code was AMQP.REPLY_SUCCESS (200) and the text equals "OK".
      static boolean isNormalShutdown​(com.rabbitmq.client.ShutdownSignalException sig)
      Return true if the ShutdownSignalException reason is AMQP.Connection.Close and the reply code was AMQP.REPLY_SUCCESS (200) and the text equals "OK".
      static boolean isPassiveDeclarationChannelClose​(com.rabbitmq.client.ShutdownSignalException sig)
      Return true if the ShutdownSignalException reason is AMQP.Channel.Close and the operation that failed was exchangeDeclare or queueDeclare.
      static boolean isPhysicalCloseRequired()
      Gets and removes a ThreadLocal indicating the channel MUST be physically closed.
      static void rollbackIfNecessary​(com.rabbitmq.client.Channel channel)  
      static void setPhysicalCloseRequired​(com.rabbitmq.client.Channel channel, boolean b)
      Sets a ThreadLocal indicating the channel MUST be physically closed.
      static com.rabbitmq.client.SaslConfig stringToSaslConfig​(java.lang.String saslConfig, com.rabbitmq.client.ConnectionFactory connectionFactory)
      Convert a String value to a SaslConfig.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DECLARE_METHOD_ID_10

        public static final int DECLARE_METHOD_ID_10
        AMQP declare method.
        See Also:
        Constant Field Values
      • CONSUME_METHOD_ID_20

        public static final int CONSUME_METHOD_ID_20
        AMQP consume method.
        See Also:
        Constant Field Values
      • EXCHANGE_CLASS_ID_40

        public static final int EXCHANGE_CLASS_ID_40
        AMQP exchange class id.
        See Also:
        Constant Field Values
      • QUEUE_CLASS_ID_50

        public static final int QUEUE_CLASS_ID_50
        AMQP queue class id.
        See Also:
        Constant Field Values
      • BASIC_CLASS_ID_60

        public static final int BASIC_CLASS_ID_60
        AMQP basic class id.
        See Also:
        Constant Field Values
      • CONNECTION_PROTOCOL_CLASS_ID_10

        public static final int CONNECTION_PROTOCOL_CLASS_ID_10
        AMQP Connection protocol class id.
        See Also:
        Constant Field Values
      • CHANNEL_PROTOCOL_CLASS_ID_20

        public static final int CHANNEL_PROTOCOL_CLASS_ID_20
        AMQP Channel protocol class id.
        See Also:
        Constant Field Values
    • Constructor Detail

      • RabbitUtils

        public RabbitUtils()
    • Method Detail

      • closeConnection

        public static void closeConnection​(@Nullable
                                           Connection connection)
        Close the given RabbitMQ Connection and ignore any thrown exception. This is useful for typical finally blocks in manual RabbitMQ code.
        Parameters:
        connection - the RabbitMQ Connection to close (may be null)
      • closeChannel

        public static void closeChannel​(@Nullable
                                        com.rabbitmq.client.Channel channel)
        Close the given RabbitMQ Channel and ignore any thrown exception. This is useful for typical finally blocks in manual RabbitMQ code.
        Parameters:
        channel - the RabbitMQ Channel to close (may be null)
      • commitIfNecessary

        public static void commitIfNecessary​(com.rabbitmq.client.Channel channel)
        Commit the Channel if not within a JTA transaction.
        Parameters:
        channel - the RabbitMQ Channel to commit
      • rollbackIfNecessary

        public static void rollbackIfNecessary​(com.rabbitmq.client.Channel channel)
      • closeMessageConsumer

        public static void closeMessageConsumer​(com.rabbitmq.client.Channel channel,
                                                java.util.Collection<java.lang.String> consumerTags,
                                                boolean transactional)
      • cancel

        public static void cancel​(com.rabbitmq.client.Channel channel,
                                  java.lang.String consumerTag)
      • declareTransactional

        public static void declareTransactional​(com.rabbitmq.client.Channel channel)
        Declare to that broker that a channel is going to be used transactionally, and convert exceptions that arise.
        Parameters:
        channel - the channel to use
      • setPhysicalCloseRequired

        public static void setPhysicalCloseRequired​(com.rabbitmq.client.Channel channel,
                                                    boolean b)
        Sets a ThreadLocal indicating the channel MUST be physically closed.
        Parameters:
        channel - the channel.
        b - true if the channel must be closed (if it's a proxy).
      • isPhysicalCloseRequired

        public static boolean isPhysicalCloseRequired()
        Gets and removes a ThreadLocal indicating the channel MUST be physically closed.
        Returns:
        true if the channel must be physically closed
      • clearPhysicalCloseRequired

        public static void clearPhysicalCloseRequired()
        Clear the physicalCloseRequired flag.
      • isNormalShutdown

        public static boolean isNormalShutdown​(com.rabbitmq.client.ShutdownSignalException sig)
        Return true if the ShutdownSignalException reason is AMQP.Connection.Close and the reply code was AMQP.REPLY_SUCCESS (200) and the text equals "OK".
        Parameters:
        sig - the exception.
        Returns:
        true for a normal connection close.
      • isNormalChannelClose

        public static boolean isNormalChannelClose​(com.rabbitmq.client.ShutdownSignalException sig)
        Return true if the ShutdownSignalException reason is AMQP.Channel.Close and the reply code was AMQP.REPLY_SUCCESS (200) and the text equals "OK".
        Parameters:
        sig - the exception.
        Returns:
        true for a normal channel close.
      • isPassiveDeclarationChannelClose

        public static boolean isPassiveDeclarationChannelClose​(com.rabbitmq.client.ShutdownSignalException sig)
        Return true if the ShutdownSignalException reason is AMQP.Channel.Close and the operation that failed was exchangeDeclare or queueDeclare.
        Parameters:
        sig - the exception.
        Returns:
        true if the failure meets the conditions.
      • isExclusiveUseChannelClose

        public static boolean isExclusiveUseChannelClose​(com.rabbitmq.client.ShutdownSignalException sig)
        Return true if the ShutdownSignalException reason is AMQP.Channel.Close and the operation that failed was basicConsumer and the failure text contains "exclusive".
        Parameters:
        sig - the exception.
        Returns:
        true if the declaration failed because of an exclusive queue.
      • isMismatchedQueueArgs

        public static boolean isMismatchedQueueArgs​(java.lang.Exception e)
        Return true if there is a ShutdownSignalException in the cause tree and its reason is "PRECONDITION_FAILED" and the operation being performed was queueDeclare. This can happen if a queue has mismatched properties (auto-delete etc) or arguments (x-message-ttl etc).
        Parameters:
        e - the exception.
        Returns:
        true if the exception was due to queue declaration precondition failed.
        Since:
        1.6
      • isExchangeDeclarationFailure

        public static boolean isExchangeDeclarationFailure​(java.lang.Exception e)
        Return true if there is a ShutdownSignalException in the cause tree and its reason is "COMMAND_INVALID" and the operation being performed was exchangeDeclare. For example attempting to declare an exchange that is not supported by the broker or its plugins.
        Parameters:
        e - the exception.
        Returns:
        true if the exception was due to exchange declaration failed.
        Since:
        1.6
      • getMaxFrame

        public static int getMaxFrame​(ConnectionFactory connectionFactory)
        Return the negotiated frame_max.
        Parameters:
        connectionFactory - the connection factory.
        Returns:
        the size or -1 if it cannot be determined.
      • stringToSaslConfig

        public static com.rabbitmq.client.SaslConfig stringToSaslConfig​(java.lang.String saslConfig,
                                                                        com.rabbitmq.client.ConnectionFactory connectionFactory)
        Convert a String value to a SaslConfig. Valid string values:
        • DefaultSaslConfig.PLAIN
        • DefaultSaslConfig.EXTERNAL
        • JDKSaslConfig
        • CRDemoSaslConfig
        Parameters:
        saslConfig - the string value.
        connectionFactory - the connection factory to get the name, pw, host.
        Returns:
        the saslConfig.