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

阿南德J. 卡迪

我正在使用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中实现这一目标。
谢谢,

爸爸

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

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

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

Axon Framework是一个框架,为您提供构建CQRS应用程序的基础。用外行术语来说,CQRS应用程序只是一种体系结构,使您可以将应用程序中执行操作的部分(写)和显示应用程序状态的部分(读)分开。

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

1)CommandBus Command Bus是Axon Framework中的组件,它提供了将命令路由到其各自的Command Handlers的机制。例如,从您的代码示例中,上的@CommandHandler注释MyAggregate意味着在NewCommand创建MyAggregate将调用您的方法。命令总线是使之成为可能的组件。

2)CommandGateway Command GateWay是一个向CommnadBus公开更友好的API的组件。尽管不需要使用网关来分派命令,但是通常这是最简单的选择。

3)EventBus EventBus处理事件的调度机制。就像命令总线对命令所做的一样。因此,当您apply(new BigEvent(command.getId(), command.getCircle()));触发BigEvent事件时,将负责确保确保调用了必要的事件处理程序的事件总线。在您的情况下,您要问的问题是如何在单独的Class中定义事件处理程序,并使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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在一个事件处理程序中引用变量,在另一个事件处理程序中

来自分类Dev

在Typescript中,我如何从同一类中的另一个方法(称为事件处理程序)中调用一个类方法

来自分类Dev

通过单击另一个类中的按钮从面板内的事件处理程序关闭 wx.Dialog

来自分类Dev

处理在另一个类/文件中定义的对象的事件

来自分类Dev

对另一个类中的事件进行承诺

来自分类Dev

为什么在模糊事件处理程序中添加setTimeout可以修复另一个单击处理程序的“遮罩”?

来自分类Dev

Javafx:如何将事件处理程序添加到另一个单独的类

来自分类Dev

Javafx:如何将事件处理程序添加到另一个单独的类

来自分类Dev

如何从另一个处理程序触发angularjs事件处理程序?

来自分类Dev

如何使一个事件在另一个类中触发

来自分类Dev

处理来自另一个类的事件时如何更改变量的值?javascript

来自分类Dev

如何在MainPage.xaml.cs的构造函数中将事件处理程序注册到另一个类的事件

来自分类Dev

为什么将事件处理程序复制到另一个变量

来自分类Dev

jQuery-在将另一个事件作为参数的事件上添加事件处理程序

来自分类Dev

存储一个事件处理程序中的变量以供另一事件处理程序使用

来自分类Dev

调用另一个类初始化程序时如何处理错误?

来自分类Dev

在Swift中用另一个类配置动作处理程序

来自分类Dev

从一个类引发事件并在另一个类中订阅它

来自分类Dev

如何异步处理另一个ajax的beforeSend事件中的ajax回调?

来自分类Dev

从另一个类处理 JDialog

来自分类Dev

C#WPF订阅从UserControl触发的另一个类中的事件

来自分类Dev

通知线程停止-在另一个线程和类中引发事件-C#

来自分类Dev

如何在另一个类中驱动Form事件?

来自分类Dev

调整来自QT中从QWindow继承的另一个类的事件的大小

来自分类Dev

Java按钮事件调用另一个类中的方法

来自分类Dev

如何从另一个事件/类中为整个面板调用无效

来自分类Dev

如何在按钮单击事件中从另一个类调用方法?

来自分类Dev

C#-如何对另一个类中引发的事件做出反应?

来自分类Dev

处理中的一个类中是否可能有另一个循环?

Related 相关文章

  1. 1

    在一个事件处理程序中引用变量,在另一个事件处理程序中

  2. 2

    在Typescript中,我如何从同一类中的另一个方法(称为事件处理程序)中调用一个类方法

  3. 3

    通过单击另一个类中的按钮从面板内的事件处理程序关闭 wx.Dialog

  4. 4

    处理在另一个类/文件中定义的对象的事件

  5. 5

    对另一个类中的事件进行承诺

  6. 6

    为什么在模糊事件处理程序中添加setTimeout可以修复另一个单击处理程序的“遮罩”?

  7. 7

    Javafx:如何将事件处理程序添加到另一个单独的类

  8. 8

    Javafx:如何将事件处理程序添加到另一个单独的类

  9. 9

    如何从另一个处理程序触发angularjs事件处理程序?

  10. 10

    如何使一个事件在另一个类中触发

  11. 11

    处理来自另一个类的事件时如何更改变量的值?javascript

  12. 12

    如何在MainPage.xaml.cs的构造函数中将事件处理程序注册到另一个类的事件

  13. 13

    为什么将事件处理程序复制到另一个变量

  14. 14

    jQuery-在将另一个事件作为参数的事件上添加事件处理程序

  15. 15

    存储一个事件处理程序中的变量以供另一事件处理程序使用

  16. 16

    调用另一个类初始化程序时如何处理错误?

  17. 17

    在Swift中用另一个类配置动作处理程序

  18. 18

    从一个类引发事件并在另一个类中订阅它

  19. 19

    如何异步处理另一个ajax的beforeSend事件中的ajax回调?

  20. 20

    从另一个类处理 JDialog

  21. 21

    C#WPF订阅从UserControl触发的另一个类中的事件

  22. 22

    通知线程停止-在另一个线程和类中引发事件-C#

  23. 23

    如何在另一个类中驱动Form事件?

  24. 24

    调整来自QT中从QWindow继承的另一个类的事件的大小

  25. 25

    Java按钮事件调用另一个类中的方法

  26. 26

    如何从另一个事件/类中为整个面板调用无效

  27. 27

    如何在按钮单击事件中从另一个类调用方法?

  28. 28

    C#-如何对另一个类中引发的事件做出反应?

  29. 29

    处理中的一个类中是否可能有另一个循环?

热门标签

归档