1.applicationContext.xml
<bean id="custTicketJobDetail" class="org.springframework.scheduling.<span style="color:#ff0000;">quartz.JobDetailBean</span>"> <property name="jobClass"> <value><span style="color:#ff0000;">com.555.common.MyDetailQuartzJobBean</span></value> </property> <property name="jobDataAsMap"> <map> <entry key="targetObject" value="customerTicketService" /> <entry key="targetMethod" value="updateCustTicketStatus" /> </map> </property> <property name="durability" value="true" /> </bean> <bean id="custTicketTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"><!-- jobDetail是Spring定时器的特殊属性targetObject --> <ref bean="custTicketJobDetail" /> </property> <property name="cronExpression"> <!-- 每天的凌晨10分时触发 --> <value>0/5 * * * * ?</value><!-- cronExpression是Spring定时器的特殊属性 --> </property> <!-- <property name="startDelay">延时1分钟启动定时任务 <value>60000</value> </property> --> </bean>
<bean autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="dataSource"> <ref bean="DataSource"/> </property> <property name="applicationContextSchedulerContextKey" value="applicationContext" /> <property name="configLocation" value="classpath:quartz.properties" /> <property name="triggers"> <!-- triggers是Spring定时器的特殊属性 --> <list> <ref bean="custTicketTrigger" /> </list> </property> </bean>
2.MyDetailQuartzJobBean.java 用反射机制动态调用job方法
import java.lang.reflect.Method; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.quartz.SchedulerContext; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.scheduling.quartz.QuartzJobBean; import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.WebApplicationContext; public class MyDetailQuartzJobBean extends QuartzJobBean implements ApplicationContextAware { protected final Log logger = LogFactory.getLog(getClass()); private String targetObject; private String targetMethod; private ApplicationContext ctx; @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { try { logger.info("execute [" + targetObject + "] at once>>>>>>"); WebApplicationContext webContext = ContextLoaderListener .getCurrentWebApplicationContext(); // ApplicationContext ctx =ContextUtil.getContext(); /* * WebApplicationContext ctx = (WebApplicationContext) context * .getScheduler().getContext().get("applicationContext"); */ Object otargetObject = webContext.getBean(targetObject); Method m = null; try { m = otargetObject.getClass().getMethod(targetMethod, new Class[] {}); m.invoke(otargetObject, new Object[] {}); } catch (SecurityException e) { logger.error(e); } catch (NoSuchMethodException e) { logger.error(e); } } catch (Exception e) { throw new JobExecutionException(e); } } public void setApplicationContext(ApplicationContext applicationContext) { this.ctx = applicationContext; } public void setTargetObject(String targetObject) { this.targetObject = targetObject; } public void setTargetMethod(String targetMethod) { this.targetMethod = targetMethod; } }
3.quartz.properties
org.quartz.scheduler.instanceName = TestScheduler1 org.quartz.scheduler.instanceId = AUTO org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount = 5 org.quartz.threadPool.threadPriority = 2 org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true org.quartz.jobStore.misfireThreshold = 60000 org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate org.quartz.jobStore.tablePrefix = QRTZ_ org.quartz.jobStore.maxMisfiresToHandleAtATime=10 org.quartz.jobStore.isClustered = true org.quartz.jobStore.clusterCheckinInterval = 20000
遇到问题1.customerTicketService要实现序列化
2.使用的jar包:quartz-1.6.1.jar,commons-dbcp-1.2.jar,commons-collections-3.2.1.jar,commons-pool-1.6.jar,commons-logging-1.1.3.jar,spring3.x.jar
4.用org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean指定类和方法,但是直接使用会报Java.io.NotSerializableException异常
5.添加quartz的12个表 quartz-1.8.6\docs\dbTables mysql或者oracle表