Quartz Task Scheduler

Source: Internet
Author: User

Background: In the recent project, cross-region goods allocation needs, such as Area A and Area B. You need to determine whether a sku in area A or area B needs to allocate goods from the other database to supplement the supply of goods, avoid order delays due to missing items, which may affect sales and user performance. Problem: The data volume is huge. If you view the data, it will seriously affect the system performance, and even cause the database and application server to fail to respond. Solution: it is required that the system automatically obtain the data to be transferred at a certain time point at, and then store the data in the database. At pm, the user traffic and other work of the system were the least. At this time, a scheduled thread was started to obtain data, which had almost no impact on the system. Technology used: Quartz, Quartz is an open-source job scheduling framework, which is fully written in Java and designed for J2SE and J2EE applications. It provides great flexibility without sacrificing simplicity. You can use this framework to schedule scheduled tasks. Step 1: Write the business processing module. To meet this requirement, this module first obtains the list of sku instances to be scheduled in the near future. This is time-consuming because it needs to view the inventory records for the last 20 days and collect the shipment quantity statistics. Then, determine whether the shipment volume meets certain conditions (the condition is the business content, and the condition preparation is complicated) to replenish the goods. When the condition is met, check whether the same sku in other reservoir areas meets the allocation conditions. Record the average daily shipment of the two databases. When the preceding conditions are met, data is generated. If the conditions are not met, skip the next sku judgment until all sku processing is complete. Demo: business processing module: [java] package com. tgb. test; public class BusinessJob {public void generateBusinessInfo () {System. out. println ("business data has been generated! ") ;}} Scheduled task module: [java] package com. tgb. test; import org. quartz. job; import org. quartz. jobExecutionContext; import org. quartz. jobExecutionException; public class TimeJob implements Job {// override method public void execute (JobExecutionContext context) throws JobExecutionException {BusinessJob businessJob = new BusinessJob (); businessJob. generateBusinessInfo () ;}} scheduled scheduling module: [java] package com. tgb. test; import or G. quartz. cronTrigger; import org. quartz. jobDetail; import org. quartz. scheduler; import org. quartz. schedulerException; import org. quartz. impl. stdSchedulerFactory; public class JobSchedule {private static Scheduler scheduler = null; private static final Object lock = new Object (); public static Scheduler getSchedulerInstance () {if (scheduler = null) {synchronized (lock) {if (scheduler = null) {try {Schedfactory = StdSchedulerFactory. getdefaschscheduler ();} catch (SchedulerException e) {return null ;}}} return scheduler ;} /*** add task ** @ param jobName task name * @ param jobGroup task group name * @ param jobClass task Class Object * @ param cronExpress regular scheduling expression */public void addJob (String jobName, string jobGroup, Class <?> JobClass, String cronExpress) {try {JobDetail jobDetail = new JobDetail (jobName, jobGroup, jobClass); // create a trigger object and set the name, group name, and the time parameter CronTrigger cronTrigger = new CronTrigger ("trigger1", "triggerGroup"); cronTrigger. setCronExpression (cronExpress); // configure JobDetail and the Trigger object JobSchedule. getSchedulerInstance (). scheduleJob (jobDetail, cronTrigger);} catch (Exception e) {throw new RuntimeException (e );} }/*** Start scheduler */public void startScheduler () {try {if (! JobSchedule. getSchedulerInstance (). isStarted () {JobSchedule. getSchedulerInstance (). start () ;}} catch (Exception e) {throw new RuntimeException (e) ;}/ *** stop scheduler */public void stopScheduler () {try {if (JobSchedule. getSchedulerInstance (). isStarted () {JobSchedule. getSchedulerInstance (). shutdown (true) ;}} catch (Exception e) {throw new RuntimeException (e) ;}} main thread entry: [java] package com. tgb. test; pub Lic class TestMain {/*** @ param args */public static void main (String [] args) {// construct the timer module TimeJob timeJob = new TimeJob (); // construct the scheduling manager JobSchedule jobSchedule = new JobSchedule (); // Add job jobSchedule. addJob ("jobName", "jobGroup", timeJob. getClass (), "0 0 23 **? "); // Start the scheduler jobSchedule. startScheduler (); // close the scheduler // jobSchedule. stopScheduler () ;}} console output:

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.