Write the implementation of timed tasks in the Java Web project

  • 2020-06-01 09:46:50
  • OfStack

The previous company has a special task scheduling framework, which can be used by adding a configuration and annotation to the jar package when needed, as well as a special platform to maintain running machines and monitor the execution status.

Now that you've suddenly lost this tool, what do you do when you have to write timed tasks?

For non-Web applications, we can use Quartz, which is simple to use and powerful.

Is, of course, you can also use the application for Java Web Quartz (1 article introduces methods: https: / / www ofstack. com article / 104105 htm), but there is a more convenient tool, that is the support spring bring timing task function.

The timing task of Spring is in spring-context. The simple configuration template is as follows:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
  http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> 
 <task:scheduler id="scheduler" pool-size="200"/> 
  <!--  your task --> 
  <task:scheduled ref="xxxTask" method="execute" cron="0 0 * * * ?"/> 
 <task:annotation-driven scheduler="scheduler"/> 

Where task:scheduler specifies the scheduler used for timed tasks, and the default is

org. springframework. scheduling. concurrent. ThreadPoolTaskScheduler;

task: annotation-driven allows @Async and @Scheduled annotations;

task: one task is defined in scheduler-tasks, where an cron expression can be used for the execution cycle and a delay or frequency can be specified.

One conversion cron online tool is quite good, recommended to you (note that there may show seven characters, remove the last 1 *) : http: / / tools ofstack. com/code/Quartz_Cron_create

Then there is another problem. Usually our online environment is a cluster environment with multiple machines, and these timed tasks usually only need to be performed on 1 platform. How to control it?

So far I have come up with two ways to share them with you:

1. Use Redis global caching


2. Judge the documents

Determine whether to execute the task (whether to load the spring configuration file corresponding to the task) by judging whether a file exists. The reference code is:

public class XxxListener implements ApplicationContextAware { 
 //  Prevent multiple loads  
 private static final AtomicInteger INIT_LOCK = new AtomicInteger(0); 
 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 
  if (INIT_LOCK.incrementAndGet() > 1) { 
   //  The class has been loaded  
  Resource resource = applicationContext.getResource("classpath:< Identity documents >"); 
  if (!resource.exists()) { 
   //  The file does not exist and is not started  
  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(applicationContext); 

Related articles: