Created
December 28, 2017 18:23
-
-
Save mikaelhg/4b02f4351dc1fae2eed5330a60460021 to your computer and use it in GitHub Desktop.
Spring 5 EnableWebSocket and EnableScheduling "Unexpected use of scheduler" issue workaround
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.mikael.poc | |
import org.slf4j.LoggerFactory | |
import org.springframework.boot.SpringApplication | |
import org.springframework.boot.autoconfigure.SpringBootApplication | |
import org.springframework.context.annotation.Bean | |
import org.springframework.context.annotation.Configuration | |
import org.springframework.scheduling.annotation.EnableScheduling | |
import org.springframework.scheduling.annotation.Scheduled | |
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler | |
import org.springframework.stereotype.Service | |
import org.springframework.web.socket.CloseStatus | |
import org.springframework.web.socket.TextMessage | |
import org.springframework.web.socket.WebSocketSession | |
import org.springframework.web.socket.config.annotation.EnableWebSocket | |
import org.springframework.web.socket.config.annotation.WebSocketConfigurer | |
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry | |
import org.springframework.web.socket.handler.TextWebSocketHandler | |
import java.time.OffsetDateTime | |
fun main(args: Array<String>) { | |
SpringApplication.run(Application::class.java, *args) | |
} | |
@SpringBootApplication | |
@EnableScheduling | |
class Application | |
@Configuration | |
@EnableWebSocket | |
class WebSocketConfig() : WebSocketConfigurer { | |
override fun registerWebSocketHandlers(registry: WebSocketHandlerRegistry) { | |
registry.addHandler(myHandler(), "/myHandler") | |
} | |
@Bean | |
fun myHandler() = MyMessageHandler() | |
@Bean | |
fun taskScheduler() = ThreadPoolTaskScheduler().apply { | |
poolSize = 1 | |
threadNamePrefix = "scheduler" | |
} | |
} | |
@Service | |
class UpdateService { | |
companion object { | |
private val log = LoggerFactory.getLogger(UpdateService::class.java) | |
} | |
@Scheduled(fixedDelay = 600_000, initialDelay = 5_000) | |
fun update() { | |
log.info("Updating at ${OffsetDateTime.now()}") | |
} | |
} | |
class MyMessageHandler : TextWebSocketHandler() { | |
override fun afterConnectionClosed(session: WebSocketSession, status: CloseStatus) { | |
} | |
override fun afterConnectionEstablished(session: WebSocketSession) { | |
session.sendMessage(TextMessage("You are now connected to the server. This is the first message.")) | |
} | |
override fun handleTextMessage(session: WebSocketSession, textMessage: TextMessage) { | |
println("Message received: ${textMessage.payload}") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment