Skip to content

Commit

Permalink
use weak references for events
Browse files Browse the repository at this point in the history
  • Loading branch information
musketyr committed Dec 9, 2024
1 parent d050d6c commit 9c8c99f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import io.micronaut.core.annotation.Introspected;

import java.lang.ref.WeakReference;

/**
* Event dispatched after successful job execution.
*/
Expand All @@ -27,16 +29,16 @@ public class JobExecutionResultEvent {

private final String name;
private final String id;
private final Object result;
private final WeakReference<Object> 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() {
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package com.agorapulse.worker.queue;

import java.lang.ref.WeakReference;

class DefaultQueueMessage<T> implements QueueMessage<T> {

static <T> DefaultQueueMessage<T> alwaysRequeue(String id, T message, Runnable doDelete, Runnable doRequeue) {
Expand All @@ -28,15 +30,15 @@ static <T> DefaultQueueMessage<T> requeueIfDeleted(String id, T message, Runnabl
}

private final String id;
private final T message;
private final WeakReference<T> message;
private final Runnable doDelete;
private final Runnable doRequeue;
private final boolean alwaysRequeue;
private boolean deleted;

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;
Expand All @@ -49,7 +51,7 @@ public String getId() {

@Override
public T getMessage() {
return message;
return message.get();
}

@Override
Expand Down

0 comments on commit 9c8c99f

Please sign in to comment.