BAEL-2435 Switch domain around

Change the domain from 'Messages' to 'Order' as that domain is better
suited for an example project like this. The Query Model would be better
 suited to revolve around the OrderedProducts rather than just printing
 out a line of text. To that end, add a OrderedProduct model with the
 OrderStatus, which is updated in the (renamed)
 OrderedProductsEventHandler upon all the events
This commit is contained in:
Steven van Beelen 2018-12-27 10:36:10 +01:00
parent eab6dfe0dd
commit 84b6dc1dbb
4 changed files with 104 additions and 21 deletions

View File

@ -0,0 +1,7 @@
package com.baeldung.axon.coreapi.queries;
public enum OrderStatus {
PLACED, CONFIRMED, SHIPPED
}

View File

@ -0,0 +1,55 @@
package com.baeldung.axon.coreapi.queries;
import java.util.Objects;
public class OrderedProduct {
private final String orderId;
private final String product;
private OrderStatus orderStatus;
public OrderedProduct(String orderId, String product) {
this.orderId = orderId;
this.product = product;
orderStatus = OrderStatus.PLACED;
}
public String getOrderId() {
return orderId;
}
public String getProduct() {
return product;
}
public OrderStatus getOrderStatus() {
return orderStatus;
}
public void setOrderConfirmed() {
this.orderStatus = OrderStatus.CONFIRMED;
}
public void setOrderShipped() {
this.orderStatus = OrderStatus.SHIPPED;
}
@Override
public int hashCode() {
return Objects.hash(orderId, product, orderStatus);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final OrderedProduct other = (OrderedProduct) obj;
return Objects.equals(this.orderId, other.orderId)
&& Objects.equals(this.product, other.product)
&& Objects.equals(this.orderStatus, other.orderStatus);
}
}

View File

@ -1,21 +0,0 @@
package com.baeldung.axon.querymodel;
import org.axonframework.eventhandling.EventHandler;
import org.springframework.stereotype.Service;
import com.baeldung.axon.coreapi.events.MessageCreatedEvent;
import com.baeldung.axon.coreapi.events.MessageReadEvent;
@Service
public class MessagesEventHandler {
@EventHandler
public void handle(MessageCreatedEvent event) {
System.out.println("Message received: " + event.getText() + " (" + event.getId() + ")");
}
@EventHandler
public void handle(MessageReadEvent event) {
System.out.println("Message read: " + event.getId());
}
}

View File

@ -0,0 +1,42 @@
package com.baeldung.axon.querymodel;
import java.util.HashMap;
import java.util.Map;
import org.axonframework.eventhandling.EventHandler;
import org.springframework.stereotype.Service;
import com.baeldung.axon.coreapi.queries.OrderedProduct;
import com.baeldung.axon.coreapi.events.OrderConfirmedEvent;
import com.baeldung.axon.coreapi.events.OrderPlacedEvent;
import com.baeldung.axon.coreapi.events.OrderShippedEvent;
@Service
public class OrderedProductsEventHandler {
private final Map<String, OrderedProduct> orderedProducts = new HashMap<>();
@EventHandler
public void on(OrderPlacedEvent event) {
String orderId = event.getOrderId();
orderedProducts.put(orderId, new OrderedProduct(orderId, event.getProduct()));
}
@EventHandler
public void on(OrderShippedEvent event) {
orderedProducts.computeIfPresent(event.getOrderId(), (orderId, orderedProduct) -> {
orderedProduct.setOrderShipped();
return orderedProduct;
});
}
@EventHandler
public void on(OrderConfirmedEvent event) {
orderedProducts.computeIfPresent(event.getOrderId(), (orderId, orderedProduct) -> {
orderedProduct.setOrderConfirmed();
return orderedProduct;
});
}
}