Spring boot: @EnableScheduling enable scheduled task support, @Scheduled scheduled task statement

Source: Internet
Author: User
Tags dateformat

Spring Boot:

@EnableScheduling turn on scheduled task support,

@Scheduled Scheduled Task statement

1 Package Ch2.scheduler2;2 3 //Date Conversion Mode4 import Java.text.SimpleDateFormat;5 import java.util.Date;6 7 //Scheduled Task Statement8 import org.springframework.scheduling.annotation.Scheduled;9 //Spring Component AnnotationsTen import Org.springframework.stereotype.Service; One  A @Service -  Public classSchedulerservice { -  the     Private StaticFinal SimpleDateFormat DateFormat =NewSimpleDateFormat ("Hh::mm::ss"); -      -@Scheduled (fixedrate= the) -      Public voidprofixcurrenttime () +     { -System. out. println ("executes every 5 seconds:"+ Dateformat.format (NewDate ())); +     } A      at@Scheduled (cron="0? * *") -      Public voidcorncurrenttime () -     { -System. out. println ("Custom Execution Time:"+ Dateformat.format (NewDate ())); -     } -      in      -}

1 Package Ch2.scheduler2;2 3 //Introducing Spring Configuration annotations4 import org.springframework.context.annotation.Configuration;5 //introducing Spring Auto-load annotations6 import Org.springframework.context.annotation.ComponentScan;7 8 //Scheduled Task declaration class: Open a Scheduled task statement9 import org.springframework.scheduling.annotation.EnableScheduling;Ten  One //spring Configuration class declaration A @Configuration - //Automatic introduction of service,component under current package .... -@ComponentScan ("Ch2.scheduler2") the //turn on support for scheduled tasks - @EnableScheduling -  Public classTaskschedulerconfig { -  +}

1 Package Ch2.scheduler2;2 //Introducing Containers3 import Org.springframework.context.annotation.AnnotationConfigApplicationContext;4 5  Public classMain {6 7      Public Static voidMain (string[] args)8     {9         TenAnnotationconfigapplicationcontext context =NewAnnotationconfigapplicationcontext (Taskschedulerconfig.class); One         //Schedulerservice schedulerservice = Context.getbean (schedulerservice.class); A          -     } -      the}

Spring boot: @EnableScheduling enable scheduled task support, @Scheduled scheduled task statement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.