使用Spring定时器定时任务的几种配置方式及示例
1个回答
2018-07-26 · 国内最优秀java资源共享平台
关注
展开全部
1.Java Timer定时
首先继承java.util.TimerTask类实现run方法
package com.land;
import java.util.Date;
import java.util.TimerTask;
public class TimerTest extends TimerTask {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("TimerTest"+(new Date()).toString());
}
}
Spring配置
<bean id="TimerTest" class="com.land.TimerTest">
</bean>
<bean id="TimerTestTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<!-- wait 10 seconds before starting repeated execution -->
<property name="delay" value="10000" /><!-- 启动后的延迟时间-->
<!--间隔时间-->
<property name="period" value="72000" />
<property name="timerTask" ref="TimerTest" />
</bean>
<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<!-- see the example above -->
<ref bean="TimerTestTask" />
</list>
</property>
</bean>
2.Quartz定时器
首先继承QuartzJobBean类实现executeInternal方法
public class SchedulerTest extends QuartzJobBean{
@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
//System.out.println("-----------");
System.out.println("1111111"+(new Date()).toString());
}
}
-------------------------------------------
public class SchedulerTest2 extends QuartzJobBean{
@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
//System.out.println("-----------");
System.out.println("22222222"+(new Date()).toString());
}
}
Spring配置
<bean id="SchedulerTask" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>com.land.SchedulerTest</value>
</property>
</bean>
<bean id="SchedulerTask2" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>com.land.SchedulerTest2</value>
</property>
</bean>
<bean id="SchedulerTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="SchedulerTask" />
<property name="cronExpression">
<value>0/10 * * * * ?</value>
</property>
</bean>
<bean id="SchedulerTaskTrigger2" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="SchedulerTask2" />
<property name="startDelay">
<value>1000</value><!--延迟时间-->
</property>
<property name="repeatInterval">
<value>20000</value><!--间隔时间-->
</property>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="SchedulerTaskTrigger" />
<ref bean="SchedulerTaskTrigger2" />
</list>
</property>
</bean>
首先继承java.util.TimerTask类实现run方法
package com.land;
import java.util.Date;
import java.util.TimerTask;
public class TimerTest extends TimerTask {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("TimerTest"+(new Date()).toString());
}
}
Spring配置
<bean id="TimerTest" class="com.land.TimerTest">
</bean>
<bean id="TimerTestTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
<!-- wait 10 seconds before starting repeated execution -->
<property name="delay" value="10000" /><!-- 启动后的延迟时间-->
<!--间隔时间-->
<property name="period" value="72000" />
<property name="timerTask" ref="TimerTest" />
</bean>
<bean id="timerFactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<!-- see the example above -->
<ref bean="TimerTestTask" />
</list>
</property>
</bean>
2.Quartz定时器
首先继承QuartzJobBean类实现executeInternal方法
public class SchedulerTest extends QuartzJobBean{
@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
//System.out.println("-----------");
System.out.println("1111111"+(new Date()).toString());
}
}
-------------------------------------------
public class SchedulerTest2 extends QuartzJobBean{
@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
// TODO Auto-generated method stub
//System.out.println("-----------");
System.out.println("22222222"+(new Date()).toString());
}
}
Spring配置
<bean id="SchedulerTask" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>com.land.SchedulerTest</value>
</property>
</bean>
<bean id="SchedulerTask2" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>com.land.SchedulerTest2</value>
</property>
</bean>
<bean id="SchedulerTaskTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="SchedulerTask" />
<property name="cronExpression">
<value>0/10 * * * * ?</value>
</property>
</bean>
<bean id="SchedulerTaskTrigger2" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="SchedulerTask2" />
<property name="startDelay">
<value>1000</value><!--延迟时间-->
</property>
<property name="repeatInterval">
<value>20000</value><!--间隔时间-->
</property>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="SchedulerTaskTrigger" />
<ref bean="SchedulerTaskTrigger2" />
</list>
</property>
</bean>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询