Setup of sping MVC timer task

Timed tasks are used in the project. Write a note to record with your notes.

First add the beans tag to the Spring configuration file ApplicationContext.xml file

xmlns:task="http://www.springframework.org/schema/task"
http://www.springframework.org/schema/task 
http://www.springframework.org/schema/task/spring-task-4.1.xsd

Currently, there are two ways I know to use timed tasks:

1. Declare calls to timed tasks in the configuration file

Declare the invoked bean in the ApplicationContext.xml file first

<bean id="quartzController" class="com.bizdata.common.socket.web.resource.QuartzTaskController"></bean>

The java method and code to invoke is as follows

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Controller;

@Controller
public class QuartzTaskController {
    
    public void sysTime(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(new Date()));
    }

}

Then declare the calling object and the method of calling the object

<!-- Define the calling object and the method of calling the object -->
    <bean id="myJobDetailA"
          class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
        <property name="targetObject" ref="quartzController">  <!--Execution Timer Class-->
        </property>
        <property name="targetMethod" value="sysTime"></property>   <!--Execution Timing Method-->
        <property name="concurrent" value="false" />
        <!-- Whether tasks are allowed to execute concurrently.When value is false Indicates that a new thread must not be started until the previous thread has finished processing -->
    </bean>
    <!-- Define trigger time -->
    <bean id="myTriggersA"
          class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="myJobDetailA">
        </property>
        <property name="cronExpression">
            <value>0 00 6 * * ?</value>    <!--Timing Time-->
            <!--Time size ranges from small to large, starting in seconds, in order of seconds, minutes, hours, days, months, years    *Any?For unlimited-->
        </property>
    </bean>

The above settings complete the timed task and are called at 6 a.m. each day.

2. Use via demodulation (recommended method, will save a lot of configuration, easy to use)

The ApplicationContext.xml file declares that bean s are scanned automatically:

<!--Turn on support for annotation injection bean s-->
    <context:annotation-config />
    <context:component-scan base-package="com.bizdata" />

Adding configuration for demodulation at the same time

<task:annotation-driven/> <!--Note with timer-->

- java code

  @Scheduled(cron="0 0 22 * * ?")
    public boolean getDataCenterData(){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        return getDataCenterData(calendar.getTime());
    }

Complete the use of timed tasks, called at 22 o'clock per day.

When invoking a timer task using the annotations configured above, the exception log will be printed, but it will not affect usage. You can add a log level filter to the log4j.properties file.

#Timer Log Level
log4j.logger.org.springframework.scheduling = INFO

Keywords: Java xml Spring log4j

Added by reddevil on Mon, 06 Jan 2020 18:25:06 +0200