项目中偶尔会用到定时任务的执行,比如定时发短信,定时发送对账文件等等,今天,我们就一起学习下,在Spring中如何实现定时器!


1 老生常谈说Timer

1.1 介绍

废话不多说,我们来了解下它的身份:

  • 所在包:java.util
  • 作者:Josh Bloch
  • 出生日期:JDK1.3
  • 直系亲属:Object
  • 个人简历:一堆English

别怕,下面是英语学习时间:

A facility for threads to schedule tasks for future execution in a background thread. Tasks may be scheduled for one-time execution, or for repeated execution at regular intervals.


线程用于计划任务以在后台线程中将来执行的工具。可以安排任务一次执行或定期执行重复任务。


Corresponding to each Timer object is a single background thread that is used to execute all of the timer’s tasks, sequentially. Timer tasks should complete quickly. If a timer task takes excessive time to complete, it “hogs” the timer’s task execution thread. This can, in turn, delay the execution of subsequent tasks, which may “bunch up” and execute in rapid succession when (and if) the offending task finally completes.


与每个Timer对象相对应的是一个后台线程,该线程用于依次执行所有Timer的任务。计时器任务应快速完成。如果计时器任务花费过多时间来完成,它将“占用”计时器的任务执行线程。反过来,这可能会延迟后续任务的执行,这可能会“累加”并在有问题的任务最终完成时(以及是否)迅速连续执行。


After the last live reference to a Timer object goes away and all outstanding tasks have completed execution, the timer’s task execution thread terminates gracefully (and becomes subject to garbage collection). However, this can take arbitrarily long to occur. By default, the task execution thread does not run as a daemon thread, so it is capable of keeping an application from terminating. If a caller wants to terminate a timer’s task execution thread rapidly, the caller should invoke the timer’s cancel method.


在最后一个对Timer对象的实时引用消失并且所有未完成的任务均已完成执行之后,计时器的任务执行线程会正常终止(并接受垃圾回收)。但是,这可能要花很长时间。默认情况下,任务执行线程不会作为守护程序线程运行,因此它能够防止应用程序终止。如果调用方想快速终止计时器的任务执行线程,则调用方应调用计时器的cancel方法。


If the timer’s task execution thread terminates unexpectedly, for example, because its stop method is invoked, any further attempt to schedule a task on the timer will result in an IllegalStateException, as if the timer’s cancel method had been invoked.


如果计时器的任务执行线程意外终止,例如,由于调用了它的stop方法,则在计时器上计划任务的任何进一步尝试都将导致IllegalStateException,就好像计时器的cancel方法已被调用一样。


This class is thread-safe: multiple threads can share a single Timer object without the need for external synchronization.


此类是线程安全的:多个线程可以共享一个Timer对象,而无需外部同步。

This class does not offer real-time guarantees: it schedules tasks using the Object.wait(long) method.


此类不提供实时保证:它使用Object.wait(long)方法调度任务。


Java 5.0 introduced the java.util.concurrent package and one of the concurrency utilities therein is the ScheduledThreadPoolExecutor which is a thread pool for repeatedly executing tasks at a given rate or delay. It is effectively a more versatile replacement for the Timer/TimerTask combination, as it allows multiple service threads, accepts various time units, and doesn’t require subclassing TimerTask (just implement Runnable). Configuring ScheduledThreadPoolExecutor with one thread makes it equivalent to Timer.


Java 5.0引入了java.util.concurrent包,其中的并发实用程序之一是ScheduledThreadPoolExecutor,它是一个线程池,用于以给定的速率或延迟重复执行任务。实际上,它是Timer / TimerTask组合的更通用的替代品,因为它允许多个服务线程,接受各种时间单位,并且不需要子类化TimerTask(只需实现Runnable)。使用一个线程配置ScheduledThreadPoolExecutor使其等效于Timer。


Implementation note: This class scales to large numbers of concurrently scheduled tasks (thousands should present no problem). Internally, it uses a binary heap to represent its task queue, so the cost to schedule a task is O(log n), where n is the number of concurrently scheduled tasks.


实现说明:此类可扩展为同时执行的大量计划任务(成千上万个也不会出现问题)。在内部,它使用二进制堆表示其任务队列,因此调度任务的成本为O(log n),其中n是同时调度的任务数。


Implementation note: All constructors start a timer thread.


实现说明:所有构造函数都启动一个计时器线程。

1.2 入门实例

我们直接在main方法中实例化一个Timer:

  Timer timer = new Timer();

当我想调用timer的方法时:

在这里插入图片描述

见名知意,大家可以每个都试一下看,这里我们用scheduleAtFixedRate举例:

在这里插入图片描述

这个方法有三个参数,依次是TimerTask的实例,延迟执行时间,持续时间,也就是说,上述代码的意思是:

启动后延迟2秒开始执行run方法中的内容,每隔3秒执行一次

运行main方法,结果如下:
在这里插入图片描述

原谅我的Gif中的鼠标乱入~囧

至此,Timer的介绍到此暂时告一段落。


2. ScheduledExecutorService->Timer的升级版

细心的同学可能会注意到,你的1.2中的timer明明报错了嘛?

其实不是的,那只是IDE给我的一个比较友好的提示,内容是:
在这里插入图片描述

ScheduledExecutorService,它来了!老规矩,还是先了解下它的身份信息:

  • 所在包:java.util.concurrent
  • 作者:Doug Lea
  • 出生日期:JDK1.5
  • 性别:interface
  • 上层亲属:

在这里插入图片描述

我们打开文档,进一步了解下它 看文档,我只用Dash

没错,英语学习时间又到了!

An ExecutorService that can schedule commands to run after a given delay, or to execute periodically.


ExecutorService可以安排命令在给定的延迟后运行或定期执行。


The schedule methods create tasks with various delays and return a task object that can be used to cancel or check execution. The scheduleAtFixedRate and scheduleWithFixedDelay methods create and execute tasks that run periodically until cancelled.


schedule方法创建具有各种延迟的任务,并返回可用于取消或检查执行的任务对象。 scheduleAtFixedRate和scheduleWithFixedDelay方法创建并执行定期运行的任务,直到被取消为止。


Commands submitted using the Executor.execute(Runnable) and ExecutorService submit methods are scheduled with a requested delay of zero. Zero and negative delays (but not periods) are also allowed in schedule methods, and are treated as requests for immediate execution.


使用Executor.execute(Runnable)和ExecutorService submit 方法提交的命令的计划延迟为零。 schedule方法中还允许零延迟和负延迟(但在周期调用中不可以),并将其视为立即执行的请求。


All schedule methods accept relative delays and periods as arguments, not absolute times or dates. It is a simple matter to transform an absolute time represented as a Date to the required form. For example, to schedule at a certain future date, you can use: schedule(task, date.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS). Beware however that expiration of a relative delay need not coincide with the current Date at which the task is enabled due to network time synchronization protocols, clock drift, or other factors.


所有schedule方法都接受相对延迟和周期作为参数,而不是绝对时间或日期作为参数。将代表日期的绝对时间转换为所需的形式很简单。例如,要在某个将来的日期进行调度,可以使用:schedule(task,date.getTime()-System.currentTimeMillis(),TimeUnit.MILLISECONDS)。但是请注意,由于网络时间同步协议,时钟漂移或其他因素,相对延迟的到期时间不必与启用任务的当前日期一致。


The Executors class provides convenient factory methods for the ScheduledExecutorService implementations provided in this package.


Executors类为此程序包中提供的ScheduledExecutorService实现提供了方便的工厂方法。

我们再来看下ScheduledExecutorService下的所有方法:

在这里插入图片描述

既然只有四个方法,我们就逐个写个小例子,试验一下:

其实不止newScheduledThreadPool这一种线程池,其他的会在后续其他文章中介绍,本文只介绍定时相关的newScheduledThreadPool。

schedule​(Runnable command, long delay, TimeUnit unit)

 /*** 测试ScheduledExecutorService中的schedule[Runnable]*/public static void testScheduledExecutorService1(){ScheduledExecutorService scheduledExecutorService =Executors.newScheduledThreadPool(1);scheduledExecutorService.schedule(new Runnable() {@Overridepublic void run() {System.out.println(" 使用ScheduledExecutorService延迟执行任务" + new Date());}}, 2, TimeUnit.SECONDS);scheduledExecutorService.shutdown();}

