1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
private void loadBean2JEC(JobExecutionContext jec, String label, Class clazz) throws SchedulerException {
//on charge le contexte applicatif
ApplicationContext appContext = (ApplicationContext) jec.getScheduler().getContext().get("applicationContext");
//on créé le bean
Object bean = appContext.getAutowireCapableBeanFactory().createBean(clazz,
AutowireCapableBeanFactory.AUTOWIRE_BY_NAME,
false);
appContext.getAutowireCapableBeanFactory().autowireBean(bean);
//on injecte le bean dans le jec
jec.put(label, bean);
}
protected void executeInternal(JobExecutionContext jec) throws JobExecutionException {
//on vérifie si le job est activé sur ce contexte (LB)
Boolean trigger = Boolean.FALSE;
SchedulerContext schedulerContext = null;
try {
schedulerContext = jec.getScheduler().getContext();
} catch (SchedulerException e) {
throw new JobExecutionException("Failure accessing scheduler context", e);
}
ApplicationContext appContext = (ApplicationContext) schedulerContext.get("applicationContext");
String jobBeanName = (String) jec.getJobDetail().getJobDataMap().get("job.bean.name");
log.info("Starting job: " + jobBeanName);
I_JobInterface jobBean = (I_JobInterface) appContext.getBean(jobBeanName);
try {
//on injecte la sf dans le jec.
SessionFactory sf = (SessionFactory) appContext.getBean("sessionFactory", SessionFactory.class);
jec.put("sessionFactory", sf);
//On injecte les DAO dans le jec.
loadBean2JEC(jec, "testDAO", testDAO.class);
jobBean.init(jec);
jobBean.execute();
} catch (Exception e) {
e.printStackTrace();
} finally {
jobBean.destroy();
log.info("Job finished: " + jobBeanName);
}
} |
Partager