Permalink
Browse files

Removing seperate command handler classes. Made Aggregates able to di…

…rectly serve as command handlers
  • Loading branch information...
dadepo committed Jul 21, 2018
1 parent e4d2e65 commit 3e9872c10610498c4ea540af48504fcbcb76c62d
@@ -2,6 +2,7 @@
import exploringaxon.model.Account;
import org.axonframework.commandhandling.SimpleCommandBus;
import org.axonframework.commandhandling.annotation.AggregateAnnotationCommandHandler;
import org.axonframework.commandhandling.gateway.DefaultCommandGateway;
import org.axonframework.contextsupport.spring.AnnotationDriven;
import org.axonframework.domain.EventMessage;
@@ -175,4 +176,18 @@ public DefaultCommandGateway commandGateway() {
eventSourcingRepository.setEventBus(clusteringEventBus());
return eventSourcingRepository;
}
/**
* Component that allows an aggregate to directly act as command handlers for commands.
*
* @return an instance of {@link AggregateAnnotationCommandHandler}
*/
@Bean
public AggregateAnnotationCommandHandler aggregateAnnotationCommandHandler(){
AggregateAnnotationCommandHandler<Account> handler = new AggregateAnnotationCommandHandler<>(Account.class, eventSourcingRepository());
for (String supportedCommand : handler.supportedCommands()) {
commandBus().subscribe(supportedCommand, handler);
}
return handler;
}
}
@@ -1,11 +1,15 @@
package exploringaxon.api.command;
import org.axonframework.commandhandling.annotation.TargetAggregateIdentifier;
/**
* Created by Dadepo Aderemi.
*/
public class CreditAccountCommand {
@TargetAggregateIdentifier
private final String account;
private final Double amount;
public CreditAccountCommand(String account, Double amount) {
@@ -1,11 +1,15 @@
package exploringaxon.api.command;
import org.axonframework.commandhandling.annotation.TargetAggregateIdentifier;
/**
* Created by Dadepo Aderemi.
*/
public class DebitAccountCommand {
@TargetAggregateIdentifier
private final String account;
private final Double amount;
public DebitAccountCommand(String account, Double amount) {

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -1,8 +1,11 @@
package exploringaxon.model;
import exploringaxon.api.command.CreditAccountCommand;
import exploringaxon.api.command.DebitAccountCommand;
import exploringaxon.api.event.AccountCreatedEvent;
import exploringaxon.api.event.AccountCreditedEvent;
import exploringaxon.api.event.AccountDebitedEvent;
import org.axonframework.commandhandling.annotation.CommandHandler;
import org.axonframework.eventsourcing.annotation.AbstractAnnotatedAggregateRoot;
import org.axonframework.eventsourcing.annotation.AggregateIdentifier;
import org.axonframework.eventsourcing.annotation.EventSourcingHandler;
@@ -39,10 +42,11 @@ public void applyAccountCreation(AccountCreatedEvent event) {
* Cannot debit with a negative amount
* Cannot debit with amount that leaves the account balance in a negative state
*
* @param debitAmount
* @param debitAccountCommand
*/
public void debit(Double debitAmount) {
@CommandHandler
public void debit(DebitAccountCommand debitAccountCommand) {
Double debitAmount = debitAccountCommand.getAmount();
if (Double.compare(debitAmount, 0.0d) > 0 &&
this.balance - debitAmount > -1) {
/**
@@ -55,7 +59,6 @@ public void debit(Double debitAmount) {
} else {
throw new IllegalArgumentException("Cannot debit with the amount");
}
}
@EventSourcingHandler
@@ -73,11 +76,13 @@ private void applyDebit(AccountDebitedEvent event) {
* Cannot credit with a negative amount
* Cannot credit with more than a million amount (You laundering money?)
*
* @param creditAmount
* @param creditAccountCommand
*/
public void credit(Double creditAmount) {
if (Double.compare(creditAmount, 0.0d) > 0 &&
Double.compare(creditAmount, 1000000) < 0) {
@CommandHandler
public void credit(CreditAccountCommand creditAccountCommand) {
Double creditAmount = creditAccountCommand.getAmount();
if (Double.compare(creditAmount, 0.0d) > 0 && Double.compare(creditAmount, 1000000) < 0) {
/**
* Instead of changing the state directly we apply an event
* that conveys what happened.
@@ -3,6 +3,8 @@
import exploringaxon.api.command.CreditAccountCommand;
import exploringaxon.api.command.DebitAccountCommand;
import exploringaxon.replay.AccountCreditedReplayEventHandler;
import org.axonframework.commandhandling.GenericCommandMessage;
import org.axonframework.commandhandling.callbacks.LoggingCallback;
import org.axonframework.commandhandling.gateway.CommandGateway;
import org.axonframework.eventhandling.replay.ReplayingCluster;
import org.springframework.beans.factory.annotation.Autowired;
@@ -56,7 +58,8 @@ public void doDebit(@RequestParam("acc") String accountNumber, @RequestParam("am
@ResponseBody
public void doCredit(@RequestParam("acc") String accountNumber, @RequestParam("amount") double amount) {
CreditAccountCommand creditAccountCommandCommand = new CreditAccountCommand(accountNumber, amount);
commandGateway.send(creditAccountCommandCommand);
GenericCommandMessage<CreditAccountCommand> message = new GenericCommandMessage<>(creditAccountCommandCommand);
commandGateway.send(creditAccountCommandCommand, new LoggingCallback(message));
}
@RequestMapping("/events")
@@ -8,10 +8,10 @@
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
/**
* Created by Dadepo Aderemi.
@@ -27,11 +27,9 @@
public List<Map<String, Double>> getAccounts() {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
List<Map<String, Double>> queryResult = jdbcTemplate.query("SELECT * from account_view ORDER BY account_no", (rs, rowNum) -> {
return new HashMap<String, Double>() {{
put(rs.getString("ACCOUNT_NO"), rs.getDouble("BALANCE"));
}};
});
List<Map<String, Double>> queryResult = jdbcTemplate.query("SELECT * from account_view ORDER BY account_no", (rs, rowNum) -> new HashMap<String, Double>() {{
put(rs.getString("ACCOUNT_NO"), rs.getDouble("BALANCE"));
}});
return queryResult;
}
@@ -5,8 +5,6 @@
import exploringaxon.api.event.AccountCreatedEvent;
import exploringaxon.api.event.AccountCreditedEvent;
import exploringaxon.api.event.AccountDebitedEvent;
import exploringaxon.commandhandler.CreditAccountHandler;
import exploringaxon.commandhandler.DebitAccountHandler;
import exploringaxon.model.Account;
import org.axonframework.test.FixtureConfiguration;
import org.axonframework.test.Fixtures;
@@ -22,15 +20,12 @@
public class ExploringAxonApplicationTests {
private FixtureConfiguration fixture;
private String accountNo;
private String accountNo = "test-acc";
@Before
public void setUp() {
fixture = Fixtures.newGivenWhenThenFixture(Account.class);
fixture.registerAnnotatedCommandHandler(new CreditAccountHandler(fixture.getRepository()))
.registerAnnotatedCommandHandler(new DebitAccountHandler(fixture.getRepository()));
accountNo = "test-acc";
}
}
@Test
public void testFirstDeposit() {

0 comments on commit 3e9872c

Please sign in to comment.