Implementation of timer
Spring boot startup class code, added @EnableScheduling annotation to enable the timer function
@SpringBootApplication @EnableScheduling // 开启定时功能 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class,args); } }
Scheduling task code, the task class needs to be injected into the @Component annotation In the IoC container
@Component public class TestSchedulingTask { // 这个cron表达式代表从0秒开始,每过3秒执行一次。 @Scheduled(cron = "0/3 * * * * ?") public void test(){ // 大家可以根据公司的业务来实现,更希望是@Autowrite注入service层的接口,业务逻辑写在service层中。 // 这里测试我就输出当前系统时间了... Date date = new Date(System.currentTimeMillis()); String rule = "yyyy-MM-dd HH:mm:ss"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(rule); String format = simpleDateFormat.format(date); System.out.println(format); } }
, note here that each task method must be annotated with @Scheduled, and the cron expression must be specified.
You can rest assured, and there are also websites that generate cron expressions online. Let’s take a look at the results output by the console.
Detailed explanation of cron expression
cron expression
Field | Range | Allowed characters |
Seconds | 0-59 | ,/-* |
Minute(minute) | 0-59 | ,/-* |
Hour(hour) | 0-23 | ,/-* |
Date (day of month) | 1-31(need to consider month inconsistency) | ,/-*?LW |
month | 1-12 (the corresponding English abbreviation is also acceptable) | , /-* |
day of week | 1-7 | ,/-*?L |
年(year) | This thing is basically not used | This thing is basically not used |
Allowed characters Explanation:
,: It is an enumeration with multiple values. For example, 5 and 20 in second represent triggers at 5 and 20 seconds.
/: value 1/value 2, value 1 represents the start time, value 2 represents how often, for example, in my case, it triggers every 3 seconds starting from 0 seconds.
-: Range value, from how long to how long.
*: Common to all ranges.
?: Only exists in date and week. If you want to trigger on the 2nd of every month, but if you use * on the week, it will trigger every day, which will cause a bug. So is it necessary? to replace *.
L: means the last, appearing in date and week. For example, using L in date means the last day of the month, and using 3L in week means triggering on the last Wednesday.
W: Indicates working days, used in dates. If 9W is used, it will be triggered directly if the 9th falls on Monday 1-5. If the 9th is a Saturday, it will be triggered on the 8th. If the 9th is a Sunday, it will be triggered on the Monday. The first trigger is trigger number 10.
The above is the detailed content of How to use SpringBoot to implement a timer. For more information, please follow other related articles on the PHP Chinese website!

There are subtle differences in Java's performance on different operating systems. 1) The JVM implementations are different, such as HotSpot and OpenJDK, which affect performance and garbage collection. 2) The file system structure and path separator are different, so it needs to be processed using the Java standard library. 3) Differential implementation of network protocols affects network performance. 4) The appearance and behavior of GUI components vary on different systems. By using standard libraries and virtual machine testing, the impact of these differences can be reduced and Java programs can be ensured to run smoothly.

Javaoffersrobustobject-orientedprogramming(OOP)andtop-notchsecurityfeatures.1)OOPinJavaincludesclasses,objects,inheritance,polymorphism,andencapsulation,enablingflexibleandmaintainablesystems.2)SecurityfeaturesincludetheJavaVirtualMachine(JVM)forsand

JavaScriptandJavahavedistinctstrengths:JavaScriptexcelsindynamictypingandasynchronousprogramming,whileJavaisrobustwithstrongOOPandtyping.1)JavaScript'sdynamicnatureallowsforrapiddevelopmentandprototyping,withasync/awaitfornon-blockingI/O.2)Java'sOOPf

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM)andbytecode.1)TheJVMinterpretsbytecode,allowingthesamecodetorunonanyplatformwithaJVM.2)BytecodeiscompiledfromJavasourcecodeandisplatform-independent.However,limitationsincludepotentialp

Java'splatformindependencemeansapplicationscanrunonanyplatformwithaJVM,enabling"WriteOnce,RunAnywhere."However,challengesincludeJVMinconsistencies,libraryportability,andperformancevariations.Toaddressthese:1)Usecross-platformtestingtools,2)

JVM'sperformanceiscompetitivewithotherruntimes,offeringabalanceofspeed,safety,andproductivity.1)JVMusesJITcompilationfordynamicoptimizations.2)C offersnativeperformancebutlacksJVM'ssafetyfeatures.3)Pythonisslowerbuteasiertouse.4)JavaScript'sJITisles

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunonanyplatformwithaJVM.1)Codeiscompiledintobytecode,notmachine-specificcode.2)BytecodeisinterpretedbytheJVM,enablingcross-platformexecution.3)Developersshouldtestacross

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
