Skip to content

SpringConfigurator uses ListableBeanFactory method which is documented to be slow for EVERY new WebSocket connection [SPR-10605] #15234

@spring-projects-issues

Description

@spring-projects-issues

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:

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions