Package org.springframework.batch.repeat

Examples of org.springframework.batch.repeat.RepeatContext


    String msg = policy.toString();
    assertTrue("String does not contain chunk size", msg.indexOf("chunkSize=2")>=0);
  }

  public void testKeyName() throws Exception, IOException {
    RepeatContext context = policy.start(null);
    assertFalse(policy.isComplete(context));
  }
View Full Code Here


   * Get the parent context (the retry is in an inner "chunk" loop and we want
   * the exception to be handled at the outer "step" level).
   * @return the {@link RepeatContext} that should hold the exhausted flag.
   */
  private RepeatContext getRepeatContext() {
    RepeatContext context = RepeatSynchronizationManager.getContext();
    if (context.getParent() != null) {
      return context.getParent();
    }
    return context;
  }
View Full Code Here

  @Test
  public void testContextClosedOnNormalCompletion() throws Exception {

    final List<String> list = new ArrayList<String>();

    final RepeatContext context = new RepeatContextSupport(null) {
            @Override
      public void close() {
        super.close();
        list.add("close");
      }
View Full Code Here

  @Test
  public void testContextClosedOnAbnormalCompletion() throws Exception {

    final List<String> list = new ArrayList<String>();

    final RepeatContext context = new RepeatContextSupport(null) {
            @Override
      public void close() {
        super.close();
        list.add("close");
      }
View Full Code Here

    Assert.notNull(context, "The context must be provided to initialize a counter");

    this.countKey = countKey;
    this.useParent = useParent;

    RepeatContext parent = context.getParent();

    if (this.useParent && parent != null) {
      this.context = parent;
    }
    else {
View Full Code Here

   * @see org.springframework.batch.repeat.RepeatOperations#iterate(org.springframework.batch.repeat.RepeatCallback)
   */
    @Override
  public RepeatStatus iterate(RepeatCallback callback) {

    RepeatContext outer = RepeatSynchronizationManager.getContext();

    RepeatStatus result = RepeatStatus.CONTINUABLE;
    try {
      // This works with an asynchronous TaskExecutor: the
      // interceptors have to wait for the child processes.
View Full Code Here

   *
   */
  private RepeatStatus executeInternal(final RepeatCallback callback) {

    // Reset the termination policy if there is one...
    RepeatContext context = start();

    // 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

   * Delegate to the {@link CompletionPolicy}.
   *
   * @see org.springframework.batch.repeat.CompletionPolicy#start(RepeatContext)
   */
  protected RepeatContext start() {
    RepeatContext parent = RepeatSynchronizationManager.getContext();
    RepeatContext context = completionPolicy.start(parent);
    RepeatSynchronizationManager.register(context);
    logger.debug("Starting repeat context.");
    return context;
  }
View Full Code Here

  /**
   * Convenience method to set the current repeat operation to complete if it
   * exists.
   */
  public static void setCompleteOnly() {
    RepeatContext context = getContext();
    if (context != null) {
      context.setCompleteOnly();
    }
  }
View Full Code Here

   *
   * @param context a new context at the start of a batch.
   * @return the old value if there was one.
   */
  public static RepeatContext register(RepeatContext context) {
    RepeatContext oldSession = getContext();
    RepeatSynchronizationManager.contextHolder.set(context);
    return oldSession;
  }
View Full Code Here

TOP

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

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.