2

私は axon 2.3.1 を使用しています。集約クラスが 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

簡単な回答:EventContainerクラスがイベント バスに発行されたイベントを処理できることを Axon に伝える必要があります。

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

より長い回答: あなたがやりたいことを達成するには、CQRS アプリケーションを構築するために Axon が提供するビルディング ブロックを理解するために一歩下がってください...

Axon Framework は、CQRS アプリケーションを構築するためのビルディング ブロックを提供するフレームワークです。CQRS アプリケーションは、平たく言えば、アクションを実行するアプリケーションの部分 (書き込み) とアプリケーションの状態を表示する部分 (読み取り) を分離できるアーキテクチャです。

これを行うために、Axon はいくつかのビルディング ブロックを提供します。

1) CommandBus Command Bus は、Axon Framework 内のコンポーネントであり、コマンドをそれぞれのコマンド ハンドラーにルーティングするメカニズムを提供します。たとえば、コード サンプルの@CommandHandler注釈 onは、作成時にメソッドが呼び出されることをMyAggregate意味します。コマンド バスは、これを可能にするコンポーネントです。NewCommandMyAggregate

2) CommandGateway Command GateWay は、より使いやすい API を CommnadBus に公開するコンポーネントです。コマンドをディスパッチするためにゲートウェイを使用する必要はありませんが、一般的にゲートウェイを使用するのが最も簡単なオプションです。

3) EventBus EventBus は、イベントのディスパッチ メカニズムを処理します。コマンドバスがコマンドに対して行うのと同じように。そのため、どのイベントがapply(new BigEvent(command.getId(), command.getCircle()));発生するBigEventかがわかった場合、必要なイベント ハンドラーが確実に呼び出されるようにする責任があるのはイベント バスです。あなたの場合、あなたが求めている質問は、イベントハンドラーを別のクラスで定義し、Axon がイベントをそれらにルーティングできるようにする方法です。

これは非常に簡単です。Spring を使用しておらず、Axon コンポーネントを手動でセットアップし、メソッドで処理したい をNewCommandトリガーするを作成していると仮定します。これを行う方法は次のようになります。SmallEventEventContainer#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 に答える