0

I want to create dynamic cron with Spring-MVC. User can toggle cron from xhtml page. Cron works perfectly but when tomcat shutdown , i faced an error and tomcat couldnt stop properly. Tried this solution but error continues. Sharing the error message and code below (JDK 17 + Tomcat 9).

02-Feb-2023 14:17:58.960 WARNING [main] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [MuhurUM] registered the JDBC driver [oracle.jdbc.OracleDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
02-Feb-2023 14:17:58.961 WARNING [main] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [MuhurUM] appears to have started a thread named [Timer-0] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.base@17.0.4/java.lang.Object.wait(Native Method)
 java.base@17.0.4/java.lang.Object.wait(Object.java:338)
 java.base@17.0.4/java.util.TimerThread.mainLoop(Timer.java:537)
 java.base@17.0.4/java.util.TimerThread.run(Timer.java:516)
02-Feb-2023 14:17:58.962 WARNING [main] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [MuhurUM] appears to have started a thread named [pool-3-thread-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.base@17.0.4/jdk.internal.misc.Unsafe.park(Native Method)
 java.base@17.0.4/java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:252)
 java.base@17.0.4/java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:1672)
 java.base@17.0.4/java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1182)
 java.base@17.0.4/java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:899)
 java.base@17.0.4/java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1062)
 java.base@17.0.4/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1122)
 java.base@17.0.4/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
 java.base@17.0.4/java.lang.Thread.run(Thread.java:833)
@Service
public class CronUtil {

    @Autowired
    private TaskScheduler taskScheduler;

    public void scheduleATask(Long id, Runnable tasklet, String cronExpression) {
        ScheduledFuture<?> scheduledTask = taskScheduler.schedule(tasklet, new CronTrigger(cronExpression, TimeZone.getTimeZone(TimeZone.getDefault().getID())));
        TestApplication.jobsMap.put(id, scheduledTask);
    }

    public void removeScheduledTask(Long id) {
        ScheduledFuture<?> scheduledTask = TestApplication.jobsMap.get(id);
        if (scheduledTask != null) {
            scheduledTask.cancel(true);
            TestApplication.jobsMap.remove(id);
        }
    }
}
  • Are you using Spring Boot? Where (and how) are you configuring the `TaskScheduler`. – M. Deinum Feb 02 '23 at 11:47
  • @M.Deinum using spring 5.3.23, but im not configuring TaskScheduler, is that a bad way or i should configure – Oytun Canatan Feb 02 '23 at 11:51
  • Plain Spring and no Spring boot (as the solution you linked to was mentioning Spring Boot). If you don't configure the `TaskScheduler` there wouldn't be any, as you are autowiring it, it must come from somewhere. – M. Deinum Feb 02 '23 at 12:12

0 Answers0