我们设置其中的run方法在延迟2秒后执行,在main方法中调用该方法,得到:
在这里插入图片描述

该方法为一次性方法,即非周期性的,只执行一次


schedule​(Callable callable, long delay, TimeUnit unit)

这个也是个一次性的方法,和schedule​(Runnable command, long delay, TimeUnit unit)的区别是,它有返回值:

   /*** 测试ScheduledExecutorService中的schedule[Callable]*/public static void testScheduledExecutorService2() throws ExecutionException, InterruptedException {ScheduledExecutorService scheduledExecutorService =Executors.newScheduledThreadPool(1);ScheduledFuture<String> s =  scheduledExecutorService.schedule(new Callable<String>() {@Overridepublic String call() throws Exception {return "hello Aran!";}}, 2, TimeUnit.SECONDS);String theValue   = s.get();System.out.println("得到的返回值是:" + theValue);scheduledExecutorService.shutdown();}

scheduleAtFixedRate​(Runnable command, long initialDelay, long period, TimeUnit unit)

private final AtomicInteger count = new AtomicInteger(0);/*** 测试ScheduledExecutorService中的scheduleAtFixedRate​*/public  void testScheduledExecutorService3() {System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));ScheduledExecutorService scheduledExecutorService =Executors.newScheduledThreadPool(1);Runnable beep = new Runnable(){int  i = 0;@Overridepublic void run() {count.getAndIncrement();System.out.println("输出时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));if(count.get() == 5){System.out.println("条件达标,结束定时任务!");scheduledExecutorService.shutdown();}}};scheduledExecutorService.scheduleAtFixedRate(beep,3, 4, TimeUnit.SECONDS);}

其中,我们用count的值对是否结束定时任务进行判断,AtomicInteger是一种原子操作,我们后续会单独介绍。

运行代码,结果如下:

在这里插入图片描述

scheduleWithFixedDelay​(Runnable command, long initialDelay, long delay, TimeUnit unit)

/*** 测试ScheduledExecutorService中的scheduleWithFixedDelay​*/public  void testScheduledExecutorService4() {System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));ScheduledExecutorService scheduledExecutorService =Executors.newScheduledThreadPool(1);Runnable beep = new Runnable(){int  i = 0;@Overridepublic void run() {count.getAndIncrement();System.out.println("输出时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));if(count.get() == 5){System.out.println("条件达标,结束定时任务!");scheduledExecutorService.shutdown();}}};scheduledExecutorService.scheduleWithFixedDelay(beep,3, 4, TimeUnit.SECONDS);}

运行上面的代码,结果如下:

在这里插入图片描述


铺垫了这么多,主角也该出现了!


3. Spring中的定时任务

3.1 大名鼎鼎的Quartz

相信大家对Quartz还是比较熟悉的,它涉及到的东西稍多,我们打开源码目录:

在这里插入图片描述
本篇文章我们暂时只介绍比较常用的,上图中选中的两个!

3.1.1 SimpleTriggerFactoryBean

经过一番严刑拷打,我们了解到它的身份如下:

  • 所在包:org.springframework.scheduling.quartz;
    源码位置:
    在这里插入图片描述

  • 作者:Juergen Hoeller

  • 出生日期:Spring 3.1

  • 性别:Class

  • 上层亲属:

在这里插入图片描述

  • 职业:(下面是英语学习时间)

A Spring FactoryBean for creating a Quartz SimpleTrigger instance, supporting bean-style usage for trigger configuration.


一个用于创建Quartz SimpleTrigger实例的Spring FactoryBean,它支持bean风格的触发器配置用法。


SimpleTrigger(Impl) itself is already a JavaBean but lacks sensible defaults. This class uses the Spring bean name as job name, the Quartz default group (“DEFAULT”) as job group, the current time as start time, and indefinite repetition, if not specified.


SimpleTrigger(Impl)本身已经是JavaBean,但是缺少合理的默认值。此类使用Spring bean名称作为作业名称,使用Quartz默认组(“ DEFAULT”)作为作业组,将当前时间用作开始时间,以及不确定的重复(如果未指定)。


This class will also register the trigger with the job name and group of a given JobDetail. This allows SchedulerFactoryBean to automatically register a trigger for the corresponding JobDetail, instead of registering the JobDetail separately.


此类还将使用给定JobDetail的作业名称和组来注册触发器。这允许SchedulerFactoryBean自动为相应的JobDetail注册触发器,而不是单独注册JobDetail。

以上,出现了一个SimpleTrigger的东东,我们找它出来撩撩:

  • 所在包:org.quartz
  • 作者:James House, contributions by Lieven Govaerts of Ebitec Nv, Belgium.
  • 出生日期:未知
  • 性别:interface
  • 上层亲属:Trigger
  • 工作内容:

A Trigger that is used to fire a Job at a given moment in time, and optionally repeated at a specified interval.


一个触发器,用于在给定的时间触发作业,并且可以选择以指定的间隔重复执行。

还有一个JobDetail又是啥?它是由JobBuilder创建的,用来创建Job实例的接口,那JobBuilder和Job又是啥?算了,这些暂时不解释,附上quartz的官方github,向深入学习的,请自行clone。

说了这么多,是时候展示真正的技术了:

首先,我们定义一个需要执行的任务:

package com.aran.modules.quartz;import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;import java.text.SimpleDateFormat;
import java.util.Date;/*** @Author Aran* @Date 2020/7/30 11:38 下午*/
public class myQuartzJob extends QuartzJobBean {private Integer timeout;public void setTimeout(Integer timeout) {this.timeout = timeout;}@Overrideprotected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {System.out.println(String.format(Thread.currentThread().getName() + "执行时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));}
}

该类继承了QuartzJobBean,重写了executeInternal方法,该方法体就是定时任务要执行的内容。

定义一个用于定时任务的配置文件[spring-config.xml]:

<?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:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"><bean name="job1" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"><property name="jobClass" value="com.aran.modules.quartz.myQuartzJob"/><property name="jobDataAsMap"><map><entry key="timeout" value="50000"/></map></property></bean><bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"><!--        需要调用的job,ref指向了job1--><property name="jobDetail" ref="job1"/><!--        首次执行延迟,单位:毫秒--><property name="startDelay" value="2000"/><!--        周期执行间隔,单位:毫秒--><property name="repeatInterval" value="5000"/></bean><bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers"><list><ref bean="simpleTrigger"/></list></property></bean>
</beans>	

虽然我是在web环境下,但测试时没必要通过web方式,所以,我在applicationContext.xml引入spring-config.xml

<?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.xsd">
<import resource="spring-config.xml"/></beans>

最后,我们在main中引入xml,实现bean的加载:

package com.aran.modules.quartz;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;/*** @Author Aran* @Date 2020/7/31 8:52 上午*/
public class Main {public static void main(String[] args) {ApplicationContext ctx = new FileSystemXmlApplicationContext("web/WEB-INF/applicationContext.xml");}
}

运行结果如下:

 
16:01:11.908 [main] INFO org.quartz.core.QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v2.3.2) 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' with instanceId 'NON_CLUSTERED'Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.NOT STARTED.Currently in standby mode.Number of jobs executed: 0Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.16:01:11.908 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' initialized from an externally provided properties instance.
16:01:11.908 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler version: 2.3.2
16:01:11.909 [main] INFO org.quartz.core.QuartzScheduler - JobFactory set to: org.springframework.scheduling.quartz.AdaptableJobFactory@78047b92
七月 31, 2020 4:01:11 下午 org.springframework.scheduling.quartz.SchedulerFactoryBean startScheduler
信息: Starting Quartz Scheduler now
16:01:11.919 [main] INFO org.quartz.core.QuartzScheduler - Scheduler org.springframework.scheduling.quartz.SchedulerFactoryBean#0_$_NON_CLUSTERED started.
16:01:11.920 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
16:01:13.785 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
16:01:13.785 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.job1
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1执行时间:2020-07-31 16:01:13
16:01:18.782 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-2] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.job1
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-2执行时间:2020-07-31 16:01:18
16:01:18.783 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
16:01:23.783 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
16:01:23.790 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-3] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.job1
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-3执行时间:2020-07-31 16:01:23
16:01:28.778 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
16:01:28.780 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-4] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.job1
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-4执行时间:2020-07-31 16:01:28
16:01:33.782 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
16:01:33.782 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-5] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.job1
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-5执行时间:2020-07-31 16:01:33
16:01:38.782 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
16:01:38.782 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-6] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.job1
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-6执行时间:2020-07-31 16:01:38
16:01:43.787 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
16:01:43.787 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-7] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.job1
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-7执行时间:2020-07-31 16:01:43
16:01:48.781 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
16:01:48.781 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-8] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.job1
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-8执行时间:2020-07-31 16:01:48
16:01:53.782 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
16:01:53.782 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-9] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.job1
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-9执行时间:2020-07-31 16:01:53
16:01:58.781 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
16:01:58.781 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-10] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.job1
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-10执行时间:2020-07-31 16:01:58
16:02:03.782 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.job1
16:02:03.782 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1执行时间:2020-07-31 16:02:03

