I have a question regarding returning values from a method on a custom command gateway. Briefly, it appears I cannot return the aggregate directly from its constructor. When I try this, the aggregate is constructed but the command gateway method always returns null.
In detail, say I have a simple custom command gateway :
public interface MyCommandGateway {
MyAggregate createMyAggregate(CreateMyAggregateCommand command);
}
In my aggregate, I attach the command handler to the constructor :
public class MyAggregate extends AbstractAnnotatedAggregateRoot<MyAggregateId> {
@AggregateIdentifier
private MyAggregateId myAggregateId;
@CommandHandler
public MyAggregate(CreateMyAggregateCommand command) {
apply(new MyAggregateCreatedEvent(command.myAggregateId()));
}
@EventHandler
protected void handle(MyAggregateCreatedEvent event) {
myAggregateId = event.myAggregateId();
...
}
}
I am using the default command bus and event bus. I'm using Spring to configure the custom command gateway factory, enabling injection of the gateway where needed :
<bean id="myCommandGatewayFactory" class="org.axonframework.commandhandling.gateway.CommandGatewayFactoryBean">
<property name="commandBus" ref="commandBus"/>
<property name="gatewayInterface" value="org.steffan.MyCommandGateway"/>
</bean>
In the main program, I can quite happily cause the aggregate to be created, but the command gateway does not return it :
public class Main {
...
MyAggregate myAggregate = myCommandGateway.createMyAggregate(new CreateMyAggregateCommand(new AggregateId()));
// myAggregate is always null
}
Can anyone point out where I may be going wrong? I struggled to find any examples that use custom command gateways.
Thanks,
Steffan
Click Travel LtdTaking business travel and expenses one step further
Winner of "Best Business Travel Management Company" at the Business Travel Awards (2012 & 2009) and the Travel Trade Gazette Awards (2011). We're also ranked 22nd in the 2012 Sunday Times Hiscox Tech Track 100.
Think: Do you really need to print this email?