2525import java .util .Objects ;
2626import java .util .Optional ;
2727import java .util .UUID ;
28+ import java .util .function .Supplier ;
2829import java .util .stream .Collectors ;
2930import 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}
0 commit comments