为普通java类中的某个方法配置跑批任务
1.定义要跑批的类和方法:
public class QuartzTest {
public void work(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD HH:mm:ss");
System.out.println("Quartz的任务调度删除临时文件开始!!!" + sdf.format(new Date()));
}
}
2.配置Spring定时器让quartz自动执行testMethod方法:
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"
default-lazy-init="false"
default-autowire="no"
>
<!-- 要调用的工作类 -->
<bean id="quartzJob" class="com.yyy.quartz.QuartzTest"></bean>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="quartzJob"/>
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>work</value>
</property>
</bean>
<!-- 定义触发时间 -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="jobtask"/>
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<!-- 中午12:00到12:59每分钟执行一次-->
<value>0/10 10 21 * * ?-</value>
</property>
</bean>
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger"/>
</list>
</property>
</bean>
</beans>
3.写测试方法进行调用(不用启动tomcat):
/**
* 测试方法
* @param args
*/
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println(ctx);
}
附:cronExpression表达式解释:
0 0 12 * *?---------------在每天中午12:00触发
0 15 10 ? **---------------每天上午10:15 触发
0 15 10 * *?---------------每天上午10:15 触发
0 15 10 * *? *---------------每天上午10:15 触发
0 15 10 * *? 2005---------------在2005年中的每天上午10:15 触发
0 * 14 * *?---------------每天在下午2:00至2:59之间每分钟触发一次
0 0/5 14 ** ?---------------每天在下午2:00至2:59之间每5分钟触发一次
0 0/5 14,18* * ?---------------每天在下午2:00至2:59和6:00至6:59之间的每5分钟触发一次
0 0-5 14 ** ?---------------每天在下午2:00至2:05之间每分钟触发一次
0 10,44 14? 3 WED---------------每三月份的星期三在下午2:00和2:44时触发
0 15 10 ? *MON-FRI---------------从星期一至星期五的每天上午10:15触发
0 15 10 15* ?---------------在每个月的每15天的上午10:15触发
0 15 10 L *?---------------在每个月的最后一天的上午10:15触发
0 15 10 ? *6L---------------在每个月的最后一个星期五的上午10:15触发
0 15 10 ? *6L 2002-2005---------------在2002, 2003, 2004 and 2005年的每个月的最后一个星期五的上午10:15触发
0 15 10 ? *6#3---------------在每个月的第三个星期五的上午10:15触发
0 0 12 1/5 *?---------------从每月的第一天起每过5天的中午12:00时触发
0 11 11 1111 ?---------------在每个11月11日的上午11:11时触发.-
点击下载:百度网盘下载(里面包括完整的jar包)