Skip to content

Commit 1474a50

Browse files
author
Serdar Hamzaoğulları
committed
Example done with app class
1 parent 9f612ec commit 1474a50

22 files changed

+708
-0
lines changed

event-sourcing/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,12 @@
3939
<artifactId>junit</artifactId>
4040
<scope>test</scope>
4141
</dependency>
42+
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
43+
<dependency>
44+
<groupId>com.google.code.gson</groupId>
45+
<artifactId>gson</artifactId>
46+
<version>2.8.1</version>
47+
</dependency>
48+
4249
</dependencies>
4350
</project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.iluwatar.event.sourcing.api;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* Created by serdarh on 06.08.2017.
7+
*/
8+
public abstract class DomainEvent implements Serializable {
9+
private final long sequenceId;
10+
private final long createdTime;
11+
private boolean replica = false;
12+
private final String eventClassName;
13+
14+
public DomainEvent(long sequenceId, long createdTime, String eventClassName) {
15+
this.sequenceId = sequenceId;
16+
this.createdTime = createdTime;
17+
this.eventClassName = eventClassName;
18+
}
19+
20+
public long getSequenceId() {
21+
return sequenceId;
22+
}
23+
24+
public long getCreatedTime() {
25+
return createdTime;
26+
}
27+
28+
public boolean isReplica() {
29+
return replica;
30+
}
31+
32+
public void setReplica(boolean replica) {
33+
this.replica = replica;
34+
}
35+
36+
public abstract void process();
37+
38+
public String getEventClassName() {
39+
return eventClassName;
40+
}
41+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.iluwatar.event.sourcing.api;
2+
3+
/**
4+
* Created by serdarh on 06.08.2017.
5+
*/
6+
public interface EventProcessor {
7+
void process(DomainEvent domainEvent);
8+
void setPrecessorJournal(ProcessorJournal precessorJournal);
9+
void addExternalEventListener(ExternalEventListener externalEventListener);
10+
void recover();
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.iluwatar.event.sourcing.api;
2+
3+
/**
4+
* Created by serdarh on 06.08.2017.
5+
*/
6+
public interface ExternalEventListener {
7+
void notify(DomainEvent domainEvent);
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.iluwatar.event.sourcing.api;
2+
3+
/**
4+
* Created by serdarh on 06.08.2017.
5+
*/
6+
public interface ProcessorJournal {
7+
void write(DomainEvent domainEvent);
8+
void reset();
9+
DomainEvent readNext();
10+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.iluwatar.event.sourcing.app;
2+
3+
import com.iluwatar.event.sourcing.journal.JsonFileJournal;
4+
import com.iluwatar.event.sourcing.processor.DomainEventProcessor;
5+
import com.iluwatar.event.sourcing.service.AccountService;
6+
import com.iluwatar.event.sourcing.service.MoneyTransactionService;
7+
import com.iluwatar.event.sourcing.state.AccountAggregate;
8+
9+
import java.math.BigDecimal;
10+
11+
/**
12+
* Created by serdarh on 06.08.2017.
13+
*/
14+
public class App {
15+
16+
public static void main(String[] args) {
17+
System.out.println("Running the system first time............");
18+
19+
DomainEventProcessor domainEventProcessor = new DomainEventProcessor();
20+
JsonFileJournal jsonFileJournal = new JsonFileJournal();
21+
jsonFileJournal.reset();
22+
domainEventProcessor.setPrecessorJournal(jsonFileJournal);
23+
24+
System.out.println("Creating th accounts............");
25+
26+
AccountService accountService = new AccountService(domainEventProcessor);
27+
MoneyTransactionService moneyTransactionService = new MoneyTransactionService(domainEventProcessor);
28+
accountService.createAccount(1,"Daenerys Targaryen");
29+
accountService.createAccount(2,"Jon Snow");
30+
31+
System.out.println("Do some money operations............");
32+
33+
moneyTransactionService.depositMoney(1,new BigDecimal("100000"));
34+
moneyTransactionService.depositMoney(2,new BigDecimal("10"));
35+
36+
moneyTransactionService.transferMoney(1,2,new BigDecimal("10000"));
37+
moneyTransactionService.withdrawalMoney(2, new BigDecimal("1000"));
38+
39+
System.out.println("...............State:............");
40+
System.out.println(AccountAggregate.getAccount(1));
41+
System.out.println(AccountAggregate.getAccount(2));
42+
43+
System.out.println("At that point system goes down state in memory cleared............");
44+
45+
AccountAggregate.resetState();
46+
47+
System.out.println("Recover the syste by the events in journal file............");
48+
49+
domainEventProcessor = new DomainEventProcessor();
50+
jsonFileJournal = new JsonFileJournal();
51+
domainEventProcessor.setPrecessorJournal(jsonFileJournal);
52+
domainEventProcessor.recover();
53+
54+
System.out.println("...............State Recovered:............");
55+
System.out.println(AccountAggregate.getAccount(1));
56+
System.out.println(AccountAggregate.getAccount(2));
57+
}
58+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.iluwatar.event.sourcing.domain;
2+
3+
import java.math.BigDecimal;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
/**
8+
* Created by serdarh on 06.08.2017.
9+
*/
10+
public class Account {
11+
private final int accountNo;
12+
private final String owner;
13+
private BigDecimal money;
14+
private List<Transaction> transactions;
15+
16+
public Account(int accountNo, String owner) {
17+
this.accountNo = accountNo;
18+
this.owner = owner;
19+
money = BigDecimal.ZERO;
20+
transactions = new ArrayList<>();
21+
}
22+
23+
public int getAccountNo() {
24+
return accountNo;
25+
}
26+
27+
public String getOwner() {
28+
return owner;
29+
}
30+
31+
public BigDecimal getMoney() {
32+
return money;
33+
}
34+
35+
public List<Transaction> getTransactions() {
36+
return transactions;
37+
}
38+
39+
public void setMoney(BigDecimal money) {
40+
this.money = money;
41+
}
42+
43+
public void setTransactions(List<Transaction> transactions) {
44+
this.transactions = transactions;
45+
}
46+
47+
public Account copy() {
48+
Account account = new Account(accountNo, owner);
49+
account.setMoney(money);
50+
account.setTransactions(transactions);
51+
return account;
52+
}
53+
54+
@Override
55+
public String toString() {
56+
return "Account{" +
57+
"accountNo=" + accountNo +
58+
", owner='" + owner + '\'' +
59+
", money=" + money +
60+
", transactions=" + transactions +
61+
'}';
62+
}
63+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.iluwatar.event.sourcing.domain;
2+
3+
import java.math.BigDecimal;
4+
5+
/**
6+
* Created by serdarh on 06.08.2017.
7+
*/
8+
public class Transaction {
9+
private final int accountNo;
10+
private final BigDecimal moneyIn;
11+
private final BigDecimal moneyOut;
12+
private final BigDecimal lastBalance;
13+
14+
public Transaction(int accountNo, BigDecimal moneyIn, BigDecimal moneyOut, BigDecimal lastBalance) {
15+
this.accountNo = accountNo;
16+
this.moneyIn = moneyIn;
17+
this.moneyOut = moneyOut;
18+
this.lastBalance = lastBalance;
19+
}
20+
21+
public int getAccountNo() {
22+
return accountNo;
23+
}
24+
25+
public BigDecimal getMoneyIn() {
26+
return moneyIn;
27+
}
28+
29+
public BigDecimal getMoneyOut() {
30+
return moneyOut;
31+
}
32+
33+
public BigDecimal getLastBalance() {
34+
return lastBalance;
35+
}
36+
37+
@Override
38+
public String toString() {
39+
return "Transaction{" +
40+
"accountNo=" + accountNo +
41+
", moneyIn=" + moneyIn +
42+
", moneyOut=" + moneyOut +
43+
", lastBalance=" + lastBalance +
44+
'}';
45+
}
46+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.iluwatar.event.sourcing.event;
2+
3+
import com.iluwatar.event.sourcing.api.DomainEvent;
4+
import com.iluwatar.event.sourcing.domain.Account;
5+
import com.iluwatar.event.sourcing.gateway.Gateways;
6+
import com.iluwatar.event.sourcing.state.AccountAggregate;
7+
8+
/**
9+
* Created by serdarh on 06.08.2017.
10+
*/
11+
public class AccountCreateEvent extends DomainEvent {
12+
private final int accountNo;
13+
private final String owner;
14+
15+
public AccountCreateEvent(long sequenceId, long createdTime, int accountNo, String owner) {
16+
super(sequenceId, createdTime, "AccountCreateEvent");
17+
this.accountNo = accountNo;
18+
this.owner = owner;
19+
}
20+
21+
public int getAccountNo() {
22+
return accountNo;
23+
}
24+
25+
public String getOwner() {
26+
return owner;
27+
}
28+
29+
@Override
30+
public void process() {
31+
Account account = AccountAggregate.getAccount(accountNo);
32+
if(account!=null){
33+
throw new RuntimeException("Account already exists");
34+
}
35+
account = new Account(accountNo,owner);
36+
AccountAggregate.putAccount(account);
37+
38+
// check if this event is replicated from journal before calling an external gateway function
39+
if(!isReplica()) {
40+
Gateways.getAccountCreateContractSender().sendContractInfo(account);
41+
}
42+
}
43+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.iluwatar.event.sourcing.event;
2+
3+
import com.iluwatar.event.sourcing.api.DomainEvent;
4+
import com.iluwatar.event.sourcing.domain.Account;
5+
import com.iluwatar.event.sourcing.domain.Transaction;
6+
import com.iluwatar.event.sourcing.gateway.Gateways;
7+
import com.iluwatar.event.sourcing.state.AccountAggregate;
8+
9+
import java.math.BigDecimal;
10+
11+
/**
12+
* Created by serdarh on 06.08.2017.
13+
*/
14+
public class MoneyDepositEvent extends DomainEvent {
15+
private BigDecimal money;
16+
private int accountNo;
17+
18+
public MoneyDepositEvent(long sequenceId, long createdTime, int accountNo,BigDecimal money) {
19+
super(sequenceId, createdTime, "MoneyDepositEvent");
20+
this.money = money;
21+
this.accountNo = accountNo;
22+
}
23+
24+
@Override
25+
public void process() {
26+
Account account = AccountAggregate.getAccount(accountNo);
27+
if(account==null){
28+
throw new RuntimeException("Account not found");
29+
}
30+
account.setMoney(account.getMoney().add(money));
31+
Transaction transaction = new Transaction(accountNo,money,BigDecimal.ZERO,account.getMoney());
32+
account.getTransactions().add(transaction);
33+
AccountAggregate.putAccount(account);
34+
if(!isReplica()) {
35+
Gateways.getTransactionLogger().log(transaction);
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)