Initial issue:
Configure timed tasks with Spring-task, task execution two times, the issue does not appear on the Eclipse debug environment
Analyze the problem:
The first step: Start doubting business logic, troubleshoot and locate the business cause (through the log you can view multiple executions)
1@Component ("Collection.car")2 Public classCollectioncarsjob {3 Private StaticLogger Logger = Loggerfactory.getlogger (collectioncarsjob.class);4@Scheduled (cron = "0 0 1 * * *")5 Public voidPackdayscollectiondata () {6 ...7Logger.info ("Insert Stat data for collections of cars. {} ", date);
Second step: Suspect timed task bug, cause the scheduled task is registered multiple times (through the log found that the initialization location is called multiple times)
1 @EnableScheduling2 Public classWebappconfigextendsWebmvcconfigureradapterImplementsschedulingconfigurer{3 Private StaticLogger Logger = Loggerfactory.getlogger (webappconfig.class);4 //TaskScheduler Configuration5 @Override6 Public voidconfiguretasks (Scheduledtaskregistrar taskregistrar) {7Logger.info ("Configure task Registor: {}", Taskregistrar);8 Taskregistrar.setscheduler (Taskexecutor ());9 }Ten One@Bean (destroymethod= "Shutdown") A PublicExecutor Taskexecutor () { - returnExecutors.newscheduledthreadpool (4); -}
On-line search, found that many due to configuration duplication caused by the project in a fully annotated configuration, so there is no multiple configuration
When viewing the boot log, it was found that the spring application was initialized two times, in addition to the task being initialized two times
Step three: The Tomcat configuration causes the container to be initialized two times
Since the app is placed directly under Tomcat's webapp and accessed through the root path, the configuration is as follows
1 <context path= "" docbase= "Tower-webmvc" debug= "0" reloadable= "true" crosscontext= "true"/>
Find out how to handle similar problems:
Method 1: Do not configure root path access
Method 2: Route the path out of the WebApp directory, specify the path in Docbase (can be a path relative to WebApp, or absolute path) "Recommended"
Method 3: Replace the root directory "recommended"
Method 4: Generate the Root.xml in Conf/catalina/localhost and add the following:
1 2
Spring is initialized two times (Spring-task scheduled task execution two times) analysis and workaround