Task scheduling system based on springboot and quartz

Source: Internet
Author: User
Tags aop unique id
First, brief

Project Address:Https://github.com/littlechare/job-manager

Please see the project's Readme for detailed information.

Download Address: https://download.csdn.net/download/w172087242/10333176

Before you want to write about the establishment of scheduled tasks, to facilitate the rapid integration of projects;

After writing, and continue to play on this basis into the system, of course, the system is still missing a lot of things;

such as business side bid generation and verification, task callback response message rules, task scheduling monitoring, how to do ha and so on;

Because it is a personal project, so the use of part of the work time and the Qingming Festival free time to achieve,

So the project can also have a shortage of places, but if developers want to use, you can download it quickly

Integration, can also be modified on this basis.


second, the project function brief 2.1 Description of task type support

Task Type Description Expression Examples Notes
Cron Cron-type tasks that do not support expression changes 0/20 * * * * *? Called once every 20 seconds
TRIGGER Trigger type of task that supports expression changes 0 0 4 * * *? Call 4 o'clock in the morning every day
FIXED Fixed type task, expression change not supported fixed=5000 The unit millisecond, which means one call every 5 seconds.
Fixed_delay Fixed_delay tasks, expression changes are not supported fixed=5000,delay=10000 Unit millisecond, indicating delay of 10 seconds after execution
2.2 Business Side Description

1. The business party needs to maintain its own business number (BID) and task number;

2. Support the task to cancel at any time, change at any time, add, etc., callback address one HTTP (s) heading 2.3 Module description and class description

Module description
Module Name function Description
Job-dispatcher-base Task Base Module Timer task capability provider (removable component)
Job-dispatcher-biz Task Business Module Business management and task triggering module
Job-dispatcher-manager Task Management Module Manage task changes and issue reminders of change events (distributed deployment can be improved based on messages)
Job-dispatcher-test Test module Standalone module with an interface for testing task callbacks

Base Module class description
class name (brief name, prefix omitted com.littlehow.job)   features /th>   description
base.config.ScheduleTaskConfig scheduled Task Configurator   Task's core manager
base.config.TaskConfig Scheduled Task cache   Caching task Information
base.confi G.tasktype timed task type   enum Cron,trigger,fixed,fixed_delay
base. Basetask Task Base information   Task entity beans, implementing runnable and Initializingbean
 
cron base class   Because of the CSDN editor issue, the class name is written in this basecrontask
base. Basefixedtask Fixed base class   Implementation Basetask class, similar to cron
Schedulebaseservi Ce service class   External Exposure service category
Manager Module Class description
class name (for short name, prefix omitted com.littlehow.job) function Description
Manager.api.TaskManagerService Task Persistence Management API The manager module can go out independently, so the API is the only access interface
Manager.event.TaskEvent Task events Event entities sent by the task, inheriting applicationevent
Manager.event.TaskEventType Task Event Type Enumerating Add,update,remove
Manager.support.MysqlTaskManagerSupport API implementation Implementation of MySQL Persistence task
Biz Module Class Description
class name (for short name, prefix omitted com.littlehow.job) function Description
Advice. Exceptionadvice Handling Exceptions uniformly
Advice. Successresponseadvice Successful unified processing
Config. Interceptorconfig Interceptor Configuration
Interceptor. Businessinterceptor Interceptor implementation The main application is to verify and obtain the bid in header
Interceptor. Businesscontext Context Bid context Management
Listener. Taskeventlistener Task Event Monitoring Monitor task events to implement Applicationlistener interface
Service. Callbackexecuteservice Callback Processing Service Callback address call for timed task
Service. Taskexecuteservice Task Processing Services
Controller. Taskmanagercontroller Task Management Web Interface Support tasks and additions and deletions to check
Init. Inittask Initializing tasks Re-adding existing tasks to the task queue after the task system restarts
Vo. Responseentity corresponding parameters
Vo. Argumentsexception Parameter exception When validating a parameter, this exception is thrown if it is not valid
Jobstart Start class Springboot Project Startup Class
Test Module Class Description
class name (for short name, prefix omitted com.littlehow.job) function Description
Test. TestController Test controller
Test. Teststart Test Startup class Springboot Startup class

