Suggested WebSocket config causes circular bean reference
See original GitHub issueAffects: Spring Boot > 2.6.0, Spring Framework 5.3.10
Hello everyone,
right before the weekend I updated our microservices to Spring Boot 2.6.0. Some of them using websockets for real-time synchronization with the UI. A while ago we set them up using the suggested configuration as seen in:
Now, without any changes to our code, our websocket configuration contains a circular dependency which can be fully removed when removing the TaskScheduler
, which of course isn’t the solution, but helps to determine the circular dependency. In my opinion the suggested configuration should not contain a circular dependency when it is not recommended to use them.
One should probably update the documentation to fix this issue.
So far I have found two ways to fix this problem. First being a lazy initialization of the TaskScheduler
bean:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
private TaskScheduler messageBrokerTaskScheduler;
// Note the @Lazy
@Autowired
public void setMessageBrokerTaskScheduler(@Lazy TaskScheduler taskScheduler) {
this.messageBrokerTaskScheduler = taskScheduler;
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/queue/", "/topic/")
.setHeartbeatValue(new long[] {10000, 20000})
.setTaskScheduler(this.messageBrokerTaskScheduler);
// ...
}
}
This does not work. The second one is overloading the method `configureMessageBroker`:
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
public void configureMessageBroker(MessageBrokerRegistry registry, TaskScheduler messageBrokerTaskScheduler) {
registry.enableSimpleBroker("/queue/", "/topic/")
.setHeartbeatValue(new long[] {10000, 20000})
.setTaskScheduler(messageBrokerTaskScheduler);
// ...
}
}
Should the documentation be updated or are there any downsides of using the suggested solutions?
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Does the second one actually work? That method wouldn’t be called by anything, so while it might appear to solve it it would/could break things.
What I think is weird is the while circular dependency in the first place, as there is none. As you are using Spring Boot it might be that parts of that configuration kicks in. Finally you might be better of using constructor injection in your
@Configuration
class instead of a setter.Oops, did not want to close it.