CodePudding user response:
First impression of not add @ EnableScheduling commentsMay you want an example debugging example,
The import org. Springframework. Scheduling. The annotation. EnableScheduling;
The import org. Springframework. Scheduling. The annotation. Scheduled;
The import org. Springframework. Stereotype. Service;
@ EnableScheduling//start the timer, an application needs a can!!!!!!
@ Service
Public class DemoScheduledService {
@ Scheduled cron (="0/5 * * * *?" )
Public void the log () {
System. Out.println (" under the Eclipse debug debugging @ Scheduled to enter the breakpoint... ");
}
}
Import the Java. Util. Concurrent. CountDownLatch;
The import org. Junit. Jupiter. API. BeforeAll;
import org.junit.jupiter.api.Test;
The import org. Springframework. Context. ApplicationContext;
The import org. Springframework. Context. The annotation. AnnotationConfigApplicationContext;
The import org. Springframework. Scheduling. Concurrent. ThreadPoolTaskExecutor;
Public class BootAdminServerApplicationTests {
Private static DemoScheduledService DemoScheduledService;
@ BeforeAll
Public static void beforeInit () {
ApplicationContext ac=new AnnotationConfigApplicationContext (DemoScheduledService. Class);
DemoScheduledService=ac. GetBean (demoScheduledService. Class);
}
@ Test
Public void logTest () {
Final CountDownLatch CountDownLatch=new CountDownLatch (1);
ThreadPoolTaskExecutor ThreadPoolTaskExecutor=new ThreadPoolTaskExecutor ();
ThreadPoolTaskExecutor. The initialize ();
//open thread
ThreadPoolTaskExecutor. Execute (() - & gt; {
System. The out. Println (" -- the child thread - ");
Try {
//simulation business code, direct call
DemoScheduledService. The log ();
//simulation business code, blocking the timer called
Thread.sleep (100000);
} the catch (Exception e) {
} the finally {
CountDownLatch. CountDown ();//to finish the work, counter minus one
}
});
Try {
//wait for the child threads all over
CountDownLatch. Await ();
} the catch (Exception e) {
e.printStackTrace();
}
System. The out. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- the main thread execution -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- ");
}
}