Java任务调度分享:
import java.util.Calendar;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class DBThread {
private ScheduledExecutorService scheduler; //它可另行安排在给定的延迟后运行命令,或者定期执行命令。
public static void main(String [] arg){
new DBThread().start();
}
private void start() {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis()); //获取系统当前时间
scheduler = Executors.newScheduledThreadPool(1); // 创建一个线程池,它可安排在给定延迟后运行命令或者定期地执行。
// 创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;也就是将在 initialDelay 后开始执行,然后 在 initialDelay+period 后执行,接着在 initialDelay + 2 * period 后执行,依此类推。
scheduler.scheduleAtFixedRate(runnable,0,5,TimeUnit.SECONDS);
//参数介绍
runnable :需要调度的任务
0:首次执行的延迟时间
5: 连续执行之间的周期
TimeUnit.SECONDS :0和5 参数的时间单位
}
Runnable runnable = new Runnable() {
public void run() {
System.out.println("running...");//调用执行任务
}
};
}
Spring 任务调度 方式介绍:
1.TimerTask 方式简单调度:(缺点: Timer无法精确指定何时运行)
public class TimerTask extends java.util.TimerTask {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("TimerTask方式调度的执行的方法");
}
}
在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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!--配置实现类-->
<bean id="timeTask" class="com.spring.TimerTask"></bean>
<bean id="TimeTasks" class="org.springframework.scheduling.timer.ScheduledTimerTask" >
<property name="timerTask" ref="timeTask"></property>
<!-- 指定任务运行周期,单位毫秒 -->
<property name="period" value="1000"></property>
<!-- 指定任务延时时间,即第一次运行之前等待时间,单位毫秒 -->
<property name="delay" value="1000"></property>
</bean>
<bean class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="scheduledTimerTasks">
<list>
<ref bean="TimeTasks"/>
</list>
</property>
</bean>
</beans>
2.Quartz实现调度:
2.1. MethodInvokingJobDetailFactoryBean 直接调用需要调度的方法
public class MyQuartz {
public void getHello()
{
System.out.println("hello");
}
}
在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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="myQuartz" class="com.spring.MyQuartz"></bean>
<bean id="newJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="myQuartz"></property>
<property name="targetMethod" value="getHello"></property>
</bean>
<bean id="simplerTrigger2" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="newJob"></property>
<!-- 设置延迟工作的第一次执行,单位毫秒 -->
<property name="startDelay" value="1000"></property>
<!-- 设置调度任务频度,单位毫秒 -->
<property name="repeatInterval" value="1000"></property>
</bean>
<!-- 设置调度周期 -->
<bean id="cronTrigger2" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="newJob"></property>
<property name="cronExpression" value="0 59 23 * * ?"></property>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- 接受一组触发器,可以接受一个列表 -->
<property name="triggers">
<list>
<ref bean="simplerTrigger2"/>
<ref bean="cronTrigger"/>
</list>
</property>
</bean>
</beans>
2.2:继承QuartzJobBean 类方式调度
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.*;
public class MyQuartzJob extends QuartzJobBean {
@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
System.out.println("running...");
}
}
在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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="quartzJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.spring.MyQuartzJob"></property>
<!-- 接受一个Map,其中包含了需要设置给jobClass的各种属性 -->
<property name="jobDataAsMap">
<map></map>
</property>
</bean>
<bean id="simplerTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="quartzJob"></property>
<!-- 设置延迟工作的第一次执行,单位毫秒 -->
<property name="startDelay" value="1000"></property>
<!-- 设置调度任务频度,单位毫秒 -->
<property name="repeatInterval" value="1000"></property>
</bean>
<!-- 设置调度时间 -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="quartzJob"></property>
<property name="cronExpression" value="0 59 23 * * ?"></property>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- 接受一组触发器,可以接受一个列表 -->
<property name="triggers">
<list>
<ref bean="simplerTrigger"/>
<ref bean="cronTrigger"/>
</list>
</property>
</bean>
</beans>
java 任务调度实例
最新推荐文章于 2024-10-30 18:07:32 发布