Spring annotation @Scheduled (cron = "0 0 1 * * *") is used to implement timed tasks

Source: Internet
Author: User
Tags arrays current time dateformat
<span style= "FONT-SIZE:14PX;" > First Contact Timing Class of small procedures, or go a lot of detours, now finally settled, summarized as follows: </span> 
<span style= "FONT-SIZE:14PX;"

>import Com.activityvip.api.service.SecurityBlockAccountService;
Import org.springframework.beans.factory.annotation.Autowired;

Import Org.springframework.stereotype.Service;
Import javax.annotation.PostConstruct;
Import java.net.InetAddress;
Import java.net.UnknownHostException;
Import Java.text.DateFormat;
Import Java.text.SimpleDateFormat;
Import Java.util.Arrays;
Import Java.util.Calendar;
Import Java.util.Date;
Import java.util.List;
Import java.util.concurrent.Executors;
Import Java.util.concurrent.ScheduledExecutorService;

Import Java.util.concurrent.TimeUnit;

Import org.springframework.scheduling.annotation.Scheduled; /** * */@Service public class Securityaccountutil {private static Logger Logger = Logger.getlogger (securityaccount
    Util.class); private static final list<string> localhost = arrays.aslist ("Emidas-activityvip-web02.nh", "
    Emidas-activityvip-web01.beta "); @Autowired Securityblockservice SecurityblockservIce
    @Autowired Securityblockaccountservice Securityblockaccountservice;

    @Autowired Securityblockaccountdao Securityblockaccountdao;

                @Scheduled (cron = "0 0 1 * * *") public void Run () {if (Checkouthostname ()) {try {
                Logger.info ("Thread is running");
                DateFormat DATEFORMAT1 = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
                String lasttime = Securityblockaccountservice.getlasttime ();

                Date myDate1 = Dateformat1.parse (lasttime);
                Date dafter = Getnextday (myDate1);
                Integer numbytime = Securityblockservice.getallnumbytime (myDate1, dafter);
                Integer accountbytime = Securityblockservice.getallaccountbytime (myDate1, dafter);

                Securityblockaccountservice.insertaccountnum (Numbytime, Accountbytime, dafter);
            Logger.info ("Thread is End"); } catch (Exception e) {logger.info ("SecurityaccouNtutil: "+ e.getmessage ());
            E.printstacktrace (); }}}/** * Gets the time of day after the current time * * @param date * @return */public static date GE
        Tnextday (date date) {Calendar calendar = calendar.getinstance ();
        Calendar.settime (date);
        Calendar.add (Calendar.day_of_month, 1);
        Date = Calendar.gettime ();
    return date;  }/** * Filters hostname * * @return */protected Boolean checkouthostname () {String hostName =
        Null
        try {hostName = Inetaddress.getlocalhost (). GetHostName ();
            } catch (Unknownhostexception e) {logger.info (e);
        HostName = "localhost";
    } return Localhost.contains (HostName); }}</span>

Recently, the internship wrote a small timing statistics program, with spring @Scheduled annotations, the note relies on the following jar package

<span style= "FONT-SIZE:14PX;" > <dependency>
            <groupId>org.springframework</groupId>
            <artifactId> spring-context</artifactid>
            <version>3.1.1.RELEASE</version>
        </dependency>< /SPAN>
Before using this annotation, please do the following to prepare and configure the appropriate XM file.

To configure timed annotations: http://blog.csdn.net/sd4000784/article/details/7745947

The meanings of each parameter in the cron parameter are given below:

Cron expression     meaning  
"0 0 * *"     trigger   at 12 o'clock Noon;
"0 15 10? * * "    daily 10:15 trigger  
" 0 * * * "    daily 10:15  
" 0 15 10 * *? *     Trigger   10:15 every day;
0 15 10 * *? 2005 "   2005 daily 10:15 trigger  
0 * * *?     daily from 2 o'clock in the afternoon to 2:59 per minute  
" 0 0/5 14 * *? "     &NBSP every 5 minutes from 2 o'clock in the afternoon to 2:55 each day,
0 0/5 14,18 * *?     Daily 2 o'clock in the afternoon to 2:55 and 6 to 6:55 each 5-minute trigger and nbsp
"0 0-5 * *"       per minute from 14:00 to 14:05;
"0 10,44 14? 3 WED "    14:10 and 14:44 trigger   per Wednesday for March;
" 0 15 10? * Mon-fri "    10:15 trigger   for each Monday, Tuesday, Wednesday, Thursday, Friday;

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.