Axon serialization "error"

I’m currently using generated Avro objects to send some of my events in my application, so I had to configure this default serializer:

less

Copiar código

    @Bean
    @Primary
    public Serializer defaultSerializer() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JavaTimeModule());
        objectMapper.addMixIn(Object.class, JacksonIgnoreAvroPropertiesMixIn.class);

        return JacksonSerializer.builder().objectMapper(objectMapper).build();
    }

But when I try to send those events, the @EventSourcingHandler works normally, but the @EventHandler doesn’t (it doesn’t throw any exception or log anything at the debug level). What can I do to use Avro objects as my events?

You have a generated java class (via apache-avro-maven-plugin), you have an instance (via newBuilder()) of that class and you serialize it via jackson/json?

When I faced this problem 2 years ago, I wrote this jackson-module: jackson-avro-module/src/test/java/io/toolisticon/jackson/module/JacksonAvroModuleJavaTest.java at develop · toolisticon/jackson-avro-module (github.com)

I’m creating an Avro object and then applying it to AggregateLifecycle:

ProductCreatedEvent event = ProductCreatedEvent
        .newBuilder()
        .setProductId(command.getProductId())
        .setName(command.getName())
        .setDescription(command.getDescription())
        .setPrice(command.getPrice())
        .setSellerId(command.getSellerId())
        .setCategoryId(command.getCategoryId())
        .setInventoryCount(command.getInventoryCount())
        .setCreatedAt(command.getCreatedAt() != null ? command.getCreatedAt().toInstant(ZoneOffset.UTC) : null)
        .setUpdatedAt(command.getUpdatedAt() != null ? command.getUpdatedAt().toInstant(ZoneOffset.UTC) : null)
        .build();

AggregateLifecycle.apply(event);

This event is supposed to be handled in my event handler:

@EventHandler
public void on(ProductCreatedEvent event) {
    ProductView productView = ProductView.builder()
            .id(event.getProductId())
            .name(event.getName())
            .description(event.getDescription())
            .price(event.getPrice())
            .sellerId(event.getSellerId())
            .categoryId(event.getCategoryId())
            .inventoryCount(event.getInventoryCount())
            .updatedAt(DateTimeConversion.fromInstant(event.getUpdatedAt()))
            .createdAt(DateTimeConversion.fromInstant(event.getCreatedAt()))
            .build();
}

However, the event handler is not being triggered. When using standard Java classes, it works fine, but with Avro, it doesn’t. I tried using JacksonAvroModule, but the issue persists.

The apply is for the write model, not for the read model. I’m not really sure what you want to do here. Is the apply part of the aggregate? Or do you just want to add an event to the store? In the latter case you should use a publish on een event store instead. I don’t think currently the event is persistent. Hence the event handler is never triggered.

What I’m trying to do is have my aggregate apply this event to itself, so that my @EventSourcingHandler handles the event, and my @EventHandler handles the same event. It worked before I started using Avro objects. The @EventSourcingHandler is working, but my @EventHandler on my projector isn’t.

You can see what I’m talking about here: E-commerce/ProductService/src/main/java/com/product/service at main · Murilo358/E-commerce · GitHub

I would expect at least something logged. For now it’s still unclear if the event is persisted at all, so if it’s an error storing the event, or if there is an error reading the event. I also fail to understand why Jackson should be involved, and why you wouldn’t just use the class name, and then use from bytebuffer directly. But that’s likely to prevent having to add all the classes explicitly to the serializer? If you use that method on the stored bytes through, does it work?