25
25
import java .util .Objects ;
26
26
import java .util .Optional ;
27
27
import java .util .UUID ;
28
+ import java .util .function .Supplier ;
28
29
import java .util .stream .Collectors ;
29
30
import java .util .stream .IntStream ;
30
31
@@ -323,7 +324,8 @@ private TargetEventPublication resultSetToPublication(ResultSet rs) throws SQLEx
323
324
var listenerId = rs .getString ("LISTENER_ID" );
324
325
var serializedEvent = rs .getString ("SERIALIZED_EVENT" );
325
326
326
- return new JdbcEventPublication (id , publicationDate , listenerId , serializedEvent , eventClass , serializer ,
327
+ return new JdbcEventPublication (id , publicationDate , listenerId ,
328
+ () -> serializer .deserialize (serializedEvent , eventClass ),
327
329
completionDate == null ? null : completionDate .toInstant ());
328
330
}
329
331
@@ -368,11 +370,10 @@ private static class JdbcEventPublication implements TargetEventPublication {
368
370
private final UUID id ;
369
371
private final Instant publicationDate ;
370
372
private final String listenerId ;
371
- private final String serializedEvent ;
372
- private final Class <?> eventType ;
373
+ private final Supplier <Object > eventSupplier ;
373
374
374
- private final EventSerializer serializer ;
375
375
private @ Nullable Instant completionDate ;
376
+ private @ Nullable Object event ;
376
377
377
378
/**
378
379
* @param id must not be {@literal null}.
@@ -383,22 +384,17 @@ private static class JdbcEventPublication implements TargetEventPublication {
383
384
* @param serializer must not be {@literal null}.
384
385
* @param completionDate can be {@literal null}.
385
386
*/
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 ) {
388
389
389
390
Assert .notNull (id , "Id must not be null!" );
390
391
Assert .notNull (publicationDate , "Publication date must not be null!" );
391
392
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!" );
395
393
396
394
this .id = id ;
397
395
this .publicationDate = publicationDate ;
398
396
this .listenerId = listenerId ;
399
- this .serializedEvent = serializedEvent ;
400
- this .eventType = eventType ;
401
- this .serializer = serializer ;
397
+ this .eventSupplier = event ;
402
398
this .completionDate = completionDate ;
403
399
}
404
400
@@ -416,8 +412,14 @@ public UUID getIdentifier() {
416
412
* @see org.springframework.modulith.events.EventPublication#getEvent()
417
413
*/
418
414
@ Override
415
+ @ SuppressWarnings ("null" )
419
416
public Object getEvent () {
420
- return serializer .deserialize (serializedEvent , eventType );
417
+
418
+ if (event == null ) {
419
+ this .event = eventSupplier .get ();
420
+ }
421
+
422
+ return event ;
421
423
}
422
424
423
425
/*
@@ -481,12 +483,10 @@ public boolean equals(@Nullable Object obj) {
481
483
}
482
484
483
485
return Objects .equals (completionDate , that .completionDate ) //
484
- && Objects .equals (eventType , that .eventType ) //
485
486
&& Objects .equals (id , that .id ) //
486
487
&& Objects .equals (listenerId , that .listenerId ) //
487
488
&& Objects .equals (publicationDate , that .publicationDate ) //
488
- && Objects .equals (serializedEvent , that .serializedEvent ) //
489
- && Objects .equals (serializer , that .serializer );
489
+ && Objects .equals (getEvent (), that .getEvent ());
490
490
}
491
491
492
492
/*
@@ -495,7 +495,7 @@ public boolean equals(@Nullable Object obj) {
495
495
*/
496
496
@ Override
497
497
public int hashCode () {
498
- return Objects .hash (completionDate , eventType , id , listenerId , publicationDate , serializedEvent , serializer );
498
+ return Objects .hash (completionDate , id , listenerId , publicationDate , getEvent () );
499
499
}
500
500
}
501
501
}
0 commit comments