|
24 | 24 |
|
25 | 25 | import com.iluwatar.event.sourcing.journal.JsonFileJournal;
|
26 | 26 | import com.iluwatar.event.sourcing.processor.DomainEventProcessor;
|
27 |
| -import com.iluwatar.event.sourcing.service.AccountService; |
28 |
| -import com.iluwatar.event.sourcing.service.MoneyTransactionService; |
| 27 | +import com.iluwatar.event.sourcing.AccountService; |
| 28 | +import com.iluwatar.event.sourcing.MoneyTransactionService; |
29 | 29 | import com.iluwatar.event.sourcing.state.AccountAggregate;
|
30 | 30 | import java.math.BigDecimal;
|
| 31 | +import org.slf4j.Logger; |
| 32 | +import org.slf4j.LoggerFactory; |
31 | 33 |
|
32 | 34 | /**
|
33 |
| - * Created by serdarh on 06.08.2017. |
| 35 | + * Event Sourcing : Instead of storing just the current state of the data in a domain, use an |
| 36 | + * append-only store to record the full series of actions taken on that data. The store acts as the |
| 37 | + * system of record and can be used to materialize the domain objects. This can simplify tasks in |
| 38 | + * complex domains, by avoiding the need to synchronize the data model and the business domain, |
| 39 | + * while improving performance, scalability, and responsiveness. It can also provide consistency for |
| 40 | + * transactional data, and maintain full audit trails and history that can enable compensating |
| 41 | + * actions. |
| 42 | + * |
| 43 | + * This App class is an example usage of Event Sourcing pattern. As an example, two bank account is |
| 44 | + * created, then some money deposit and transfer actions are taken so a new state of accounts is |
| 45 | + * created. At that point, state is cleared in order to represent a system shot down. After the shot |
| 46 | + * down, system state is recovered by re-creating the past events from event journal. Then state is |
| 47 | + * printed so a user can view the last state is same with the state before system shot down. |
| 48 | + * |
| 49 | + * Created by Serdar Hamzaogullari on 06.08.2017. |
34 | 50 | */
|
35 | 51 | public class App {
|
36 | 52 |
|
| 53 | + private static final Logger LOGGER = LoggerFactory.getLogger(App.class); |
| 54 | + public static final int ACCOUNT_OF_DAENERYS = 1; |
| 55 | + public static final int ACCOUNT_OF_JON = 2; |
| 56 | + |
37 | 57 | /**
|
38 | 58 | * The entry point of application.
|
39 | 59 | *
|
40 | 60 | * @param args the input arguments
|
41 | 61 | */
|
42 | 62 | public static void main(String[] args) {
|
43 |
| - System.out.println("Running the system first time............"); |
44 | 63 |
|
45 | 64 | DomainEventProcessor domainEventProcessor = new DomainEventProcessor();
|
46 | 65 | JsonFileJournal jsonFileJournal = new JsonFileJournal();
|
47 |
| - jsonFileJournal.reset(); |
48 | 66 | domainEventProcessor.setPrecessorJournal(jsonFileJournal);
|
49 |
| - |
50 |
| - System.out.println("Creating th accounts............"); |
51 |
| - |
52 | 67 | AccountService accountService = new AccountService(domainEventProcessor);
|
53 | 68 | MoneyTransactionService moneyTransactionService = new MoneyTransactionService(
|
54 | 69 | domainEventProcessor);
|
55 |
| - accountService.createAccount(1, "Daenerys Targaryen"); |
56 |
| - accountService.createAccount(2, "Jon Snow"); |
57 | 70 |
|
58 |
| - System.out.println("Do some money operations............"); |
| 71 | + LOGGER.info("Running the system first time............"); |
| 72 | + jsonFileJournal.reset(); |
| 73 | + |
| 74 | + LOGGER.info("Creating th accounts............"); |
| 75 | + |
| 76 | + accountService.createAccount(ACCOUNT_OF_DAENERYS, "Daenerys Targaryen"); |
| 77 | + accountService.createAccount(ACCOUNT_OF_JON, "Jon Snow"); |
59 | 78 |
|
60 |
| - moneyTransactionService.depositMoney(1, new BigDecimal("100000")); |
61 |
| - moneyTransactionService.depositMoney(2, new BigDecimal("100")); |
| 79 | + LOGGER.info("Do some money operations............"); |
62 | 80 |
|
63 |
| - moneyTransactionService.transferMoney(1, 2, new BigDecimal("10000")); |
64 |
| - moneyTransactionService.withdrawalMoney(2, new BigDecimal("1000")); |
| 81 | + moneyTransactionService.depositMoney(ACCOUNT_OF_DAENERYS, new BigDecimal("100000")); |
| 82 | + moneyTransactionService.depositMoney(ACCOUNT_OF_JON, new BigDecimal("100")); |
65 | 83 |
|
66 |
| - System.out.println("...............State:............"); |
67 |
| - System.out.println(AccountAggregate.getAccount(1)); |
68 |
| - System.out.println(AccountAggregate.getAccount(2)); |
| 84 | + moneyTransactionService.transferMoney(ACCOUNT_OF_DAENERYS, ACCOUNT_OF_JON, new BigDecimal("10000")); |
| 85 | + moneyTransactionService.withdrawalMoney(ACCOUNT_OF_JON, new BigDecimal("1000")); |
69 | 86 |
|
70 |
| - System.out.println("At that point system goes down state in memory cleared............"); |
| 87 | + LOGGER.info("...............State:............"); |
| 88 | + LOGGER.info(AccountAggregate.getAccount(ACCOUNT_OF_DAENERYS).toString()); |
| 89 | + LOGGER.info(AccountAggregate.getAccount(ACCOUNT_OF_JON).toString()); |
71 | 90 |
|
| 91 | + LOGGER.info("At that point system had a shot down, state in memory is cleared............"); |
72 | 92 | AccountAggregate.resetState();
|
73 | 93 |
|
74 |
| - System.out.println("Recover the syste by the events in journal file............"); |
| 94 | + LOGGER.info("Recover the system by the events in journal file............"); |
75 | 95 |
|
76 | 96 | domainEventProcessor = new DomainEventProcessor();
|
77 | 97 | jsonFileJournal = new JsonFileJournal();
|
78 | 98 | domainEventProcessor.setPrecessorJournal(jsonFileJournal);
|
79 | 99 | domainEventProcessor.recover();
|
80 | 100 |
|
81 |
| - System.out.println("...............State Recovered:............"); |
82 |
| - System.out.println(AccountAggregate.getAccount(1)); |
83 |
| - System.out.println(AccountAggregate.getAccount(2)); |
| 101 | + LOGGER.info("...............Recovered State:............"); |
| 102 | + LOGGER.info(AccountAggregate.getAccount(ACCOUNT_OF_DAENERYS).toString()); |
| 103 | + LOGGER.info(AccountAggregate.getAccount(ACCOUNT_OF_JON).toString()); |
84 | 104 | }
|
85 | 105 | }
|
0 commit comments