Skip to content
Advertisement

Spring SmartLifecycle start() method not executed

In my spring app I have a thread pool manager that during start() creates some threads and add those to the pool. But while running the start() method is never executed.

@Component
public class ThreadPoolManager implements SmartLifecycle {

    private static final Logger logger = 
      LoggerFactory.getLogger(ThreadPoolkManager.class);

    ScheduledExecutorService httpCheckPool = Executors.newScheduledThreadPool(10);

    @Override
    public void start() {
       /**
        * Start some threads
        */
    }

    @Override
    public void stop() {
        httpCheckPool.shutdown();
    }

    @Override
    public boolean isRunning() {
        return !httpCheckPool.isTerminated();
    }

    public synchronized void addServiceToCheck(NewService service){
        /**
         * Add service to the threadpool
         */
    }

Any help will be really appreciated.

Advertisement

Answer

This is for someone facing similar problem. isRunning() is called before start() therefore make sure isRunning() returns false initially then only start() is executed. Simple way to do this is use a flag.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement