ConnectionListener.kt

  1. package com.hexagontk.messaging.rabbitmq

  2. import com.hexagontk.core.debug
  3. import com.hexagontk.core.loggerOf
  4. import com.hexagontk.core.warn
  5. import com.rabbitmq.client.*
  6. import java.lang.System.Logger

  7. internal class ConnectionListener : ShutdownListener, RecoveryListener {

  8.     private val log: Logger = loggerOf(this::class)

  9.     /** @see ShutdownListener.shutdownCompleted */
  10.     override fun shutdownCompleted(cause: ShutdownSignalException) {
  11.         if (cause.isHardError) { // connection error
  12.             if (!cause.isInitiatedByApplication)
  13.                 log.warn { "Connection shutdown was caused by broker " + cause.reason }
  14.             else
  15.                 log.warn { "Connection shutdown is initiated by application. Ignoring it" }
  16.         }
  17.         else { // channel error
  18.             if (!cause.isInitiatedByApplication) {
  19.                 val channel: Channel = cause.reference as Channel
  20.                 log.warn { "Channel shutdown was caused by broker " + channel.closeReason }
  21.             }
  22.             else
  23.                 log.warn { "Channel shutdown is initiated by application. Ignoring it" }
  24.         }
  25.     }

  26.     /** @see RecoveryListener.handleRecovery */
  27.     override fun handleRecovery(recoverable: Recoverable) {
  28.         if (recoverable is Connection)
  29.             log.debug { "Connection was recovered" }
  30.         else if (recoverable is Channel)
  31.             log.debug { "Connection to channel #" + recoverable.channelNumber + " was recovered" }
  32.     }

  33.     /** @see RecoveryListener.handleRecoveryStarted */
  34.     override fun handleRecoveryStarted(recoverable: Recoverable) {
  35.         log.debug { "Automatic connection recovery starts" }
  36.     }
  37. }