Package org.springframework.batch.repeat

Examples of org.springframework.batch.repeat.RepeatListener


    // Make sure if we are already marked complete before we start then no
    // processing takes place.
    boolean running = !isMarkedComplete(context);

    for (int i = 0; i < listeners.length; i++) {
      RepeatListener interceptor = listeners[i];
      interceptor.open(context);
      running = running && !isMarkedComplete(context);
      if (!running)
        break;
    }

    // Return value, default is to allow continued processing.
    RepeatStatus result = RepeatStatus.CONTINUABLE;

    RepeatInternalState state = createInternalState(context);
    // This is the list of exceptions thrown by all active callbacks
    Collection<Throwable> throwables = state.getThrowables();
    // Keep a separate list of exceptions we handled that need to be
    // rethrown
    Collection<Throwable> deferred = new ArrayList<Throwable>();

    try {

      while (running) {

        /*
         * Run the before interceptors here, not in the task executor so
         * that they all happen in the same thread - it's easier for
         * tracking batch status, amongst other things.
         */
        for (int i = 0; i < listeners.length; i++) {
          RepeatListener interceptor = listeners[i];
          interceptor.before(context);
          // Allow before interceptors to veto the batch by setting
          // flag.
          running = running && !isMarkedComplete(context);
        }

        // Check that we are still running (should always be true) ...
        if (running) {

          try {

            result = getNextResult(context, callback, state);
            executeAfterInterceptors(context, result);

          }
          catch (Throwable throwable) {
            doHandle(throwable, context, deferred);
          }

          // N.B. the order may be important here:
          if (isComplete(context, result) || isMarkedComplete(context) || !deferred.isEmpty()) {
            running = false;
          }

        }

      }

      result = result.and(waitForResults(state));
      for (Throwable throwable : throwables) {
        doHandle(throwable, context, deferred);
      }

      // Explicitly drop any references to internal state...
      state = null;

    }
    /*
     * No need for explicit catch here - if the business processing threw an
     * exception it was already handled by the helper methods. An exception
     * here is necessarily fatal.
     */
    finally {

      try {

        if (!deferred.isEmpty()) {
          Throwable throwable = deferred.iterator().next();
          logger.debug("Handling fatal exception explicitly (rethrowing first of " + deferred.size() + "): "
              + throwable.getClass().getName() + ": " + throwable.getMessage());
          rethrow(throwable);
        }

      }
      finally {

        try {
          for (int i = listeners.length; i-- > 0;) {
            RepeatListener interceptor = listeners[i];
            interceptor.close(context);
          }
        }
        finally {
          context.close();
        }
View Full Code Here


    // continuing
    Throwable unwrappedThrowable = unwrapIfRethrown(throwable);
    try {

      for (int i = listeners.length; i-- > 0;) {
        RepeatListener interceptor = listeners[i];
        // This is not an error - only log at debug
        // level.
        logger.debug("Exception intercepted (" + (i + 1) + " of " + listeners.length + ")", unwrappedThrowable);
        interceptor.onError(context, unwrappedThrowable);
      }

      logger.debug("Handling exception: " + throwable.getClass().getName() + ", caused by: "
          + unwrappedThrowable.getClass().getName() + ": " + unwrappedThrowable.getMessage());
      exceptionHandler.handleException(context, unwrappedThrowable);
View Full Code Here

    // Don't re-throw exceptions here: let the exception handler deal with
    // that...

    if (value != null && value.isContinuable()) {
      for (int i = listeners.length; i-- > 0;) {
        RepeatListener interceptor = listeners[i];
        interceptor.after(context, value);
      }

    }

  }
View Full Code Here

TOP

Related Classes of org.springframework.batch.repeat.RepeatListener

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.