All Downloads are FREE. Search and download functionalities are using the official Maven repository.

co.cask.cdap.etl.batch.CompositeFinisher Maven / Gradle / Ivy

/*
 * Copyright © 2016 Cask Data, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package co.cask.cdap.etl.batch;

import co.cask.cdap.etl.api.batch.BatchConfigurable;
import co.cask.cdap.etl.api.batch.BatchContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * A finisher of one or more {@link BatchConfigurable BatchConfigurables}.
 * If any of them throws an exception, the exception will be logged before moving on to finish the other objects.
 */
public class CompositeFinisher implements Finisher {
  private static final Logger LOG = LoggerFactory.getLogger(CompositeFinisher.class);
  private final List finishers;

  private CompositeFinisher(List finishers) {
    this.finishers = finishers;
  }

  /**
   * Run logic on program finish.
   *
   * @param succeeded whether the program run succeeded or not
   */
  public void onFinish(boolean succeeded) {
    for (Finisher finisher : finishers) {
      finisher.onFinish(succeeded);
    }
  }

  /**
   * @return builder for creating a composite finisher.
   */
  public static Builder builder() {
    return new Builder();
  }

  /**
   * Builder to create a composite finisher
   */
  public static class Builder {
    private final List finishers;

    public Builder() {
      this.finishers = new ArrayList<>();
    }

    public  Builder add(final BatchConfigurable configurable, final T context) {
      finishers.add(new Finisher() {
        @Override
        public void onFinish(boolean succeeded) {
          try {
            configurable.onRunFinish(succeeded, context);
          } catch (Throwable t) {
            LOG.warn("Error calling onRunFinish on stage {}.", context.getStageName(), t);
          }
        }
      });
      return this;
    }

    public CompositeFinisher build() {
      return new CompositeFinisher(finishers);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy