com.elephantdrummer.executor.engine.ManagedScheduledExecutorService 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.engine;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.logging.Logger;
import com.elephantdrummer.annotation.DrummerJob;
import com.elephantdrummer.dictionary.DictPeriod;
import com.elephantdrummer.executor.base.ExecutorBase;
import com.elephantdrummer.trigger.Trigger;
/**
* 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 ManagedScheduledExecutorService extends Thread{
private volatile boolean active=false;
private static final long period_to_interrupt_waiting_thread=DictPeriod.PERIOD_SECOND*30; //30 seconds
private volatile List jobCollection=new LinkedList<>();
private volatile Set nextToRun;
private volatile int commonThreadPoolSize=-1;
private volatile long millis=-1;
private ThreadFactory drummerThreadfactory = null;
private ExecutorService pool=null;
private ExecutorService drummerCachedPool;
Logger log=Logger.getLogger(ManagedScheduledExecutorService.class.getSimpleName());
public void schedule(Callable callable,Trigger trigger,DrummerJob dj){
log.fine("add new job .... trigger: "+trigger.toString()+" drumjob "+dj);
jobCollection.add(new DrummerJobWrapper(dj.name(),callable, trigger,dj.cacheThreads()));
}
@Override
public void run(){
while (isActive()){
if (jobCollection==null){
waitForMe(1000*10);
}else{
nextToRun= getNextRunTime();
if (nextToRun==null||nextToRun.isEmpty()) return;
waitForMe(nextToRun.iterator().next().howMuchLonger());
//waitUntil( (new Date()).getTime()+nextToRun.get(0).howMuchLonger());
if (isAlive()==false) return;
if (nextToRun.size()>0){
// ExecutorService cachedpool=Executors.newCachedThreadPool(drummerThreadfactory);
// ExecutorService pool = Executors.newFixedThreadPool(nextToRun.size(),drummerThreadfactory);
for (DrummerJobWrapper wr:nextToRun){
ExecutorBase> executor=(ExecutorBase>)wr.getJob();
try{
if (wr.isCachedThread()) {
getCachedPool().submit(executor);
}else {
getDrummerPool().submit(executor);
}
}catch (Throwable t){
t.printStackTrace();
}
}
// getDrummerPool().shutdown();
//getCachedPool().shutdown();
}
}
}
getDrummerPool().shutdown();
getCachedPool().shutdown();
}
private ExecutorService getDrummerPool() {
if (pool==null) {
pool=Executors.newFixedThreadPool(nextToRun.size(),getDrummerFactory());
}
return pool;
}
private boolean isTheSameDateWithSecondAccurancy(Date first,Date second){
if (first==null||second==null) return false;
long d1= (first.getTime()/DictPeriod.PERIOD_SECOND)*DictPeriod.PERIOD_SECOND;
long d2= (second.getTime()/DictPeriod.PERIOD_SECOND)*DictPeriod.PERIOD_SECOND;
return d1==d2;
}
private ThreadFactory getDrummerFactory() {
if (drummerThreadfactory==null) {
drummerThreadfactory = new DrummerFactoryBuilder()
.setNamePrefix("Drummer-T").setDaemon(false)
.setPriority(Thread.MAX_PRIORITY).build();
}
return drummerThreadfactory;
}
private ExecutorService getCachedPool() {
if (drummerCachedPool==null) {
drummerCachedPool =Executors.newCachedThreadPool(getDrummerFactory());
}
return drummerCachedPool;
}
public Set getNextRunTime() {
Set toExecuteAtTheMoment=new HashSet<>();
if (jobCollection==null||jobCollection.isEmpty()) return new HashSet<>();
Collections.sort(jobCollection);
DrummerJobWrapper first=jobCollection.get(0);
Date firstdate=first.getTrigger().getNextRunTime(new Date());
toExecuteAtTheMoment.add(first);
Date executeat=null;
for (int i=1;i0){
try {
Thread.sleep(period_to_interrupt_waiting_thread getJobCallable(String name) {
for (DrummerJobWrapper djw:jobCollection) {
if (name.equals(djw.getName())){
return djw.getJob();
}
}
return null;
}
// public Trigger getJobTrigger(String name) {
// System.out.println("szukam dla "+name+" mam elementow "+jobCollection.size());
// for (DrummerJobWrapper djw:jobCollection) {
// if (name.equals(djw.getName())){
// System.out.println("mam trigger dla "+name);
// return djw.getTrigger();
// }else {
// System.out.println("name "+name+" jest inne od "+djw.getName());
// }
// }
// return null;
// }
}