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
@@ -369,7 +370,8 @@ private TargetEventPublication resultSetToPublication(ResultSet rs) throws SQLEx
369
370
var listenerId = rs .getString ("LISTENER_ID" );
370
371
var serializedEvent = rs .getString ("SERIALIZED_EVENT" );
371
372
372
- return new JdbcEventPublication (id , publicationDate , listenerId , serializedEvent , eventClass , serializer ,
373
+ return new JdbcEventPublication (id , publicationDate , listenerId ,
374
+ () -> serializer .deserialize (serializedEvent , eventClass ),
373
375
completionDate == null ? null : completionDate .toInstant ());
374
376
}
375
377
@@ -414,11 +416,10 @@ private static class JdbcEventPublication implements TargetEventPublication {
414
416
private final UUID id ;
415
417
private final Instant publicationDate ;
416
418
private final String listenerId ;
417
- private final String serializedEvent ;
418
- private final Class <?> eventType ;
419
+ private final Supplier <Object > eventSupplier ;
419
420
420
- private final EventSerializer serializer ;
421
421
private @ Nullable Instant completionDate ;
422
+ private @ Nullable Object event ;
422
423
423
424
/**
424
425
* @param id must not be {@literal null}.
@@ -429,22 +430,17 @@ private static class JdbcEventPublication implements TargetEventPublication {
429
430
* @param serializer must not be {@literal null}.
430
431
* @param completionDate can be {@literal null}.
431
432
*/
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 ) {
434
435
435
436
Assert .notNull (id , "Id must not be null!" );
436
437
Assert .notNull (publicationDate , "Publication date must not be null!" );
437
438
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!" );
441
439
442
440
this .id = id ;
443
441
this .publicationDate = publicationDate ;
444
442
this .listenerId = listenerId ;
445
- this .serializedEvent = serializedEvent ;
446
- this .eventType = eventType ;
447
- this .serializer = serializer ;
443
+ this .eventSupplier = event ;
448
444
this .completionDate = completionDate ;
449
445
}
450
446
@@ -462,8 +458,14 @@ public UUID getIdentifier() {
462
458
* @see org.springframework.modulith.events.EventPublication#getEvent()
463
459
*/
464
460
@ Override
461
+ @ SuppressWarnings ("null" )
465
462
public Object getEvent () {
466
- return serializer .deserialize (serializedEvent , eventType );
463
+
464
+ if (event == null ) {
465
+ this .event = eventSupplier .get ();
466
+ }
467
+
468
+ return event ;
467
469
}
468
470
469
471
/*
@@ -527,12 +529,10 @@ public boolean equals(@Nullable Object obj) {
527
529
}
528
530
529
531
return Objects .equals (completionDate , that .completionDate ) //
530
- && Objects .equals (eventType , that .eventType ) //
531
532
&& Objects .equals (id , that .id ) //
532
533
&& Objects .equals (listenerId , that .listenerId ) //
533
534
&& Objects .equals (publicationDate , that .publicationDate ) //
534
- && Objects .equals (serializedEvent , that .serializedEvent ) //
535
- && Objects .equals (serializer , that .serializer );
535
+ && Objects .equals (getEvent (), that .getEvent ());
536
536
}
537
537
538
538
/*
@@ -541,7 +541,7 @@ public boolean equals(@Nullable Object obj) {
541
541
*/
542
542
@ Override
543
543
public int hashCode () {
544
- return Objects .hash (completionDate , eventType , id , listenerId , publicationDate , serializedEvent , serializer );
544
+ return Objects .hash (completionDate , id , listenerId , publicationDate , getEvent () );
545
545
}
546
546
}
547
547
}
0 commit comments