spring-projects / spring-projects/spring-modulith

Allow other ways to recognize events than by class type in JpaEventPublication

Open
#1,682 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

in: event publication registry meta: waiting for feedback
Dominant language
Java
Stars
1.2k
Forks
222
PR merge metrics
No merged PRs in 30d

Description

The problem

I would like to make the event outbox completely independent of my classes, so that I can always safely rename and move classes between packages without having to create migrations for the event_publication table.

Currently, two columns within that table are dependent on class information:

  • listener_id,
  • event_type.

listener_id is easily solvable by always declaring a custom id in @TransactionalEventListener.

Unfortunately, event_type is hardcoded as Class<T> in EventSerializer, JpaEventPublicationRepository and JpaEventPublication itself.

I could probably work around EventSerializer and JpaEventPublicationRepository via some casting shenanigans. However, Hibernate will throw an exception when it tries to retrieve a JpaEventPublication with an eventType class that is not on the class path, so there currently seems to be no path to a solution.

Use case example

I created the following annotation in my project, which I'm using along with a custom EventSerializer:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface OutboxEventMapper {
    String id();
    Class<?> domain();
    Class<?> outbox();
}

It allows me to very simply declare mappers between my internal domain events and their outbox DTOs, e.g.,:

@OutboxEventMapper(
        id = "FileBackupFinishedEvent",
        domain = FileBackupFinishedEvent.class,
        outbox = FileBackupFinishedOutboxEvent.class
)
@Mapper(unmappedSourcePolicy = ReportingPolicy.IGNORE)
public interface FileBackupFinishedOutboxEventMapper {

    FileBackupFinishedOutboxEvent toOutbox(FileBackupFinishedEvent domain);

    FileBackupFinishedEvent toDomain(FileBackupFinishedOutboxEvent outbox);
}

I would like to be able to use the id property to identify which domain event class to deserialize an outbox event into.

Suggested solution

I think the best solution would be to abstract away the eventType as a string and allow clients to provide their own resolvers for it.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading EventSerializer, JpaEventPublicationRepository, and JpaEventPublication, then trace how eventType is mapped to the event_publication table and loaded by Hibernate. Define the resolver boundary and verify that custom string identifiers can be persisted, retrieved, and resolved without requiring the original event class on the classpath.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
backend, databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.