Skip to content

Commit 9c7537f

Browse files
committed
GH-753 - Avoid superfluous, repeated deserialization in JdbcEventPublication.getEvent().
1 parent 4802089 commit 9c7537f

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

@@ -369,7 +370,8 @@ private TargetEventPublication resultSetToPublication(ResultSet rs) throws SQLEx
369370
var listenerId = rs.getString("LISTENER_ID");
370371
var serializedEvent = rs.getString("SERIALIZED_EVENT");
371372

372-
return new JdbcEventPublication(id, publicationDate, listenerId, serializedEvent, eventClass, serializer,
373+
return new JdbcEventPublication(id, publicationDate, listenerId,
374+
() -> serializer.deserialize(serializedEvent, eventClass),
373375
completionDate == null ? null : completionDate.toInstant());
374376
}
375377

@@ -414,11 +416,10 @@ private static class JdbcEventPublication implements TargetEventPublication {
414416
private final UUID id;
415417
private final Instant publicationDate;
416418
private final String listenerId;
417-
private final String serializedEvent;
418-
private final Class<?> eventType;
419+
private final Supplier<Object> eventSupplier;
419420

420-
private final EventSerializer serializer;
421421
private @Nullable Instant completionDate;
422+
private @Nullable Object event;
422423

423424
/**
424425
* @param id must not be {@literal null}.
@@ -429,22 +430,17 @@ private static class JdbcEventPublication implements TargetEventPublication {
429430
* @param serializer must not be {@literal null}.
430431
* @param completionDate can be {@literal null}.
431432
*/
432-
public JdbcEventPublication(UUID id, Instant publicationDate, String listenerId, String serializedEvent,
433-
Class<?> eventType, EventSerializer serializer, @Nullable Instant completionDate) {
433+
public JdbcEventPublication(UUID id, Instant publicationDate, String listenerId, Supplier<Object> event,
434+
@Nullable Instant completionDate) {
434435

435436
Assert.notNull(id, "Id must not be null!");
436437
Assert.notNull(publicationDate, "Publication date must not be null!");
437438
Assert.hasText(listenerId, "Listener id must not be null or empty!");
438-
Assert.hasText(serializedEvent, "Serialized event must not be null or empty!");
439-
Assert.notNull(eventType, "Event type must not be null!");
440-
Assert.notNull(serializer, "EventSerializer must not be null!");
441439

442440
this.id = id;
443441
this.publicationDate = publicationDate;
444442
this.listenerId = listenerId;
445-
this.serializedEvent = serializedEvent;
446-
this.eventType = eventType;
447-
this.serializer = serializer;
443+
this.eventSupplier = event;
448444
this.completionDate = completionDate;
449445
}
450446

@@ -462,8 +458,14 @@ public UUID getIdentifier() {
462458
* @see org.springframework.modulith.events.EventPublication#getEvent()
463459
*/
464460
@Override
461+
@SuppressWarnings("null")
465462
public Object getEvent() {
466-
return serializer.deserialize(serializedEvent, eventType);
463+
464+
if (event == null) {
465+
this.event = eventSupplier.get();
466+
}
467+
468+
return event;
467469
}
468470

469471
/*
@@ -527,12 +529,10 @@ public boolean equals(@Nullable Object obj) {
527529
}
528530

529531
return Objects.equals(completionDate, that.completionDate) //
530-
&& Objects.equals(eventType, that.eventType) //
531532
&& Objects.equals(id, that.id) //
532533
&& Objects.equals(listenerId, that.listenerId) //
533534
&& Objects.equals(publicationDate, that.publicationDate) //
534-
&& Objects.equals(serializedEvent, that.serializedEvent) //
535-
&& Objects.equals(serializer, that.serializer);
535+
&& Objects.equals(getEvent(), that.getEvent());
536536
}
537537

538538
/*
@@ -541,7 +541,7 @@ public boolean equals(@Nullable Object obj) {
541541
*/
542542
@Override
543543
public int hashCode() {
544-
return Objects.hash(completionDate, eventType, id, listenerId, publicationDate, serializedEvent, serializer);
544+
return Objects.hash(completionDate, id, listenerId, publicationDate, getEvent());
545545
}
546546
}
547547
}

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
@@ -323,6 +323,25 @@ void marksPublicationAsCompletedById() {
323323
.containsExactly(publication.getIdentifier());
324324
}
325325

326+
@Test // GH-753
327+
void returnsSameEventInstanceFromPublication() {
328+
329+
// An event not implementing equals(…) / hashCode()
330+
var event = new Sample();
331+
332+
// Serialize to whatever
333+
doReturn("").when(serializer).serialize(event);
334+
335+
// Return fresh instances for every deserialization attempt
336+
doAnswer(__ -> new Sample()).when(serializer).deserialize("", Sample.class);
337+
338+
repository.create(TargetEventPublication.of(event, TARGET_IDENTIFIER));
339+
340+
var publication = repository.findIncompletePublications().get(0);
341+
342+
assertThat(publication.getEvent()).isSameAs(publication.getEvent());
343+
}
344+
326345
abstract String table();
327346

328347
private TargetEventPublication createPublication(Object event) {
@@ -422,4 +441,6 @@ class MysqlWithNoDefinedSchemaName extends WithNoDefinedSchemaName {}
422441
private static final class TestEvent {
423442
String eventId;
424443
}
444+
445+
private static final class Sample {}
425446
}

0 commit comments

Comments
 (0)