'How to group observed data from Java CDI EVENT to send with a delay?
We are working on a feature where we want to observe events from our backend and propagate them forward to an external service. The issue is that sometimes these events come in rapid succession and cause performance issues.
We use Java 8 with JavaEE 7, more specifically we use the Event feature in an @ApplicationScoped CDI class. The current way we have it implemented is the following (code was trimmed to barebones for this thread):
@ApplicationScoped
public class DataObserver {
public void observeData(@Observes Data data) {
pushForward(data);
}
...
}
The goal now is to group multiple requests together and send them with a tiny bit of delay, if necessary. Instead of 10 request with Data each, we could do 1 request with 10 Data each.
Additionally we don't want to wait for 10 objects to accumulate either. Data should be forwarded every few seconds regardless of how many Data objects were accumulated by then. This is the part we are struggling with, since I cannot think of the proper solution.
Is there a clean way of doing this that guarantees me that no data will be missed? Maybe use ScheduledExecutorService with pool size 1 and submit tasks with a delay, while also adding an additional task for safety?
Would appreciate the help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
