Preface
There was a period of time not to blog, said ashamed, recently has been to the company to do peer-to the service side of the project, but this is not an excuse, Gorky once said (not quite sure t_t), time is like a sponge water, as long as willing to squeeze always have! Therefore, the rhythm of blogging must be pulsating back, must develop continuous learning, constantly record, and constantly summarize the good habits. Needless to say, the recent need to do some server-side timing tasks, decided to use quartz to achieve, have not touched before, so the learning side to do, by the way to record this process.
first knowledge of quartz
first look at the encyclopedia of the Quartz in the introduction: Quartz is a opensymphony open source organization in the Job scheduling field another open source project, which can be combined with the Java EE and J2SE application can also be used alone. Quartz can be used to create a simple or complex program that runs 10, hundreds, or even tens of thousands of jobs. Jobs can be made into standard Java components or EJBs. The latest version of quartz is Quartz 2.2.1.
Yes, quartz is an open-source task scheduling framework written by Java. The latest version that has been released is 2.2.1:Http://www.quartz-scheduler.org/downloadsafter the download is complete, you can see the following structure diagram,
When we use quartz, we need to copy the Core jar package (Quartz-2.2.1.jar) from the Lib folder into the classpath of our project, then we can use the related API of quartz, and we will get a quick experience of quartz based on the official online tutorials. But before using it, we need to know about the 3 core interfaces of Quartz:Job,Trigger , and Scheduler. Simply put, the job represents the task we need to dispatch, trigger is the trigger that triggers the task, and scheduler is used to bind the job and trigger and perform the task. First, we need to prepare a job,
Package Com.test;import Java.util.date;import Org.quartz.job;import org.quartz.jobexecutioncontext;import Org.quartz.jobexecutionexception;public class Simplejob implements Job {@Overridepublic void execute ( Jobexecutioncontext jobctx) throws Jobexecutionexception {//TODO auto-generated method StubSystem.out.println (" triggered. Time is "+ New Date ());}}
Let our own job class implement the job interface and rewrite the Execute method, in which we write the business logic code that we need to call periodically, where we only print the current time. Then we can write a test class to execute the job, of course, there are 2 key objects missing, we need a trigger to trigger the job, and a scheduler to manage the job, trigger and execute them, Refer to the method of instantiating scheduler in the official tutorial
Schedulerfactory schedfact = new Org.quartz.impl.StdSchedulerFactory (); Scheduler sched = Schedfact.getscheduler ();
You can see the scheduler instance that is obtained by schedulerfactory and start the dispatch by the Start method. The next step is to instantiate the job, still referencing the code in the official tutorial,
Define the job and tie it to our Hellojob class Jobdetail job = newjob (Simplejob.class) . Withidentity ("MyJob", "Group1")//Name "MyJob", Group "group1" . Build ();
here again to see a new interface--jobdetail, in fact, quartz every time the job will be re-created a job instance, so it is not directly receive a job instance, but to receive a job implementation class, This allows the runtime to instantiate the job through the reflection invocation mechanism of the Newinstance method. In the 2.2.x version of the construction method can not be used directly, but need to instantiate the Jobdetail object through Jobbuilder, as shown in the code above, through static import introduced the Jobbuilder class and through its static method Newjob to instantiate the Jobdetail object. Now that a trigger is missing to trigger the job, continue referencing the code in the tutorial,
Trigger the job to run now, and then every seconds Trigger Trigger = Newtrigger () . Withidentity ("Mytrigger", "Group1") . Startnow () . Withschedule (Simpleschedule (). withintervalinseconds (5) . RepeatForever ( ))
You can see the wording is very similar, through the Triggerbuilder.newtrigger () method to instantiate the trigger object, actually trigger has 2 subclasses, respectively Simpletrigger and Crontrigger, Simpletrigger is suitable for performing one-time or fixed-interval periodic execution, while Crontrigger can define a variety of complex invocation scenarios through cron expressions, such as Wednesday execution per week, 6 o'clock in the afternoon per day, and so on. I set the 5-second call once, repeatforever that is infinite call, everything is only owed to the east wind, and finally through the scheduler binding execution job and trigger can be,
Tell Quartz to schedule the job using our trigger sched.schedulejob (job, trigger);
Finally, take a look at the complete code in the test class and run the program:
package Com.test;import Org.quartz.jobbuilder;import Org.quartz.jobdetail;import Org.quartz.scheduler;import Org.quartz.schedulerexception;import Org.quartz.simpleschedulebuilder;import Org.quartz.Trigger;import Org.quartz.triggerbuilder;import Org.quartz.impl.stdschedulerfactory;public class Test04 {public static void main ( String[] args) {try {//Grab the Scheduler instance from the Factoryscheduler Scheduler = STDSCHEDULERFACTORY.GETDEFAULTSC Heduler (); Scheduler.start ();//Jobjobdetail job = Jobbuilder.newjob (Simplejob.class). Withidentity ("MyTrigger", " Group1 "). Build ();//Triggertrigger trigger = Triggerbuilder.newtrigger (). Withidentity (" Trigger1 "," group1 "). Startnow (). Withschedule (Simpleschedulebuilder.simpleschedule (). Withintervalinseconds (5). RepeatForever ()). Build (); Scheduler.schedulejob (job, trigger);} catch (Schedulerexception se) {se.printstacktrace ();}}}
after running, you can see that the console will print output statements every 5S.
Summary
This blog is an introduction to quartz, understand the three key basic concepts: Task (Job), Trigger (Trigger) and Scheduler (Scheduler), in the actual project in general we will be in the Web project to perform some scheduled tasks, The post-order blog will introduce quartz integration into spring to achieve server-side timing tasks and some of the advanced features of quartz, so stay tuned.
Quartz realization of timed tasks (i)