xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- Scheduler task -->
<bean name="schedulerTask" class="com.mkyong.quartz.SchedulerTask" />
<!-- Scheduler job -->
<bean name="schedulerJob"
class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.mkyong.quartz.SchedulerJob" />
<property name="jobDataAsMap">
<map>
<entry key="schedulerTask" value-ref="schedulerTask" />
</map>
</property>
</bean>
<!-- Cron Trigger -->
<bean id="cronTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="schedulerJob" />
<property name="cronExpression" value="0/10 * * * * ?" />
</bean>
<!-- Scheduler -->
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
<list>
<ref bean="schedulerJob" />
</list>
</property>
<property name="triggers">
<list>
<ref bean="cronTrigger" />
</list>
</property>
</bean>
</beans>
石英工作:
package com.mkyong.quartz;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
public class SchedulerJob extends QuartzJobBean
{
private SchedulerTask schedulerTask;
public void setSchedulerTask(SchedulerTask schedulerTask) {
this.schedulerTask = schedulerTask;
}
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
schedulerTask.printSchedulerMessage();
}
}
要执行的任务:
package com.mkyong.quartz;
public class SchedulerTask {
public void printSchedulerMessage() {
System.out.println("Struts 2 + Spring + Quartz ......");
}
}
我想在任务类中注入另一个处理数据库的DTO类 在任务中做一些数据库工作,怎么做?
答案 0 :(得分:14)
在您的解决方案中,您在未由Spring实例化的类中使用spring @Autowired批注。如果删除@Autowired注释,您的解决方案仍然有效,因为Quartz正在设置属性,而不是Spring。
Quartz将尝试将JobDataMap中的每个键设置为属性。例如。因为你有一个键“myDao”Quartz将寻找一个名为“setMyDao”的方法,并将键的值传递给该方法。
如果您希望Spring将spring bean注入您的作业,请创建一个SpringBeanJobFactory并使用Spring环境中的 jobFactory 属性将其设置为SchedulerFactoryBean。
SpringBeanJobFactory javadoc:
应用调度程序上下文,作业数据映射和触发数据映射条目 作为bean属性值
答案 1 :(得分:10)
不确定这是否是您想要的,但您可以将一些配置值传递给Quartz作业。我相信你可以利用你已经设置的jobDataAsMap
属性,例如:
<property name="jobDataAsMap">
<map>
<entry key="schedulerTask" value-ref="schedulerTask" />
<entry key="param1" value="com.custom.package.ClassName"/>
</map>
</property>
然后,您应该能够以手动方式在实际Java代码中访问它:
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
schedulerTask.printSchedulerMessage();
System.out.println(context.getJobDetail().getJobDataMap().getString("param1"));
}
或者使用神奇的Spring方法 - 使用getter / setter定义param1
属性。您可以尝试使用java.lang.Class
类型定义它并自动完成(Spring会为您完成):
private Class<?> param1;
// getter & setter
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
schedulerTask.printSchedulerMessage();
System.out.println("Class injected" + getParam1().getName());
}
我还没有测试过它。
答案 2 :(得分:9)
ApplicationContext springContext =
WebApplicationContextUtils.getWebApplicationContext(
ContextLoaderListener.getCurrentWebApplicationContext().getServletContext()
);
Bean bean = (Bean) springContext.getBean("beanName");
bean.method();
答案 3 :(得分:3)
如inject bean reference into a Quartz job in Spring?中所述,您可以使用spring SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
@Named
public class SampleJob implements Job {
@Inject
private AService aService;
@Override
public void execute(JobExecutionContext context)
throws JobExecutionException {
//Do injection with spring
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
aService.doIt();
}
}
如前所述,它可能不会出现在某些弹簧版本上,但我已经在4.2.1.RELEASE上进行了测试,它运行良好。
答案 4 :(得分:0)
这是我的解决方案:
public class MySpringBeanJobFactory extends
org.springframework.scheduling.quartz.SpringBeanJobFactory implements
ApplicationContextAware {
private ApplicationContext ctx;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.ctx = applicationContext;
}
@Override
protected Object createJobInstance(TriggerFiredBundle bundle)
throws Exception {
Object jobInstance = super.createJobInstance(bundle);
ctx.getAutowireCapableBeanFactory().autowireBean(jobInstance);
return jobInstance;
}
}
然后在xml中配置MySpringBeanJobFactory类:
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobFactory">
<bean class="com.xxxx.MySpringBeanJobFactory" />
</property>
<property name="configLocation" value="classpath:quartz.properties" />
<property name="triggers">
<list>
<ref bean="cronTrigger"/>
</list>
</property>
</bean>
祝你好运! :)