question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggested WebSocket config causes circular bean reference

See original GitHub issue

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

https://github.com/spring-projects/spring-framework/blob/main/src/docs/asciidoc/web/websocket.adoc#simple-broker

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:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mdeinumcommented, Nov 29, 2021

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.

0reactions
MoritzHorchcommented, Nov 29, 2021

Oops, did not want to close it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring Framework Reference Documentation
If you configure beans for classes A and B to be injected into each other, the Spring IoC container detects this circular reference...
Read more >
Circular Dependencies in Spring - Baeldung
But with a circular dependency, Spring cannot decide which of the beans should be created first since they depend on one another.
Read more >
Spring Security circular bean dependency - Stack Overflow
If you configure beans for classes A and B to be injected into each other, the Spring IoC container detects this circular reference...
Read more >
Management & Monitoring - Micronaut Documentation
With 3.4.0, you can reference other beans properties in @Requires to load beans conditionally. @Requires(bean=Config.class, beanProperty="foo", value="John") ...
Read more >
spring-projects/spring-boot - Gitter
@Configuration @ConditionalOnBean(AsyncConfigurer.class) public class ... is provided for tomcat via tomcat-websocket-api , debating on if I should open a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found