Primary knowledge quartz (introductory case) + common cron expressions

Source: Internet
Author: User

1.Quartz Frame composition

1. Physical Layer

 Packagecn.happy.entity;//1. Public classPlan {//Time    PrivateString date; //Task    PrivateString task;  PublicPlan (string date, string task) { This. Date =date;  This. Task =task; }     PublicPlan () {} @Override PublicString toString () {return"Plan [date=" + Date + ", task=" + task + "]"; }     PublicString getDate () {returndate; }     Public voidsetDate (String date) { This. Date =date; }     PublicString Gettask () {returntask; }     Public voidsettask (String Task) { This. Task =task; }        }
Plan entity class

2. Service Layer

 PackageCn.happy.service;Importjava.util.ArrayList;Importjava.util.List;//2.ImportCn.happy.entity.Plan; Public classRemindservice {//0.2 ways to print reminders in a collection     Public voidPrintmessage () {List<Plan> list=Getplasnfortoday ();  for(Plan plan:list) {//the individual planSystem.out.println ("Planned Time" +plan.getdate () + "\ t plan content:" +Plan.gettask ()); }    }        //creates a collection, and the method return value is a collection type     PublicList<plan>Getplasnfortoday () {List<Plan> list=NewArraylist<plan>(); Plan Plan1=NewPlan ("December 17, 2016", "The end of the month can buy a ticket home!" "); Plan plan2=NewPlan ("December 17, 2016", "WebService");        List.add (PLAN2);        List.add (PLAN1); returnlist; }}
Remindservice

3. Definition of Task class Remindjob implements job

 PackageCn.happy.quartz;ImportCn.happy.service.RemindService;//make a normal class into a plan Public classRemindjobImplementsjob{//Implant Service Objects    PrivateRemindservice service=NewRemindservice (); @Override Public  voidExecute (Jobexecutioncontext context)throwsjobexecutionexception{service.printmessage (); }     PublicRemindservice GetService () {returnService; }     Public voidSetservice (Remindservice service) { This. Service =Service; }    }
Remindjob

3. Synthesize our plans and triggers in the test class

Scheduler ≈job+trigger

 Packagecn.happy.test;Importjava.sql.Date;ImportCn.happy.quartz.RemindJob; Public classMyquertztest { Public Static voidTool ()throwsschedulerexception{//The first step to build the jobJobdetail Job=jobbuilder.newjob (remindjob.class)//. Withidentity ("Job1", "group1")//. Build (); //Step two build trigger        /*Date Runtime=datebuilder.evenminutedate (new Date (System.currenttimemillis ())); Trigger Trigger=triggerbuilder.newtrigger (). Withidentity ("Trigger1", "group1")//. StartAt (RunTime). build (); /c0>*/                /*Trigger Trigger=triggerbuilder.newtrigger ()//. Withidentity (Triggerkey.triggerkey ("Mytrigger", "MyTrigge Rgroup "))//. Withschedule (Simpleschedulebuilder.simpleschedule ()////.withintervalin Minutes (1)//One minute appears once//.withintervalinmilliseconds (2)//Two milliseconds appears once Withintervalin Seconds (2)//2 second appears once//.repeatforever ()//Can be omitted)//. StartAt (New D Ate (System.currenttimemillis () +2000))//. Build ();*/Trigger Trigger=triggerbuilder.newtrigger ()//. Withidentity ("Mytrigger", "Mytriggergroup")//. Withschedule (Cronschedulebuilder.cronschedule ("0 55 08? * 1#3 2016 ") . Build (); //step three bind Job and TriggerSchedulerfactory sf=Newstdschedulerfactory (); Scheduler sched=Sf.getscheduler ();        Sched.schedulejob (Job,trigger);        Sched.start (); //Thread.Sleep (+);//Sleep 3 secondsSched.shutdown ();//Stop    }             Public Static voidMain (string[] args)throwsschedulerexception {tool (); System.out.println ("Aaaaaaaaa"); }    }
Test Class

Summary: Commonly used cron expressions

Primary knowledge quartz (introductory case) + common cron expressions

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.