Java 任务调度

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);  

      // 创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;

      scheduler.scheduleAtFixedRate(runnable,0,5,TimeUnit.SECONDS);

   //scheduleAtFixedRate方式: 如果任务的任何一个执行遇到异常,则后续执行都会被取消 

  //参数介绍 

     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>

CronTrigger配置格式:

格式: [] [] [小时] [] [] [] []

通配符说明:

序号

说明

是否必填

允许填写的值

允许的通配符

1

0-59 

, - * /

2

0-59

, - * /

3

小时

0-23

, - * /

4

1-31

, - * ? / L W

5

1-12 or JAN-DEC

, - * /

6

1-7 or SUN-SAT

, - * ? / L #

7

empty 1970-2099

, - * /

* :表示所有值. 例如:在分的字段上设置 "*",表示每一分钟都会触发。
?
:表示不指定值。使用的场景为不需要关心当前设置这个字段的值。例如:要在每月的10号触发一个操作,但不关心是周几,所以需要周位置的那个字段设置为"?" 具体设置为 0 0 0 10 * ?
-
:表示区间。例如 在小时上设置 "10-12",表示 10,11,12点都会触发。

,
:表示指定多个值,例如在周字段上设置 "MON,WED,FRI" 表示周一,周三和周五触发
/
:用于递增触发。如在秒上面设置"5/15" 表示从5秒开始,每增15秒触发(5,20,35,50)。 在月字段上设置'1/3'所示每月1号开始,每隔三天触发一次。
L
:表示最后的意思。在日字段设置上,表示当月的最后一天(依据当前月份,如果是二月还会依据是否是润年[leap]), 在周字段上表示星期六,相当于"7""SAT"。如果在"L"前加上数字,则表示该数据的最后一个。

例如在周字段上设置"6L"这样的格式,则表示本月最后一个星期五"

W :表示离指定日期的最近那个工作日(周一至周五). 例如在日字段上设置"15W",表示离每月15号最近的那个工作日触发。如果15号正好是周六,则找最近的周五(14)触发, 如果15号是周未,则找最近的下周一(16)触发.如果15号正好在工作日(周一至周五),则就在该天触发。如果指定格式为 "1W",它则表示每月1号往后最近的工作日触发。如果1号正是周六,则将在3号下周一触发。(注,"W"前只能设置具体的数字,不允许区间"-").

'L' 'W'可以一组合使用。如果在日字段上设置"LW",则表示在本月的最后一个工作日触发

 

# :序号(表示每月的第几周星期几),例如在周字段上设置"6#3"表示在每月的第三个周星期六.注意如果指定"6#5",正好第五周没有星期六,则不会触发该配置(用在母亲节和父亲节再合适不过了)

周字段的设置,若使用英文字母是不区分大小写的 MON mon相同.

 

表达式 解释
0 0 12 * * ? 在每天中午12:00触发
0 15 10 ? * * 每天上午10:15 触发
0 15 10 * * ? 每天上午10:15 触发
0 15 10 * * ? * 每天上午10:15 触发
0 15 10 * * ? 2005 在2005年中的每天上午10:15 触发
0 * 14 * * ? 每天在下午2:00至2:59之间每分钟触发一次
0 0/5 14 * * ? 每天在下午2:00至2:59之间每5分钟触发一次
0 0/5 14,18 * * ? 每天在下午2:00至2:59和6:00至6:59之间的每5分钟触发一次
0 0-5 14 * * ? 每天在下午2:00至2:05之间每分钟触发一次
0 10,44 14 ? 3 WED 每三月份的星期三在下午2:00和2:44时触发
0 15 10 ? * MON-FRI 从星期一至星期五的每天上午10:15触发
0 15 10 15 * ? 在每个月的每15天的上午10:15触发
0 15 10 L * ? 在每个月的最后一天的上午10:15触发
0 15 10 ? * 6L 在每个月的最后一个星期五的上午10:15触发
0 15 10 ? * 6L 2002-2005 在2002, 2003, 2004 and 2005年的每个月的最后一个星期五的上午10:15触发
0 15 10 ? * 6#3 在每个月的第三个星期五的上午10:15触发
0 0 12 1/5 * ? 从每月的第一天起每过5天的中午12:00时触发
0 11 11 11 11 ? 在每个11月11日的上午11:11时触发.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值