Java 11 migrate remaining e (#1112)
* Moves eip-aggregator to Java 11 * Moves eip-message-channel to Java 11 * Moves eip-publish-subscribe to Java 11 * Moves eip-splitter to Java 11 * Moves eip-wire-tap to Java 11 * Moves event-aggregator to Java 11 * Moves event-asynchronous to Java 11 * Moves event-driven-architecture to Java 11 * Moves event-queue to Java 11 * Moves event-sourcing to Java 11 * Moves execute-around to Java 11 * Moves extension-objects to Java 11
This commit is contained in:
committed by
Ilkka Seppälä
parent
b09b100614
commit
fb2c026822
@ -69,7 +69,7 @@ public class App {
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
DomainEventProcessor eventProcessor = new DomainEventProcessor();
|
||||
var eventProcessor = new DomainEventProcessor();
|
||||
|
||||
|
||||
LOGGER.info("Running the system first time............");
|
||||
|
@ -104,7 +104,7 @@ public class Account {
|
||||
* @return the account
|
||||
*/
|
||||
public Account copy() {
|
||||
Account account = new Account(accountNo, owner);
|
||||
var account = new Account(accountNo, owner);
|
||||
account.setMoney(money);
|
||||
return account;
|
||||
}
|
||||
@ -135,7 +135,7 @@ public class Account {
|
||||
}
|
||||
|
||||
private void handleWithdrawal(BigDecimal money, boolean realTime) {
|
||||
if (this.money.compareTo(money) == -1) {
|
||||
if (this.money.compareTo(money) < 0) {
|
||||
throw new RuntimeException("Insufficient Account Balance");
|
||||
}
|
||||
|
||||
|
@ -72,7 +72,7 @@ public class AccountCreateEvent extends DomainEvent {
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
Account account = AccountAggregate.getAccount(accountNo);
|
||||
var account = AccountAggregate.getAccount(accountNo);
|
||||
if (account != null) {
|
||||
throw new RuntimeException("Account already exists");
|
||||
}
|
||||
|
@ -23,9 +23,9 @@
|
||||
|
||||
package com.iluwatar.event.sourcing.event;
|
||||
|
||||
import com.iluwatar.event.sourcing.domain.Account;
|
||||
import com.iluwatar.event.sourcing.state.AccountAggregate;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* This is the class that implements money deposit event. Holds the necessary info for a money
|
||||
@ -73,10 +73,8 @@ public class MoneyDepositEvent extends DomainEvent {
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
Account account = AccountAggregate.getAccount(accountNo);
|
||||
if (account == null) {
|
||||
throw new RuntimeException("Account not found");
|
||||
}
|
||||
var account = Optional.ofNullable(AccountAggregate.getAccount(accountNo))
|
||||
.orElseThrow(() -> new RuntimeException("Account not found"));
|
||||
account.handleEvent(this);
|
||||
}
|
||||
}
|
||||
|
@ -23,9 +23,9 @@
|
||||
|
||||
package com.iluwatar.event.sourcing.event;
|
||||
|
||||
import com.iluwatar.event.sourcing.domain.Account;
|
||||
import com.iluwatar.event.sourcing.state.AccountAggregate;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* This is the class that implements money transfer event. Holds the necessary info for a money
|
||||
@ -86,15 +86,10 @@ public class MoneyTransferEvent extends DomainEvent {
|
||||
|
||||
@Override
|
||||
public void process() {
|
||||
Account accountFrom = AccountAggregate.getAccount(accountNoFrom);
|
||||
if (accountFrom == null) {
|
||||
throw new RuntimeException("Account not found " + accountNoFrom);
|
||||
}
|
||||
Account accountTo = AccountAggregate.getAccount(accountNoTo);
|
||||
if (accountTo == null) {
|
||||
throw new RuntimeException("Account not found " + accountNoTo);
|
||||
}
|
||||
|
||||
var accountFrom = Optional.ofNullable(AccountAggregate.getAccount(accountNoFrom))
|
||||
.orElseThrow(() -> new RuntimeException("Account not found " + accountNoFrom));
|
||||
var accountTo = Optional.ofNullable(AccountAggregate.getAccount(accountNoTo))
|
||||
.orElseThrow(() -> new RuntimeException("Account not found " + accountNoTo));
|
||||
accountFrom.handleTransferFromEvent(this);
|
||||
accountTo.handleTransferToEvent(this);
|
||||
}
|
||||
|
@ -57,13 +57,8 @@ public class DomainEventProcessor {
|
||||
*/
|
||||
public void recover() {
|
||||
DomainEvent domainEvent;
|
||||
while (true) {
|
||||
domainEvent = processorJournal.readNext();
|
||||
if (domainEvent == null) {
|
||||
break;
|
||||
} else {
|
||||
domainEvent.process();
|
||||
}
|
||||
while ((domainEvent = processorJournal.readNext()) != null) {
|
||||
domainEvent.process();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ -60,8 +60,8 @@ public class JsonFileJournal {
|
||||
public JsonFileJournal() {
|
||||
file = new File("Journal.json");
|
||||
if (file.exists()) {
|
||||
try (BufferedReader input = new BufferedReader(
|
||||
new InputStreamReader(new FileInputStream(file), "UTF-8"))) {
|
||||
try (var input = new BufferedReader(
|
||||
new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) {
|
||||
String line;
|
||||
while ((line = input.readLine()) != null) {
|
||||
events.add(line);
|
||||
@ -81,7 +81,7 @@ public class JsonFileJournal {
|
||||
* @param domainEvent the domain event
|
||||
*/
|
||||
public void write(DomainEvent domainEvent) {
|
||||
Gson gson = new Gson();
|
||||
var gson = new Gson();
|
||||
JsonElement jsonElement;
|
||||
if (domainEvent instanceof AccountCreateEvent) {
|
||||
jsonElement = gson.toJsonTree(domainEvent, AccountCreateEvent.class);
|
||||
@ -93,9 +93,9 @@ public class JsonFileJournal {
|
||||
throw new RuntimeException("Journal Event not recegnized");
|
||||
}
|
||||
|
||||
try (Writer output = new BufferedWriter(
|
||||
new OutputStreamWriter(new FileOutputStream(file, true), "UTF-8"))) {
|
||||
String eventString = jsonElement.toString();
|
||||
try (var output = new BufferedWriter(
|
||||
new OutputStreamWriter(new FileOutputStream(file, true), StandardCharsets.UTF_8))) {
|
||||
var eventString = jsonElement.toString();
|
||||
output.write(eventString + "\r\n");
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
@ -120,13 +120,13 @@ public class JsonFileJournal {
|
||||
if (index >= events.size()) {
|
||||
return null;
|
||||
}
|
||||
String event = events.get(index);
|
||||
var event = events.get(index);
|
||||
index++;
|
||||
|
||||
JsonParser parser = new JsonParser();
|
||||
JsonElement jsonElement = parser.parse(event);
|
||||
String eventClassName = jsonElement.getAsJsonObject().get("eventClassName").getAsString();
|
||||
Gson gson = new Gson();
|
||||
var parser = new JsonParser();
|
||||
var jsonElement = parser.parse(event);
|
||||
var eventClassName = jsonElement.getAsJsonObject().get("eventClassName").getAsString();
|
||||
var gson = new Gson();
|
||||
DomainEvent domainEvent;
|
||||
if (eventClassName.equals("AccountCreateEvent")) {
|
||||
domainEvent = gson.fromJson(jsonElement, AccountCreateEvent.class);
|
||||
|
@ -26,6 +26,7 @@ package com.iluwatar.event.sourcing.state;
|
||||
import com.iluwatar.event.sourcing.domain.Account;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* This is the static accounts map holder class. This class holds the state of the accounts.
|
||||
@ -55,11 +56,10 @@ public class AccountAggregate {
|
||||
* @return the copy of the account or null if not found
|
||||
*/
|
||||
public static Account getAccount(int accountNo) {
|
||||
Account account = accounts.get(accountNo);
|
||||
if (account == null) {
|
||||
return null;
|
||||
}
|
||||
return account.copy();
|
||||
return Optional.of(accountNo)
|
||||
.map(accounts::get)
|
||||
.map(Account::copy)
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -21,25 +21,23 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import com.iluwatar.event.sourcing.domain.Account;
|
||||
import static com.iluwatar.event.sourcing.app.App.ACCOUNT_OF_DAENERYS;
|
||||
import static com.iluwatar.event.sourcing.app.App.ACCOUNT_OF_JON;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import com.iluwatar.event.sourcing.event.AccountCreateEvent;
|
||||
import com.iluwatar.event.sourcing.event.MoneyDepositEvent;
|
||||
import com.iluwatar.event.sourcing.event.MoneyTransferEvent;
|
||||
import com.iluwatar.event.sourcing.processor.DomainEventProcessor;
|
||||
import com.iluwatar.event.sourcing.state.AccountAggregate;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
|
||||
import static com.iluwatar.event.sourcing.app.App.ACCOUNT_OF_DAENERYS;
|
||||
import static com.iluwatar.event.sourcing.app.App.ACCOUNT_OF_JON;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* Intergartion Test for Event Sourcing state recovery
|
||||
*
|
||||
* <p>
|
||||
* Created by Serdar Hamzaogullari on 19.08.2017.
|
||||
*/
|
||||
public class IntegrationTest {
|
||||
@ -71,25 +69,25 @@ public class IntegrationTest {
|
||||
1, new Date().getTime(), ACCOUNT_OF_JON, "Jon Snow"));
|
||||
|
||||
eventProcessor.process(new MoneyDepositEvent(
|
||||
2, new Date().getTime(), ACCOUNT_OF_DAENERYS, new BigDecimal("100000")));
|
||||
2, new Date().getTime(), ACCOUNT_OF_DAENERYS, new BigDecimal("100000")));
|
||||
|
||||
eventProcessor.process(new MoneyDepositEvent(
|
||||
3, new Date().getTime(), ACCOUNT_OF_JON, new BigDecimal("100")));
|
||||
3, new Date().getTime(), ACCOUNT_OF_JON, new BigDecimal("100")));
|
||||
|
||||
eventProcessor.process(new MoneyTransferEvent(
|
||||
4, new Date().getTime(), new BigDecimal("10000"), ACCOUNT_OF_DAENERYS,
|
||||
ACCOUNT_OF_JON));
|
||||
|
||||
Account accountOfDaenerysBeforeShotDown = AccountAggregate.getAccount(ACCOUNT_OF_DAENERYS);
|
||||
Account accountOfJonBeforeShotDown = AccountAggregate.getAccount(ACCOUNT_OF_JON);
|
||||
var accountOfDaenerysBeforeShotDown = AccountAggregate.getAccount(ACCOUNT_OF_DAENERYS);
|
||||
var accountOfJonBeforeShotDown = AccountAggregate.getAccount(ACCOUNT_OF_JON);
|
||||
|
||||
AccountAggregate.resetState();
|
||||
|
||||
eventProcessor = new DomainEventProcessor();
|
||||
eventProcessor.recover();
|
||||
|
||||
Account accountOfDaenerysAfterShotDown = AccountAggregate.getAccount(ACCOUNT_OF_DAENERYS);
|
||||
Account accountOfJonAfterShotDown = AccountAggregate.getAccount(ACCOUNT_OF_JON);
|
||||
var accountOfDaenerysAfterShotDown = AccountAggregate.getAccount(ACCOUNT_OF_DAENERYS);
|
||||
var accountOfJonAfterShotDown = AccountAggregate.getAccount(ACCOUNT_OF_JON);
|
||||
|
||||
assertEquals(accountOfDaenerysBeforeShotDown.getMoney(),
|
||||
accountOfDaenerysAfterShotDown.getMoney());
|
||||
|
Reference in New Issue
Block a user