third, the task system code example; 3.1 Task Configuration class (Scheduletaskconfig)

Package com.littlehow.job.base.config;
Import Com.littlehow.job.base.BaseTask;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import org.springframework.beans.factory.annotation.Configurable;
Import Org.springframework.context.annotation.Lazy;
Import org.springframework.scheduling.annotation.EnableScheduling;
Import Org.springframework.scheduling.annotation.SchedulingConfigurer;
Import org.springframework.scheduling.config.*;
Import Org.springframework.scheduling.support.CronTrigger;

Import org.springframework.stereotype.Component;

Import Java.util.Date; @Lazy (False) @Configurable @Component @EnableScheduling public class Scheduletaskconfig implements
    Schedulingconfigurer {private Logger log = Loggerfactory.getlogger (Scheduletaskconfig.class);
    Private final static String task_not_exists = "Not EXISTS";
    Private final static String task_exists = "EXISTS";
    Private final static String failure = "failure";
  Private final static String SUCCESS = "SUCCESS";  Private Scheduledtaskregistrar Scheduledtaskregistrar; @Override public void Configuretasks (Scheduledtaskregistrar scheduledtaskregistrar) {this.scheduledtaskregist
        rar = Scheduledtaskregistrar;
    Inittask (); /** * Add Tasks * @param task * @return/public String AddTask (basetask Task) {if (sc Heduledtaskregistrar = = NULL | |
        task = null) {return failure;
        } if (Taskconfig.containstask (Task.getid ())) {return task_exists;
            try {addTask0 (Task);
            Taskconfig.addtask (Task);
        return SUCCESS;
            catch (Exception e) {log.error ("New scheduled Task failed:" + task, E);
        Throw e; /** * Change Task Execution frequency * @param taskId * @param expression * @return/public String CH
        Angetask (string taskId, string expression) {Basetask Basetask = Taskconfig.gettask (taskId); if (Basetask == NULL | | Tasktype.trigger!= Basetask.tasktype | |
        expression = = null) {return task_not_exists;
        } log.info ("Change Trigger expression: (id=" + TaskId + ", expression=" + expression+ ")");
        Basetask.setexpression (expression);
    return SUCCESS;
        /** * Cancel timed task * @param taskId * @return/public string Canceltask (string taskId) { if (!
        Taskconfig.containstask (taskId)) {return task_not_exists;
            try {log.info ("Cancel task:" + taskId);
        Taskconfig.removetask (taskId). Getscheduledtask (). Cancel ();
            catch (Exception e) {log.error ("Cancel task failed:" + taskId, E);
        Throw e;
    return SUCCESS; /** * Initialize the configured task/private void Inittask () {taskconfig.gettasks (). ForEach (Task-> AddTask
    0 (Task));
        } private void AddTask0 (Basetask Task) {log.info ("Add Task:" + Task); Switch (Task.tasktype) {Case TRIGGER:task.setScheduledTask (Addtriggertask (Task));
            Break
                Case CRON:task.setScheduledTask (Addcrontask (Task, task.getexpression ()));
            Break
                Case FIXED_RATE:task.setScheduledTask (Addfixedratetask (Task, Task.interval ()));
            Break
                Case FIXED_DELAY:task.setScheduledTask (Addfixeddelaytask (Task, Task.interval (), Task.delay ());
            Break Default:}/** * Add a scheduled task that cannot be changed schedule * @param task/private Scheduledtask Addcrontask (R 
    Unnable task, String expression) {return Scheduledtaskregistrar.schedulecrontask (new Crontask (task, expression)); /** * Add Variable Time task * @param task * @return/private Scheduledtask Addtriggertask (BaseT
            Ask task} {return scheduledtaskregistrar.scheduletriggertask new Triggertask (task, Triggercontext-> { Crontrigger Trigger = newCrontrigger (Task.getexpression ());
            Date nextexec = Trigger.nextexecutiontime (Triggercontext);
        return nextexec;
    })); /** * Set Fixed frequency timer task * @param task * @param interval/private Scheduledtask Addfixedratetask (Runnable task, long interval)
    {return Scheduledtaskregistrar.schedulefixedratetask (new Intervaltask (task, interval, 0L)); /** * Set a timed task that is delayed at a fixed frequency * @param task * @param interval * @param delay * * Private Sched Uledtask Addfixeddelaytask (Runnable task, long interval, long delay) {return scheduledtaskregistrar.schedulefixed
    Delaytask (New Intervaltask (task, interval, delay));
 }
}

3.2 Task Basic entity Class (Basetask)
Package com.littlehow.job.base;
Import Com.littlehow.job.base.config.TaskConfig;
Import Com.littlehow.job.base.config.TaskType;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import Org.springframework.beans.factory.InitializingBean;

Import Org.springframework.scheduling.config.ScheduledTask;  /** * Basic Task Configuration class */Public abstract class Basetask implements Runnable,initializingbean {protected final Logger log =
    Loggerfactory.getlogger (This.getclass ());
    Public final TaskType TaskType;
    Private Scheduledtask Scheduledtask;
    Private final String ID;
        Public Basetask (TaskType TaskType, String id) {this.tasktype = TaskType;
    This.id = ID; /** * Get task expression such as: 0 0 0/1 * * *?

    (executed per hour) * @return/public abstract String getexpression ();

    /** * fixed-frequency interval * @return/public abstract long interval ();

    /** * Fixed Frequency execution delay time * @return/public abstract long delay (); /** * SettingsTask expression * @param expression */public abstract void SetExpression (String expression);
    /** * Get task Unique ID * @return/public String getId () {return id;
    Public final Scheduledtask Getscheduledtask () {return scheduledtask;
    Public final void Setscheduledtask (Scheduledtask scheduledtask) {this.scheduledtask = Scheduledtask;
    @Override public void Afterpropertiesset () {taskconfig.addtask (this); Public String toString () {return This.getclass (). Getsimplename () + (ID: "+ id +", expression: "+ Getexpre
    Ssion () + ", type:" + TaskType + ", Interval:" + interval () + ", delay:" + delay () + ")";
 }
}
3.3  Task Execution Class (Taskexecuteservice)
Package com.littlehow.job.service;
Import Com.littlehow.job.base.BaseCronTask;
Import Com.littlehow.job.base.BaseFixedTask;
Import Com.littlehow.job.base.ScheduleBaseService;
Import Com.littlehow.job.manager.pojo.TaskDto;
Import Com.littlehow.job.manager.pojo.TaskType;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Service;

Import Org.springframework.util.StringUtils;
Import Java.util.regex.Matcher;

Import Java.util.regex.Pattern;
    @Service public class Taskexecuteservice {@Autowired private schedulebaseservice schedulebaseservice;
    @Autowired private Callbackexecuteservice Executeservice;

    Private static final Pattern fixed = Pattern.compile ("fixed= (\\d+) \\s* (, \\s*delay= (\\d+))");  /** * New Task * The expression parsing error may throw an exception * @param taskdto */public void AddTask (Taskdto taskdto) {if (taskdto = null)
        {return;
        String taskId = GetTaskID (taskdto); CalLbackexecuteservice.addorupdatetask (TaskId, Taskdto.getcallbackurl ());
        TaskType tasktype = Taskdto.gettasktype (); Switch (tasktype) {case Fixed:case fixed_delay:long[] params = Getfixedparamete
                RS (Taskdto.getexpression ());
                    Basefixedtask basefixedtask = new Basefixedtask (TaskId, params[0], params[1]) {
                    @Override public void Run () {Executeservice.execute (This.getid ());
                }
                };
                Schedulebaseservice.addtask (Basefixedtask);
            Break Case Cron:case Trigger:basecrontask basecrontask = new Basecrontask (TaskType = = Tasktype.cro
                N?
                    Com.littlehow.job.base.config.TaskType.CRON:com.littlehow.job.base.config.TaskType.TRIGGER, TaskId) { @Override public void Run () {
                        Executeservice.execute (This.getid ());
                }
                };
                Basecrontask.setexpression (Taskdto.getexpression ());
        Schedulebaseservice.addtask (Basecrontask); }/** * Modify task * @param taskdto */public void Updatetask (Taskdto taskdto) {if (! Stringutils.isempty (Taskdto.getexpression ())) {//Modify expression Schedulebaseservice.changetask (GetTaskID t
        Askdto), taskdto.getexpression ()); } if (! Stringutils.isempty (Taskdto.getcallbackurl ())) {//Modify callback address Callbackexecuteservice.addorupdatetask (g
        Ettaskid (Taskdto), Taskdto.getcallbackurl ()); }/** * Delete task * @param taskdto */public void Removetask (Taskdto taskdto) {String ta
        SkId = GetTaskID (taskdto);
        Schedulebaseservice.removetask (TASKID);
    Callbackexecuteservice.removetask (TASKID); /** * Gets the actual parameter information of the fixed type
     * @param expression * @return * * private static long[] Getfixedparameters (String expression) {
        Matcher Matcher = fixed.matcher (expression);
            if (Matcher.find ()) {long[] Fixedparam = {0L, 0L};
            Fixedparam[0] = Long.parselong (Matcher.group (1));
            String delay = Matcher.group (3);
            if (delay!= null) {fixedparam[1] = Long.parselong (delay);
        return fixedparam;
    } throw new IllegalArgumentException ("Invalid expression:" +expression); /** * Get Task number * @param tasks * @return/private String GetTaskID (taskdto Task) {RE
    Turn task.getbusinessid () + "-" + task.gettaskid ();
 }
}
3.4  Task callback Address processing class (Callbackexecuteservice)
Package com.littlehow.job.service;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import org.springframework.http.ResponseEntity;
Import Org.springframework.stereotype.Service;

Import Org.springframework.web.client.RestTemplate;
Import Java.util.HashMap;

Import Java.util.Map;
    /** * The callback address of the call timed task * Can be agreed to the timing task of the receipt message, for further analysis * littlehow 2018/4/7/@Service public class Callbackexecuteservice {
    Private final static Logger log = Loggerfactory.getlogger (Callbackexecuteservice.class);

    /** * The relationship between cache task and callback * * private static final map<string, string> callbackinfo = new hashmap<> ();

    /** * HTTP Task Processing interface * * Private resttemplate resttemplate = new Resttemplate (); /** * Perform remote task * @param taskId/public void execute (string taskId) {string url = callbackinfo.
        Get (TASKID);
            if (url = = null) {log.error ("task [+ TaskId +"] callback address is null);
     Fixme can add monitoring or notification here, or it can be thrown in the form of an exception, using AOP for unified processing       Return try {//fixme If you have a return value rule, you can parse it in detail or simply parse the status code responseentity<string> response = Rest
            Template.getforentity (URL, string.class);
                if (Response.getstatuscodevalue () >= && response.getstatuscodevalue () <300) {//successful
            Log.info ("Timed task [" + TaskId + "] call complete");
            else {//Failed Log.error (Task ["+ TaskId +": "+ URL +"] failed to execute, "+ response.tostring ());
            The catch (Throwable t) {log.error ("task [" + TaskId + ":" + URL + "] executes an exception", T); Fixme can add monitoring or notification here, or you can throw this part of the exception, use AOP Unified processing}/** * Add or modify task callback * @param taskId * @param call Backurl */static string Addorupdatetask (String taskId, String callbackurl) {return Callbackinfo.put (TAS
    KId, Callbackurl); /** * Delete Task callback * @param taskId * @return/static string Removetask (String taskId) {return callbackinfo.remove (taskId);
 }
}


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.