美文网首页
Spring Scheduler用法

Spring Scheduler用法

作者: sunpy | 来源:发表于2018-10-27 14:04 被阅读142次

介绍

如果我们想执行定时任务,那么JDK为我们提供了Timer,但是Timer会有很多问题,如时间可能延迟、时间依赖系统导致不准确、出现异常停止等各种问题。spring为我们提供了定时器Spring Scheduler,但是它也不是很完美,有很多坑去踩。

结构

spirng执行定时任务的三个模块:

  1. TaskExecutor任务执行器
    TaskExecutor类图.jpg
    SyncTaskExecutor:没有采用异步的方式执行调用,实际上每个任务的调用都是执行自己线程的run方法。
    ThreadPoolTaskExecutor:其本身的实现采用jdk提供的线程池ThreadPoolExecutor实现的。
    ConcurrentTaskExecutor:该类主要是为jdk中的Executor设计的对象适配器模式。
    WorkManagerTaskExecutor:主要采用WorkManager来实现的。
    SimpleAsyncTaskExecutor:主要就是每个调用就为其分配一个线程,如果超出指定的并发限制,任务就会阻塞,直到有空闲线程为止。
  2. TaskScheduler任务调度器
    TaskScheduler类图.jpg
    ThreadPoolTaskScheduler:主要还是依赖于jdk的线程池来实现的。
  3. Trigger任务触发器
    Trigger类图.jpg
    PeriodicTrigger:在给定的时间间隔触发任务,类似于quartz的SimpleTrigger。
    CronTrigger:使用cron表达式来触发任务。

使用Spring Scheduler

  1. 配置文件方式
    ① 配置文件:
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:task="http://www.springframework.org/schema/task" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-4.0.xsd  
    http://www.springframework.org/schema/aop  
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-4.0.xsd">  
    
    <beans:bean id="springSchedulerTask" class="cn.sunpy.task.SpringSchedulerTask"></beans:bean>
    
    <task:scheduled-tasks>
        <task:scheduled ref="springSchedulerTask" method="executeTask" cron="0/3 * * * * ?"/>
    </task:scheduled-tasks>
    
</beans>

② 任务:

public class SpringSchedulerTask {
    
    public void executeTask() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
        System.out.println("executeTask" + sdf.format(new Date()));
    }
}
  1. 注解方式
    ① 配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" 
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:tx="http://www.springframework.org/schema/tx" 
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns:task="http://www.springframework.org/schema/task" 
       xsi:schemaLocation="http://www.springframework.org/schema/beans  
                           http://www.springframework.org/schema/beans/spring-beans-4.3.xsd  
                           http://www.springframework.org/schema/context  
                           http://www.springframework.org/schema/context/spring-context-4.3.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
                           http://www.springframework.org/schema/aop 
                           http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
                           http://code.alibabatech.com/schema/dubbo 
                           http://code.alibabatech.com/schema/dubbo/dubbo.xsd
                           http://www.springframework.org/schema/task  
                           http://www.springframework.org/schema/task/spring-task.xsd">

    <context:component-scan base-package="com.hlw.grhc" />
    <context:component-scan base-package="com.hlw.sch" />

    <!-- 开启quartz的注解 -->
    <task:annotation-driven/>
</beans>

② 任务:

@Component
public class OrgDataSch {
        
    /**
     * 每日00:00:00执行 ,如2018/7/31 0:00:00
     */
    @Scheduled(cron="0 30 0 * * ?")
    public void countPhsTask() throws ServiceException{
        ......
    }
}

思考

现在的机器都是多台集群部署的,而Spring Scheduler本身对于集群没什么完美的解决方案,提供的方案是通过指定ip来让一台机器执行定时任务,感觉如果这台机器出现问题,这个定时任务就玩不转了,高可用就没戏了,这个方案不是真正的集群解决方式,发现这个Spring Scheduler功能有点鸡肋了。打算深入研究下quartz组件。

相关文章

网友评论

      本文标题:Spring Scheduler用法

      本文链接:https://www.haomeiwen.com/subject/rmcftqtx.html