QUTE Concurrent loading of CDI beans. #30253
-
Imagine I am working with a complex page, with many tags / components. Imagine these tags need some time to resolve their data. What is the best way of allowing those tags to build their data concurrently? Let me show you an example of what I have so far, with just 2 tags. This is based on the QUTE example. I have a page with 2 tags. comp1 and comp2.
Comp1 and Comp2 both make use of a injected CDI bean, C1 and C2. (Comp1 below. Comp2 is identical).
C1 and C2 beans look like the following. Note there is a Thread.sleep(1000), to simulate the bean getting data it requires, from say a remote server.
There is some code in there for timing how long this takes. When I show the page I get: On the console I get: Note that its taking about 2 seconds to build. So what is happening is both C1 and C2 beans are being instantiated only at the very moment QUTE requests val. Because of this, it is impossible for both beans to be resolve the data they require concurrently. - Its like QUTE says, "Create C1, and give me Val1", and blocks while this happens, then it says "Create C2 and give me Val2", and blocks while this happens. What I would really like is for QUTE to resolve and instantiate every bean first, and then pull the data from those beans only after all beans have been instanciated. I can simulate this by in my controller, inject these beans, and call toString on them, to force them to be realised.
Once this is done, the console says this: Can you see how the time to show the page, is now 1 second, rather than 2 seconds before? This is because the controller is forcing these components to be instantiated up front, without requesting the data. This allows them to be built concurrently. Though this is my preferred behaviour, I really do not like having to put references to the beans in my controller, just to force them to build. Is there an alternative pattern I should be using, to give me the same asynchronous behaviour, which will allow me to build a screen where the data for many tags, is built concurrently? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Beta Was this translation helpful? Give feedback.
-
You should just return the |
Beta Was this translation helpful? Give feedback.
You should just return the
CompletableFuture
from thegetVal()
method, i.e. don't block when the{inject:c1.val}
is evaluated.