1. The origin

When doing Java server development, it is inevitable to contact the development of timing task-related modules, what timing mail, what timing report. Here I use quartz open source library, powerful, but also automatically maintain the database, easy to get started. However, it is not very convenient in terms of ease of use. In the process of using the project, we met some common points. After several iterations, the current software interface is the effect you see below. I like to call her Task.

Function of 2.

After several iterations, the main functions are as follows:

  • Innovative management interface design, intuitive display of task enabling status, task execution status and other information, put common functions in a list, easy to use;
  • In Quartz, job is the smallest unit of task execution. We abstract more fine-grained tasks based on job. A job can contain multiple tasks.
  • Each task can be configured with its own parameters. Configuration parameters in the task are added through annotations.
  • Tasks are of two types. Common tasks are executed in the configured order. Another special type of task is flat, where you can take the data from a List one by one and send it one by one to a subsequent List of tasks. This name is from rxJava, heh heh;
  • – Added more complete task interrupt function, Quartz job interrupt only gives a bool tag, need to be handled by the developer in job. The Task library is also based on the bool tag, which will judge the tag during the execution of the Task chain and interrupt the Task by throwing exceptions. This method still cannot interrupt the task in real time.
  • Tasks have the sorting function. Tasks are executed in the order of tasks in the task list.
  • The task log function is added. During the execution of tasks, the results returned by each task will be sent to the front end for display in the form of real-time logs through websocket. Later, the function of logging manually in logger is added.
  • Task in the library has a built-in management interface, only need to input to the browser: http://ip:port/task/index.html to view the Task management interface.
Use 3.

Rely on

<dependency>
     <groupId>com.basic</groupId>
     <artifactId>basic-task</artifactId>
     <version>1.1.6</version>
</dependency>
Copy the code
# yml to add the following configuration, task - packages deposit for the task of the package name, can be set to project the package name task: task - packages: com. Piesat. Project. Admin. TaskCopy the code

The sample

/** * Tasks are divided into public groups and custom groups. When selecting tasks, */ @taskAnnotation (name = "public test") */ @taskannotation (name = "public test ", Group = Constants.PUBLIC) PUBLIC class PublicTask extends BaseTask<String> {/* * @taskParam annotated variables are displayed in the Task property configuration, After the configuration tasks automatically injected * type/defaultValue/options three options temporarily can't use * / private final FileFilter filter; @taskParam (alias = "Directory to scan ", type = DataType.STRING) private STRING sourceDirectory; @taskParam (alias =" directory to scan ", type = DataType.STRING) private STRING sourceDirectory; @taskParam (alias = "whether to scan subdirectories ", type = DataType.BOOLEAN, defaultValue = "true", options = {"true", "false"}) private boolean needScanChildren; @taskparam (alias = "fileFilter ", type = DataType.OBJECT) private String fileFilter; public PublicTask(TaskInfo taskInfo) { super(taskInfo); filter = new FileFilter(); filter.createFromFilterInfo(new FilterInfo(FilterType.FILE.getType(), fileFilter)); } @taskParam public TaskResult<? > execute() { return null; }}Copy the code
/** * Method annotations are most commonly used in actual projects. It is a good idea to write them directly in a service. You can inject any number of tasks into a service if you like. You can also put all tasks into a service if you like. Methods annotated by @Parser will eventually parse into tasks. TaskInstanceCreator */ @service public class TestMethodTask {/** * name defines the task name * group defines the task group. * Params is used to define the parameters to be received by a task. There is no limit to the number of parameters that can be aliases using the ':' delimiter. */ @parser (name = "task1", group = "test", params = {"path: source address ", "DestPath: destination address "}) public String execute(Map<String, Object> Map) {String path = (String) map.get("path"); String destPath = (String) map.get("destPath"); Return B. } @Parser(name = "flat_task", group = "test", params = {"targetPath", "destPath"}) public String test1(Map<String, Object> map) {return "success "; }}Copy the code

After the preceding two configurations, the following effect is displayed on the management page

4. After

The current version should be the third generation

  • The primary logic is written in job, and the task parameters are written in YML and injected by Spring.
  • In the second generation, the task execution form was changed to TaskFlow, which refined the job and abstracted the concept of task. Realization combines a variety of tasks into a job to achieve task reuse;
  • Customize the parameters of the third generation join task, display the log of the join task in real time, redesign the display interface of the task;
  • Fourth generation still in the pipeline, perfect log component?
  • .

All of these iterations are pushed along by the project, and it has been found to be very difficult to maintain, so we have to change. Maybe this is the epitome of The Times we live in. Sometimes we are pushed by The Times rather than actively choosing to become better. To lazy self!

If you have any other good ideas, please comment in the comments section and let’s work on them together.

5. The final chapter

The following project will be sent to the Maven central repository. If you have no experience, please feel free to comment in the comments. The server has been bought, the subsequent use of good file editing sent to the server, convenient for you to use.

Project address: Basic-task