BAEL-2274 DDD Bounded Contexts and Java Modules
This commit is contained in:
parent
7693dea699
commit
0cc2c4f50d
|
@ -0,0 +1 @@
|
|||
## Relevant Articles
|
|
@ -0,0 +1,53 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.dddmodules.infrastructure</groupId>
|
||||
<artifactId>infrastructure</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.dddmodules</groupId>
|
||||
<artifactId>dddmodules</artifactId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.baeldung.dddmodules.shippingcontext</groupId>
|
||||
<artifactId>shippingcontext</artifactId>
|
||||
<version>${appmodules.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baeldung.dddmodules.ordercontext</groupId>
|
||||
<artifactId>ordercontext</artifactId>
|
||||
<version>${appmodules.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baeldung.dddmodules.sharedkernel</groupId>
|
||||
<artifactId>sharedkernel</artifactId>
|
||||
<version>${appmodules.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${source.version}</source>
|
||||
<target>${target.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<source.version>9</source.version>
|
||||
<target.version>9</target.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,79 @@
|
|||
package com.baeldung.dddmodules.infrastructure.db;
|
||||
|
||||
import com.baeldung.dddmodules.ordercontext.model.CustomerOrder;
|
||||
import com.baeldung.dddmodules.ordercontext.repository.CustomerOrderRepository;
|
||||
import com.baeldung.dddmodules.shippingcontext.model.PackageItem;
|
||||
import com.baeldung.dddmodules.shippingcontext.model.ShippableOrder;
|
||||
import com.baeldung.dddmodules.shippingcontext.repository.ShippingOrderRepository;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class InMemoryOrderStore implements CustomerOrderRepository, ShippingOrderRepository {
|
||||
private Map<Integer, PersistenceOrder> ordersDb = new HashMap<>();
|
||||
private volatile static InMemoryOrderStore instance = new InMemoryOrderStore();
|
||||
|
||||
@Override
|
||||
public void saveCustomerOrder(CustomerOrder order) {
|
||||
this.ordersDb.put(order.getOrderId(), new PersistenceOrder(order.getOrderId(),
|
||||
order.getPaymentMethod(),
|
||||
order.getAddress(),
|
||||
order
|
||||
.getOrderItems()
|
||||
.stream()
|
||||
.map(orderItem ->
|
||||
new PersistenceOrder.OrderItem(orderItem.getProductId(),
|
||||
orderItem.getQuantity(),
|
||||
orderItem.getUnitWeight(),
|
||||
orderItem.getUnitPrice()))
|
||||
.collect(Collectors.toList())
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<ShippableOrder> findShippableOrder(int orderId) {
|
||||
if (!this.ordersDb.containsKey(orderId)) return Optional.empty();
|
||||
PersistenceOrder orderRecord = this.ordersDb.get(orderId);
|
||||
return Optional.of(
|
||||
new ShippableOrder(orderRecord.orderId, orderRecord.orderItems
|
||||
.stream().map(orderItem -> new PackageItem(orderItem.productId,
|
||||
orderItem.itemWeight,
|
||||
orderItem.quantity * orderItem.unitPrice)
|
||||
).collect(Collectors.toList())));
|
||||
}
|
||||
|
||||
public static InMemoryOrderStore provider() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static class PersistenceOrder {
|
||||
public int orderId;
|
||||
public String paymentMethod;
|
||||
public String address;
|
||||
public List<OrderItem> orderItems;
|
||||
|
||||
public PersistenceOrder(int orderId, String paymentMethod, String address, List<OrderItem> orderItems) {
|
||||
this.orderId = orderId;
|
||||
this.paymentMethod = paymentMethod;
|
||||
this.address = address;
|
||||
this.orderItems = orderItems;
|
||||
}
|
||||
|
||||
public static class OrderItem {
|
||||
public int productId;
|
||||
public float unitPrice;
|
||||
public float itemWeight;
|
||||
public int quantity;
|
||||
|
||||
public OrderItem(int productId, int quantity, float unitWeight, float unitPrice) {
|
||||
this.itemWeight = unitWeight;
|
||||
this.quantity = quantity;
|
||||
this.unitPrice = unitPrice;
|
||||
this.productId = productId;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.baeldung.dddmodules.infrastructure.events;
|
||||
|
||||
import com.baeldung.dddmodules.sharedkernel.events.ApplicationEvent;
|
||||
import com.baeldung.dddmodules.sharedkernel.events.EventBus;
|
||||
import com.baeldung.dddmodules.sharedkernel.events.EventSubscriber;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
|
||||
public class SimpleEventBus implements EventBus {
|
||||
private final Map<String, Set<EventSubscriber>> subscribers = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public <E extends ApplicationEvent> void publish(E event) {
|
||||
if (subscribers.containsKey(event.getType())) {
|
||||
subscribers.get(event.getType())
|
||||
.forEach(subscriber -> subscriber.onEvent(event));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends ApplicationEvent> void subscribe(String eventType, EventSubscriber subscriber) {
|
||||
Set<EventSubscriber> eventSubscribers = subscribers.get(eventType);
|
||||
if (eventSubscribers == null) {
|
||||
eventSubscribers = new CopyOnWriteArraySet<>();
|
||||
subscribers.put(eventType, eventSubscribers);
|
||||
}
|
||||
eventSubscribers.add(subscriber);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <E extends ApplicationEvent> void unsubscribe(String eventType, EventSubscriber subscriber) {
|
||||
if (subscribers.containsKey(eventType)) {
|
||||
subscribers.get(eventType).remove(subscriber);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
module com.baeldung.dddmodules.infrastructure {
|
||||
requires transitive com.baeldung.dddmodules.sharedkernel;
|
||||
requires transitive com.baeldung.dddmodules.ordercontext;
|
||||
requires transitive com.baeldung.dddmodules.shippingcontext;
|
||||
provides com.baeldung.dddmodules.sharedkernel.events.EventBus
|
||||
with com.baeldung.dddmodules.infrastructure.events.SimpleEventBus;
|
||||
provides com.baeldung.dddmodules.ordercontext.repository.CustomerOrderRepository
|
||||
with com.baeldung.dddmodules.infrastructure.db.InMemoryOrderStore;
|
||||
provides com.baeldung.dddmodules.shippingcontext.repository.ShippingOrderRepository
|
||||
with com.baeldung.dddmodules.infrastructure.db.InMemoryOrderStore;
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.dddmodules.mainapp</groupId>
|
||||
<artifactId>mainapp</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.dddmodules</groupId>
|
||||
<artifactId>dddmodules</artifactId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.baeldung.dddmodules.infrastructure</groupId>
|
||||
<artifactId>infrastructure</artifactId>
|
||||
<version>${appmodules.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.16</version>
|
||||
<configuration>
|
||||
<enableAssertions>true</enableAssertions>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${compiler.plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${source.version}</source>
|
||||
<target>${target.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<source.version>9</source.version>
|
||||
<target.version>9</target.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,54 @@
|
|||
package com.baeldung.dddmodules.mainapp;
|
||||
|
||||
import com.baeldung.dddmodules.ordercontext.model.CustomerOrder;
|
||||
import com.baeldung.dddmodules.ordercontext.model.OrderItem;
|
||||
import com.baeldung.dddmodules.ordercontext.repository.CustomerOrderRepository;
|
||||
import com.baeldung.dddmodules.ordercontext.service.OrderService;
|
||||
import com.baeldung.dddmodules.sharedkernel.events.EventBus;
|
||||
import com.baeldung.dddmodules.shippingcontext.repository.ShippingOrderRepository;
|
||||
import com.baeldung.dddmodules.shippingcontext.service.ShippingService;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String args[]) {
|
||||
Map<Class<?>, Object> container = createContainer();
|
||||
OrderService orderService = (OrderService) container.get(OrderService.class);
|
||||
ShippingService shippingService = (ShippingService) container.get(ShippingService.class);
|
||||
shippingService.listenToOrderEvents();
|
||||
|
||||
CustomerOrder customerOrder = new CustomerOrder();
|
||||
int orderId = 1;
|
||||
customerOrder.setOrderId(orderId);
|
||||
List<OrderItem> orderItems = new ArrayList<OrderItem>();
|
||||
orderItems.add(new OrderItem(1, 2, 3, 1));
|
||||
orderItems.add(new OrderItem(2, 1, 1, 1));
|
||||
orderItems.add(new OrderItem(3, 4, 11, 21));
|
||||
customerOrder.setOrderItems(orderItems);
|
||||
customerOrder.setPaymentMethod("PayPal");
|
||||
customerOrder.setAddress("Full address here");
|
||||
orderService.placeOrder(customerOrder);
|
||||
|
||||
if (orderId == shippingService.getParcelByOrderId(orderId).get().getOrderId()) {
|
||||
System.out.println("Order has been processed and shipped successfully");
|
||||
}
|
||||
}
|
||||
|
||||
public static Map<Class<?>, Object> createContainer() {
|
||||
EventBus eventBus = ServiceLoader.load(EventBus.class).findFirst().get();
|
||||
CustomerOrderRepository customerOrderRepository = ServiceLoader.load(CustomerOrderRepository.class).findFirst().get();
|
||||
ShippingOrderRepository shippingOrderRepository = ServiceLoader.load(ShippingOrderRepository.class).findFirst().get();
|
||||
ShippingService shippingService = ServiceLoader.load(ShippingService.class).findFirst().get();
|
||||
shippingService.setEventBus(eventBus);
|
||||
shippingService.setOrderRepository(shippingOrderRepository);
|
||||
OrderService orderService = ServiceLoader.load(OrderService.class).findFirst().get();
|
||||
orderService.setEventBus(eventBus);
|
||||
orderService.setOrderRepository(customerOrderRepository);
|
||||
HashMap<Class<?>, Object> container = new HashMap<>();
|
||||
container.put(OrderService.class, orderService);
|
||||
container.put(ShippingService.class, shippingService);
|
||||
return container;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
module com.baeldung.dddmodules.mainapp {
|
||||
uses com.baeldung.dddmodules.sharedkernel.events.EventBus;
|
||||
uses com.baeldung.dddmodules.ordercontext.service.OrderService;
|
||||
uses com.baeldung.dddmodules.ordercontext.repository.CustomerOrderRepository;
|
||||
uses com.baeldung.dddmodules.shippingcontext.repository.ShippingOrderRepository;
|
||||
uses com.baeldung.dddmodules.shippingcontext.service.ShippingService;
|
||||
requires transitive com.baeldung.dddmodules.infrastructure;
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.dddmodules.ordercontext</groupId>
|
||||
<artifactId>ordercontext</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.dddmodules</groupId>
|
||||
<artifactId>dddmodules</artifactId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.baeldung.dddmodules.sharedkernel</groupId>
|
||||
<artifactId>sharedkernel</artifactId>
|
||||
<version>${appmodules.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${source.version}</source>
|
||||
<target>${target.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<source.version>9</source.version>
|
||||
<target.version>9</target.version>
|
||||
<entitymodule.version>1.0</entitymodule.version>
|
||||
<daomodule.version>1.0</daomodule.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.dddmodules.ordercontext.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CustomerOrder {
|
||||
private int orderId;
|
||||
private String paymentMethod;
|
||||
private String address;
|
||||
private List<OrderItem> orderItems;
|
||||
|
||||
public CustomerOrder() {
|
||||
|
||||
}
|
||||
|
||||
public float calculateTotalPrice() {
|
||||
return orderItems.stream().map(OrderItem::getTotalPrice)
|
||||
.reduce(0F, Float::sum);
|
||||
}
|
||||
|
||||
public void setOrderItems(List<OrderItem> orderItems) {
|
||||
this.orderItems = orderItems;
|
||||
}
|
||||
|
||||
public int getOrderId() {
|
||||
return orderId;
|
||||
}
|
||||
|
||||
public List<OrderItem> getOrderItems() {
|
||||
return orderItems;
|
||||
}
|
||||
|
||||
public void setOrderId(int orderId) {
|
||||
this.orderId = orderId;
|
||||
}
|
||||
|
||||
public String getPaymentMethod() {
|
||||
return paymentMethod;
|
||||
}
|
||||
|
||||
public void setPaymentMethod(String paymentMethod) {
|
||||
this.paymentMethod = paymentMethod;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.dddmodules.ordercontext.model;
|
||||
|
||||
public class OrderItem {
|
||||
private int productId;
|
||||
private int quantity;
|
||||
private float unitPrice;
|
||||
private float unitWeight;
|
||||
|
||||
public OrderItem(int productId, int quantity, float unitPrice, float unitWeight) {
|
||||
this.productId = productId;
|
||||
this.quantity = quantity;
|
||||
this.unitPrice = unitPrice;
|
||||
this.unitWeight = unitWeight;
|
||||
}
|
||||
|
||||
public int getProductId() {
|
||||
return productId;
|
||||
}
|
||||
|
||||
public void setProductId(int productId) {
|
||||
this.productId = productId;
|
||||
}
|
||||
|
||||
public int getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public void setQuantity(int quantity) {
|
||||
this.quantity = quantity;
|
||||
}
|
||||
|
||||
public float getTotalPrice() {
|
||||
return this.quantity * this.unitPrice;
|
||||
}
|
||||
|
||||
public void setUnitPrice(float unitPrice) {
|
||||
this.unitPrice = unitPrice;
|
||||
}
|
||||
|
||||
public float getUnitWeight() {
|
||||
return unitWeight;
|
||||
}
|
||||
|
||||
public float getUnitPrice() {
|
||||
return unitPrice;
|
||||
}
|
||||
|
||||
public void setUnitWeight(float unitWeight) {
|
||||
this.unitWeight = unitWeight;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.baeldung.dddmodules.ordercontext.repository;
|
||||
|
||||
import com.baeldung.dddmodules.ordercontext.model.CustomerOrder;
|
||||
|
||||
public interface CustomerOrderRepository {
|
||||
void saveCustomerOrder(CustomerOrder order);
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.baeldung.dddmodules.ordercontext.service;
|
||||
|
||||
import com.baeldung.dddmodules.ordercontext.model.CustomerOrder;
|
||||
import com.baeldung.dddmodules.ordercontext.repository.CustomerOrderRepository;
|
||||
import com.baeldung.dddmodules.sharedkernel.events.ApplicationEvent;
|
||||
import com.baeldung.dddmodules.sharedkernel.events.EventBus;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class CustomerOrderService implements OrderService {
|
||||
public static final String EVENT_ORDER_READY_FOR_SHIPMENT = "OrderReadyForShipmentEvent";
|
||||
|
||||
private CustomerOrderRepository orderRepository;
|
||||
private EventBus eventBus;
|
||||
|
||||
@Override
|
||||
public void placeOrder(CustomerOrder order) {
|
||||
this.orderRepository.saveCustomerOrder(order);
|
||||
Map<String, String> payload = new HashMap<>();
|
||||
payload.put("order_id", String.valueOf(order.getOrderId()));
|
||||
ApplicationEvent event = new ApplicationEvent(payload) {
|
||||
@Override
|
||||
public String getType() {
|
||||
return EVENT_ORDER_READY_FOR_SHIPMENT;
|
||||
}
|
||||
};
|
||||
this.eventBus.publish(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public EventBus getEventBus() {
|
||||
return eventBus;
|
||||
}
|
||||
|
||||
public void setOrderRepository(CustomerOrderRepository orderRepository) {
|
||||
this.orderRepository = orderRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEventBus(EventBus eventBus) {
|
||||
this.eventBus = eventBus;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.dddmodules.ordercontext.service;
|
||||
|
||||
import com.baeldung.dddmodules.ordercontext.model.CustomerOrder;
|
||||
import com.baeldung.dddmodules.ordercontext.repository.CustomerOrderRepository;
|
||||
import com.baeldung.dddmodules.sharedkernel.service.ApplicationService;
|
||||
|
||||
public interface OrderService extends ApplicationService {
|
||||
void placeOrder(CustomerOrder order);
|
||||
|
||||
void setOrderRepository(CustomerOrderRepository orderRepository);
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
module com.baeldung.dddmodules.ordercontext {
|
||||
requires com.baeldung.dddmodules.sharedkernel;
|
||||
exports com.baeldung.dddmodules.ordercontext.service;
|
||||
exports com.baeldung.dddmodules.ordercontext.model;
|
||||
exports com.baeldung.dddmodules.ordercontext.repository;
|
||||
provides com.baeldung.dddmodules.ordercontext.service.OrderService
|
||||
with com.baeldung.dddmodules.ordercontext.service.CustomerOrderService;
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.dddmodules</groupId>
|
||||
<artifactId>dddmodules</artifactId>
|
||||
<version>1.0</version>
|
||||
<name>ddd-modules</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<module>sharedkernel</module>
|
||||
<module>infrastructure</module>
|
||||
<module>shippingcontext</module>
|
||||
<module>ordercontext</module>
|
||||
<module>mainapp</module>
|
||||
</modules>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${compiler.plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${source.version}</source>
|
||||
<target>${target.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<compiler.plugin.version>3.8.1</compiler.plugin.version>
|
||||
<source.version>9</source.version>
|
||||
<target.version>9</target.version>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<assertj-core.version>3.12.2</assertj-core.version>
|
||||
<appmodules.version>1.0</appmodules.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,34 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.dddmodules.sharedkernel</groupId>
|
||||
<artifactId>sharedkernel</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.dddmodules</groupId>
|
||||
<artifactId>dddmodules</artifactId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${source.version}</source>
|
||||
<target>${target.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<source.version>9</source.version>
|
||||
<target.version>9</target.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.dddmodules.sharedkernel.events;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class ApplicationEvent {
|
||||
protected Map<String, String> payload;
|
||||
|
||||
public abstract String getType();
|
||||
|
||||
public String getPayloadValue(String key) {
|
||||
if (this.payload.containsKey(key)) {
|
||||
return this.payload.get(key);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public ApplicationEvent(Map<String, String> payload) {
|
||||
this.payload = payload;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.dddmodules.sharedkernel.events;
|
||||
|
||||
public interface EventBus {
|
||||
<E extends ApplicationEvent> void publish(E event);
|
||||
|
||||
<E extends ApplicationEvent> void subscribe(String eventType, EventSubscriber subscriber);
|
||||
|
||||
<E extends ApplicationEvent> void unsubscribe(String eventType, EventSubscriber subscriber);
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.dddmodules.sharedkernel.events;
|
||||
|
||||
public interface EventSubscriber {
|
||||
<E extends ApplicationEvent> void onEvent(E event);
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.dddmodules.sharedkernel.service;
|
||||
|
||||
import com.baeldung.dddmodules.sharedkernel.events.ApplicationEvent;
|
||||
import com.baeldung.dddmodules.sharedkernel.events.EventBus;
|
||||
import com.baeldung.dddmodules.sharedkernel.events.EventSubscriber;
|
||||
|
||||
public interface ApplicationService {
|
||||
|
||||
default <E extends ApplicationEvent> void publishEvent(E event) {
|
||||
EventBus eventBus = getEventBus();
|
||||
if (eventBus != null) {
|
||||
eventBus.publish(event);
|
||||
}
|
||||
}
|
||||
|
||||
default <E extends ApplicationEvent> void subscribe(String eventType, EventSubscriber subscriber) {
|
||||
EventBus eventBus = getEventBus();
|
||||
if (eventBus != null) {
|
||||
eventBus.subscribe(eventType, subscriber);
|
||||
}
|
||||
}
|
||||
|
||||
default <E extends ApplicationEvent> void unsubscribe(String eventType, EventSubscriber subscriber) {
|
||||
EventBus eventBus = getEventBus();
|
||||
if (eventBus != null) {
|
||||
eventBus.unsubscribe(eventType, subscriber);
|
||||
}
|
||||
}
|
||||
|
||||
EventBus getEventBus();
|
||||
|
||||
void setEventBus(EventBus eventBus);
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
module com.baeldung.dddmodules.sharedkernel {
|
||||
exports com.baeldung.dddmodules.sharedkernel.events;
|
||||
exports com.baeldung.dddmodules.sharedkernel.service;
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.dddmodules.shippingcontext</groupId>
|
||||
<artifactId>shippingcontext</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.dddmodules</groupId>
|
||||
<artifactId>dddmodules</artifactId>
|
||||
<version>1.0</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.baeldung.dddmodules.sharedkernel</groupId>
|
||||
<artifactId>sharedkernel</artifactId>
|
||||
<version>${appmodules.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${source.version}</source>
|
||||
<target>${target.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<source.version>9</source.version>
|
||||
<target.version>9</target.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,37 @@
|
|||
package com.baeldung.dddmodules.shippingcontext.model;
|
||||
|
||||
public class PackageItem {
|
||||
private int productId;
|
||||
private float weight;
|
||||
private float estimatedValue;
|
||||
|
||||
public PackageItem(int productId, float weight, float estimatedValue) {
|
||||
this.productId = productId;
|
||||
this.weight = weight;
|
||||
this.estimatedValue = estimatedValue;
|
||||
}
|
||||
|
||||
public int getProductId() {
|
||||
return productId;
|
||||
}
|
||||
|
||||
public void setProductId(int productId) {
|
||||
this.productId = productId;
|
||||
}
|
||||
|
||||
public float getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
public void setWeight(float weight) {
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
public float getEstimatedValue() {
|
||||
return estimatedValue;
|
||||
}
|
||||
|
||||
public void setEstimatedValue(float estimatedValue) {
|
||||
this.estimatedValue = estimatedValue;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package com.baeldung.dddmodules.shippingcontext.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Parcel {
|
||||
private int orderId;
|
||||
private String address;
|
||||
private String trackingId;
|
||||
private List<PackageItem> packageItems;
|
||||
|
||||
public Parcel(int orderId, String address, List<PackageItem> packageItems) {
|
||||
this.orderId = orderId;
|
||||
this.address = address;
|
||||
this.packageItems = packageItems;
|
||||
}
|
||||
|
||||
public float calculateTotalWeight() {
|
||||
return packageItems.stream().map(PackageItem::getWeight)
|
||||
.reduce(0F, Float::sum);
|
||||
}
|
||||
|
||||
public boolean isTaxable() {
|
||||
return calculateEstimatedValue() > 100;
|
||||
}
|
||||
|
||||
public float calculateEstimatedValue() {
|
||||
return packageItems.stream().map(PackageItem::getWeight)
|
||||
.reduce(0F, Float::sum);
|
||||
}
|
||||
|
||||
public int getOrderId() {
|
||||
return orderId;
|
||||
}
|
||||
|
||||
public void setOrderId(int orderId) {
|
||||
this.orderId = orderId;
|
||||
}
|
||||
|
||||
public String getTrackingId() {
|
||||
return trackingId;
|
||||
}
|
||||
|
||||
public void setTrackingId(String trackingId) {
|
||||
this.trackingId = trackingId;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.dddmodules.shippingcontext.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ShippableOrder {
|
||||
private int orderId;
|
||||
private String address;
|
||||
private List<PackageItem> packageItems;
|
||||
|
||||
public ShippableOrder(int orderId, List<PackageItem> packageItems) {
|
||||
this.orderId = orderId;
|
||||
this.packageItems = packageItems;
|
||||
}
|
||||
|
||||
public int getOrderId() {
|
||||
return orderId;
|
||||
}
|
||||
|
||||
public void setOrderId(int orderId) {
|
||||
this.orderId = orderId;
|
||||
}
|
||||
|
||||
public List<PackageItem> getPackageItems() {
|
||||
return packageItems;
|
||||
}
|
||||
|
||||
public void setPackageItems(List<PackageItem> packageItems) {
|
||||
this.packageItems = packageItems;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.dddmodules.shippingcontext.repository;
|
||||
|
||||
import com.baeldung.dddmodules.shippingcontext.model.ShippableOrder;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface ShippingOrderRepository {
|
||||
Optional<ShippableOrder> findShippableOrder(int orderId);
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
package com.baeldung.dddmodules.shippingcontext.service;
|
||||
|
||||
import com.baeldung.dddmodules.sharedkernel.events.ApplicationEvent;
|
||||
import com.baeldung.dddmodules.sharedkernel.events.EventBus;
|
||||
import com.baeldung.dddmodules.sharedkernel.events.EventSubscriber;
|
||||
import com.baeldung.dddmodules.shippingcontext.model.Parcel;
|
||||
import com.baeldung.dddmodules.shippingcontext.model.ShippableOrder;
|
||||
import com.baeldung.dddmodules.shippingcontext.repository.ShippingOrderRepository;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class ParcelShippingService implements ShippingService {
|
||||
public static final String EVENT_ORDER_READY_FOR_SHIPMENT = "OrderReadyForShipmentEvent";
|
||||
private ShippingOrderRepository orderRepository;
|
||||
private EventBus eventBus;
|
||||
private Map<Integer, Parcel> shippedParcels = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public void shipOrder(int orderId) {
|
||||
Optional<ShippableOrder> order = this.orderRepository.findShippableOrder(orderId);
|
||||
order.ifPresent(completedOrder -> {
|
||||
Parcel parcel = new Parcel(completedOrder.getOrderId(), completedOrder.getAddress(), completedOrder.getPackageItems());
|
||||
if (parcel.isTaxable()) {
|
||||
// Calculate additional taxes
|
||||
}
|
||||
// Ship parcel
|
||||
this.shippedParcels.put(completedOrder.getOrderId(), parcel);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void listenToOrderEvents() {
|
||||
this.eventBus.subscribe(EVENT_ORDER_READY_FOR_SHIPMENT, new EventSubscriber() {
|
||||
@Override
|
||||
public <E extends ApplicationEvent> void onEvent(E event) {
|
||||
shipOrder(Integer.parseInt(event.getPayloadValue("order_id")));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Parcel> getParcelByOrderId(int orderId) {
|
||||
return Optional.ofNullable(this.shippedParcels.get(orderId));
|
||||
}
|
||||
|
||||
public void setOrderRepository(ShippingOrderRepository orderRepository) {
|
||||
this.orderRepository = orderRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EventBus getEventBus() {
|
||||
return eventBus;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEventBus(EventBus eventBus) {
|
||||
this.eventBus = eventBus;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.dddmodules.shippingcontext.service;
|
||||
|
||||
import com.baeldung.dddmodules.sharedkernel.service.ApplicationService;
|
||||
import com.baeldung.dddmodules.shippingcontext.model.Parcel;
|
||||
import com.baeldung.dddmodules.shippingcontext.repository.ShippingOrderRepository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface ShippingService extends ApplicationService {
|
||||
void shipOrder(int orderId);
|
||||
|
||||
void listenToOrderEvents();
|
||||
|
||||
Optional<Parcel> getParcelByOrderId(int orderId);
|
||||
|
||||
void setOrderRepository(ShippingOrderRepository orderRepository);
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
module com.baeldung.dddmodules.shippingcontext {
|
||||
requires com.baeldung.dddmodules.sharedkernel;
|
||||
exports com.baeldung.dddmodules.shippingcontext.service;
|
||||
exports com.baeldung.dddmodules.shippingcontext.model;
|
||||
exports com.baeldung.dddmodules.shippingcontext.repository;
|
||||
provides com.baeldung.dddmodules.shippingcontext.service.ShippingService
|
||||
with com.baeldung.dddmodules.shippingcontext.service.ParcelShippingService;
|
||||
}
|
2
pom.xml
2
pom.xml
|
@ -408,6 +408,7 @@
|
|||
<module>dagger</module>
|
||||
<module>data-structures</module>
|
||||
<module>ddd</module>
|
||||
<!-- <module>ddd-modules</module>--> <!-- we haven't upgraded to Java 9 -->
|
||||
<module>deeplearning4j</module>
|
||||
<module>disruptor</module>
|
||||
<module>dozer</module>
|
||||
|
@ -919,6 +920,7 @@
|
|||
<module>dagger</module>
|
||||
<module>data-structures</module>
|
||||
<module>ddd</module>
|
||||
<!-- <module>ddd-modules</module>--> <!-- we haven't upgraded to Java 9 -->
|
||||
<module>deeplearning4j</module>
|
||||
<module>disruptor</module>
|
||||
<module>dozer</module>
|
||||
|
|
Loading…
Reference in New Issue