com.elephantdrummer.executor.AsynchronousJobExecutor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of drummer Show documentation
Show all versions of drummer Show documentation
Elephant Drummer Java Job Scheduler
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 extends DrummerJobProvider> observeClass() {
return getJobLogicProvider().getClass();
}
}