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

com.elephantdrummer.executor.AsynchronousJobExecutor Maven / Gradle / Ivy

There is a newer version: 1.2.5
Show newest version
package com.elephantdrummer.executor;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

import com.elephantdrummer.container.Container;
import com.elephantdrummer.executor.base.ExecutorBase;
import com.elephantdrummer.executor.base.JobLogicWrapperNoData;
import com.elephantdrummer.executor.base.JobType;
import com.elephantdrummer.executor.base.structure.DrummerJobProvider;
import com.elephantdrummer.executor.engine.DrummerFactoryBuilder;
import com.elephantdrummer.tool.HistoryBuffer;

/**
 * Copyright 2018 Elephant Software Klaudiusz Wojtkowiak e-mail: [email protected]
 * 
 * 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. 
 */
public class AsynchronousJobExecutor extends ExecutorBase>> {
	 
	
	public AsynchronousJobExecutor() {
		historyBuffer=Container.getElement(HistoryBuffer.class);
	}
	
	@Override
	protected final JobType getJobType() {
		return JobType.ASYNCHRONOUS_JOB;
	}
	

	 @Override
	public Queue> call() throws InterruptedException, ExecutionException{
		
		if (isEnabled()==false|| (isUnderExecution()==true&&isSkipExecutionWhenPreviousJobIsRunning()) ){
			log.fine("Thread "+Thread.currentThread().getId()+ " is under execution. Skipping...");
			return null;
		}
		
		setUnderExecution(true);
		 ConcurrentLinkedQueue> results=null;
		   
		 
		try{
			
		    startJobProcedure();
		    
		    if (isAbandoned()) return null;
		    
		    ThreadFactory drummerThreadfactory = new DrummerFactoryBuilder()
					.setNamePrefix("Drummer-Job").setDaemon(false)
					.setPriority(Thread.MAX_PRIORITY).build();
		    
			//pool with n threads
		    ExecutorService exec = isCacheThreads()?Executors.newCachedThreadPool(drummerThreadfactory):Executors.newFixedThreadPool(getPoolSize(),drummerThreadfactory);
		    results = new ConcurrentLinkedQueue>();
		    getJobData().setPoolSize(getPoolSize());
		    
	
		    for (int i=0;i entry = results.poll();
	            if (entry != null) {
	                entry.get();
	            }
	        }
		
			finishJobProcedure();
		}catch (Exception e){
			e.printStackTrace();
		}finally{
			setUnderExecution(false);
		}
    	return results;
	}
	
	void shutdownAndAwaitTermination(ExecutorService pool) {
	    pool.shutdown(); // Disable new tasks from being submitted
	    try {
	        // Wait a while for existing tasks to terminate
	        if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
	            pool.shutdownNow(); // Cancel currently executing tasks
	            // Wait a while for tasks to respond to being cancelled
	            if (!pool.awaitTermination(60, TimeUnit.SECONDS))
	                System.err.println("Pool did not terminate");
	        }
	    } catch (InterruptedException ie) {
	        // (Re-)Cancel if current thread also interrupted
	        pool.shutdownNow();
	        // Preserve interrupt status
	        Thread.currentThread().interrupt();
	    }
	}
	
    
	@Override
	public Class observeClass() {
		return getJobLogicProvider().getClass();
	}
	


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy