public class ClockTimeBeanTask { /** * 自定义的run方法 */ public void run() { System.out.println("ClockTimeBeanTask: "+ TimeUtil.showCurrentTime()); } }
spring配置:
<!-- 申明执行任务的类 --> <bean id="clockTimeBeanTask" class="task.ClockTimeBeanTask"></bean> <!-- 加载任务,这里的设置可以把普通的pojo类中的方法当作任务执行 --> <bean id="methodInvokeTask" class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean"> <property name="targetObject" ref="clockTimeBeanTask" /> <property name="targetMethod" value="run" /> </bean> <bean id="clockTimeBeanScheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"> <property name="timerTask" ref="methodInvokeTask" /> <!-- 开始执行延迟时间 --> <property name="delay" value="1000" /> <!-- 下一次执行时间间隔 --> <property name="period" value="1000" /> </bean> <!-- 装载调度器,容器启动时开始执行 --> <bean id="timer" class="org.springframework.scheduling.timer.TimerFactoryBean"> <property name="scheduledTimerTasks"> <list> <ref local="clockTimeBeanScheduledTask" /> </list> </property> <property name="daemon" value="true" /> </bean>
public class ClockTimerTask extends TimerTask { @Override public void run() { System.out.println("ClockTimerTask: " + TimeUtil.showCurrentTime()); } }
Spring配置:
<!-- 申明执行任务的类 --> <bean id="clockTimerTask" class="task.ClockTimerTask"></bean> <!-- 加载任务,并设置任务参数 --> <bean id="clockTimeScheduledTask" class="org.springframework.scheduling.timer.ScheduledTimerTask"> <property name="timerTask" ref="clockTimerTask" /> <!-- 开始执行延迟时间 --> <property name="delay" value="1000" /> <!-- 下一次执行时间间隔 --> <property name="period" value="1000" /> </bean> <!-- 装载调度器,容器启动时开始执行 --> <bean id="timer" class="org.springframework.scheduling.timer.TimerFactoryBean"> <property name="scheduledTimerTasks"> <list> <ref local="clockTimeScheduledTask" /> </list> </property> <property name="daemon" value="true" /> </bean>
public class ClockTimeRunnableTask implements Runnable { @Override public void run() { System.out.println("ClockTimeRunnableTask: " + TimeUtil.showCurrentTime()); } }
Spring配置文件:
<!-- 申明任务类 --> <bean id="clockTimeRunnableTask" class="task.ClockTimeRunnableTask"></bean> <!-- 加载任务 --> <bean id="scheduledExecutorTask" class="org.springframework.scheduling.concurrent.ScheduledExecutorTask"> <property name="runnable" ref="clockTimeRunnableTask" /> <property name="period" value="1000" /> </bean> <!-- 装载调度器,容器启动时运行 --> <bean id="scheduledExecutorFactoryBean" class="org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean"> <property name="poolSize" value="5" /> <property name="scheduledExecutorTasks"> <list> <ref bean="scheduledExecutorTask" /> </list> </property> </bean>
public class ClockTimeQuartzTask extends QuartzJobBean { private String s; @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { System.out.println(s + TimeUtil.formatDate(context.getNextFireTime())); } public String getS() { return s; } public void setS(String s) { this.s = s; } }
Spring配置文件:
<!-- 加载任务 --> <bean id="clockTimeQuartzDetail" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass" value="task.ClockTimeQuartzTask" /> <!-- 为ClockTimeQuartzTask中的属性注入 --> <property name="jobDataAsMap"> <map> <entry key="s" value="下次执行时间: " /> </map> </property> </bean> <!-- 设置任务执行参数 --> <bean id="clockTimeQuartzTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref="clockTimeQuartzDetail" /> <!-- 开始执行延迟时间 --> <property name="startDelay" value="1000" /> <!-- 下一次执行时间间隔 --> <property name="repeatInterval" value="3000" /> </bean> <!-- 装载调度器,容器启动时监听 --> <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="clockTimeQuartzTrigger" /> </list> </property> </bean>
public class QuartzTask { public void doSomething() { System.out.println("I do."); } }
Spring配置文件:
<!-- 设置任务类 --> <bean id="quartzTask" class="task.QuartzTask"></bean> <!-- 将任务加载到quartz配置中 --> <bean id="quartzTaskDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="quartzTask" /> <property name="targetMethod" value="doSomething" /> <property name="concurrent" value="true" /> </bean> <!-- 指定任务触发时间 --> <bean id="quartzTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="quartzTaskDetail" /> <!-- 每天16点10分触发 --> <property name="cronExpression" value="0 10 16 * * ?" /> </bean> <!-- 装载任务,容器启动时开始监听--> <bean id="scheduler2" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="quartzTaskTrigger" /> </list> </property> </bean>