'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:

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