Annotation OutboxEvent

  • All Implemented Interfaces:

    @Target(allowedTargets = {AnnotationTarget.CLASS}) 
    public @interface OutboxEvent
    
                        

    Marks an event class for automatic outbox persistence.

    When an event annotated with @OutboxEvent is published via Spring's ApplicationEventPublisher, it will be automatically persisted to the outbox database before (optionally) being delivered to event listeners. This ensures durability of events and enables reliable event propagation.

    Recommended Usage with @DomainEvents: The best practice is to use @DomainEvents in your Aggregate Root combined with @OutboxEvent on your event classes. This way, Spring Data JPA automatically publishes domain events after a successful transaction commit, and the OutboxEvent multicaster persists them to the outbox.

    @Entity
    class Customer {
        @DomainEvents
        fun domainEvents(): Collection<Any> = this.events
    
        fun activate() {
            this.events.add(CustomerActivatedEvent(this.id))
        }
    }
    
    @OutboxEvent(aggregateId = "customerId")
    data class CustomerActivatedEvent(val customerId: String) : DomainEvent

    Then publish via repository.save():

    @Transactional
    fun activateCustomer(customerId: String) {
        val customer = repository.findById(customerId)
        customer.activate()
        repository.save(customer)  // triggers @DomainEvents -> @OutboxEvent -> outbox persistence
    }
    Since:

    0.3.0

    Author:

    Roland Beisel

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Constructor Summary

      Constructors 
      Constructor Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

    • Method Detail