@SuppressWarnings("unchecked") public void registerTask(ScheduledTask task) throws SchedulerException, ClassNotFoundException { LOG.debug("Registrando task {}", task); Class<? extends Job> jobClass = (Class<? extends Job>) classLoader.loadClass(task.getClassName()); registerJob(jobClass, task.getCronExpression()); }
@SuppressWarnings("unchecked") public void execute(ScheduledTask task) throws SchedulerException, ClassNotFoundException { LOG.debug("Ejecutando tarea {}", task); Class<? extends Job> jobClass = (Class<? extends Job>) classLoader.loadClass(task.getClassName()); String name = String.format("%s_%s", System.currentTimeMillis(), jobClass.getName()); JobDetail jobDetail = JobBuilder.newJob(jobClass).withDescription(name).withIdentity(name).build(); SimpleScheduleBuilder builder = SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(1).withRepeatCount(1); Trigger trigger = TriggerBuilder.newTrigger().withIdentity("Trigger_" + name).withSchedule(builder).build(); scheduler.scheduleJob(jobDetail, trigger); }
/** * Podemos consultar la aplicaciĆ³n <a href="http://www.cronmaker.com/">cronmaker.com</a> para * generar las expresiones cron. * * @throws SchedulerException */ @SuppressWarnings("unchecked") public void registerJobs() throws SchedulerException { LOG.debug("Registrando tareas programadas"); try { EntityManager entityManager = entityManagerProvider.get(); String qlString = "select e from ScheduledTask e where e.disabled is null or e.disabled = false"; TypedQuery<ScheduledTask> query = entityManager.createQuery(qlString, ScheduledTask.class); List<ScheduledTask> tasks = query.getResultList(); ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); LOG.debug("Encontradas {} tareas programadas", tasks.size()); for (ScheduledTask task : tasks) { try { Class<? extends Job> jobClass = (Class<? extends Job>) classLoader.loadClass(task.getClassName()); registerJob(jobClass, task.getCronExpression(), task.getParams()); } catch (Exception ex) { LOG.error("Error al registrar la tarea {}", task); } } } catch (Exception ex) { throw new RuntimeException("Error al registrar las tareas programadas", ex); } }
public void unregisterTask(ScheduledTask task) throws SchedulerException { LOG.debug("Eliminando task {}", task); JobKey jobKey = new JobKey(task.getClassName()); scheduler.deleteJob(jobKey); }