3.1.2 CronTriggerFactoryBean

同样,我们还是了解一下它的身份信息:

  • 所在包:org.springframework.scheduling.quartz;
    在这里插入图片描述
  • 作者:Juergen Hoeller
  • 出生日期:spring 3.1
  • 性别:Class
  • 上层亲属关系:

在这里插入图片描述

  • 工作内容:

A Spring FactoryBean for creating a Quartz CronTrigger instance, supporting bean-style usage for trigger configuration.


一个用于创建Quartz CronTrigger实例的Spring FactoryBean,支持Bean风格的触发器配置使用。


CronTrigger(Impl) itself is already a JavaBean but lacks sensible defaults. This class uses the Spring bean name as job name, the Quartz default group (“DEFAULT”) as job group, the current time as start time, and indefinite repetition, if not specified.


CronTrigger(Impl)本身已经是JavaBean,但是缺少明智的默认值。此类使用Spring bean名称作为作业名称,使用Quartz默认组(“ DEFAULT”)作为作业组,将当前时间用作开始时间,以及不确定的重复(如果未指定)。


This class will also register the trigger with the job name and group of a given JobDetail. This allows SchedulerFactoryBean to automatically register a trigger for the corresponding JobDetail, instead of registering the JobDetail separately.


此类还将使用给定JobDetail的作业名称和组来注册触发器。这允许SchedulerFactoryBean自动为相应的JobDetail注册触发器,而不是单独注册JobDetail。

同样,CronTrigger也是一个触发器,此处不再赘述。

和上面的例子一样,我们只需要在xml中动一下手脚就可以了:

<?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:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"><bean name="job1" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"><property name="jobClass" value="com.aran.modules.quartz.myQuartzJob"/><property name="jobDataAsMap"><map><entry key="timeout" value="50000"/></map></property></bean><bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"><!--        需要调用的job,ref指向了job1--><property name="jobDetail" ref="job1"/><!--        首次执行延迟,单位:毫秒--><property name="startDelay" value="2000"/><!--        周期执行间隔,单位:毫秒--><property name="repeatInterval" value="5000"/></bean><!--  调度触发器  --><bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"><property name="jobDetail" ref="job1"/><property name="cronExpression" value="0/5 * * * * ?"/></bean><bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers"><list><ref bean="cronTrigger"/></list></property></bean>
</beans>

我们设置的是每5秒执行一次:

/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/bin/java -Dvisualvm.id=28525250453841 "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=59457:/Applications/IntelliJ IDEA.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home/lib/tools.jar:/Users/aran/IdeaProjects/Spring-examples/out/production/spring-examples:/Users/aran/IdeaProjects/Spring-examples/lib/spring-instrument-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-aspects-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-jms-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-beans-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-expression-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-oxm-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-context-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-jdbc-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/commons-logging-1.2.jar:/Users/aran/IdeaProjects/Spring-examples/lib/aopalliance-1.0.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-context-support-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-tx-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-aop-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-orm-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-messaging-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-test-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-core-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-webmvc-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-web-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-security-aspects/5.2.1.RELEASE/spring-security-aspects-5.2.1.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-security-messaging/5.2.1.RELEASE/spring-security-messaging-5.2.1.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-security-ldap/5.2.1.RELEASE/spring-security-ldap-5.2.1.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-security-cas/5.2.1.RELEASE/spring-security-cas-5.2.1.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-security-oauth2-resource-server/5.2.1.RELEASE/spring-security-oauth2-resource-server-5.2.1.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-security-remoting/5.2.1.RELEASE/spring-security-remoting-5.2.1.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-security-taglibs/5.2.1.RELEASE/spring-security-taglibs-5.2.1.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-security-openid/5.2.1.RELEASE/spring-security-openid-5.2.1.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-security-oauth2-core/5.2.1.RELEASE/spring-security-oauth2-core-5.2.1.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-security-test/5.2.1.RELEASE/spring-security-test-5.2.1.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-security-crypto/5.2.1.RELEASE/spring-security-crypto-5.2.1.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-security-acl/5.2.1.RELEASE/spring-security-acl-5.2.1.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-security-web/5.2.1.RELEASE/spring-security-web-5.2.1.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-security-core/5.2.1.RELEASE/spring-security-core-5.2.1.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-security-config/5.2.1.RELEASE/spring-security-config-5.2.1.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-ws.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-data-jpa/2.2.4.RELEASE/spring-data-jpa-2.2.4.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-data-commons/2.2.4.RELEASE/spring-data-commons-2.2.4.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-binding-2.4.0.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-js-2.4.0.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-faces-2.4.0.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-js-resources-2.4.0.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-webflow-2.4.0.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-batch-infrastructure-2.2.6.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-batch-core-2.2.6.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-event/5.2.3.RELEASE/spring-integration-event-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-ftp/5.2.3.RELEASE/spring-integration-ftp-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-feed/5.2.3.RELEASE/spring-integration-feed-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-groovy/5.2.3.RELEASE/spring-integration-groovy-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-gemfire/5.2.3.RELEASE/spring-integration-gemfire-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-http/5.2.3.RELEASE/spring-integration-http-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-amqp/5.2.3.RELEASE/spring-integration-amqp-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-jdbc/5.2.3.RELEASE/spring-integration-jdbc-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-jmx/5.2.3.RELEASE/spring-integration-jmx-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-jpa/5.2.3.RELEASE/spring-integration-jpa-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-mongodb/5.2.3.RELEASE/spring-integration-mongodb-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-mqtt/5.2.3.RELEASE/spring-integration-mqtt-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-rmi/5.2.3.RELEASE/spring-integration-rmi-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-mail/5.2.3.RELEASE/spring-integration-mail-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-redis/5.2.3.RELEASE/spring-integration-redis-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-scripting/5.2.3.RELEASE/spring-integration-scripting-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-stream/5.2.3.RELEASE/spring-integration-stream-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-syslog/5.2.3.RELEASE/spring-integration-syslog-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-test/5.2.3.RELEASE/spring-integration-test-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-security/5.2.3.RELEASE/spring-integration-security-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-sftp/5.2.3.RELEASE/spring-integration-sftp-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-twitter/5.0.9.RELEASE/spring-integration-twitter-5.0.9.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-ws/5.2.3.RELEASE/spring-integration-ws-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-xml/5.2.3.RELEASE/spring-integration-xml-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-xmpp/5.2.3.RELEASE/spring-integration-xmpp-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-jms/5.2.3.RELEASE/spring-integration-jms-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-ip/5.2.3.RELEASE/spring-integration-ip-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-file/5.2.3.RELEASE/spring-integration-file-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/spring-integration-core/5.2.3.RELEASE/spring-integration-core-5.2.3.RELEASE.jar:/Users/aran/IdeaProjects/Spring-examples/lib/quartz-2.3.2.jar:/Users/aran/IdeaProjects/Spring-examples/lib/slf4j-api-1.7.30.jar:/Users/aran/IdeaProjects/Spring-examples/lib/logback-core-1.2.3.jar:/Users/aran/IdeaProjects/Spring-examples/lib/logback-classic-1.2.3.jar com.aran.modules.quartz.Main
16:43:32.546 [main] INFO org.quartz.impl.StdSchedulerFactory - Using default implementation for ThreadExecutor
16:43:32.563 [main] INFO org.quartz.core.SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
16:43:32.563 [main] INFO org.quartz.core.QuartzScheduler - Quartz Scheduler v.2.3.2 created.
16:43:32.564 [main] INFO org.quartz.simpl.RAMJobStore - RAMJobStore initialized.
16:43:32.565 [main] INFO org.quartz.core.QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v2.3.2) 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' with instanceId 'NON_CLUSTERED'Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.NOT STARTED.Currently in standby mode.Number of jobs executed: 0Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.16:43:32.565 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' initialized from an externally provided properties instance.
16:43:32.565 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler version: 2.3.2
16:43:32.566 [main] INFO org.quartz.core.QuartzScheduler - JobFactory set to: org.springframework.scheduling.quartz.AdaptableJobFactory@78047b92
七月 31, 2020 4:43:32 下午 org.springframework.scheduling.quartz.SchedulerFactoryBean startScheduler
信息: Starting Quartz Scheduler now
16:43:32.581 [main] INFO org.quartz.core.QuartzScheduler - Scheduler org.springframework.scheduling.quartz.SchedulerFactoryBean#0_$_NON_CLUSTERED started.
16:43:32.582 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
16:43:35.011 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.job1
16:43:35.011 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1执行时间:2020-07-31 16:43:35
16:43:40.008 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-2] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.job1
16:43:40.008 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-2执行时间:2020-07-31 16:43:40
16:43:45.006 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-3] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.job1
16:43:45.006 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-3执行时间:2020-07-31 16:43:45
16:43:50.005 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-4] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.job1
16:43:50.005 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-4执行时间:2020-07-31 16:43:50
16:43:55.001 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-5] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.job1
16:43:55.001 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-5执行时间:2020-07-31 16:43:55
16:44:00.003 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_QuartzSchedulerThread] DEBUG org.quartz.core.QuartzSchedulerThread - batch acquisition of 1 triggers
16:44:00.003 [org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-6] DEBUG org.quartz.core.JobRunShell - Calling execute on job DEFAULT.job1
org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-6执行时间:2020-07-31 16:44:00

