Quartz中在Job类中得到Spring中的WebApplicationContext

来源:互联网 发布:php分页固定套路 编辑:程序博客网 时间:2024/05/22 12:14

一、在Job类中要调用Service中的方法,首先要从Spring容器中得到,问题就来了......普通java类要得到WebApplicationContext,注意不能自己new,

listener filter启动都是被动的

从Web.xml当中我们可以看出Spring启动是靠ContextLoaderListener监听器来启动的,进入源码中可以看到,该类是继承ContextLoader的且实现了ServletContextListener接口,

所以当容器启动时,ServletContex被创建,ServletContextListener 是 ServletContext 的监听者

public class ContextLoaderListener extends ContextLoader implements ServletContextListener {

this.context = createWebApplicationContext(servletContext, parent);servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
从源码可以看出WebApplicationContext存储在ServletContext中,可以通过Key来找到,但是原因是在普通类怎么得到ServletContext

二、从Quartz文档中可以看到Quartz与Web应用,你可以通过org.quartz.ee.servlet.QuartzInitializerServlet配置也可以通过启动Quartz监听器

org.quartz.ee.servlet.QuartzInitializerListener

public class QuartzInitializerListener implements ServletContextListener {
这个类也实现了ServletContextListener接口,从而也可以获得ServletContext,从而我们可以在Web.xml中配置quartz监听器,QuartzInitializerListener

这个类实现了ServletContextListener接口中的方法contextInitialized初始化方法和contextDestroyed销毁方法,其中contextInitialized()加载配置,

我们编写一个自己的类继承QuartzInitializerListener,或者你也可以不继承,直接实现ServletContextListener接口,但是自己写的还是不那么优雅,所以我

在这里继承.(quartz.properties属性和quartz_jobs.xml、web.xml在这里省略)

public class My_QuartZInitializerListener extends QuartzInitializerListener{@Overridepublic void contextInitialized(ServletContextEvent sce) {// TODO Auto-generated method stubsuper.contextInitialized(sce);ServletContext sc=sce.getServletContext();StdSchedulerFactory fac=(StdSchedulerFactory) sc.getAttribute(QUARTZ_FACTORY_KEY);try {fac.getScheduler().getContext().put("sct", sc);} catch (SchedulerException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

以上这个类是自定义的类,先跑父类的contextInitialized方法,该方法已经帮我们封装好了quartz的配置和属性,通过源码

 servletContext.setAttribute(factoryKey, factory);
我可以通过factorykey直接去封装好了的StdSchedulerFactory,我们在My_QuartZInitializerListener类中可以在初始化方法中把ServletContext中放到调度器的上下文中。

job类

public class My_Job implements Job{public void execute(JobExecutionContext arg0) throws JobExecutionException{// TODO Auto-generated method stub//System.out.println("ABC");System.out.println("aaaaaaaaaaaaaaaaaaaa");ServletContext sct=null;try {sct = (ServletContext) arg0.getScheduler().getContext().get("sct");//sct=(ServletContext) arg0.getJobDetail().getJobDataMap().get("sct");} catch (SchedulerException e) {// TODO Auto-generated catch blocke.printStackTrace();}//WebApplicationContext webctx=WebApplicationContextUtils.getRequiredWebApplicationContext(sct);WebApplicationContext webctx=(WebApplicationContext) sct.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);TPoiService tp=webctx.getBean("poiservice", TPoiService.class);System.out.println(tp);//WebApplicationContext webctx=ContextLoader.getCurrentWebApplicationContext();//TPoiService tp=webctx.getBean("poiservice", TPoiService.class);//System.out.println(tp);}}

在My_Job类中可们可以通过JobExecutionContext得到调度器上下文,从而取到ServerContext--------WebApplicationContext-------bean
最后在web.xml中别忘记了配置:

 <listener>         <listener-class>             org.hzy.quartzs.My_QuartZInitializerListener         </listener-class>     </listener> 
和Spring中的监听器一样随tomcat的启动而启动,从而可以得到共用的ServletContext。


原创粉丝点击