转载自:http://liulijun-cn-2011.iteye.com/blog/1597648
Quartz线程不是一个HTTP请求,不包含request对象,在Quartz的Job类中无法取得ServletContext,
所以无法取得ApplicationContext,Job类中的Spring注解都是无效的。现在通过定义一个静态的变量ServletContext在服务器容器启动的时候,实例化ServletContext。
ContextLoaderListener:
- public class ContextLoaderListener implements ServletContextListener {
- /**
- * @see ServletContextListener#contextInitialized(ServletContextEvent)
- */
- public void contextInitialized(final ServletContextEvent event) {
- String appRoot = event.getServletContext().getRealPath("/");
- AppContext.init(appRoot);
- QuartzContext.getInstance().setContext(event.getServletContext());
- }
- /**
- * @see ServletContextListener#contextDestroyed(ServletContextEvent)
- */
- public void contextDestroyed(final ServletContextEvent event) {
- // do nothing
- }
- }
Web.xml
- <listener>
- <listener-class>
- jp.xx.xxx.xxxxx.xxx.servlet.listener.ContextLoaderListener
- </listener-class>
- </listener>
QuartzContext:
- public class QuartzContext {
- private final static QuartzContext instance = new QuartzContext ();
- private static ServletContext context;
- private QuartzContext () {
- }
- public static QuartzContext getInstance() {
- return instance;
- }
- protected void setContext(final ServletContext context) {
- context = context;
- }
- public ServletContext getContext() {
- return context;
- }
- }
XML设定:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd">
- <bean name="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean"
- p:jobClass="jp.xx.xxx.xxxxx.xxx.quartz.base.MyJob"
- p:applicationContextJobDataKey="applicationContext">
- </bean>
- <bean id="checkImagesTrigger"
- class="org.springframework.scheduling.quartz.CronTriggerBean"
- p:jobDetail-ref="jobDetail"
- p:cronExpression="0 0 0 1 */3 ?">
- <property name="jobDataAsMap">
- <map>
- <entry key="name" value="checkImagesTrigger" />
- </map>
- </property>
- </bean>
- <bean id="checkImagesTrigger2"
- class="org.springframework.scheduling.quartz.CronTriggerBean"
- p:jobDetail-ref="jobDetail"
- p:cronExpression="10/10 * * * * ?">
- <property name="jobDataAsMap">
- <map>
- <entry key="name" value="checkImagesTrigger2" />
- </map>
- </property>
- </bean>
- <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
- <property name="triggers">
- <list>
- <ref bean="checkImagesTrigger" />
- <ref bean="checkImagesTrigger2" />
- </list>
- </property>
- <property name="schedulerContextAsMap">
- <map>
- <entry key="timeout" value="30" />
- </map>
- </property>
- <property name="configLocation" value="classpath:/config/quartz/quartz.properties" />
- </bean>
- </beans>
MyJob:
- public class MyJob extends BaseServiceImpl implements Job {
- public void execute(JobExecutionContext jctx) throws JobExecutionException {
- ServletContext scontext = QuartzContext.getInstance().getContext();
- ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(scontext);
- SysUserService sysUserService = (SysUserService)ctx.getBean("sysUserService");
- SysUser sysUser = sysUserService.getSysUser(1);
- System.out.println(sysUser.getName());
- }
- }