Small spring scheduled task example

Source: Internet
Author: User

I wrote a small example of a spring scheduled task and accumulated it to avoid forgetting javaee during Android development! The following is the code of the spring scheduled task:

This is a very simple mytime class. There is a method in it that gets the current time and prints it out.

Package COM. zhangkeinfo. spring. time; import Java. SQL. timestamp;/*** spring scheduled task * @ author spring sky * Email: vipa1888@163.com * QQ: 840950105 **/public class mytime {public void gettime () {timestamp TT = new timestamp (system. currenttimemillis (); system. out. println (TT. tostring ());}}

Core code:

Package COM. zhangkeinfo. spring. time; import Org. quartz. jobexecutioncontext; import Org. quartz. jobexecutionexception; import Org. springframework. scheduling. quartz. quartzjobbean;/*** spring timing task * @ author spring sky * Email: vipa1888@163.com * QQ: 840950105 * ##### it is the most common method for spring scheduled tasks to inherit quartzjobbean! * ##### The abstract method executeinternal is also implemented **/public class updatetime extends quartzjobbean {private mytime; /*** dependency injection mytime * @ Param mytime */Public void setmytime (mytime) {This. mytime = mytime;}/*** I did a very simple task in this scheduled task, that is, to print the current time! * // @ Overrideprotected void executeinternal (jobexecutioncontext context) throws jobexecutionexception {mytime. gettime ();}}

Note that the above parameter is: context. This parameter has a major role in obtaining some important values. context has a get (key) method, this is to get the required data!

 

Configuration in applicationcontext. xml:

<! -- Spring scheduled task configuration --> <Bean class = "com. zhangkeinfo. Spring. Time. mytime" id = "mytime"/> <! -- Basic configuration-related class injection --> <Bean class = "org. springframework. scheduling. quartz. jobdetailbean "id =" mytimespringjob "> <property name =" jobclass "value =" com. zhangkeinfo. spring. time. updatetime "/> <! -- The updatetime class name configured in value is spring's jobclass --> <property name = "jobdataasmap"> <! -- When other beans need to be injected in the scheduled task, they can be injected here, I entered a mytime class --> <map> <Entry key = "mytime" value-ref = "mytime"/> </map> </property> </bean> <! -- Related time configuration --> <Bean class = "org. springframework. scheduling. quartz. simpletriggerbean "id =" mytimesimpletriggerbean "> <property name =" jobdetail "ref =" mytimespringjob "/> <! -- Configure the time information for mytimespringjob. jobdetail of name is the name to be injected inside spring --> <property name = "repeatinterval" value = "1000"/> <! -- Execute once every 1000 milliseconds --> <property name = "startdelay" value = "5000"/> <! -- Start the task 5000 milliseconds after tomcat or JBoss is started --> </bean> <! -- The content configured in the current annotation is very powerful. You can configure the day of the week, the day of the day, the year, or the second of the day. The current configuration can be configured as follows: every week's Sunday 23:59:50 this order is the content in the values configured in inverted order: 6th bits: 1 represents Sunday, and Sunday in foreign countries is the beginning of every week! 5th bits: * represents the day of the week for all months. 4th bits :? The representative does not have a specific day of the week 3rd bits: 23 represents 23 points, note: the use of 24 hours 2nd bits: 59 represents the minute 1st bits: 50 represents the second <Bean class = "org. springframework. scheduling. quartz. crontriggerbean "> <property name =" jobdetail "ref =" mytimespringjob "/> <property name =" cronexpression "value =" 50 59 23? * 1 "/> </bean> --> <! -- ********************************* --> <Bean class = "org. springframework. scheduling. quartz. schedulerfactorybean "> <property name =" triggers "> <list> <ref bean =" mytimesimpletriggerbean "/> </List> </property> </bean>

The basic configurations in the above spring XML file have already been described in detail, and the learning also lies in accumulation. Only accumulation will continue to grow!

 

 

 

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.