Skip to content

Instantly share code, notes, and snippets.

@rtio
Last active February 10, 2017 13:41
Show Gist options
  • Select an option

  • Save rtio/195740296a159c7050630d6a8cfdae9f to your computer and use it in GitHub Desktop.

Select an option

Save rtio/195740296a159c7050630d6a8cfdae9f to your computer and use it in GitHub Desktop.
private void clear(Scheduler scheduler) {
Object quartzScheduler = getFieldValue(scheduler, "sched");
Object jobMgr = getFieldValue(quartzScheduler, "jobMgr");
@SuppressWarnings("unchecked")
Map<String, JobExecutionContext> executingJobs = (Map<String, JobExecutionContext>)getFieldValue(jobMgr, "executingJobs");
List<String> removalList = new ArrayList<String>();
for(Map.Entry<String, JobExecutionContext> entry : executingJobs.entrySet()) {
if(entry.getValue().getNextFireTime().before(new Date())) {
removalList.add(entry.getKey());
}
}
for(String key : removalList) {
executingJobs.remove(key);
}
}
private Object getFieldValue(Object parent, String fieldName) {
Object value = null;
Class clazz = parent.getClass();
Field[] fields = clazz.getFields();
fields = clazz.getDeclaredFields();
for(Field field : fields) {
if(field.getName().equalsIgnoreCase(fieldName)) {
field.setAccessible(true);
try {
value = field.get(parent);
} catch (IllegalArgumentException e) {
value = null;
} catch (IllegalAccessException e) {
value = null;
}
}
}
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment