-
Notifications
You must be signed in to change notification settings - Fork 38.8k
Description
Nick Williams opened SPR-10605 and commented
When using the server endpoint configurator org.springframework.web.socket.server.endpoint.SpringConfigurator to manage creation of WebSocket server endpoints, the SpringConfigurator#getEndpointInstance method will be called every time a new WebSocket connection is received.
getEndpointInstance calls the <T> T getBeansOfType(Class<T> ) method of the web application context (technically this method comes from ListableBeanFactory) each time it is called to determine whether to return a new instance, return an existing singleton instance, or throw an exception. However, the JavaDoc for ListableBeanFactory says this:
With the exception of getBeanDefinitionCount and containsBeanDefinition, the methods in this interface are not designed for frequent invocation. Implementations may be slow.
I do not believe SpringConfigurator should be using this method as it could have seriously negative performance consequences. I recommend something more like this:
...
try {
T singleton = wac.getBean(endpointClass);
if (logger.isTraceEnabled()) {
logger.trace("Using @ServerEndpoint singleton " + singleton);
}
return singleton;
} catch(NoSuchBeanDefinitionException e) {
if (logger.isTraceEnabled()) {
logger.trace("Creating new @ServerEndpoint instance of type " + endpointClass);
}
return wac.getAutowireCapableBeanFactory().createBean(endpointClass);
} catch(NoUniqueBeanDefinitionException e) {
// Should not happen ..
String message = "Found more than one matching @ServerEndpoint beans of type " + endpointClass;
logger.error(message);
throw new IllegalStateException(message);
}
...
Affects: 4.0 M1
Issue Links:
- @Scheduled does not work on @ServerEndpoint deployed with per-connection lifecycle [SPR-10611] #15240
@Scheduleddoes not work on@ServerEndpointdeployed with per-connection lifecycle