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

com.jpattern.batch.core.CallableJobExecutor Maven / Gradle / Ivy

package com.jpattern.batch.core;

import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicBoolean;

import com.jpattern.batch.JobExecutor;
import com.jpattern.batch.AJob;
import com.jpattern.batch.JobExecutionStrategy;
import com.jpattern.batch.JobStatus;
import com.jpattern.batch.logger.JobPoolLogger;
import com.jpattern.batch.util.PauseThread;
import com.jpattern.logger.ILogger;

/**
 * 
 * @author Francesco Cina'
 *
 * 27/mar/2010
 */
public class CallableJobExecutor implements JobExecutor, Callable{

	private final AJob job;
	private final AtomicBoolean serverStarted;
	private final JobStatusImpl jobStatus;
	private final ILogger logger = JobPoolLogger.getLogger(this.getClass());

	public CallableJobExecutor(final AJob job, final JobExecutionStrategy jobExecutionStrategy, final AtomicBoolean booleanWrapper) {
		this.job = job;
		this.serverStarted = booleanWrapper;
		this.jobStatus = new JobStatusImpl(job.getName(), job.getGroup());
		this.setExecutionStrategy(jobExecutionStrategy);
	}

	@Override
	public void setExecutionStrategy(final JobExecutionStrategy aJobExecution) {
		this.jobStatus.setExecutionStrategy( aJobExecution );
	}

	@Override
	public void kill() {
		this.jobStatus.kill();
	}

	@Override
	public Object call() throws Exception {
		//System.out.println("partito!!");
		while(!this.jobStatus.isKilled()) {
			this.checkInterruptStatus();
			//System.out.println("pre-eseguito");
			if (this.serverStarted.get() && !this.jobStatus.isPaused()) {
				//System.err.println("ESEGUITO!!!!");
				this.jobStatus.setRunning(true);
				this.execute();
				this.jobStatus.setRunning(false);
			}
			new PauseThread().pauseThread();
		}
		return new Object();
	}

	private void execute() {
		if ( this.jobStatus.getExecutionStrategy().canExecute() ) {
			this.jobStatus.executionStarted(new Date());
			this.logger.info("Starting execution of job name [" + this.job.getName() + "] group [" + this.job.getGroup() + "]");
			try {
				this.job.execute();
				this.jobStatus.executionEnded(new Date());
				this.logger.info("Ended execution of job name [" + this.job.getName() + "] group [" + this.job.getGroup() + "]");
			} catch (final Exception e) {
				this.jobStatus.executionError(new Date());
				this.logger.error("Error during execution of job name [" + this.job.getName() + "] group [" + this.job.getGroup() + "]", e);
			}
		}
	}

	private void checkInterruptStatus() throws InterruptedException {
		if (Thread.interrupted()) {
			throw new InterruptedException("Thread was interrupted");
		}
	}

	@Override
	public JobStatus getJobStatus() {
		return this.jobStatus;
	}

	@Override
	public void pause() {
		if (!this.jobStatus.isPaused()) {
			this.jobStatus.pause();
		}
	}

	@Override
	public void resume() {
		if (this.jobStatus.isPaused()) {
			this.jobStatus.resume();
		}
	}

}