A kata to learn and practice Choreography as opposed to Orchestration, as in a microservices architecture
Below the english and french versions.
Let's consider a system to sell theater tickets online. It's made of:
requestBooking(int numberOfSeats)
function (that prints "booking requested" in the standard out while pretending it's done its work)reserveInventory(int numberOfSeats)
function (that checks if there's enough capacity left, then decrements the internal capacity counter then prints the remaining capacity in the standard out)printTicket(int numberOfSeats)
function (that prints "ticket printed" in the standard out while pretending it's done its work)Traditional Approach
Alternative Approach
/** The listener interface */
public interface Listener {
void onMessage(Object msg);
}
/**
* A simple in-memory, observer-pattern-based single-threaded message bus for designing architecture and testing using unit tests before switching to using actual middleware
*/
public class MessageBus {
private List<Listener> subs = new ArrayList<Listener>();
public void subscribe(Listener l) {
this.subs.add(l);
}
public void publish(Object msg) {
for (Listener l : subs) {
l.onMessage(msg);
}
}
}
You may want to restrict the messages on the bus to be only events:
/**
* A basic event with a name and one single integer value
*/
public class Event {
private final String name;
private final int value;
public Event(String name, int value) {
this.name = name;
this.value = value;
}
public String getName() {return name;}
public int getValue() {return value;}
}
This kata covers the following aspects: Event-Driven Architecture, Choreography over Orchestration and Smart Endpoints, Dump Pipes, which together form the microservices architectural style.
Considérons un système de distributions de billets de spectacles en ligne. Le processus de vente consiste typiquement à Réserver (Booking), puis à réduire l'inventaire en correspondance s'il reste suffisamment de place (Inventory), puis à envoyer les billets (Ticketing), chacune de ces étapes étant un service distinct :
requestBooking(int numberOfSeats)
qui imprime "booking requested" dans la sortie standard quand l'action est considérée comme faite.reserveInventory(int numberOfSeats)
qui vérifie qu'il y a assez de places, et dans ce cas décrémente le compteur interne de places disponibles et affiche le nombre de places restantes.printTicket(int numberOfSeats)
qui affiche "ticket printed" quand l'action est considérée comme faite.Approche traditionnelle
Approche alternative
Si vous le souhaitez, vous pouvez aussi n'envoyer que des messages de type événements métier.
Ce kata couvre les aspects Event-Driven Architecture, Choreography over Orchestration et Smart Endpoints, Dump Pipes, qui ensemble font le style d'architecture microservices.