'SpringBoot+Stomp How to Monitor the Lifecycle??
@Controller
@EnableScheduling
public class WsController {
@MessageMapping("/welcome")
@SendTo("/topic/getResponse")
public ResponseMessage say(RequestMessage message) throws InterruptedException {
Thread.sleep(3000);
return new ResponseMessage("Welcome, " + message.getName() + "!");
}
}
I want to listen to endpoint's subscribe event and unsubscribe event on WebSocket like this:
@ServerEndpoint("/websocket/{user}")
@Slf4j
public class WebSocketUtil {
@OnOpen
public void onOpen(){ log.info("open");}
@OnClose
public void onClose(){ log.info("close");}
}
How can I achieve this?
Solution 1:[1]
Spring should be firing websocket subscribe/unsubscribe events that you could listen on.
@Component
public class MyListener {
@EventListener(SessionSubscribeEvent.class)
public void onSessionSubscribeEvent(SessionSubscribeEvent event) {
// ...
}
}
Additional references:
- If you're unfamiliar with listening/publishing events in Spring, take a look at https://spring.io/blog/2015/02/11/better-application-events-in-spring-framework-4-2.
- Spring 4.3.x WebSocket reference - Events and Interception
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tim Tong |
