quartz2与springmvc集成的2种方式
第一种方式:jobDetail使用MethodInvokingJobDetailFactoryBean
(1)spring配置
<!-- 定时任务Bean: quartz获取不到注解的userService, 使用xml文件再配一个UserService对象set注入Job -->
<bean id="myJob" class="cn.mgr.job.demo.MyJob">
<property name="userService" ref="_userService"></property>
</bean>
<bean id="_userService" class="cn.mgr.user.service.UserService"></bean>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="myJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject" ref="myJob" />
<!-- 调用类中的方法 -->
<property name="targetMethod" value="execute" />
<!-- 是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程 -->
<property name="concurrent" value="false" />
</bean>
<!-- quartz-2.x后的配置 -->
<bean id="myJobTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="myJobDetail" />
</property>
<property name="cronExpression">
<value>0/5 * * * * ?</value>
</property>
</bean>
<!-- 总管理类:如果lazy-init='false',那么容器启动后就会执行调度程序 -->
<!-- 如果lazy-init='true',则需要实例化该bean才能执行调度程序 -->
<bean name="simpleScheduler" lazy-init="true" autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="myJobTrigger" />
</list>
</property>
<property name="autoStartup" value="true"/>
</bean>
(2)Job
public class MyJob {
private IUserService userService;
public void execute(){
System.out.println((new SimpleDateFormat("yyyy-MM-dd hh:mm:ss:SSS")).format(new Date()));
System.out.println(userService.getMessage() + "%S%%");
}
public void setUserService(IUserService userService) {
this.userService = userService;
}
}
第二种方式:jobDetail使用org.springframework.scheduling.quartz.JobDetailFactoryBean
(1)spring配置
<bean id="roleService2" class="cn.mgr.role.service.RoleService"></bean>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="simpleJobDetail"
class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<!-- 调用的类
<property name="targetObject" ref="simpleJob" />-->
<!-- 调用类中的方法
<property name="targetMethod" value="execute" /> -->
<!-- 是否允许任务并发执行。当值为false时,表示必须等到前一个线程处理完毕后才再启一个新的线程
<property name="concurrent" value="false" />-->
<property name="jobClass" value="cn.mgr.job.demo.SimpleJob" />
<property name="group" value="TEST" />
<property name="durability" value="true" />
</bean>
<!-- quartz-2.x后的配置 -->
<bean id="simpleJobTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="simpleJobDetail" />
</property>
<property name="cronExpression">
<value>0/5 * * * * ?</value>
</property>
</bean>
<!-- 总管理类:如果lazy-init='false',那么容器启动后就会执行调度程序 -->
<!-- 如果lazy-init='true',则需要实例化该bean才能执行调度程序 -->
<bean name="simpleScheduler"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleJobTrigger" />
</list>
</property>
<!-- <property name="autoStartup" value="true"/> -->
<!-- 延迟30秒启动Scheduler -->
<property name="startupDelay" value="20"></property>
<!-- 通过applicationContextSchedulerContextKey属性配置spring上下文 -->
<property name="applicationContextSchedulerContextKey">
<value>applicationContext</value>
</property>
<!-- <property name="dataSource" ref="dataSource" /> -->
<property name="schedulerContextAsMap">
<map>
<!-- spring 管理的service需要放到这里,才能够注入成功 -->
<description>schedulerContextAsMap</description>
<entry key="roleService2" value-ref="roleService2"/>
<entry key="userService" value-ref="userService"/>
</map>
</property>
<!-- <property name="configLocation" value="classpath:quartz.properties" /> -->
</bean>
(2)Job
public abstract class Job extends QuartzJobBean{
protected void init(JobExecutionContext context){
Scheduler scheduler = (Scheduler) context.getScheduler();
DataSource dataSource = null;
//获取JobExecutionContext中的service对象
SchedulerContext schCtx = null;
try {
schCtx = context.getScheduler().getContext();
} catch (SchedulerException e) {
e.printStackTrace();
}
//获取Spring中的上下文
ApplicationContext appCtx = (ApplicationContext)schCtx.get(Const.APPLICATION_CONTEXT);
//sqlSessionFactory = (SqlSessionFactory)appCtx.getBean(Const.SQL_SESSION_FACTORY);
dataSource = (DataSource)appCtx.getBean(Const.DATA_SOURCE);
System.out.println("**** ds = "+ dataSource);
Object o = appCtx.getBean("roleService2");
System.out.println("**** roleService = "+ o);
Object userService = appCtx.getBean("userService");
System.out.println("**** userService = "+ userService);
}
}
public class SimpleJob extends Job{
private IRoleService roleService;
@Override
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
init(context);
System.out.println("roleService=" + roleService);
}
}