I am using the PerConnectionWebSocketHandler like so:
@Bean
public WebSocketHandler myHandler() {
return new PerConnectionWebSocketHandler(MyHandler.class));
}
where MyHandler is a class implementing the Spring WebSocketHandler interface.
I would like to add an exception handling decorator, that for a single instance can be created like this:
new ExceptionWebSocketHandlerDecorator(new MyHandler() );
I need the ExceptionWebSocketHandlerDecorator to be wrapped by the PerConnectionWebSocketHandler. When the ExceptionWebSocketHandlerDecorator is on the outside, exceptions thrown by MyHandler result in the MyHandler.afterConnectionClosed not being called.
How do I combine this with the PerConnectionWebSocketHandler?
WebSocketHandler
s can be nested within each other:
@Bean
public WebSocketHandler myHandler() {
return new PerConnectionWebSocketHandler(MyHandler.class));
}
@Bean
public WebSocketHandler myHandlerDecorator() {
return new ExceptionWebSocketHandlerDecorator(myHandler());
}
This is for case, when you are going to use BeanFactory
to autowire your target MyHandler
or via simple way:
@Bean
public WebSocketHandler myHandlerDecorator() {
return new ExceptionWebSocketHandlerDecorator(
new PerConnectionWebSocketHandler(MyHandler.class)));
}
if not.
In addition you can consider to chain to the LoggingWebSocketHandlerDecorator
as well.
UPDATE
According to your comment I'd suggest to write your own ExceptionWebSocketHandler
or just implement all that ExceptionWebSocketHandlerDecorator
logic in your MyHandler
. That's because PerConnectionWebSocketHandler
requires the class with default constructor, even it is delegated to the this.beanFactory.createBean(this.handlerType)
.
UPDATE2
The difficulty with this approach is that I have more than one handler,
Well, then you can implement your own PerConnectionExceptionWebSocketHandler
! With the same logic from the ExceptionWebSocketHandlerDecorator
and PerConnectionWebSocketHandler
. Or just extend PerConnectionWebSocketHandler
and override what you need.