Package org.jberet.spi.ThreadContextSetup

Examples of org.jberet.spi.ThreadContextSetup.TearDownHandle


        return split.getFlows();
    }

    @Override
    public void run() {
        final TearDownHandle handle = jobContext.getBatchEnvironment().getThreadContextSetup().setup();
        try {
            batchContext.setBatchStatus(BatchStatus.STARTED);
            final List<Flow> flows = split.getFlows();
            final CountDownLatch latch = new CountDownLatch(flows.size());
            try {
                for (final Flow f : flows) {
                    runFlow(f, latch);
                }
                latch.await(SPLIT_FLOW_TIMEOUT_SECONDS, TimeUnit.SECONDS);

                //check FlowResults from each flow
                final List<FlowExecutionImpl> fes = batchContext.getFlowExecutions();
                for (int i = 0; i < fes.size(); i++) {
                    if (fes.get(i).getBatchStatus().equals(BatchStatus.FAILED)) {
                        batchContext.setBatchStatus(BatchStatus.FAILED);
                        for (final AbstractContext c : batchContext.getOuterContexts()) {
                            c.setBatchStatus(BatchStatus.FAILED);
                        }
                        break;
                    }
                }
                if (batchContext.getBatchStatus().equals(BatchStatus.STARTED)) {
                    batchContext.setBatchStatus(BatchStatus.COMPLETED);
                }
            } catch (Throwable e) {
                LOGGER.failToRunJob(e, jobContext.getJobName(), split.getId(), split);
                for (final AbstractContext c : batchContext.getOuterContexts()) {
                    c.setBatchStatus(BatchStatus.FAILED);
                }
            }

            if (batchContext.getBatchStatus() == BatchStatus.COMPLETED) {
                final String next = split.getAttributeNext()//split has no transition elements
                if (next != null) {
                    //the last StepExecution of each flow is needed if the next element after this split is a decision
                    final List<FlowExecutionImpl> fes = batchContext.getFlowExecutions();
                    final StepExecution[] stepExecutions = new StepExecution[fes.size()];
                    for (int i = 0; i < fes.size(); i++) {
                        stepExecutions[i] = fes.get(i).getLastStepExecution();
                    }
                    enclosingRunner.runJobElement(next, stepExecutions);
                }
            }
        } finally {
            handle.tearDown();
        }
    }
View Full Code Here


        return job.getJobElements();
    }

    @Override
    public void run() {
        final TearDownHandle handle = jobContext.getBatchEnvironment().getThreadContextSetup().setup();
        try {
            // the job may be stopped right after starting
            if (batchContext.getBatchStatus() != BatchStatus.STOPPING) {
                batchContext.setBatchStatus(BatchStatus.STARTED);
            }
            try {
                // run job listeners beforeJob()
                for (final JobListener l : batchContext.getJobListeners()) {
                    try {
                        l.beforeJob();
                    } catch (Throwable e) {
                        BatchLogger.LOGGER.failToRunJob(e, job.getId(), "", l);
                        batchContext.setBatchStatus(BatchStatus.FAILED);
                        return;
                    }
                }

                runFromHeadOrRestartPoint(batchContext.getJobExecution().getRestartPoint());

                for (final JobListener l : batchContext.getJobListeners()) {
                    try {
                        l.afterJob();
                    } catch (Throwable e) {
                        BatchLogger.LOGGER.failToRunJob(e, job.getId(), "", l);
                        batchContext.setBatchStatus(BatchStatus.FAILED);
                        return;
                    }
                }
            } catch (Throwable e) {
                BatchLogger.LOGGER.failToRunJob(e, job.getId(), "", job);
                batchContext.setBatchStatus(BatchStatus.FAILED);
            }

            batchContext.destroyArtifact(batchContext.getJobListeners());

            if (batchContext.getBatchStatus() == BatchStatus.STARTED) {
                batchContext.setBatchStatus(BatchStatus.COMPLETED);
            } else if (batchContext.getBatchStatus() == BatchStatus.STOPPING) {
                batchContext.setBatchStatus(BatchStatus.STOPPED);
            }
            batchContext.saveInactiveStepExecutions();
            batchContext.getJobExecution().cleanUp();
        } finally {
            handle.tearDown();
        }
    }
