How to use @SessionScope beans with Vaadin Broadcaster?

186 Views Asked by At

I am using Spring Boot and Vaadin Flow (latest). And I tried the broadcaster example and it works. But as soon as I try to pass the received message to a @SessionScope annotated bean, I am getting an exception

java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request

Any hints on this? Thanks in advance for your time!

1

There are 1 best solutions below

0
ollitietavainen On

Here's what's happening according to my understanding: Spring's @SessionScope beans are created for each HTTP Session, or as the documentation says,

[@SessionScope] Scopes a single bean definition to the lifecycle of a HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.

This means you need to be working in the context of an HTTP session, and as the error message says,

No thread-bound request found: Are you referring to request attributes outside of an actual web request?

The example implementation of the Broadcaster pattern uses a simple background thread executor created by the Executors.newSingleThreadExecutor()- the thread doesn't know anything about HTTP requests or sessions, and it's specifically not running in the context of a web-aware Spring ApplicationContext..

Your actual problem isn't visible in the question, but hopefully, this answer clarifies the error you're getting and helps you move forward.