axon聚合

jc3wubiy  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(311)

我正在使用axon和mongodb实现一个使用CQR和事件源模式的微服务。当我第一次启动应用程序时,一切正常。我可以创建订单并更新该订单。重新启动应用程序后,我仍然可以创建订单并更新该订单,但当我尝试更新以前的订单时,axon会抛出 "Command 'com.example.demo.command.command.UpdateOrderCommand' resulted in org.axonframework.modelling.command.AggregateNotFoundException(The aggregate was not found in the event store)" . 我的理论是,当我创建订单时,它会创建orderaggregate的一个示例,并使用该示例进行更新。但是,当我重新启动应用程序时,该示例将消失,而在更新时,我并没有创建orderaggregate示例。以下是我的orderaggregate.java文件:

package com.example.demo.command.aggreagate;

import com.example.demo.command.command.CreateOrderCommand;
import com.example.demo.command.command.UpdateOrderCommand;
import com.example.demo.common.event.OrderCreatedEvent;
import com.example.demo.common.event.OrderUpdatedEvent;
import com.example.demo.query.entity.Address;
import com.example.demo.query.entity.Product;
import lombok.extern.slf4j.Slf4j;
import org.axonframework.commandhandling.CommandHandler;
import org.axonframework.eventsourcing.EventSourcingHandler;
import org.axonframework.modelling.command.AggregateIdentifier;
import org.axonframework.modelling.command.AggregateLifecycle;
import org.axonframework.spring.stereotype.Aggregate;

@Aggregate
@Slf4j
public class OrderAggregate {

    @AggregateIdentifier
    private String _id;
    private String customerId;
    private int quantity;
    private double price;
    private String status;
    private Product product;
    private Address address;
    private String createdAt;
    private String updatedAt;

    public OrderAggregate() {
    }

    @CommandHandler
    public OrderAggregate(CreateOrderCommand command) {
        log.info("CreateOrderCommand received.");

        AggregateLifecycle.apply(new OrderCreatedEvent(command.getId(), command.getCustomerId(),
                command.getQuantity(), command.getPrice(), "ORDERED",
                command.getProduct(), command.getAddress(), command.getCreatedAt(), command.getUpdatedAt()));
    }

    @EventSourcingHandler
    public void on(OrderCreatedEvent event) {
        log.info("An OrderCreatedEvent occurred.");
        this._id = event.getId();
        this.customerId = event.getCustomerId();
        this.quantity = event.getQuantity();
        this.price = event.getPrice();
        this.status = "CREATED";
        this.product = event.getProduct();
        this.address = event.getAddress();
        this.createdAt = event.getCreatedAt();
        this.updatedAt = event.getUpdatedAt();
    }

    @CommandHandler
    public void on(UpdateOrderCommand command) {
        log.info("UpdateOrderCommand received.");

        AggregateLifecycle.apply(new OrderUpdatedEvent(command.getId(), command.getQuantity(), command.getPrice(),
                "UPDATED", command.getProduct(), command.getAddress(), updatedAt));
    }

    @EventSourcingHandler
    public void on(OrderUpdatedEvent event) {
        log.info("An OrderUpdatedEvent occurred.");

        this.quantity = event.getQuantity();
        this.price = event.getPrice();
        this.status = event.getStatus();
        this.product = event.getProduct();
        this.address = event.getAddress();
        this.updatedAt = event.getUpdatedAt();
    }

}

我试着做了 on(UpdateOrderCommand) orderaggregate构造函数。它可以正常工作,但在更新到数据库时会删除updatedat字段。感谢您的帮助!
编辑:这是我的axonfig.java和mongoconfig.java文件:axonfig.java

import com.mongodb.client.MongoClient;
import org.axonframework.eventsourcing.eventstore.EmbeddedEventStore;
import org.axonframework.eventsourcing.eventstore.EventStorageEngine;
import org.axonframework.eventsourcing.eventstore.EventStore;
import org.axonframework.extensions.mongo.DefaultMongoTemplate;
import org.axonframework.extensions.mongo.MongoTemplate;
import org.axonframework.extensions.mongo.eventsourcing.eventstore.MongoEventStorageEngine;
import org.axonframework.spring.config.AxonConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.data.mongodb.core.mapping.event.ValidatingMongoEventListener;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

public class AxonConfig {

    @Bean
    public EmbeddedEventStore eventStore(EventStorageEngine storageEngine, AxonConfiguration configuration) {
        return EmbeddedEventStore.builder()
                .storageEngine(storageEngine)
                .messageMonitor(configuration.messageMonitor(EventStore.class, "eventStore"))
                .build();
    }

    // The `MongoEventStorageEngine` stores each event in a separate MongoDB document
    @Bean
    public EventStorageEngine storageEngine(MongoClient client) {
        return MongoEventStorageEngine.builder().mongoTemplate(DefaultMongoTemplate.builder().mongoDatabase(client).build()).build();
    }
}

mongoconfig.java:

package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.mapping.event.ValidatingMongoEventListener;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

@Configuration
public class MongoConfig {
    @Bean
    ValidatingMongoEventListener validatingMongoEventListener(LocalValidatorFactoryBean validator) {
        return new ValidatingMongoEventListener(validator);
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题