targetMethod:指定将指定Bean实例的该方法包装成任务的执行体。
二、实例(JobDetailBean方式)
1、将spring核心jar包、quartz.jar和Spring-context-support.jar导入类路径。
2、编写Job类PunchJob(该类必须继承QuartzJobBean)
package org.crazyit.hrsystem.schedule; import java.util.Date; import org.springframework.scheduling.quartz.QuartzJobBean; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.crazyit.hrsystem.service.EmpManager; public class PunchJob extends QuartzJobBean { //判断作业是否执行的旗标 private boolean isRunning = false; //该作业类所依赖的业务逻辑组件 private EmpManager empMgr; public void setEmpMgr(EmpManager empMgr){ this.empMgr = empMgr; } //定义任务执行体 public void executeInternal(JobExecutionContext ctx) throws JobExecutionException { if (!isRunning){ System.out.println("开始调度自动打卡"); isRunning = true; //调用业务逻辑方法 empMgr.autoPunch(); isRunning = false; } } }
3、编写quartz.xml配置文件
<?xml version="1.0" encoding="GBK"?> <!-- 指定Spring配置文件的Schema信息 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-lazy-init="false"> <!-- 定义<span style="font-family:'courier new','courier';">一</span>个业务逻辑组件,继承业务逻辑组件的模板 --> <bean id="empManager" class="org.crazyit.hrsystem.service.impl.EmpManagerImpl" parent="managerTemplate"/> <span style="white-space:pre"> </span><!-- 定义触发器来管理任务Bean --> <span style="white-space:pre"> </span><bean id="cronTriggerPunch" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <span style="white-space:pre"> </span><!-- 使用嵌套Bean的方式来定义任务Bean --> <bean class="org.springframework.scheduling.quartz.JobDetailBean"> <!-- 指定任务Bean的实现类 --> <property name="jobClass" value="org.crazyit.hrsystem.schedule.PunchJob"/> <!-- 为任务Bean注入属性 --> <property name="jobDataAsMap"> <map> <entry key="empMgr" value-ref="empManager"/> </map> </property> </bean> </property> <!-- 指定Cron表达式:周一到周五7点、12点执行调度 --> <property name="cronExpression" value="0 0 7,12 ? * MON-FRI"/> </bean> <!-- 执行实际的调度器--> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTriggerPunch"></ref> <!-- <ref local="cronTriggerPunch"/> 两者都可以用 --> </list> </property> </bean> </beans>
1、配置文件:
<beans> <!-- 调度工厂 --> <bean id="scheduler" lazy-init="true" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="startupDelay" value="30"/> <property name="triggers"> <list> <!--<ref bean="exposureTrigger"/>--> </list> </property> </bean> <!-- 定义触发时间 --> <bean id="exposureTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="exposureDetail"></property> <property name="cronExpression"> <value>*/5 * * * * ?</value> </property> </bean> <!-- 自定义任务 --> <bean id="exposureDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="adResourceServiceImpl"></property> <property name="targetMethod" value="executeExposure"></property> <property name="concurrent" value="false" /> </bean> </beans>
2、java类
public void executeExposure() throws Exception{ //1获取可变现的广告位 List<AdResource> listIis = adResourceIisDao.selectParam(new HashMap()); for (AdResource ad : listIis) { Long id = ad.getId(); Integer expValue = adResourceIamsDao.count(id, "getExposureValue"); if (expValue != null) { Map map = new HashMap(); map.put("expValue", expValue); map.put("id", id); adResourceIisDao.updateParam(map,"updateExp"); } }