Skip to content

Commit 5199c84

Browse files
committed
GH-756 - Avoid superfluous, repeated deserialization in JdbcEventPublication.getEvent().
1 parent ea7b299 commit 5199c84

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

spring-modulith-events/spring-modulith-events-jdbc/src/main/java/org/springframework/modulith/events/jdbc/JdbcEventPublicationRepository.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Objects;
2626
import java.util.Optional;
2727
import java.util.UUID;
28+
import java.util.function.Supplier;
2829
import java.util.stream.Collectors;
2930
import java.util.stream.IntStream;
3031

@@ -323,7 +324,8 @@ private TargetEventPublication resultSetToPublication(ResultSet rs) throws SQLEx
323324
var listenerId = rs.getString("LISTENER_ID");
324325
var serializedEvent = rs.getString("SERIALIZED_EVENT");
325326

326-
return new JdbcEventPublication(id, publicationDate, listenerId, serializedEvent, eventClass, serializer,
327+
return new JdbcEventPublication(id, publicationDate, listenerId,
328+
() -> serializer.deserialize(serializedEvent, eventClass),
327329
completionDate == null ? null : completionDate.toInstant());
328330
}
329331

@@ -368,11 +370,10 @@ private static class JdbcEventPublication implements TargetEventPublication {
368370
private final UUID id;
369371
private final Instant publicationDate;
370372
private final String listenerId;
371-
private final String serializedEvent;
372-
private final Class<?> eventType;
373+
private final Supplier<Object> eventSupplier;
373374

374-
private final EventSerializer serializer;
375375
private @Nullable Instant completionDate;
376+
private @Nullable Object event;
376377

377378
/**
378379
* @param id must not be {@literal null}.
@@ -383,22 +384,17 @@ private static class JdbcEventPublication implements TargetEventPublication {
383384
* @param serializer must not be {@literal null}.
384385
* @param completionDate can be {@literal null}.
385386
*/
386-
public JdbcEventPublication(UUID id, Instant publicationDate, String listenerId, String serializedEvent,
387-
Class<?> eventType, EventSerializer serializer, @Nullable Instant completionDate) {
387+
public JdbcEventPublication(UUID id, Instant publicationDate, String listenerId, Supplier<Object> event,
388+
@Nullable Instant completionDate) {
388389

389390
Assert.notNull(id, "Id must not be null!");
390391
Assert.notNull(publicationDate, "Publication date must not be null!");
391392
Assert.hasText(listenerId, "Listener id must not be null or empty!");
392-
Assert.hasText(serializedEvent, "Serialized event must not be null or empty!");
393-
Assert.notNull(eventType, "Event type must not be null!");
394-
Assert.notNull(serializer, "EventSerializer must not be null!");
395393

396394
this.id = id;
397395
this.publicationDate = publicationDate;
398396
this.listenerId = listenerId;
399-
this.serializedEvent = serializedEvent;
400-
this.eventType = eventType;
401-
this.serializer = serializer;
397+
this.eventSupplier = event;
402398
this.completionDate = completionDate;
403399
}
404400

@@ -416,8 +412,14 @@ public UUID getIdentifier() {
416412
* @see org.springframework.modulith.events.EventPublication#getEvent()
417413
*/
418414
@Override
415+
@SuppressWarnings("null")
419416
public Object getEvent() {
420-
return serializer.deserialize(serializedEvent, eventType);
417+
418+
if (event == null) {
419+
this.event = eventSupplier.get();
420+
}
421+
422+
return event;
421423
}
422424

423425
/*
@@ -481,12 +483,10 @@ public boolean equals(@Nullable Object obj) {
481483
}
482484

483485
return Objects.equals(completionDate, that.completionDate) //
484-
&& Objects.equals(eventType, that.eventType) //
485486
&& Objects.equals(id, that.id) //
486487
&& Objects.equals(listenerId, that.listenerId) //
487488
&& Objects.equals(publicationDate, that.publicationDate) //
488-
&& Objects.equals(serializedEvent, that.serializedEvent) //
489-
&& Objects.equals(serializer, that.serializer);
489+
&& Objects.equals(getEvent(), that.getEvent());
490490
}
491491

492492
/*
@@ -495,7 +495,7 @@ public boolean equals(@Nullable Object obj) {
495495
*/
496496
@Override
497497
public int hashCode() {
498-
return Objects.hash(completionDate, eventType, id, listenerId, publicationDate, serializedEvent, serializer);
498+
return Objects.hash(completionDate, id, listenerId, publicationDate, getEvent());
499499
}
500500
}
501501
}

spring-modulith-events/spring-modulith-events-jdbc/src/test/java/org/springframework/modulith/events/jdbc/JdbcEventPublicationRepositoryIntegrationTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,25 @@ void findsCompletedPublications() {
310310
.isEqualTo(event);
311311
}
312312

313+
@Test // GH-753
314+
void returnsSameEventInstanceFromPublication() {
315+
316+
// An event not implementing equals(…) / hashCode()
317+
var event = new Sample();
318+
319+
// Serialize to whatever
320+
doReturn("").when(serializer).serialize(event);
321+
322+
// Return fresh instances for every deserialization attempt
323+
doAnswer(__ -> new Sample()).when(serializer).deserialize("", Sample.class);
324+
325+
repository.create(TargetEventPublication.of(event, TARGET_IDENTIFIER));
326+
327+
var publication = repository.findIncompletePublications().get(0);
328+
329+
assertThat(publication.getEvent()).isSameAs(publication.getEvent());
330+
}
331+
313332
private TargetEventPublication createPublication(Object event) {
314333

315334
var token = event.toString();
@@ -343,4 +362,6 @@ class MySQL extends TestBase {}
343362
private static final class TestEvent {
344363
String eventId;
345364
}
365+
366+
private static final class Sample {}
346367
}

0 commit comments

Comments
 (0)