View Full Code Here

        return flow.getJobElements();
    }

    @Override
    public void run() {
        final TearDownHandle handle = jobContext.getBatchEnvironment().getThreadContextSetup().setup();
        try {
            batchContext.setBatchStatus(BatchStatus.STARTED);
            jobContext.setBatchStatus(BatchStatus.STARTED);

            try {
                runFromHeadOrRestartPoint(null);
            } catch (Throwable e) {
                LOGGER.failToRunJob(e, jobContext.getJobName(), flow.getId(), flow);
                batchContext.setBatchStatus(BatchStatus.FAILED);
                for (final AbstractContext c : batchContext.getOuterContexts()) {
                    c.setBatchStatus(BatchStatus.FAILED);
                }
            } finally {
                if (latch != null) {
                    latch.countDown();
                }
            }

            if (batchContext.getBatchStatus() == BatchStatus.STARTED) {  //has not been marked as failed, stopped or abandoned
                batchContext.setBatchStatus(BatchStatus.COMPLETED);
            }

            if (batchContext.getBatchStatus() == BatchStatus.COMPLETED) {
                final String next = resolveTransitionElements(flow.getTransitionElements(), flow.getAttributeNext(), false);
                enclosingRunner.runJobElement(next, batchContext.getFlowExecution().getLastStepExecution());
            }
        } finally {
            handle.tearDown();
        }
    }
View Full Code Here

        createChunkRelatedListeners();
    }

    @Override
    public void run() {
        final TearDownHandle handle = jobContext.getBatchEnvironment().getThreadContextSetup().setup();
        try {
            try {
                ut.setTransactionTimeout(checkpointTimeout());
                ut.begin();
                try {
                    itemReader.open(batchContext.getStepExecution().getReaderCheckpointInfo());
                    itemWriter.open(batchContext.getStepExecution().getWriterCheckpointInfo());
                } catch (Exception e) {
                    ut.rollback();
                    throw e;
                }
                ut.commit();

                readProcessWriteItems();

                ut.begin();
                try {
                    itemReader.close();
                    itemWriter.close();
                } catch (Exception e) {
                    ut.rollback();
                    throw e;
                }
                //collect data at the end of the partition
                if (collector != null) {
                    stepRunner.collectorDataQueue.put(collector.collectPartitionData());
                }
                ut.commit();
            } catch (Exception e) {
                batchContext.setException(e);
                LOGGER.failToRunJob(e, jobContext.getJobName(), batchContext.getStepName(), chunk);
                batchContext.setBatchStatus(BatchStatus.FAILED);
            } finally {
                try {
                    if (stepRunner.collectorDataQueue != null) {
                        stepRunner.collectorDataQueue.put(batchContext.getStepExecution());
                    }
                } catch (InterruptedException e) {
                    //ignore
                }
                if (stepRunner.completedPartitionThreads != null) {
                    stepRunner.completedPartitionThreads.offer(Boolean.TRUE);
                }
                jobContext.destroyArtifact(itemReader, itemWriter, itemProcessor, collector, checkpointAlgorithm);
                jobContext.destroyArtifact(allChunkRelatedListeners);
            }
        } finally {
            handle.tearDown();
        }
    }
View Full Code Here

            if (timeLimit > 0) {
                final Timer timer = new Timer("chunk-checkpoint-timer", true);
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        final TearDownHandle handle = jobContext.getBatchEnvironment().getThreadContextSetup().setup();
                        try {
                            processingInfo.timerExpired = true;
                        } finally {
                            handle.tearDown();
                        }
                    }
                }, timeLimit * 1000);
            }
        } else {
View Full Code Here

        initPartitionConfig();
    }

    @Override
    public void run() {
        final TearDownHandle handle = jobContext.getBatchEnvironment().getThreadContextSetup().setup();
        try {
            final Boolean allowStartIfComplete = batchContext.getAllowStartIfComplete();
            if (allowStartIfComplete != Boolean.FALSE) {
                try {
                    final List<Step> executedSteps = jobContext.getExecutedSteps();
                    if (executedSteps.contains(step)) {
                        final StringBuilder stepIds = BatchUtil.toElementSequence(executedSteps);
                        stepIds.append(step.getId());
                        throw LOGGER.loopbackStep(step.getId(), stepIds.toString());
                    }


                    final int startLimit = step.getStartLimitInt();
                    if (startLimit > 0) {
                        final int startCount = stepExecution.getStartCount();
                        if (startCount >= startLimit) {
                            throw LOGGER.stepReachedStartLimit(step.getId(), startLimit, startCount);
                        }
                    }

                    stepExecution.incrementStartCount();
                    batchContext.setBatchStatus(BatchStatus.STARTED);
                    jobContext.getJobRepository().addStepExecution(jobContext.getJobExecution(), stepExecution);

                    final Chunk chunk = step.getChunk();
                    final RefArtifact batchlet = step.getBatchlet();
                    if (chunk == null && batchlet == null) {
                        batchContext.setBatchStatus(BatchStatus.ABANDONED);
                        LOGGER.stepContainsNoChunkOrBatchlet(id);
                        return;
                    }

                    if (chunk != null && batchlet != null) {
                        batchContext.setBatchStatus(BatchStatus.ABANDONED);
                        LOGGER.cannotContainBothChunkAndBatchlet(id);
                        return;
                    }

                    for (final StepListener l : stepListeners) {
                        l.beforeStep();
                    }

                    runBatchletOrChunk(batchlet, chunk);

                    //record the fact this step has been executed
                    executedSteps.add(step);

                    for (final StepListener l : stepListeners) {
                        try {
                            l.afterStep();
                        } catch (Throwable e) {
                            BatchLogger.LOGGER.failToRunJob(e, jobContext.getJobName(), step.getId(), l);
                            batchContext.setBatchStatus(BatchStatus.FAILED);
                            return;
                        }
                    }
                    batchContext.savePersistentData();
                } catch (Throwable e) {
                    LOGGER.failToRunJob(e, jobContext.getJobName(), step.getId(), step);
                    if (e instanceof Exception) {
                        batchContext.setException((Exception) e);
                    } else {
                        batchContext.setException(new BatchRuntimeException(e));
                    }
                    batchContext.setBatchStatus(BatchStatus.FAILED);
                }

                jobContext.destroyArtifact(mapper, reducer, analyzer);
                jobContext.destroyArtifact(stepListeners);

                final BatchStatus stepStatus = batchContext.getBatchStatus();
                switch (stepStatus) {
                    case STARTED:
                        batchContext.setBatchStatus(BatchStatus.COMPLETED);
                        break;
                    case FAILED:
                        for (final AbstractContext e : batchContext.getOuterContexts()) {
                            e.setBatchStatus(BatchStatus.FAILED);
                        }
                        break;
                    case STOPPING:
                        batchContext.setBatchStatus(BatchStatus.STOPPED);
                        break;
                }
            }

            if (batchContext.getBatchStatus() == BatchStatus.COMPLETED) {
                final String next = resolveTransitionElements(step.getTransitionElements(), step.getAttributeNext(), false);
                enclosingRunner.runJobElement(next, stepExecution);
            }
        } finally {
            handle.tearDown();
        }
    }
