refactor(banking-demo): introduce DemoApplication orchestrator and add controlled lifecycle management

- Replace DemoFactory with DemoApplication to centralize system setup, execution, and shutdown
- Move ExecutorService and ScheduledExecutorService lifecycle management into application layer
- Ensure proper shutdown sequence for worker and monitoring threads
- Simplify Main class to a minimal entry-point wrapper
- Improve clarity of execution flow: build → run → shutdown
- Prevent resource leakage by explicitly awaiting termination of thread pools
This commit is contained in:
2026-06-05 20:31:08 +03:30
parent 43a53bdabf
commit 353873f10d
5 changed files with 117 additions and 83 deletions
@@ -0,0 +1,108 @@
package dev.banking;
import dev.banking.model.*;
import dev.banking.monitor.LiveMonitor;
import dev.banking.processor.TransactionProcessor;
import dev.banking.service.BankingSystem;
import java.util.*;
import java.util.concurrent.*;
/**
* DemoApplication is responsible for:
* - Building the system
* - Running the simulation
* - Managing lifecycle (threads, schedulers)
*
* This class replaces DemoFactory as a full runner.
*/
public final class DemoApplication {
private DemoApplication() {
}
public static void run() {
System.out.println("Initializing Banking Simulation...\n");
/*
* 1. Create bank accounts
*/
BankAccount acc1 = new BankAccount(1, 1000);
BankAccount acc2 = new BankAccount(2, 2000);
BankAccount acc3 = new BankAccount(3, 1500);
Map<Integer, BankAccount> accounts = new HashMap<>();
accounts.put(1, acc1);
accounts.put(2, acc2);
accounts.put(3, acc3);
/*
* 2. Create transactions
*/
List<Transaction> transactions = List.of(
new DepositTransaction(1, 200),
new WithdrawTransaction(2, 300),
new TransferTransaction(1, 2, 150),
new TransferTransaction(2, 3, 400),
new DepositTransaction(3, 500),
new WithdrawTransaction(1, 100),
new TransferTransaction(3, 1, 250)
);
/*
* 3. Thread pool (workers)
*/
ExecutorService executor = Executors.newFixedThreadPool(4);
/*
* 4. Processor
*/
TransactionProcessor processor = new TransactionProcessor(accounts);
/*
* 5. Banking system
*/
BankingSystem bankingSystem = new BankingSystem(executor, processor);
/*
* 6. Live monitor (UI simulation)
*/
LiveMonitor monitor = new LiveMonitor();
ScheduledExecutorService monitorExecutor = Executors.newSingleThreadScheduledExecutor();
monitorExecutor.scheduleAtFixedRate(() -> {
monitor.update(accounts.values());
System.out.println("----------------------");
}, 0, 1, TimeUnit.SECONDS);
/*
* 7. Run simulation
*/
bankingSystem.processTransactions(transactions);
/*
* 8. Shutdown / lifecycle management
*/
shutdown(executor, monitorExecutor);
System.out.println("\nSimulation completed.");
}
private static void shutdown(
ExecutorService executor,
ScheduledExecutorService monitorExecutor
) {
try {
executor.shutdown();
executor.awaitTermination(5, TimeUnit.SECONDS);
monitorExecutor.shutdown();
monitorExecutor.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}