Spring Boot Application Startup Error with WebSocket Enabled

Jing Xue
1 min readDec 24, 2018

--

Trying to add a simple text-based WebSocket handler to a Spring Boot application, following this sample. Got this error when trying to bring up the app:

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named ‘defaultSockJsTaskScheduler’ is expected to be of type ‘org.springframework.scheduling.TaskScheduler’ but was actually of type ‘org.springframework.beans.factory.support.NullBean’

Turns out the app has an existing scheduled task, something like:

@Scheduled(fixedRate = 500)
fun doSomething() {
// ...
}

Apparently the default task scheduler is somehow conflicting with the one needed by the WebSocket support. My solution is to explicitly set a task scheduler for the existing app task:

override fun configureTasks(taskRegistrar: ScheduledTaskRegistrar) {
taskRegistrar.setTaskScheduler(serverTaskScheduler())
}
@Bean
fun serverTaskScheduler(): TaskScheduler {
val taskScheduler = ThreadPoolTaskScheduler()
taskScheduler.poolSize = 3
taskScheduler.threadNamePrefix = “app-timer-”
taskScheduler.isDaemon = true
return taskScheduler
}

If anybody knows an easier way, let me know.

--

--