Spring comes with timers for timed tasks

Source: Internet
Author: User

There are at least 2 ways to implement timed tasks in the spring framework (excluding Java native timers and executor implementations), and the integration of third-party scheduled task frameworks, such as ubiquitous quartz The other is spring's own timer (only for versions after 3.0). This article will focus on the spring comes with a timer, simulating a simple timing task to see how easy it is to use.

    1. The second step, start the schedule configuration, the XML configuration please self-search, this article only for the annotation method implementation provides the description.

@EnableScheduling

@EnableScheduling annotations, which are used to introduce the relevant configuration of schedule, are visible from its source code.

@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Import({SchedulingConfiguration.class})@Documentedpublic @interface EnableScheduling {}

The key to Schedulingconfiguration is the definition of scheduledannotationbeanpostprocessor. As the name implies, scheduled processing is done for annotation-based bean components.

    1. The second step is to add annotations to the methods that require timed execution
      @Scheduled (Fixeddelay = 5000L)
      @Scheduled (fixedrate = 5000L)
      @Scheduled (cron = "xxx")

Read scheduled source code,

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Repeatable(Schedules.class)public @interface Scheduled {    String cron() default "";    String zone() default "";    long fixedDelay() default -1L;    String fixedDelayString() default "";    long fixedRate() default -1L;    String fixedRateString() default "";    long initialDelay() default -1L;    String initialDelayString() default "";}

The target of this annotation is found only for methods and other annotation types and can be duplicated. Parameters include zone-time zone, fixedrate-fixed start frequency (in milliseconds), fixeddelay-fixed execution period (in milliseconds), cron-custom cron expression.

The difference between fixedrate and Fixeddelay is simple, one is that the start time is performed at a fixed frequency, regardless of whether there is an end before, and the other from the end of the previous task to the beginning of the next task, at a fixed time interval. Write a simple sample to see the effect:

@Componentpublic class SpringTaskDemo {    private int round = 0;    @Scheduled(fixedRate = 5000L)    public void counting(){        round++;        System.out.println(">>>>>>>>>" + "Counting Round " + round);        System.out.println("Start at : " + new DateFormatter("yyyy-MM-dd HH:mm:sss").print(new Date(), Locale.CHINESE));        try {            Thread.sleep(3000L);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println("Finish at: "+ new DateFormatter("yyyy-MM-dd HH:mm:sss").print(new Date(), Locale.CHINESE));    }}

The Fixedrate effect is as follows:

>>>>>>>>>Counting Round 1Start at : 2018-09-03 00:28:023Finish at: 2018-09-03 00:28:026>>>>>>>>>Counting Round 2Start at : 2018-09-03 00:28:028Finish at: 2018-09-03 00:28:031>>>>>>>>>Counting Round 3Start at : 2018-09-03 00:28:033Finish at: 2018-09-03 00:28:036>>>>>>>>>Counting Round 4Start at : 2018-09-03 00:28:038Finish at: 2018-09-03 00:28:041>>>>>>>>>Counting Round 5Start at : 2018-09-03 00:28:043Finish at: 2018-09-03 00:28:046

The Fixeddelay effect is as follows:

>>>>>>>>>Counting Round 1Start at : 2018-09-03 00:30:031Finish at: 2018-09-03 00:30:034>>>>>>>>>Counting Round 2Start at : 2018-09-03 00:30:039Finish at: 2018-09-03 00:30:042>>>>>>>>>Counting Round 3Start at : 2018-09-03 00:30:047Finish at: 2018-09-03 00:30:050>>>>>>>>>Counting Round 4Start at : 2018-09-03 00:30:055Finish at: 2018-09-03 00:30:058>>>>>>>>>Counting Round 5Start at : 2018-09-03 00:31:003Finish at: 2018-09-03 00:31:006

In this way, the timing of their own tasks is very punctual. Cron expressions support defining more complex task cycles, and the cron example is no longer enumerated, please search and test yourself.

Spring comes with timers for timed tasks

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.