我正在使用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] 删除。
我来说两句