2

我正在使用 axon 2.3.1 ,我有一个聚合类

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
   }

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

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
   }

我尝试将它们放在另一个类中,但没有触发这些事件。
知道如何在 AXON 中实现这一点。
谢谢,

4

1 回答 1

10

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

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

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

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

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

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

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

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

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

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

于 2015-03-13T16:27:14.120 回答