View Full Code Here

        this.batchlet = batchlet;
    }

    @Override
    public void run() {
        final TearDownHandle handle = jobContext.getBatchEnvironment().getThreadContextSetup().setup();
        try {
            try {
                final RefArtifact collectorConfig;
                if (stepRunner.collectorDataQueue != null) {
                    collectorConfig = batchContext.getStep().getPartition().getCollector();
                    if (collectorConfig != null) {
                        collector = jobContext.createArtifact(collectorConfig.getRef(), null, collectorConfig.getProperties(), batchContext);
                    }
                }
                batchletObj = jobContext.createArtifact(batchlet.getRef(), null, batchlet.getProperties(), batchContext);
                jobContext.getBatchEnvironment().getExecutorService().submit(new Runnable() {
                    @Override
                    public void run() {
                        final TearDownHandle handle = jobContext.getBatchEnvironment().getThreadContextSetup().setup();
                        try {
                            jobContext.getJobExecution().awaitStop(JobExecutionImpl.JOB_EXECUTION_TIMEOUT_SECONDS_DEFAULT, TimeUnit.SECONDS);
                            if (batchContext.getBatchStatus() == BatchStatus.STARTED) {
                                batchContext.setBatchStatus(BatchStatus.STOPPING);
                                batchletObj.stop();
                            }
                        } catch (Exception e) {
                            LOGGER.failToStopJob(e, jobContext.getJobName(), batchContext.getStepName(), batchletObj);
                        } finally {
                            handle.tearDown();
                        }
                    }
                });

                final String exitStatus = batchletObj.process();
                batchContext.setExitStatus(exitStatus);
                if (collector != null) {
                    stepRunner.collectorDataQueue.put(collector.collectPartitionData());
                }
            } catch (Exception e) {
                //TODO remove this block.  collector is not called for unhandled exceptions.
                try {
                    if (collector != null) {
                        stepRunner.collectorDataQueue.put(collector.collectPartitionData());
                    }
                } catch (Exception e1) {
                    //ignore
                }
                batchContext.setException(e);
                LOGGER.failToRunBatchlet(e, batchlet);
                batchContext.setBatchStatus(BatchStatus.FAILED);
            } finally {
                try {
                    if (stepRunner.collectorDataQueue != null) {
                        stepRunner.collectorDataQueue.put(batchContext.getStepExecution());
                    }
                } catch (InterruptedException e) {
                    //ignore
                }
                if (stepRunner.completedPartitionThreads != null) {
                    stepRunner.completedPartitionThreads.offer(Boolean.TRUE);
                }
                jobContext.destroyArtifact(batchletObj, collector);
            }
        } finally {
            handle.tearDown();
        }
    }
View Full Code Here

TOP

Related Classes of org.jberet.spi.ThreadContextSetup.TearDownHandle

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.