Skip to content

Commit

Permalink
[Performance #285] fix event ordering in long-running tasks registry
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaskabc authored and ledsoft committed Sep 13, 2024
1 parent a7a23eb commit d4e2676
Showing 1 changed file with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;

@Component
public class LongRunningTasksRegistry {
Expand All @@ -25,20 +26,30 @@ public class LongRunningTasksRegistry {

private final ApplicationEventPublisher eventPublisher;

private final ReentrantLock lock = new ReentrantLock(true); // using fair ordering

@Autowired
public LongRunningTasksRegistry(ApplicationEventPublisher eventPublisher) {
this.eventPublisher = eventPublisher;
}

@Order(0) // ensures that registry is updated before controllers
@EventListener
public void onTaskChanged(LongRunningTaskChangedEvent event) {
public void onTaskChanged(final LongRunningTaskChangedEvent event) {
// using fair ReentrantLock guarantees receiving events in the order
lock.lock();
try {
handleTaskChanged(event);
} finally {
lock.unlock();
}
}

private void handleTaskChanged(final LongRunningTaskChangedEvent event) {
final LongRunningTaskStatus status = event.getStatus();
if (LOG.isTraceEnabled()) {
synchronized (LongRunningTasksRegistry.class) {
LOG.atTrace().setMessage("Long running task changed state: {}{}").addArgument(status::getName)
.addArgument(status).log();
}
LOG.atTrace().setMessage("Long running task changed state: {}{}").addArgument(status::getName)
.addArgument(status).log();
}

if(status.getState() == LongRunningTaskStatus.State.DONE) {
Expand Down

0 comments on commit d4e2676

Please sign in to comment.