cron是一种规则,如果你对它还不是很熟,请参考这篇文章之后,自己试着玩一下

Quartz的使用就暂时介绍到这里,如果你觉得有点复杂,咱接着往下看!

3.2 Spring Task

Spring Task可以视为一个轻量级的Quartz,可通过配置文件或注解两种方式实现:

3.2.1 配置文件方式

我们先定义一个需要执行的任务:

package com.aran.modules.TimerSchedule.springTask;import org.springframework.stereotype.Service;/*** @Author Aran* @Date 2020/7/31 4:58 下午*/
@Service
public class TaskJob {public void job(){System.out.println("job is active!");}}
  • 此处@Service 和 @Component当然都可以,见名知意,还是Service好一些

下面,我们在配置文件中对该任务进行扫描:

<task:scheduled-tasks><task:scheduled ref="taskJob" method="job" cron="0/3 * * * * ?"/></task:scheduled-tasks><context:component-scan base-package=" com.aran.modules.TimerSchedule.springTask " />

此处我们将其定义为每3秒执行一次,ref为需要扫描的类,methods为执行的方法,cron为执行策略,当然,component-scan进行组件扫描

好了,运行一下main:

执行时间:2020-07-31 17:10:48
执行时间:2020-07-31 17:10:51
执行时间:2020-07-31 17:10:54
执行时间:2020-07-31 17:10:57
执行时间:2020-07-31 17:11:00
执行时间:2020-07-31 17:11:03
执行时间:2020-07-31 17:11:06
执行时间:2020-07-31 17:11:09
执行时间:2020-07-31 17:11:12
执行时间:2020-07-31 17:11:15
执行时间:2020-07-31 17:11:18
执行时间:2020-07-31 17:11:21
执行时间:2020-07-31 17:11:24
执行时间:2020-07-31 17:11:27
执行时间:2020-07-31 17:11:30
执行时间:2020-07-31 17:11:33
执行时间:2020-07-31 17:11:36
执行时间:2020-07-31 17:11:39
执行时间:2020-07-31 17:11:42
执行时间:2020-07-31 17:11:45
执行时间:2020-07-31 17:11:48
执行时间:2020-07-31 17:11:51
执行时间:2020-07-31 17:11:54
执行时间:2020-07-31 17:11:57
执行时间:2020-07-31 17:12:00
执行时间:2020-07-31 17:12:03

3.2.2 注解方式

一个@Scheduled解决所有问题:

我们先看下@Scheduled的源码

/** Copyright 2002-2019 the original author or authors.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      https://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package org.springframework.scheduling.annotation;import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;import org.springframework.scheduling.config.ScheduledTaskRegistrar;/*** Annotation that marks a method to be scheduled. Exactly one of the* {@link #cron}, {@link #fixedDelay}, or {@link #fixedRate} attributes must be* specified.* 标记要调度的方法的注释。* 必须指定{@link #cron},{@ link #fixedDelay}或{@link #fixedRate}其中一个属性。* * * <p>The annotated method must expect no arguments. It will typically have* a {@code void} return type; if not, the returned value will be ignored* when called through the scheduler.*带注释的方法必须不包含任何参数。它通常具有{@code void}返回类型;如果不是,则通过调度程序调	 	 用时将忽略返回的值。* <p>Purocessing of {@code @Scheduled} annotations is performed by* registering a {@link ScheduledAnnotationBeanPostProcessor}. This can be* done manually or, more conveniently, through the {@code <task:annotation-driven/>}* element or @{@link EnableScheduling} annotation.*{@code @Scheduled}注解由注册{@link ScheduledAnnotationBeanPostProcessor}进行处理。可以手动完成此操作,也可以通过{@code <task:annotation-driven />}元素或@ {@ link EnableScheduling}注解更方便地完成此操作。* <p>This annotation may be used as a <em>meta-annotation</em> to create custom* <em>composed annotations</em> with attribute overrides.*<p>此注释可以用作<em>元注释</ em>来创建自定义*  具有属性覆盖的<em>组成的注释</ em>。* @author Mark Fisher* @author Juergen Hoeller* @author Dave Syer* @author Chris Beams* @since 3.0* @see EnableScheduling* @see ScheduledAnnotationBeanPostProcessor* @see Schedules*/
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Schedules.class)
public @interface Scheduled {/*** A special cron expression value that indicates a disabled trigger: {@value}.* <p>This is primarily meant for use with <code>${...}</code> placeholders,* allowing for external disabling of corresponding scheduled methods.* @since 5.1* @see ScheduledTaskRegistrar#CRON_DISABLED*/String CRON_DISABLED = ScheduledTaskRegistrar.CRON_DISABLED;/*** A cron-like expression, extending the usual UN*X definition to include triggers* on the second, minute, hour, day of month, month, and day of week.* <p>For example, {@code "0 * * * * MON-FRI"} means once per minute on weekdays* (at the top of the minute - the 0th second).* <p>The fields read from left to right are interpreted as follows.* <ul>* <li>second</li>* <li>minute</li>* <li>hour</li>* <li>day of month</li>* <li>month</li>* <li>day of week</li>* </ul>* <p>The special value {@link #CRON_DISABLED "-"} indicates a disabled cron* trigger, primarily meant for externally specified values resolved by a* <code>${...}</code> placeholder.* @return an expression that can be parsed to a cron schedule* @see org.springframework.scheduling.support.CronSequenceGenerator* 一个cron表达式*/String cron() default "";/*** A time zone for which the cron expression will be resolved. By default, this* attribute is the empty String (i.e. the server's local time zone will be used).* @return a zone id accepted by {@link java.util.TimeZone#getTimeZone(String)},* or an empty String to indicate the server's default time zone* @since 4.0* @see org.springframework.scheduling.support.CronTrigger#CronTrigger(String, java.util.TimeZone)* @see java.util.TimeZone* 一个时间区*/String zone() default "";/*** Execute the annotated method with a fixed period in milliseconds between the* end of the last invocation and the start of the next.* @return the delay in milliseconds* 本次开始与上次结束执行的时间间隔,单位:毫秒*/long fixedDelay() default -1;/*** Execute the annotated method with a fixed period in milliseconds between the* end of the last invocation and the start of the next.* @return the delay in milliseconds as a String value, e.g. a placeholder* or a {@link java.time.Duration#parse java.time.Duration} compliant value* @since 3.2.2* 字符串形式*/String fixedDelayString() default "";/*** Execute the annotated method with a fixed period in milliseconds between* invocations.* @return the period in milliseconds* 按一定的速率执行*/long fixedRate() default -1;/*** Execute the annotated method with a fixed period in milliseconds between* invocations.* @return the period in milliseconds as a String value, e.g. a placeholder* or a {@link java.time.Duration#parse java.time.Duration} compliant value* @since 3.2.2* 字符串形式*/String fixedRateString() default "";/*** Number of milliseconds to delay before the first execution of a* {@link #fixedRate} or {@link #fixedDelay} task.* @return the initial delay in milliseconds* @since 3.2* 初始延迟*/long initialDelay() default -1;/*** Number of milliseconds to delay before the first execution of a* {@link #fixedRate} or {@link #fixedDelay} task.* @return the initial delay in milliseconds as a String value, e.g. a placeholder* or a {@link java.time.Duration#parse java.time.Duration} compliant value* @since 3.2.2* 字符串形式*/String initialDelayString() default "";}

