另一个 class 中的轴突事件处理程序

axon event handler in another class

我用的是axon 2.3.1,我有一个聚合体class

public class MyAggregate extends AbstractAnnotatedAggregateRoot<MBAggregate>   {


@AggregateIdentifier
private MyId Id;
private Circle circle;
EventDispatcher a=new EventDispatcher();

public MyAggregate() {
}

@CommandHandler
public MyAggregate(NewCommand command ) {
    apply(new SmallEvent(command.getId(), command.getCircle()));
}

@CommandHandler
public MyAggregate( StoreDestinationsCommand command ) {
    apply(new BigEvent(command.getId(), command.getCircle()));
}
//And some event handlers like

   @EventHandler                                                                                                                             
   public void onSmallEvent(SmallEvent event)    
   {
    //Some logic here
   }
   @EventHandler                                                                                                                             
   public void onBigEvent(BigEvent event)    
   {
    //Some logic here
   }

现在我希望这些事件处理程序包含在其他一些 class 中,并在该事件被触发时被调用

public class EventContainer {


private static final long serialVersionUID = -6640657879730853388L;



  @EventHandler                                                                                                                             
   public void onSmallEvent(SmallEvent event)    
   {
    //Some logic here
   }
   @EventHandler                                                                                                                             
   public void onBigEvent(BigEvent event)    
   {
    //Some logic here
   }

我尝试将它们放在另一个 class 但这些事件没有被触发。
知道我怎样才能在 AXON 中实现这一点。
谢谢,

简短回答:您需要告诉 Axon 您的 EventContainer class 可以处理发布到事件总线的事件。

   AnnotationEventListenerAdapter.subscribe(new EventContainer(), eventBus);

更长的答案: 要实现您想做的事情,退后一步了解 Axon 提供的用于构建 CQRS 应用程序的构建块将有所帮助...

Axon Framework 是一个框架,可为您提供构建 CQRS 应用程序的构建块。而 CQRS 应用程序,通俗地说就是一种架构,它允许您将应用程序中执行操作(写入)的部分与显示应用程序状态(读取)的部分分开。

为此,Axon 提供了几个构建块。

1) 命令总线 命令总线是 Axon Framework 中的组件,它提供了将命令路由到它们各自的命令处理程序的机制。例如,根据您的代码示例,MyAggregate 上的 @CommandHandler 批注意味着当创建 NewCommand 时,将调用您的 MyAggregate 方法。命令总线是使这成为可能的组件。

2) 命令网关 Command GateWay 是一个向 CommnadBus 暴露更友好 API 的组件。虽然您不需要使用网关来发送命令,但它通常是最简单的选择。

3) 事件总线 EventBus 处理事件的调度机制。就像命令总线对命令所做的那样。因此,当您有 apply(new BigEvent(command.getId(), command.getCircle())); 触发 BigEvent 时,事件总线负责确保调用必要的事件处理程序。在您的情况下,您要问的问题是如何在单独的 Class 中定义事件处理程序,并且仍然让 Axon 能够将事件路由给它们。

这很简单。我假设您没有使用 Spring 并且您正在手动手动设置 Axon 组件并创建 NewCommand 触发您想要在 [=21] 中处理的 SmallEvent =] 方法。完成此操作的方法可能如下所示:

public class FireCommandAndCaptureEventInAnotherClass {

public static void main(String[] args) {
    // We use the simple Command Bus.
    // There are different implementation available. For example axon provides a distributed command bus that can be used to distribute commands over multiple nodes
    CommandBus commandBus = new SimpleCommandBus();

    // The friendlier API to send commands with
    CommandGateway commandGateway = new DefaultCommandGateway(commandBus);

    // You may skip this as it may not pertain to your question but since we are using event sourcing, we need a place to store the events. we'll store Events on the FileSystem, in the "events" folder
    EventStore eventStore = new FileSystemEventStore(new SimpleEventFileResolver(new File("./events")));

    // a Simple Event Bus will do
    EventBus eventBus = new SimpleEventBus();

    // You may skip this as it may not pertain to your question but since event sourcing is used in this example we need to configure the repository: an event sourcing repository.
    EventSourcingRepository<MyAggregate> repository = new EventSourcingRepository<MyAggregate>(MyAggregate.class,
                                                                                         eventStore); 

    // Sets the event bus to which newly stored events should be published 
    repository.setEventBus(eventBus);

    // Tells Axon that MyAggregate can handle commands
    AggregateAnnotationCommandHandler.subscribe(MyAggregate.class, repository, commandBus);

    // This is the part you need. With this We register an event listener to be able to handle events published on to an event bus. In this case EventContainer.
    AnnotationEventListenerAdapter.subscribe(new EventContainer(), eventBus);

    // and let's send some Commands on the CommandBus.
    commandGateway.send(id, circle);
  }
}

使用此设置,EventContainer 中的处理程序将能够对从 MyAggregate

触发的事件做出反应