Ejemplo n.º 1
0
  /**
   * Handles any failure by trying to reconnect
   *
   * @param failure the reason for the failure
   */
  public void handleFailure(Throwable failure) {
    if (failure instanceof HornetQException
        && ((HornetQException) failure).getType() == HornetQExceptionType.QUEUE_DOES_NOT_EXIST) {
      HornetQRALogger.LOGGER.awaitingTopicQueueCreation(getActivationSpec().getDestination());
    } else if (failure instanceof HornetQException
        && ((HornetQException) failure).getType() == HornetQExceptionType.NOT_CONNECTED) {
      HornetQRALogger.LOGGER.awaitingJMSServerCreation();
    } else {
      HornetQRALogger.LOGGER.failureInActivation(failure, spec);
    }
    int reconnectCount = 0;
    int setupAttempts = spec.getSetupAttempts();
    long setupInterval = spec.getSetupInterval();

    // Only enter the failure loop once
    if (inFailure.getAndSet(true)) return;
    try {
      Throwable lastException = failure;
      while (deliveryActive.get() && (setupAttempts == -1 || reconnectCount < setupAttempts)) {
        teardown();

        try {
          Thread.sleep(setupInterval);
        } catch (InterruptedException e) {
          HornetQRALogger.LOGGER.debug("Interrupted trying to reconnect " + spec, e);
          break;
        }

        if (reconnectCount < 1) {
          HornetQRALogger.LOGGER.attemptingReconnect(spec);
        }
        try {
          setup();
          HornetQRALogger.LOGGER.reconnected();
          break;
        } catch (Throwable t) {
          if (failure instanceof HornetQException
              && ((HornetQException) failure).getType()
                  == HornetQExceptionType.QUEUE_DOES_NOT_EXIST) {
            if (lastException == null || !(t instanceof HornetQNonExistentQueueException)) {
              lastException = t;
              HornetQRALogger.LOGGER.awaitingTopicQueueCreation(
                  getActivationSpec().getDestination());
            }
          } else if (failure instanceof HornetQException
              && ((HornetQException) failure).getType() == HornetQExceptionType.NOT_CONNECTED) {
            if (lastException == null || !(t instanceof HornetQNotConnectedException)) {
              lastException = t;
              HornetQRALogger.LOGGER.awaitingJMSServerCreation();
            }
          } else {
            HornetQRALogger.LOGGER.errorReconnecting(t, spec);
          }
        }
        ++reconnectCount;
      }
    } finally {
      // Leaving failure recovery loop
      inFailure.set(false);
    }
  }