使用Spring来实现任务计划服务一:继承java.util.TimerTask

本文介绍如何通过Java和Spring框架实现定时任务。首先定义了一个名为DemoTask的任务类,然后在Spring配置文件beans-task.xml中进行任务配置,包括使用ScheduledTimerTask设置执行周期及利用TimerFactoryBean进行任务调度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、定义一个DemoTask类,并继承java.util.TimerTask.

package junit.test;

import java.util.TimerTask;

public class DemoTask extends TimerTask{
	
	public void run(){
		System.out.println("Task is excuted:"+System.currentTimeMillis());
	}

}
2.在springConfig文件beans-task.xml中配置DemoTask bean定义.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

     <bean id="demoTask" class="junit.test.DemoTask" />     

</beans>
3.使用Sping的org.springframework.scheduling.timer.ScheduledTimerTask来定义执行周期。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

     <bean id="demoTask" class="junit.test.DemoTask" />

     <bean id="scheduledTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
     	<property name="timerTask">
     		<ref bean="demoTask"/>
     	</property>
     	<property name="period">
     		<value>6000</value><!-- 设置间隔时间,毫秒 -->
     	</property>
     	<property name="delay">
     		<value>2000</value><!-- 设置延迟时间开始执行 -->
     	</property>
     </bean>

</beans>
4.使用Spring的org.springframework.scheduling.timer.TimerFactoryBean类来加入所有的任务计划。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
     <bean id="demoTask" class="junit.test.DemoTask" />
     <bean id="scheduledTimerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
     	<property name="timerTask">
     		<ref bean="demoTask"/>
     	</property>
     	<property name="period">
     		<value>6000</value><!-- 设置间隔时间,毫秒 -->
     	</property>
     	<property name="delay">
     		<value>2000</value><!-- 设置延迟时间开始执行 -->
     	</property>
     </bean>
     
     <bean id="timerFactoryBean" class="org.springframework.scheduling.timer.TimerFactoryBean">
     	<property name="scheduledTimerTasks">
     	 <list>
     	   <ref bean="scheduledTimerTask"/>
     	 </list>
     	</property>
     </bean>
</beans>
到这里,整个beans-task.xml文件配置完成,程序代码也完成,下面我们写个测试方法来测试测试....

 

package junit.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class DemoTaskTestg {
	
	public static void main(String[] args){
		new ClassPathXmlApplicationContext("beans-task.xml");	} }

 

转载于:https://my.oschina.net/mszhang/blog/16178

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值