schrobe
schrobe

Reputation: 837

NullPointerException when creating a Scheduler Bean with Spring

I am trying to use Spring to create a scheduler.

@Configuration
@EnableScheduling
public class MyScheduler {


  @Autowired
  MyBusinessService businessService;


  @Scheduled(cron = "* * * * * *")
  public void myCronMethod() {

  }
}

During application startup I get the following error:

java.lang.NullPointerException
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.resolveSchedulerBean(ScheduledAnnotationBeanPostProcessor.java:281)
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.finishRegistration(ScheduledAnnotationBeanPostProcessor.java:221)
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:200)
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:94)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:383)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:337)
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:882)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:545)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5068)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5584)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1572)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1562)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

In the ScheduledAnnotationBeanPostProcessor the holder is null:

NamedBeanHolder<T> holder = ((AutowireCapableBeanFactory) this.beanFactory).resolveNamedBean(schedulerType);

The schedulerType is:

interface org.springframework.scheduling.TaskScheduler

The application is a JSF application. Im using java 8 and spring version 4.3.6, however with Ant and without dependency management, so missing libraries or a library mismatch is a possibility.

Upvotes: 3

Views: 1952

Answers (1)

Adrian Cox
Adrian Cox

Reputation: 6334

Having just had the same error in a Kotlin application, my workaround was to manually add a TaskScheduler to my application configuration:

The Java example is taken from here.

@Bean
public ThreadPoolTaskScheduler threadPoolTaskScheduler(){
    ThreadPoolTaskScheduler threadPoolTaskScheduler
      = new ThreadPoolTaskScheduler();
    threadPoolTaskScheduler.setPoolSize(5);
    threadPoolTaskScheduler.setThreadNamePrefix(
      "ThreadPoolTaskScheduler");
    return threadPoolTaskScheduler;
}

My Kotlin version is

@Bean
fun threadPoolTaskScheduler()
    = ThreadPoolTaskScheduler().apply {
        setPoolSize(5)
        setThreadNamePrefix("ThreadPoolTaskScheduler")
}

(I've not yet tried it, but I believe upgrading to Spring 5 will also fix this - this commit throws an Exception where the null pointer would have been generated.)

Upvotes: 1

Related Questions