quartz scheduler - How to inject Spring DAO class in QuartzJobBean, using JobDetailFactoryBean Annotated way -


how inject spring dao class in quartzjobbean, using jobdetailfactorybean instantiated @ config class level. using spring4 quartz 2.2.1 annotation ways

            @configuration             public class schedulerconfig {             @bean             public jobdetailfactorybean jobdetailfactorybean(){             jobdetailfactorybean factory = new jobdetailfactorybean();             factory.setjobclass(schedulerservice.class);              //should inject dao here??              factory.setgroup("mygroup");             factory.setname("myjob");             return factory;             }             } 

quartzjobbean been extended execute

            @persistjobdataafterexecution             @disallowconcurrentexecution             @service             public class schedulerservice extends quartzjobbean {              @autowire             public schedulerdao schdao;              protected void executeinternal(jobexecutioncontext ctx) throws jobexecutionexception {              system.out.println("---schedulerservice .executeinternal ----");              try {             init(ctx.getjobdetail().getjobdatamap(),              ctx.getscheduler().getcontext());             } catch (schedulerexception e) {             e.printstacktrace();             }              // want dao methods used here - how that??>              //can access dao             schdao.getsomemethods();             } 

your problem quartz, not know configuration class, can't autowired unknown bean. go schedulerconfig.java , declare service or dao autowired, use jobdata , map object in order passed in quartz. can use inside executeinternal method of quartz. example

@configuration  public class mplampla{  @autowired private yourdao dao; @bean public jobdetailfactorybean jobdetailfactorybean(){     jobdetailfactorybean factory = new jobdetailfactorybean();     map<string,object> map = new hashmap<string,object>();     map.put("yourdao", dao);     factory.setjobdataasmap(map);     return factory; } } 

then in yourjob.java

public class yourjob extends quartzjobbean { @override protected void executeinternal(jobexecutioncontext ctx) throws jobexecutionexception {     jobdatamap datamap = ctx.getjobdetail().getjobdatamap();     yourdao dao = (yourdao) datamap.get("yourdao");     dao.getmethods(); } } 

be carefull if using @service class , yourdao class @transational, error undeclared bean. in case have pass inteface of dao.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -