From 9c8c99f09fe82ea3b77572c5906f584b7bd4fdd5 Mon Sep 17 00:00:00 2001 From: musketyr Date: Mon, 9 Dec 2024 11:50:50 +0100 Subject: [PATCH] use weak references for events --- .../worker/event/JobExecutionResultEvent.java | 10 ++++++---- .../agorapulse/worker/queue/DefaultQueueMessage.java | 8 +++++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/libs/micronaut-worker/src/main/java/com/agorapulse/worker/event/JobExecutionResultEvent.java b/libs/micronaut-worker/src/main/java/com/agorapulse/worker/event/JobExecutionResultEvent.java index ab379868..feefbacb 100644 --- a/libs/micronaut-worker/src/main/java/com/agorapulse/worker/event/JobExecutionResultEvent.java +++ b/libs/micronaut-worker/src/main/java/com/agorapulse/worker/event/JobExecutionResultEvent.java @@ -19,6 +19,8 @@ import io.micronaut.core.annotation.Introspected; +import java.lang.ref.WeakReference; + /** * Event dispatched after successful job execution. */ @@ -27,16 +29,16 @@ public class JobExecutionResultEvent { private final String name; private final String id; - private final Object result; + private final WeakReference result; public JobExecutionResultEvent(String name, String id, Object result) { this.name = name; this.id = id; - this.result = result; + this.result = new WeakReference<>(result); } public Object getResult() { - return result; + return result.get(); } public String getId() { @@ -49,6 +51,6 @@ public String getName() { @Override public String toString() { - return "JobExecutionResultEvent{name='%s', id='%s', result=%s}".formatted(name, id, result); + return "JobExecutionResultEvent{name='%s', id='%s', result=%s}".formatted(name, id, result.get()); } } diff --git a/libs/micronaut-worker/src/main/java/com/agorapulse/worker/queue/DefaultQueueMessage.java b/libs/micronaut-worker/src/main/java/com/agorapulse/worker/queue/DefaultQueueMessage.java index 9fc73489..02aefef8 100644 --- a/libs/micronaut-worker/src/main/java/com/agorapulse/worker/queue/DefaultQueueMessage.java +++ b/libs/micronaut-worker/src/main/java/com/agorapulse/worker/queue/DefaultQueueMessage.java @@ -17,6 +17,8 @@ */ package com.agorapulse.worker.queue; +import java.lang.ref.WeakReference; + class DefaultQueueMessage implements QueueMessage { static DefaultQueueMessage alwaysRequeue(String id, T message, Runnable doDelete, Runnable doRequeue) { @@ -28,7 +30,7 @@ static DefaultQueueMessage requeueIfDeleted(String id, T message, Runnabl } private final String id; - private final T message; + private final WeakReference message; private final Runnable doDelete; private final Runnable doRequeue; private final boolean alwaysRequeue; @@ -36,7 +38,7 @@ static DefaultQueueMessage requeueIfDeleted(String id, T message, Runnabl private DefaultQueueMessage(String id, T message, Runnable doDelete, Runnable doRequeue, boolean alwaysRequeue) { this.id = id; - this.message = message; + this.message = new WeakReference<>(message); this.doDelete = doDelete; this.doRequeue = doRequeue; this.alwaysRequeue = alwaysRequeue; @@ -49,7 +51,7 @@ public String getId() { @Override public T getMessage() { - return message; + return message.get(); } @Override