Spring is initialized two times (Spring-task scheduled task execution two times) analysis and workaround

Source: Internet
Author: User

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

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.