一个实现类,简单的测试,打印出当前调度实例的名称和下次触发时间:
然后就是配置好quartz.xml,这里包括非集群和集群的方法,非集群也可以用插件实现,不过要多个配置文件,我嫌麻烦就直接写这里了:
package com.christ.test.action;
import java.io.Serializable;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerException;
import org.springframework.scheduling.quartz.QuartzJobBean;
/**
* @author
*/
public class HelloJob extends QuartzJobBean implements Serializable {
/**
*
*/
static Log log = LogFactory.getLog(HelloJob.class);
/**
*
*/
public void helloJob() {
log.info("每分钟第10秒非集群测试" + (new Date().toString()));
}
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
try {
log.info(context.getScheduler().getSchedulerName() + "集群测试下一次运行时间应该为: "+ context.getTrigger().getNextFireTime());
// Scheduler sc = context.getScheduler();
// sc.shutdown();
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}
然后就是配置好quartz.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">
<!-- 集群任务 -->
<bean id="job" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.christ.test.action.HelloJob" />
</bean>
<!-- 集群作业触发器 -->
<bean id="clusterJobTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="job" />
<!-- cron表达式 -->
<property name="cronExpression" value="30 * * * * ?" />
</bean>
<!-- 集群调度 -->
<bean name="clusterQuartzScheduler" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!--启动时更新数据库中己存在的Job-->
<property name="overwriteExistingJobs" value="true"/>
<!-- 应用启动后启动QuartzScheduler -->
<property name="startupDelay" value="30"/>
<property name="triggers">
<list>
<ref bean="clusterJobTime" />
</list>
</property>
<property name="configLocation" value="classpath:quartz.properties"/>
</bean>
<!--非集群作业-->
<bean id="helloJob" class="com.christ.test.action.HelloJob"></bean>
<bean id="jobtest"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject" ref="helloJob"/>
<!-- 调用类中的方法 -->
<property name="targetMethod" value="helloJob"/>
</bean>
<!-- 非集群作业触发器-->
<bean id="JobTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="jobtest" />
<!-- cron表达式 -->
<property name="cronExpression" value="10 * * * * ?" />
</bean>
<!-- 非集群调度 -->
<bean name="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- 触发器 -->
<property name="triggers">
<list>
<ref bean="JobTime" />
</list>
</property>
</bean>
</beans>