开始的时候,我和你一样奇怪,fixedDelay 和 fixedRate到底是啥区别,于是乎,百度一下,你就知道:

@Scheduled注解可以控制方法定时执行,其中有三个参数可选择:

  • fixedDelay控制方法执行的间隔时间,是以上一次方法执行完开始算起,如上一次方法执行阻塞住了,那么直到上一次执行完,并间隔给定的时间后,执行下一次。
  • fixedRate是按照一定的速率执行,是从上一次方法执行开始的时间算起,如果上一次方法阻塞住了,下一次也是不会执行,但是在阻塞这段时间内累计应该执行的次数,当不再阻塞时,一下子把这些全部执行掉,而后再按照固定速率继续执行。
  • cron表达式可以定制化执行任务,但是执行的方式是与fixedDelay相近的,也是会按照上一次方法结束时间开始算起。

好了,我们同样在TaskJob类中定义一个使用注解的定时任务方法:

@Scheduled(cron = "0/3 * * * * ?")public void annotationJob(){System.out.println("注解定时任务执行时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));}

为了更好的验证注解定时任务的执行,我们注解掉配置文件中的相关内容:

在这里插入图片描述

此时启动Main,得到:

在这里插入图片描述

就说到这吧,以后遇到更好的,更完善的方式,再回来记录!

完整代码地址:spring-examples@github


不管前方的路有多苦,只要走的方向正确,不管多么崎岖不平,都比站在原地更接近幸福。
——宫崎骏

查看全文
如若内容造成侵权/违法违规/事实不符,请联系编程学习网邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

相关文章

  1. es集群+head插件部署

    一、单节点部署 1、下载elasticsearch-6.8.0.tar.gz wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.8.0.tar.gz 2、创建es运行用户 useradd es 3、将安装包上传至elasticsearch用户家目录下并解压 tar -xf elasticsearch-6.8.0.tar.gz 进入elas…...

    2024/5/2 12:31:01
  2. vnc远程控制软件下载,盘点三款好用的vnc远程控制软件下载

    看到vnc远程控制软件下载就可以知道这是一款什么软件,远程控制软件讲究的是方便好操作,因为vnc远程控制软件下载这个功能就是比较麻烦的。这三款vnc远程控制软件下载,你一定会喜欢。 第一款:IIS7服务器管理工具 这个工具里面的VNC功能可以说是使用感非常棒的。它可以一键导…...

    2024/4/15 18:57:28
  3. 开源实时音视频技术WebRTC在Windows下的简明编译教程

    1、前言随着音视频技术的不断普及,Google推出的 WebRTC 越来越受到大家的喜欢。现在很多直播产品都是基于WebRTC 进行二次开发做出来的。WebRTC是提供了一整套处理实时音视频的开源库。它包括了音视频处理(采集,编解码,前处理,后处理,渲染),数据传输(实时传输,流控)…...

    2024/4/17 4:49:49
  4. 循环语句while和for

    1.while循环 通过一个条件来控制是否要继续反复执行循环体中的语句 while 条件表达式:循环体 当条件为True时,重复执行循环体中的语句,当条件为False时,退出循环i = 1 while i < 10:print(i)i += 1 i初始值为1,当i<10时,输出i,i自增1,当i=10时,不满足条件,退出…...

    2024/4/26 6:45:53
  5. 基于RFID技术的智能仓储管理系统解决方案—铨顺宏

    一、行业背景 简单、静态的传统仓储管理模式普遍存的物资库存量巨大、物资跟踪困难、资金和物资周转效率较低、人力成本偏高、物流管理的信息和手段落后等缺点,已不能适应新的仓储管理需求。破除传统的仓储管理模式,积极探讨新的信息管理技术,在适应企业原有管理流程的基础上…...

    2024/4/15 18:57:25
  6. 量化投资学习-26:静等花开的鸡汤文,有时候能养生,有时候就是自我的迷幻剂

    每个孩子都是种子,只不过每个人的花期不同。每个正弦波都有自己的周期,只是频率和相位不同而已。每只股票都会上涨,只不过节奏不同。理是这个理,然而,现实是多样的。这个理这说明了实现世界的一部分,而不是全部。这个理,存在的几个问题是:(1)忽略了辨证哲学中提到的个…...

    2024/4/19 6:43:19
  7. 学习次世代游戏建模需要了解什么?零基础成为次世代游戏设计师需要哪些阶段

    次世代这个名词一直伴随着游戏机产业。从字面上看,次世代游戏就是“下一个游戏”的日文字面内容。如今众多的游戏都被冠以“次世代”,但是“次世代”究竟是什么呢?现阶段我们可以这样理解,次世代就是一PS4、Wii U、XboxOne等第八世代游戏机为代表的,相对于上一代PS3、Wii、…...

    2024/4/15 18:57:23
  8. 秒懂系列——人脸识别

    前言: 本文仅对人脸识别方法做了简单的整理,适合想了解人脸识别的人。也欢迎专业人员提提意见~ 背景 用途 识别出图片或视频中出现的人脸的身份。 常见的业务场景:安检、打卡、刷脸支付等需要基于人脸进行身份识别的场合。当然,身份识别除了人脸识别外还包括指纹识别、虹膜…...

    2024/5/1 5:36:52
  9. 新闻文本分类Task6

    Task6 基于深度学习的文本分类3 基于深度学习的文本分类 学习目标了解Transformer的原理和基于预训练语言模型(Bert)的词表示 学会Bert的使用,具体包括pretrain和finetune文本表示方法Part4 Transformer原理 Transformer是在"Attention is All You Need"中提出的,…...

    2024/4/15 18:57:22
  10. BGP线路服务器延迟怎么样

    BGP机房的基本要素 BGP机房便是服务器租用商根据技术的方式,完成不一样营运商可以相互浏览一个IP,而且不一样营运商中间都能做到更快的连接速度的有关互联网技术问题。 最先说一说中国存在着的南北方路线互通的问题。这个问题从中国电信网和网通电信分离以后刚开始出現的,因…...

    2024/4/15 14:35:45
  11. WebRTC学习

    一. WebRTC学习1.1 WebRTC现状本人最早接触WebRTC是在2011年底, 那时Google已经在Android源码中加入了webrtc源码,放在/external/webrtc/, 但是Android并没有用到它,更没有被浏览器使用。 当时试图在Android 2.3(Gingerbread)高通平台的手机上 用H.264 硬件codec替换掉…...

    2024/4/27 11:17:54
  12. RabbitMQ(二)

    三、RabbitMQ高级特性 7. 消费端ACK与重回队列 7.1 消费端的手工ACK和NACK 消费端进行消费的时候,如果由于业务异常我们可以进行日志的记录,然后进行补偿! 如果由于服务器宕机等严重问题,那我们就需要手工进行ACK保障消费端消费成功! 7.2 消费端的重回队列 消费端重回队列…...

    2024/4/15 14:35:43
  13. SOLID原则-开闭原则

    开闭原则定义 Robert C. Martin 认为这个原则是面向对象设计中最重要的一个原则,但他不是第一个定义这个原则的人,Bertrand Meyer 在1988年写的一本书《Object-Oriented Software Construction》,其中解释了开闭原则:软件实体(类,模块,方法等)应该对扩展开放,对修改关…...

    2024/4/19 10:17:24
  14. vue双向绑定原理分析

    当我们学习angular或者vue的时候,其双向绑定为我们开发带来了诸多便捷,今天我们就来分析一下vue双向绑定的原理。简易vue源码地址:https://github.com/jiangzhenfei/simple-Vue1.vue双向绑定原理vue.js 则是采用数据劫持结合发布者-订阅者模式的方式,通过Object.defineProp…...

    2024/4/17 4:49:57
  15. Vue配置TinyMCE富文本编辑器 + 图片(本地)上传到服务器

    一、TinyMCE是什么? TinyMCE是一款易用、且功能强大的所见即所得的富文本编辑器。同类程序有:UEditor、Kindeditor、Simditor、CKEditor、wangEditor、Suneditor、froala等等。 我们可以先大体看一下配置完成后的样子注:博主使用的TinyMCE版本是 “tinymce”: “^4.8.2” …...

    2024/4/15 18:57:19
  16. KiBiEx打造放心、信任、稳定、安全的合约交易平台什么是加密货币?

    1848年第一个近代期货交易所——芝加哥期货交易所(CBOT)成立至今,期货合约成为传统金融市场中最常见的金融工具。随着区块链行业的极速发展,合约时代的来临成为历史必然,这也意味着一个媲美现货市场的新风口的到来。 从去年开始,合约交易呈“井喷式”发展,各路玩家纷至沓来…...

    2024/4/15 18:57:18
  17. Redis缓存穿透和缓存雪崩以及解决方案

    Redis缓存穿透和缓存雪崩以及解决方案参考文章: (1)Redis缓存穿透和缓存雪崩以及解决方案 (2)https://www.cnblogs.com/george1994/p/10668889.html 备忘一下。...

    2024/4/15 18:57:19
  18. 详谈MySQL和MariaDB区别与性能全面对比

    MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险。MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为My…...

    2024/4/30 11:16:38
  19. 千亿独角兽亏损31亿,装下链家的贝壳上市后如何装下利润?

    终于,那个装着链家的贝壳向SEC递交招股书,正式赴美IPO。 招股书显示,贝壳此次计划募资不超过30亿元,承销商包括高盛、摩根士丹利、华兴资本等。若顺利完成上市流程,或许将成为2020年最大规模的中赴美IPO。 而关于上市原因,市场观点认为,其创始人左晖曾与投资人签署对赌协…...

    2024/4/15 18:57:16
  20. 二、一切都是对象

    二、一切都是对象 2.1 用引用操作对象 遥控器(引用)来操作电视机(对象) 例如String s一个引用对其初始化String s = “asdf"你就可以对s进行操作,此时s没有与任何事物相关联 2.2 必须由你创建所有对象 String s = new String(“asdf”)这里与新的对象关联 2.2.1 存储到…...

    2024/4/15 18:57:14

最新文章

  1. python 关键字(import)

    4、import 在Python编程中&#xff0c;import 是一个至关重要的关键字&#xff0c;它用于导入其他Python模块或库中的代码&#xff0c;以便在当前程序中使用。无论是Python新手还是经验丰富的开发者&#xff0c;都需要对import有深入的理解。 基础知识&#xff1a;import 的基…...

    2024/5/2 19:35:40
  2. 梯度消失和梯度爆炸的一些处理方法

    在这里是记录一下梯度消失或梯度爆炸的一些处理技巧。全当学习总结了如有错误还请留言&#xff0c;在此感激不尽。 权重和梯度的更新公式如下&#xff1a; w w − η ⋅ ∇ w w w - \eta \cdot \nabla w ww−η⋅∇w 个人通俗的理解梯度消失就是网络模型在反向求导的时候出…...

    2024/3/20 10:50:27
  3. 基于FPGA轻松玩转AI

    启动人工智能应用从来没有像现在这样容易&#xff01;受益于像Xilinx Zynq UltraScale MPSoC 这样的FPGA&#xff0c;AI现在也可以离线使用或在边缘部署、使用.可用于开发和部署用于实时推理的机器学习应用&#xff0c;因此将AI集成到应用中变得轻而易举。图像检测或分类、模式…...

    2024/4/30 1:21:06
  4. 3d representation的一些基本概念

    顶点&#xff08;Vertex&#xff09;&#xff1a;三维空间中的一个点&#xff0c;可以有多个属性&#xff0c;如位置坐标、颜色、纹理坐标和法线向量。它是构建三维几何形状的基本单元。 边&#xff08;Edge&#xff09;&#xff1a;连接两个顶点形成的直线段&#xff0c;它定…...

    2024/5/2 17:20:39
  5. c++类的继承方式

    在 C 中&#xff0c;类的继承方式有三种&#xff1a;公有继承&#xff08;public inheritance&#xff09;、保护继承&#xff08;protected inheritance&#xff09;和私有继承&#xff08;private inheritance&#xff09;。这些继承方式决定了派生类对基类成员的访问权限。 …...

    2024/5/1 9:08:04
  6. 416. 分割等和子集问题(动态规划)

    题目 题解 class Solution:def canPartition(self, nums: List[int]) -> bool:# badcaseif not nums:return True# 不能被2整除if sum(nums) % 2 ! 0:return False# 状态定义&#xff1a;dp[i][j]表示当背包容量为j&#xff0c;用前i个物品是否正好可以将背包填满&#xff…...

    2024/5/2 11:19:01
  7. 【Java】ExcelWriter自适应宽度工具类(支持中文)

    工具类 import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet;/*** Excel工具类** author xiaoming* date 2023/11/17 10:40*/ public class ExcelUti…...

    2024/5/2 16:04:58
  8. Spring cloud负载均衡@LoadBalanced LoadBalancerClient

    LoadBalance vs Ribbon 由于Spring cloud2020之后移除了Ribbon&#xff0c;直接使用Spring Cloud LoadBalancer作为客户端负载均衡组件&#xff0c;我们讨论Spring负载均衡以Spring Cloud2020之后版本为主&#xff0c;学习Spring Cloud LoadBalance&#xff0c;暂不讨论Ribbon…...

    2024/5/1 21:18:12
  9. TSINGSEE青犀AI智能分析+视频监控工业园区周界安全防范方案

    一、背景需求分析 在工业产业园、化工园或生产制造园区中&#xff0c;周界防范意义重大&#xff0c;对园区的安全起到重要的作用。常规的安防方式是采用人员巡查&#xff0c;人力投入成本大而且效率低。周界一旦被破坏或入侵&#xff0c;会影响园区人员和资产安全&#xff0c;…...

    2024/5/2 9:47:31
  10. VB.net WebBrowser网页元素抓取分析方法

    在用WebBrowser编程实现网页操作自动化时&#xff0c;常要分析网页Html&#xff0c;例如网页在加载数据时&#xff0c;常会显示“系统处理中&#xff0c;请稍候..”&#xff0c;我们需要在数据加载完成后才能继续下一步操作&#xff0c;如何抓取这个信息的网页html元素变化&…...

    2024/5/2 9:47:31
  11. 【Objective-C】Objective-C汇总

    方法定义 参考&#xff1a;https://www.yiibai.com/objective_c/objective_c_functions.html Objective-C编程语言中方法定义的一般形式如下 - (return_type) method_name:( argumentType1 )argumentName1 joiningArgument2:( argumentType2 )argumentName2 ... joiningArgu…...

    2024/5/2 6:03:07
  12. 【洛谷算法题】P5713-洛谷团队系统【入门2分支结构】

    &#x1f468;‍&#x1f4bb;博客主页&#xff1a;花无缺 欢迎 点赞&#x1f44d; 收藏⭐ 留言&#x1f4dd; 加关注✅! 本文由 花无缺 原创 收录于专栏 【洛谷算法题】 文章目录 【洛谷算法题】P5713-洛谷团队系统【入门2分支结构】&#x1f30f;题目描述&#x1f30f;输入格…...

    2024/5/2 9:47:30
  13. 【ES6.0】- 扩展运算符(...)

    【ES6.0】- 扩展运算符... 文章目录 【ES6.0】- 扩展运算符...一、概述二、拷贝数组对象三、合并操作四、参数传递五、数组去重六、字符串转字符数组七、NodeList转数组八、解构变量九、打印日志十、总结 一、概述 **扩展运算符(...)**允许一个表达式在期望多个参数&#xff0…...

    2024/5/1 11:24:00
  14. 摩根看好的前智能硬件头部品牌双11交易数据极度异常!——是模式创新还是饮鸩止渴?

    文 | 螳螂观察 作者 | 李燃 双11狂欢已落下帷幕&#xff0c;各大品牌纷纷晒出优异的成绩单&#xff0c;摩根士丹利投资的智能硬件头部品牌凯迪仕也不例外。然而有爆料称&#xff0c;在自媒体平台发布霸榜各大榜单喜讯的凯迪仕智能锁&#xff0c;多个平台数据都表现出极度异常…...

    2024/5/2 5:31:39
  15. Go语言常用命令详解(二)

    文章目录 前言常用命令go bug示例参数说明 go doc示例参数说明 go env示例 go fix示例 go fmt示例 go generate示例 总结写在最后 前言 接着上一篇继续介绍Go语言的常用命令 常用命令 以下是一些常用的Go命令&#xff0c;这些命令可以帮助您在Go开发中进行编译、测试、运行和…...

    2024/5/1 20:22:59
  16. 用欧拉路径判断图同构推出reverse合法性:1116T4

    http://cplusoj.com/d/senior/p/SS231116D 假设我们要把 a a a 变成 b b b&#xff0c;我们在 a i a_i ai​ 和 a i 1 a_{i1} ai1​ 之间连边&#xff0c; b b b 同理&#xff0c;则 a a a 能变成 b b b 的充要条件是两图 A , B A,B A,B 同构。 必要性显然&#xff0…...

    2024/5/2 9:47:28
  17. 【NGINX--1】基础知识

    1、在 Debian/Ubuntu 上安装 NGINX 在 Debian 或 Ubuntu 机器上安装 NGINX 开源版。 更新已配置源的软件包信息&#xff0c;并安装一些有助于配置官方 NGINX 软件包仓库的软件包&#xff1a; apt-get update apt install -y curl gnupg2 ca-certificates lsb-release debian-…...

    2024/5/2 9:47:27
  18. Hive默认分割符、存储格式与数据压缩

    目录 1、Hive默认分割符2、Hive存储格式3、Hive数据压缩 1、Hive默认分割符 Hive创建表时指定的行受限&#xff08;ROW FORMAT&#xff09;配置标准HQL为&#xff1a; ... ROW FORMAT DELIMITED FIELDS TERMINATED BY \u0001 COLLECTION ITEMS TERMINATED BY , MAP KEYS TERMI…...

    2024/5/2 0:07:22
  19. 【论文阅读】MAG:一种用于航天器遥测数据中有效异常检测的新方法

    文章目录 摘要1 引言2 问题描述3 拟议框架4 所提出方法的细节A.数据预处理B.变量相关分析C.MAG模型D.异常分数 5 实验A.数据集和性能指标B.实验设置与平台C.结果和比较 6 结论 摘要 异常检测是保证航天器稳定性的关键。在航天器运行过程中&#xff0c;传感器和控制器产生大量周…...

    2024/5/2 8:37:00
  20. --max-old-space-size=8192报错

    vue项目运行时&#xff0c;如果经常运行慢&#xff0c;崩溃停止服务&#xff0c;报如下错误 FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory 因为在 Node 中&#xff0c;通过JavaScript使用内存时只能使用部分内存&#xff08;64位系统&…...

    2024/5/2 9:47:26
  21. 基于深度学习的恶意软件检测

    恶意软件是指恶意软件犯罪者用来感染个人计算机或整个组织的网络的软件。 它利用目标系统漏洞&#xff0c;例如可以被劫持的合法软件&#xff08;例如浏览器或 Web 应用程序插件&#xff09;中的错误。 恶意软件渗透可能会造成灾难性的后果&#xff0c;包括数据被盗、勒索或网…...

    2024/5/2 9:47:25
  22. JS原型对象prototype

    让我简单的为大家介绍一下原型对象prototype吧&#xff01; 使用原型实现方法共享 1.构造函数通过原型分配的函数是所有对象所 共享的。 2.JavaScript 规定&#xff0c;每一个构造函数都有一个 prototype 属性&#xff0c;指向另一个对象&#xff0c;所以我们也称为原型对象…...

    2024/5/1 14:33:22
  23. C++中只能有一个实例的单例类

    C中只能有一个实例的单例类 前面讨论的 President 类很不错&#xff0c;但存在一个缺陷&#xff1a;无法禁止通过实例化多个对象来创建多名总统&#xff1a; President One, Two, Three; 由于复制构造函数是私有的&#xff0c;其中每个对象都是不可复制的&#xff0c;但您的目…...

    2024/5/2 18:46:52
  24. python django 小程序图书借阅源码

    开发工具&#xff1a; PyCharm&#xff0c;mysql5.7&#xff0c;微信开发者工具 技术说明&#xff1a; python django html 小程序 功能介绍&#xff1a; 用户端&#xff1a; 登录注册&#xff08;含授权登录&#xff09; 首页显示搜索图书&#xff0c;轮播图&#xff0…...

    2024/5/2 7:30:11
  25. 电子学会C/C++编程等级考试2022年03月(一级)真题解析

    C/C++等级考试(1~8级)全部真题・点这里 第1题:双精度浮点数的输入输出 输入一个双精度浮点数,保留8位小数,输出这个浮点数。 时间限制:1000 内存限制:65536输入 只有一行,一个双精度浮点数。输出 一行,保留8位小数的浮点数。样例输入 3.1415926535798932样例输出 3.1…...

    2024/5/1 20:56:20
  26. 配置失败还原请勿关闭计算机,电脑开机屏幕上面显示,配置失败还原更改 请勿关闭计算机 开不了机 这个问题怎么办...

    解析如下&#xff1a;1、长按电脑电源键直至关机&#xff0c;然后再按一次电源健重启电脑&#xff0c;按F8健进入安全模式2、安全模式下进入Windows系统桌面后&#xff0c;按住“winR”打开运行窗口&#xff0c;输入“services.msc”打开服务设置3、在服务界面&#xff0c;选中…...

    2022/11/19 21:17:18
  27. 错误使用 reshape要执行 RESHAPE,请勿更改元素数目。

    %读入6幅图像&#xff08;每一幅图像的大小是564*564&#xff09; f1 imread(WashingtonDC_Band1_564.tif); subplot(3,2,1),imshow(f1); f2 imread(WashingtonDC_Band2_564.tif); subplot(3,2,2),imshow(f2); f3 imread(WashingtonDC_Band3_564.tif); subplot(3,2,3),imsho…...

    2022/11/19 21:17:16
  28. 配置 已完成 请勿关闭计算机,win7系统关机提示“配置Windows Update已完成30%请勿关闭计算机...

    win7系统关机提示“配置Windows Update已完成30%请勿关闭计算机”问题的解决方法在win7系统关机时如果有升级系统的或者其他需要会直接进入一个 等待界面&#xff0c;在等待界面中我们需要等待操作结束才能关机&#xff0c;虽然这比较麻烦&#xff0c;但是对系统进行配置和升级…...

    2022/11/19 21:17:15
  29. 台式电脑显示配置100%请勿关闭计算机,“准备配置windows 请勿关闭计算机”的解决方法...

    有不少用户在重装Win7系统或更新系统后会遇到“准备配置windows&#xff0c;请勿关闭计算机”的提示&#xff0c;要过很久才能进入系统&#xff0c;有的用户甚至几个小时也无法进入&#xff0c;下面就教大家这个问题的解决方法。第一种方法&#xff1a;我们首先在左下角的“开始…...

    2022/11/19 21:17:14
  30. win7 正在配置 请勿关闭计算机,怎么办Win7开机显示正在配置Windows Update请勿关机...

    置信有很多用户都跟小编一样遇到过这样的问题&#xff0c;电脑时发现开机屏幕显现“正在配置Windows Update&#xff0c;请勿关机”(如下图所示)&#xff0c;而且还需求等大约5分钟才干进入系统。这是怎样回事呢&#xff1f;一切都是正常操作的&#xff0c;为什么开时机呈现“正…...

    2022/11/19 21:17:13
  31. 准备配置windows 请勿关闭计算机 蓝屏,Win7开机总是出现提示“配置Windows请勿关机”...

    Win7系统开机启动时总是出现“配置Windows请勿关机”的提示&#xff0c;没过几秒后电脑自动重启&#xff0c;每次开机都这样无法进入系统&#xff0c;此时碰到这种现象的用户就可以使用以下5种方法解决问题。方法一&#xff1a;开机按下F8&#xff0c;在出现的Windows高级启动选…...

    2022/11/19 21:17:12
  32. 准备windows请勿关闭计算机要多久,windows10系统提示正在准备windows请勿关闭计算机怎么办...

    有不少windows10系统用户反映说碰到这样一个情况&#xff0c;就是电脑提示正在准备windows请勿关闭计算机&#xff0c;碰到这样的问题该怎么解决呢&#xff0c;现在小编就给大家分享一下windows10系统提示正在准备windows请勿关闭计算机的具体第一种方法&#xff1a;1、2、依次…...

    2022/11/19 21:17:11
  33. 配置 已完成 请勿关闭计算机,win7系统关机提示“配置Windows Update已完成30%请勿关闭计算机”的解决方法...

    今天和大家分享一下win7系统重装了Win7旗舰版系统后&#xff0c;每次关机的时候桌面上都会显示一个“配置Windows Update的界面&#xff0c;提示请勿关闭计算机”&#xff0c;每次停留好几分钟才能正常关机&#xff0c;导致什么情况引起的呢&#xff1f;出现配置Windows Update…...

    2022/11/19 21:17:10
  34. 电脑桌面一直是清理请关闭计算机,windows7一直卡在清理 请勿关闭计算机-win7清理请勿关机,win7配置更新35%不动...

    只能是等着&#xff0c;别无他法。说是卡着如果你看硬盘灯应该在读写。如果从 Win 10 无法正常回滚&#xff0c;只能是考虑备份数据后重装系统了。解决来方案一&#xff1a;管理员运行cmd&#xff1a;net stop WuAuServcd %windir%ren SoftwareDistribution SDoldnet start WuA…...

    2022/11/19 21:17:09
  35. 计算机配置更新不起,电脑提示“配置Windows Update请勿关闭计算机”怎么办?

    原标题&#xff1a;电脑提示“配置Windows Update请勿关闭计算机”怎么办&#xff1f;win7系统中在开机与关闭的时候总是显示“配置windows update请勿关闭计算机”相信有不少朋友都曾遇到过一次两次还能忍但经常遇到就叫人感到心烦了遇到这种问题怎么办呢&#xff1f;一般的方…...

    2022/11/19 21:17:08
  36. 计算机正在配置无法关机,关机提示 windows7 正在配置windows 请勿关闭计算机 ,然后等了一晚上也没有关掉。现在电脑无法正常关机...

    关机提示 windows7 正在配置windows 请勿关闭计算机 &#xff0c;然后等了一晚上也没有关掉。现在电脑无法正常关机以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容&#xff0c;让我们赶快一起来看一下吧&#xff01;关机提示 windows7 正在配…...

    2022/11/19 21:17:05
  37. 钉钉提示请勿通过开发者调试模式_钉钉请勿通过开发者调试模式是真的吗好不好用...

    钉钉请勿通过开发者调试模式是真的吗好不好用 更新时间:2020-04-20 22:24:19 浏览次数:729次 区域: 南阳 > 卧龙 列举网提醒您:为保障您的权益,请不要提前支付任何费用! 虚拟位置外设器!!轨迹模拟&虚拟位置外设神器 专业用于:钉钉,外勤365,红圈通,企业微信和…...

    2022/11/19 21:17:05
  38. 配置失败还原请勿关闭计算机怎么办,win7系统出现“配置windows update失败 还原更改 请勿关闭计算机”,长时间没反应,无法进入系统的解决方案...

    前几天班里有位学生电脑(windows 7系统)出问题了&#xff0c;具体表现是开机时一直停留在“配置windows update失败 还原更改 请勿关闭计算机”这个界面&#xff0c;长时间没反应&#xff0c;无法进入系统。这个问题原来帮其他同学也解决过&#xff0c;网上搜了不少资料&#x…...

    2022/11/19 21:17:04
  39. 一个电脑无法关闭计算机你应该怎么办,电脑显示“清理请勿关闭计算机”怎么办?...

    本文为你提供了3个有效解决电脑显示“清理请勿关闭计算机”问题的方法&#xff0c;并在最后教给你1种保护系统安全的好方法&#xff0c;一起来看看&#xff01;电脑出现“清理请勿关闭计算机”在Windows 7(SP1)和Windows Server 2008 R2 SP1中&#xff0c;添加了1个新功能在“磁…...

    2022/11/19 21:17:03
  40. 请勿关闭计算机还原更改要多久,电脑显示:配置windows更新失败,正在还原更改,请勿关闭计算机怎么办...

    许多用户在长期不使用电脑的时候&#xff0c;开启电脑发现电脑显示&#xff1a;配置windows更新失败&#xff0c;正在还原更改&#xff0c;请勿关闭计算机。。.这要怎么办呢&#xff1f;下面小编就带着大家一起看看吧&#xff01;如果能够正常进入系统&#xff0c;建议您暂时移…...

    2022/11/19 21:17:02
  41. 还原更改请勿关闭计算机 要多久,配置windows update失败 还原更改 请勿关闭计算机,电脑开机后一直显示以...

    配置windows update失败 还原更改 请勿关闭计算机&#xff0c;电脑开机后一直显示以以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容&#xff0c;让我们赶快一起来看一下吧&#xff01;配置windows update失败 还原更改 请勿关闭计算机&#x…...

    2022/11/19 21:17:01
  42. 电脑配置中请勿关闭计算机怎么办,准备配置windows请勿关闭计算机一直显示怎么办【图解】...

    不知道大家有没有遇到过这样的一个问题&#xff0c;就是我们的win7系统在关机的时候&#xff0c;总是喜欢显示“准备配置windows&#xff0c;请勿关机”这样的一个页面&#xff0c;没有什么大碍&#xff0c;但是如果一直等着的话就要两个小时甚至更久都关不了机&#xff0c;非常…...

    2022/11/19 21:17:00
  43. 正在准备配置请勿关闭计算机,正在准备配置windows请勿关闭计算机时间长了解决教程...

    当电脑出现正在准备配置windows请勿关闭计算机时&#xff0c;一般是您正对windows进行升级&#xff0c;但是这个要是长时间没有反应&#xff0c;我们不能再傻等下去了。可能是电脑出了别的问题了&#xff0c;来看看教程的说法。正在准备配置windows请勿关闭计算机时间长了方法一…...

    2022/11/19 21:16:59
  44. 配置失败还原请勿关闭计算机,配置Windows Update失败,还原更改请勿关闭计算机...

    我们使用电脑的过程中有时会遇到这种情况&#xff0c;当我们打开电脑之后&#xff0c;发现一直停留在一个界面&#xff1a;“配置Windows Update失败&#xff0c;还原更改请勿关闭计算机”&#xff0c;等了许久还是无法进入系统。如果我们遇到此类问题应该如何解决呢&#xff0…...

    2022/11/19 21:16:58
  45. 如何在iPhone上关闭“请勿打扰”

    Apple’s “Do Not Disturb While Driving” is a potentially lifesaving iPhone feature, but it doesn’t always turn on automatically at the appropriate time. For example, you might be a passenger in a moving car, but your iPhone may think you’re the one dri…...

    2022/11/19 21:16:57