@Override
public void jobWasExecuted(final JobExecutionContext context, final JobExecutionException jobException) {
final Trigger trigger = context.getTrigger();
final JobKey key = context.getJobDetail().getKey();
final String jobKey = key.getName() + SchedulerServiceConstants.JOB_KEY_SEPERATOR + key.getGroup();
final ScheduledJobDetail scheduledJobDetails = this.schedularService.findByJobKey(jobKey);
final Long version = this.schedularService.fetchMaxVersionBy(jobKey) + 1;
String status = SchedulerServiceConstants.STATUS_SUCCESS;
String errorMessage = null;
String errorLog = null;
if (jobException != null) {
status = SchedulerServiceConstants.STATUS_FAILED;
this.stackTraceLevel = 0;
final Throwable throwable = getCauseFromException(jobException);
this.stackTraceLevel = 0;
StackTraceElement[] stackTraceElements = null;
errorMessage = throwable.getMessage();
stackTraceElements = throwable.getStackTrace();
final StringBuffer sb = new StringBuffer(throwable.toString());
for (final StackTraceElement element : stackTraceElements) {
sb.append("\n \t at ").append(element.getClassName()).append(".").append(element.getMethodName()).append("(")
.append(element.getLineNumber()).append(")");
}
errorLog = sb.toString();
}
String triggerType = SchedulerServiceConstants.TRIGGER_TYPE_CRON;
if (context.getMergedJobDataMap().containsKey(SchedulerServiceConstants.TRIGGER_TYPE_REFERENCE)) {
triggerType = context.getMergedJobDataMap().getString(SchedulerServiceConstants.TRIGGER_TYPE_REFERENCE);
}
if (triggerType == SchedulerServiceConstants.TRIGGER_TYPE_CRON && trigger.getNextFireTime() != null
&& trigger.getNextFireTime().after(scheduledJobDetails.getNextRunTime())) {
scheduledJobDetails.updateNextRunTime(trigger.getNextFireTime());
}
scheduledJobDetails.updatePreviousRunStartTime(context.getFireTime());
scheduledJobDetails.updateCurrentlyRunningStatus(false);
final ScheduledJobRunHistory runHistory = new ScheduledJobRunHistory(scheduledJobDetails, version, context.getFireTime(),
new Date(), status, errorMessage, triggerType, errorLog);
// scheduledJobDetails.addRunHistory(runHistory);