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

com.alicloud.openservices.tablestore.tunnel.pipeline.Pipeline Maven / Gradle / Ivy

Go to download

Aliyun Open Services SDK for Java Copyright (C) Alibaba Cloud Computing All rights reserved. 版权所有 (C)阿里云计算有限公司 http://www.aliyun.com

The newest version!
package com.alicloud.openservices.tablestore.tunnel.pipeline;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Pipeline extends AbstractStage {
    private static final Logger LOG = LoggerFactory.getLogger(Pipeline.class);

    private final List> stages = new LinkedList>();
    /**
     * helperExecutor is used for initialization, error handling during runtime, etc.
     */
    private final ExecutorService helperExecutor;

    public Pipeline(final ExecutorService helperExecutor) {
        super();
        this.helperExecutor = helperExecutor;
    }

    /**
     * Initialize the correlation (before/after relationship) of each Stage.
     *
     * @param context
     */
    @Override
    public void init(PipelineContext context) {
        Stage preStage = this;
        for (Stage stage : stages) {
            preStage.setNextStage(stage);
            preStage = stage;
        }
        helperExecutor.submit(new PipelineInitTask(context, stages));
    }

    static class PipelineInitTask implements Runnable {
        final List> stages;
        final PipelineContext context;

        public PipelineInitTask(PipelineContext context, List> stages) {
            this.context = context;
            this.stages = stages;
        }

        @Override
        public void run() {
            try {
                for (Stage stage : stages) {
                    stage.init(context);
                }
            } catch (Exception e) {
                LOG.error("Pipeline Init Error", e);
            }
        }
    }


    public  void addExecutorForStage(Stage stage, ExecutorService executorService) {
        stages.add(new ThreadPoolStageDecorator(stage, executorService));
    }


    @Override
    @SuppressWarnings("unchecked")
    public void process(INPUT input) {
        if (!stages.isEmpty()) {
            Stage firstStage = (Stage)stages.get(0);
            firstStage.process(input);
        }
    }

    @Override
    public OUTPUT doProcess(INPUT input) throws StageException {
        return null;
    }

    @Override
    public void shutdown() {
        shutdown(false);
    }

    /**
     * Close the current Pipeline.
     * @param isHalt: true means the thread pool resources need to be closed, false means not to close.
     */
    public void shutdown(boolean isHalt) {
        for (Stage stage : stages) {
            stage.shutdown();
        }
        if (isHalt) {
            LOG.info("shutdown pipeline helper executor.");
            helperExecutor.shutdownNow();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy