我正在使用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中实现这一目标 谢谢,
答案 0 :(得分:8)
简答:您需要告诉Axon您的EventContainer
班级可以处理发布到事件总线的事件。
AnnotationEventListenerAdapter.subscribe(new EventContainer(), eventBus);
更长的答案: 为了实现您想要做的事情,退一步了解Axon提供的构建块以构建CQRS应用程序将有所帮助......
Axon Framework是一个框架,为您提供构建CQRS应用程序的构建块。在外行人的术语中,CQRS应用程序只是一种架构,允许您将执行操作(写入)的应用程序部分与显示应用程序状态(读取)的部分分开。
为此,Axon提供了几个构建块。
1)CommandBus
命令总线是Axon Framework中的组件,它提供了将命令路由到各自的命令处理程序的机制。例如,在您的代码示例中,@CommandHandler
上的MyAggregate
注释意味着在创建NewCommand
时,将调用您的MyAggregate
方法。命令总线是使这成为可能的组件。
2)CommandGateway 命令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