@Override public JobHandle getJobHandle() { findDelegate(); return delegate.getJobHandle(); }
@Override public Void call() throws Exception { try { this.delegate.call(); return null; } catch (Exception e) { GlobalJDKJobHandle jobHandle = (GlobalJDKJobHandle) ((TimerJobInstance)this.delegate).getJobHandle(); if (retries < FAILED_JOB_RETRIES) { ScheduledFuture<Void> future = this.scheduler.schedule( this, FAILED_JOB_DELAY, TimeUnit.MILLISECONDS ); jobHandle.setFuture( future ); retries++; } else { logger.error("Timer execution failed {} times in a roll, unscheduling ({})", FAILED_JOB_RETRIES, jobHandle); } throw e; } }
@Override public void afterCompletion(int status) { if ( status == TransactionManager.STATUS_COMMITTED && !timerJobInstance.getJobHandle().isCancel()) { this.schedulerService.internalSchedule(timerJobInstance); } }
public void removeTimerJobInstance(TimerJobInstance instance) { Long sessionId = null; JobContext ctx = instance.getJobContext(); if (ctx instanceof SelfRemovalJobContext) { ctx = ((SelfRemovalJobContext) ctx).getJobContext(); } Map<Long, TimerJobInstance> instances = null; if (ctx instanceof ProcessJobContext) { sessionId = ((ProcessJobContext)ctx).getSessionId(); instances = timerInstances.get(sessionId); if (instances == null) { instances = new ConcurrentHashMap<Long, TimerJobInstance>(); timerInstances.put(sessionId, instances); } } else { instances = singleTimerInstances; } instances.remove( instance.getJobHandle().getId() ); if (sessionId != null && instances.isEmpty()) { timerInstances.remove(sessionId); } }
public void addTimerJobInstance(TimerJobInstance instance) { JobContext ctx = instance.getJobContext(); if (ctx instanceof SelfRemovalJobContext) { ctx = ((SelfRemovalJobContext) ctx).getJobContext(); } Map<Long, TimerJobInstance> instances = null; if (ctx instanceof ProcessJobContext) { long sessionId = ((ProcessJobContext)ctx).getSessionId(); instances = timerInstances.get(sessionId); if (instances == null) { instances = new ConcurrentHashMap<Long, TimerJobInstance>(); timerInstances.put(sessionId, instances); } } else { instances = singleTimerInstances; } instances.put( instance.getJobHandle().getId(), instance ); }
protected void findDelegate() { if (delegate == null) { Collection<TimerJobInstance> timers = TimerServiceRegistry.getInstance().get(timerServiceId) .getTimerJobFactoryManager().getTimerJobInstances(); for (TimerJobInstance instance : timers) { if (((GlobalQuartzJobHandle)instance.getJobHandle()).getJobName().equals(jobname)) { delegate = instance; break; } } } }
@Override public void internalSchedule(TimerJobInstance timerJobInstance) { if (scheduler.isShutdown()) { return; } Date date = timerJobInstance.getTrigger().hasNextFireTime(); Callable<Void> item = (Callable<Void>) timerJobInstance; GlobalJDKJobHandle jobHandle = (GlobalJDKJobHandle) timerJobInstance.getJobHandle(); long then = date.getTime(); long now = System.currentTimeMillis(); ScheduledFuture<Void> future = null; if ( then >= now ) { future = scheduler.schedule( new RetriggerCallable(scheduler, item), then - now, TimeUnit.MILLISECONDS ); } else { future = scheduler.schedule( new RetriggerCallable(scheduler, item), 0, TimeUnit.MILLISECONDS ); } jobHandle.setFuture( future ); globalTimerService.getTimerJobFactoryManager().addTimerJobInstance( timerJobInstance ); }
@Override public void internalSchedule(TimerJobInstance timerJobInstance) { GlobalQuartzJobHandle quartzJobHandle = (GlobalQuartzJobHandle) timerJobInstance.getJobHandle();
return timerJobInstance.getJobHandle();
@Override public JobHandle getJobHandle() { findDelegate(); return delegate.getJobHandle(); }
@Override public Void call() throws Exception { try { this.delegate.call(); return null; } catch (Exception e) { GlobalJDKJobHandle jobHandle = (GlobalJDKJobHandle) ((TimerJobInstance)this.delegate).getJobHandle(); if (retries < FAILED_JOB_RETRIES) { ScheduledFuture<Void> future = this.scheduler.schedule( this, FAILED_JOB_DELAY, TimeUnit.MILLISECONDS ); jobHandle.setFuture( future ); retries++; } else { logger.error("Timer execution failed {} times in a roll, unscheduling ({})", FAILED_JOB_RETRIES, jobHandle); } throw e; } }
@Override public void afterCompletion(int status) { if ( status == TransactionManager.STATUS_COMMITTED && !timerJobInstance.getJobHandle().isCancel()) { this.schedulerService.internalSchedule(timerJobInstance); } }
public void removeTimerJobInstance(TimerJobInstance instance) { JobContext ctx = instance.getJobContext(); if (ctx instanceof SelfRemovalJobContext) { ctx = ((SelfRemovalJobContext) ctx).getJobContext(); } Map<Long, TimerJobInstance> instances = null; if (hasMethod(ctx.getClass(), "getSessionId")) { int sessionId = getMethodIntValue(ctx, "getSessionId"); instances = timerInstances.get(sessionId); if (instances == null) { instances = new ConcurrentHashMap<Long, TimerJobInstance>(); timerInstances.put(sessionId, instances); } } else { instances = singleTimerInstances; } instances.remove( instance.getJobHandle().getId() ); }
public void internalSchedule(TimerJobInstance timerJobInstance) { TimerConfig config = new TimerConfig(new EjbTimerJob(timerJobInstance), true); Date expirationTime = timerJobInstance.getTrigger().hasNextFireTime(); logger.debug("Timer expiration date is {}", expirationTime); if (expirationTime != null) { timerService.createSingleActionTimer(expirationTime, config); logger.debug("Timer scheduled {} on {} scheduler service", timerJobInstance); localCache.putIfAbsent(((EjbGlobalJobHandle) timerJobInstance.getJobHandle()).getUuid(), timerJobInstance); } else { logger.info("Timer that was to be scheduled has already expired"); } }
public void addTimerJobInstance(TimerJobInstance instance) { JobContext ctx = instance.getJobContext(); if (ctx instanceof SelfRemovalJobContext) { ctx = ((SelfRemovalJobContext) ctx).getJobContext(); } Map<Long, TimerJobInstance> instances = null; if (hasMethod(ctx.getClass() ,"getSessionId")) { int sessionId = getMethodIntValue(ctx, "sessionId"); instances = timerInstances.get(sessionId); if (instances == null) { instances = new ConcurrentHashMap<Long, TimerJobInstance>(); timerInstances.put(sessionId, instances); } } else { instances = singleTimerInstances; } instances.put( instance.getJobHandle().getId(), instance ); }
public TimerJobInstance getTimerByName(String jobName) { if (localCache.containsKey(jobName)) { logger.debug("Found job {} in cache returning", jobName); return localCache.get(jobName); } TimerJobInstance found = null; for (Timer timer : timerService.getTimers()) { try { Serializable info = timer.getInfo(); if (info instanceof EjbTimerJob) { EjbTimerJob job = (EjbTimerJob) info; EjbGlobalJobHandle handle = (EjbGlobalJobHandle) job.getTimerJobInstance().getJobHandle(); localCache.putIfAbsent(jobName, handle.getTimerJobInstance()); if (handle.getUuid().equals(jobName)) { logger.debug("Job {} does match timer and is going to be returned", jobName); found = handle.getTimerJobInstance(); } } } catch (NoSuchObjectLocalException e) { logger.debug("Timer info for {} was not found ", timer); } } return found; }
public boolean removeJob(JobHandle jobHandle) { EjbGlobalJobHandle ejbHandle = (EjbGlobalJobHandle) jobHandle; for (Timer timer : timerService.getTimers()) { try { Serializable info = timer.getInfo(); if (info instanceof EjbTimerJob) { EjbTimerJob job = (EjbTimerJob) info; EjbGlobalJobHandle handle = (EjbGlobalJobHandle) job.getTimerJobInstance().getJobHandle(); if (handle.getUuid().equals(ejbHandle.getUuid())) { logger.debug("Job handle {} does match timer and is going to be canceled", jobHandle); localCache.remove(handle.getUuid()); try { timer.cancel(); } catch (Throwable e) { logger.debug("Timer cancel error due to {}", e.getMessage()); return false; } return true; } } } catch (NoSuchObjectLocalException e) { logger.debug("Timer {} has already expired or was canceled ", timer); } } logger.debug("Job handle {} does not match any timer on {} scheduler service", jobHandle, this); return false; }
protected void findDelegate() { if (delegate == null) { Collection<TimerJobInstance> timers = TimerServiceRegistry.getInstance().get(timerServiceId) .getTimerJobFactoryManager().getTimerJobInstances(); for (TimerJobInstance instance : timers) { if (((GlobalQuartzJobHandle)instance.getJobHandle()).getJobName().equals(jobname)) { delegate = instance; break; } } } }
@Override public void internalSchedule(TimerJobInstance timerJobInstance) { if (scheduler.isShutdown()) { return; } Date date = timerJobInstance.getTrigger().hasNextFireTime(); Callable<Void> item = (Callable<Void>) timerJobInstance; GlobalJDKJobHandle jobHandle = (GlobalJDKJobHandle) timerJobInstance.getJobHandle(); long then = date.getTime(); long now = System.currentTimeMillis(); ScheduledFuture<Void> future = null; if ( then >= now ) { future = scheduler.schedule( new RetriggerCallable(scheduler, item), then - now, TimeUnit.MILLISECONDS ); } else { future = scheduler.schedule( new RetriggerCallable(scheduler, item), 0, TimeUnit.MILLISECONDS ); } jobHandle.setFuture( future ); globalTimerService.getTimerJobFactoryManager().addTimerJobInstance( timerJobInstance ); }
@Override public JobHandle scheduleJob(Job job, JobContext ctx, Trigger trigger) { Long id = idCounter.getAndIncrement(); String jobName = getJobName(ctx, id); EjbGlobalJobHandle jobHandle = new EjbGlobalJobHandle(id, jobName, ((GlobalTimerService) globalTimerService).getTimerServiceId()); TimerJobInstance jobInstance = null; // check if given timer job is marked as new timer meaning it was never scheduled before, // if so skip the check by timer name as it has no way to exist if (!isNewTimer(ctx)) { jobInstance = scheduler.getTimerByName(jobName); if (jobInstance != null) { return jobInstance.getJobHandle(); } } jobInstance = globalTimerService.getTimerJobFactoryManager().createTimerJobInstance( job, ctx, trigger, jobHandle, (InternalSchedulerService) globalTimerService); jobHandle.setTimerJobInstance((TimerJobInstance) jobInstance); interceptor.internalSchedule(jobInstance); return jobHandle; }