com.elephantdrummer.DrummerBoot 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;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PreDestroy;
import com.elephantdrummer.annotation.DrummerJob;
import com.elephantdrummer.annotation.TestJob;
import com.elephantdrummer.classconfig.DrummerJobConfiguration;
import com.elephantdrummer.classconfig.JobClassConfig;
import com.elephantdrummer.configuration.DrummerPropertyHolder;
import com.elephantdrummer.container.Container;
import com.elephantdrummer.container.ContainerElement;
import com.elephantdrummer.exception.DrummerException;
import com.elephantdrummer.exception.DrummerMethodCanNotBeExecutedException;
import com.elephantdrummer.executor.AsynchronousJobExecutor;
import com.elephantdrummer.executor.SimpleJobExecutor;
import com.elephantdrummer.executor.base.ExecutorBase;
import com.elephantdrummer.executor.base.structure.DrummerJobProvider;
import com.elephantdrummer.executor.base.structure.DrummerMethodJobWrapper;
import com.elephantdrummer.executor.engine.ManagedScheduledExecutorService;
import com.elephantdrummer.scope.DrummerObservable;
import com.elephantdrummer.tool.PlatformInformation;
import com.elephantdrummer.tool.ProducerCDIObjectFromClass;
import com.elephantdrummer.tool.reflect.ScannerDrummerJobProvided;
import com.elephantdrummer.trigger.Trigger;
import com.elephantdrummer.trigger.base.DrumTrigger;
import com.elephantdrummer.trigger.resolver.TriggerResolver;
import com.elephantdrummer.trigger.test.JobTester;
import com.elephantdrummer.validator.IValidatorDrummerJob;
/**
* 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 DrummerBoot implements ContainerElement,DrummerJobManager{
private Logger log=Logger.getLogger(DrummerBoot.class.getSimpleName());
private PlatformInformation platformInformation=Container.getElement(PlatformInformation.class);
private TriggerResolver triggerResolver=Container.getElement(TriggerResolver.class);
private ProducerCDIObjectFromClass cdiproducer=Container.getElement(ProducerCDIObjectFromClass.class);
private Set singletonControlSet=new HashSet<>();
private List> executorsList=new LinkedList>();
private List> onStartupList=new LinkedList>();
private int commonThreadPoolSize=-1;
private DrummerPropertyHolder propertyHolder=Container.getElement(DrummerPropertyHolder.class);
private ManagedScheduledExecutorService managedscheduler=new ManagedScheduledExecutorService();
private List mytasks=new LinkedList<>();
private List drummerObservsableInstanves;
protected JobTester tester=Container.getElement(JobTester.class);
public final void boot(List singletons) {
if (singletons==null) return;
log.info("boot for "+singletons.size()+" classes");
if (drummerObservsableInstanves==null) {
drummerObservsableInstanves=singletons;
}
mytasks=ScannerDrummerJobProvided.getCreatedDrummerJobsFromAnnotations(singletons,mytasks);
managedscheduler.setCommonThreadPoolSize(getCommonThreadPoolSize());
int okjobs=0;
int skippedjobs=0;
int testedjobs=0;
for (DrummerMethodJobWrapper wrapperJob:mytasks){
Boolean succes=false;
try{
succes=addDrummerJob(managedscheduler,wrapperJob);
}catch(DrummerException de){
log.log(Level.SEVERE,de.getDrummerError()+" - "+de.getMessage());
}
if (Boolean.TRUE.equals(succes)){
okjobs=okjobs+1;
log.info("Drummer Job: "+wrapperJob.toString()+" has been scheduled.");
}else if (Boolean.FALSE.equals(succes)){
log.severe("ERROR: Drummer Job: "+wrapperJob.toString()+" has been skipped because of error.");
skippedjobs=skippedjobs+1;
}else{
//null- test only
testedjobs=testedjobs+1;
}
}
String version=DrummerBoot.class.getPackage().getImplementationVersion();
String msg="***** Elephant Drummer "+(version==null?"":version)+" started. Jobs scheduled: "+okjobs+" Errors: "+skippedjobs+" Tests: "+testedjobs+" *****";
// String msg2=" Free for non-commercial usage. Purchase commecial licence on www.elephantdrummer.com ";
String msg2=" (C) Elephant Software Klaudiusz Wojtkowiak, www.elephantdrummer.com ";
StringBuffer sb=new StringBuffer();
for (int i=0;i job:onStartupList){
try {
job.call();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
platformInformation.setAllJobs(executorsList);
platformInformation.init();
if (managedscheduler.isActive()==false) {
managedscheduler.setActive(true);
managedscheduler.start();
}
}
// private DrummerJob retrieveDrummerJobAnnotation(DrummerJobProvider joblogicclass) {
// DrummerJob drummerjobannotation = getMethod(joblogicclass).getAnnotation(DrummerJob.class);
// if (drummerjobannotation == null) {
// drummerjobannotation = joblogicclass.getClass().getAnnotation(DrummerJob.class);
// }
// return drummerjobannotation;
// }
private Method getMethod(DrummerJobProvider joblogicclass) {
Method method = null;
try {
method = joblogicclass.getClass().getMethod(DrummerJobProvider.JOB_METHOD_SIGNATURE);
} catch (NoSuchMethodException | SecurityException e) {
log.log(Level.SEVERE, "initiation error", e);
throw new DrummerMethodCanNotBeExecutedException(method,e);
}
return method;
}
private final Boolean addDrummerJob(ManagedScheduledExecutorService managedscheduler, DrummerMethodJobWrapper joblogicclass){
DrummerJob drummerjobannotation=joblogicclass.getDrummerJobAnnotation();
Method method=getMethod(joblogicclass);
// if (drummerjobannotation==null){
// drummerjobannotation=joblogicclass.getClass().getAnnotation(DrummerJob.class);
// }
IValidatorDrummerJob.isDrummerJobCorrect(drummerjobannotation);
if (IValidatorDrummerJob.isDrummerJobNeedExternalConfiguration(drummerjobannotation)){
drummerjobannotation=propertyHolder.getDrummerJob(drummerjobannotation.name());
}
Class extends ExecutorBase>> executorClass=null;
if (joblogicclass.getCustomisedDrummerJobConfiguration()==null){
//Determine executor...
if (drummerjobannotation.threads()>1){
executorClass=AsynchronousJobExecutor.class;
}else{
executorClass=SimpleJobExecutor.class;
}
log.fine("Found executor class: "+executorClass.getName());
}else{
JobClassConfig customTrigger=joblogicclass.getCustomisedDrummerJobConfiguration();
//Jesli wskazany jest DummyExecutor lub null - odczytaj klase egzekutora na podstaie parametrow
//Determine executor...
if (customTrigger instanceof JobClassConfig && customTrigger.getDrummerJob()==null==false&&customTrigger.getDrummerJob().threads()>1){
executorClass=AsynchronousJobExecutor.class;
}else{
executorClass=SimpleJobExecutor.class;
}
}
DrumTrigger trigger=null;
if (joblogicclass.getCustomisedDrummerJobConfiguration()==null){
trigger=triggerResolver.getTrigger(drummerjobannotation);
}else{
trigger=triggerResolver.getTrigger(joblogicclass.getCustomisedDrummerJobConfiguration().getDrummerJob());
}
ExecutorBase> executor=cdiproducer.get(executorClass);
DrummerJob dj=joblogicclass.getCustomisedDrummerJobConfiguration()==null?drummerjobannotation:joblogicclass.getCustomisedDrummerJobConfiguration().getDrummerJob();
TestJob test=joblogicclass.getTestJobAnnotation() ;//: method.getAnnotation(TestJob.class);
if (test==null==false){
tester.testTrigger(joblogicclass.toString(),
trigger,
test);
}
if (test==null==false&&test.executeJob()==false) {
return null;
}
executor.init(dj,method,joblogicclass,trigger);
managedscheduler.schedule(executor, trigger, dj);
executorsList.add(executor);
singletonControlSet.add(joblogicclass.toString());
if (drummerjobannotation.onStartup()) onStartupList.add(executor);
return Boolean.TRUE;
}
@PreDestroy
public final void destroy() {
managedscheduler.setActive(false);
managedscheduler.clear();
for (ExecutorBase> exec:executorsList){
exec.setEnabled(false);
}
for (ExecutorBase> exec:executorsList){
exec.destroy();
}
}
public int getCommonThreadPoolSize() {
return commonThreadPoolSize;
}
public void setCommonThreadPoolSize(int commonThreadPoolSize) {
this.commonThreadPoolSize = commonThreadPoolSize;
}
public void setConfigProperties(Map map){
propertyHolder.addProperties(map);
}
/******* Drummer Job Manager impementations ***/
@Override
public void executeJobImmediatelly(String name) {
try {
managedscheduler.getJobCallable(name).call();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public Callable> getJobCallable(String name) {
return managedscheduler.getJobCallable(name);
}
@Override
public Trigger getJobTrigger(String name) {
for (DrummerMethodJobWrapper job:mytasks) {
if (name.equals(job.getDrummerJobAnnotation().name())) {
job.getCustomisedDrummerJobConfiguration().getDrumTrigger();
}
}
return null;
}
@Override
public boolean scheduleAllDrummerJobs(DrummerObservable clazz) {
if (clazz==null) return false;
List in=new LinkedList<>();
in.add(clazz);
boot(in);
return true;
}
@Override
public DrumTrigger getTriggerOf(DrummerJobConfiguration config) {
return triggerResolver.getTrigger(config.getDrummerJob());
}
}