定时任务demo

来源:互联网 发布:数据分析师证 编辑:程序博客网 时间:2024/04/30 12:38

首先你需要定义一个bean类,和需要执行的方法(需要引入quartz-1.8.6.jar和org.springframework.context.support-3.0.2.RELEASE.jar)

public class QuartzTask {    public void doSimpleTriggerTask() {        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        System.out.println("doing simpleTrigger task..." + sdf.format(new Date()));    }    public void doCronTriggerTask() {        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        System.out.println("doing cronTrigger task..." + sdf.format(new Date()));    }}

然后在spring配置文件中配置

<?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:p="http://www.springframework.org/schema/p"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop-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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">   <!-- 注册一个普通bean -->   <bean id="quartzTask" class="cn.itcast.nsfw.complain.QuartzTask"></bean>   <!-- 1、制定任务信息信息 -->   <bean id="jobDetail1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">           <!-- 设置执行对象 -->           <property name="targetObject" ref="quartzTask"></property>           <!-- 设置执行对象中对应的执行方法 -->           <property name="targetMethod" value="doSimpleTriggerTask"></property>           <!-- 是否可以同步执行;不可同步执行 -->           <property name="concurrent" value="false"></property>   </bean>   <bean id="jobDetail2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">           <!-- 设置执行对象 -->           <property name="targetObject" ref="quartzTask"></property>           <!-- 设置执行对象中对应的执行方法 -->           <property name="targetMethod" value="doCronTriggerTask"></property>           <!-- 是否可以同步执行;不可同步执行 -->           <property name="concurrent" value="false"></property>   </bean>   <!-- 2、制定任务执行时机(任务执行触发器) -->   <bean id="simplerTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">        <!-- 设置任务详细 -->        <property name="jobDetail" ref="jobDetail1"></property>        <!-- 设置任务延迟执行时间 ;延迟2秒执行-->        <property name="startDelay" value="2000"></property>        <!-- 设置任务执行频率;执行频率为每4秒执行一下 -->        <property name="repeatInterval" value="2000"></property>   </bean>   <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">        <!-- 设置任务详细 -->        <property name="jobDetail" ref="jobDetail2"></property>        <!-- 设置任务执行时机,cron表达式 -->        <property name="cronExpression" value="* * * 18c * ?"></property>   </bean>   <!-- 3、设置调度工厂 -->   <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">        <property name="triggers">            <list>                <!-- <ref bean="simplerTrigger"/> -->                <ref bean="cronTrigger"/>            </list>        </property>   </bean></beans>