Giving a step the tools it needs
What you want to do
The charge talks to the bank, and the shipping to the carrier. You want each step to ask for the tool it needs and use it, without knowing which bank or which carrier it is, while the workflow decides which one it gets.
The code
Each tool is an interface that says what the tool can do, and nothing more: a payment client that charges, a carrier that books and collects. These interfaces are all a step knows of its tools.
/** A payment client: it charges an order's amount, and gives back a receipt. */
interface Payments {
String charge(String order, long amount);
}
/**
* A carrier: it books a parcel and gives back its tracking number, then says whether it collects a
* parcel of this weight in kg.
*/
interface Carrier {
String book(String order);
boolean collects(long weight);
}
The shop's bank and carrier are still faked, now as classes implementing each interface, and print what they do as before. The steps are those of chapter 6, with one change: each asks for its tool as a parameter of run marked @Service, next to its contributor, and calls the tool instead of a fake's static method.
/** Charges an order's price through the payment client, and gives back its receipt. */
@Step
final class Charge implements ItineraStep {
private final String order;
private final long price;
Charge(@Input String order, @Input long price) {
this.order = order;
this.price = price;
}
public Outcome run(@Service Payments payments, Contributor contributor) {
if (price <= 0) return fail("nothing-to-charge");
String receipt = payments.charge(order, price);
contributor.contribute("receipt", receipt);
return succeed();
}
}
/**
* Ships an order through the carrier, with its receipt in the parcel, and gives back its tracking
* number.
*/
@Step
final class Ship implements ItineraStep {
private final String order;
private final long weight;
private final String receipt;
Ship(@Input String order, @Input long weight, @Input String receipt) {
this.order = order;
this.weight = weight;
this.receipt = receipt;
}
public Outcome run(@Service Carrier carrier, Contributor contributor) {
String tracking = carrier.book(order);
contributor.contribute("tracking", tracking);
if (!carrier.collects(weight)) return fail("too-heavy");
System.out.println("the parcel leaves with receipt " + receipt);
return succeed();
}
}
The workflow lists the same steps, and its class gets a body: two methods marked @Provider, each making one tool. Each prints when the shop gets its tool.
/** The shop's workflow: it charges, then ships, with the services each step needs. */
@Workflow(name = "orders")
@WithStep(step = Charge.class, name = "charge")
@WithStep(step = Ship.class, name = "ship")
final class Orders implements ItineraWorkflow {
/** The bank, as the payment client of the journey. */
@Provider(key = "payments")
public Provided<Payments> bank() {
System.out.println("the shop connects to the bank");
return provide(new Bank());
}
/** The main carrier, for the journey. */
@Provider
public Provided<Carrier> carrier() {
System.out.println("the shop calls the carrier");
return provide(new MainCarrier(20));
}
}
The shop runs Ann's and Gil's orders as in chapter 6, with its NewOrder and Order unchanged, and reads back the same output. It prints:
Ann's order:
the shop connects to the bank
the bank charges 40 for order A-1
the shop calls the carrier
the carrier books order A-1
the parcel leaves with receipt R-A-1
the journey succeeded, with receipt R-A-1 and tracking T-A-1
Gil's order:
the shop connects to the bank
the bank charges 45 for order A-7
the shop calls the carrier
the carrier books order A-7
the journey failed, with receipt R-A-7 and tracking noneHow it works
- A tool a step uses is a service. A payment client or a carrier is something a step uses to do its business, and in Itinera it is a service. It isn't a value: it never goes into the data bag. In Java it is an object, of the interface the step asks for.
- A step asks for a service by a key, and by its type. The parameter
@Service Payments paymentsasks for the service under the keypayments, the parameter's name, and says it must be aPayments. The step asks for the interface, never for the shop's bank itself: it uses what it gets without knowing which implementation it is, or where it came from. - The workflow provides each service. For every key a step asks for, the workflow has a provider: a method of the workflow's class marked
@Provider, fromorg.itineradev.itinera.service, which makes the service. Its key is the method's name, as forcarrier, unlesskeynames another, asbankprovidespayments. A provider returnsprovide(...)with the object it made, a method thatItineraWorkflowgives the class, and its return type,Provided<Payments>, makes the compiler check that the object is aPayments. A step decides what it does with a service; the workflow decides which service it gets. To charge through another bank, you change the provider, and no step. - A service is made once for each journey, the first time a step asks for it. The output shows when. In Ann's journey, the bank is connected just before her charge, and the carrier called just before her shipping. Gil's journey connects to the bank and calls the carrier again: every journey gets services of its own, and lets them go when it ends.
- The step has the service only for its attempt. The service is lent to the step for its attempt, as its contributor is. That is why it is a parameter of
run, and never of the constructor: the step's fields hold its data, and nothing it borrows. - What a service answers is the step's to handle. The carrier says it won't collect Gil's parcel of 60 kg, and the shipping turns that answer into its outcome, a failure with the reason
too-heavy, as it did in chapter 6. The executor never sees what a service answers, only the outcome the step reports.
So the steps no longer reach the bank or the carrier on their own, and the workflow says which ones they use. Now look at the output again. Every line of it comes from a System.out.println in the shop's own code: the steps, the faked tools and the providers print what they do, and main prints how each journey ended. What the executor did in between, such as when it ran each step and which step's failure ended Gil's journey, the shop sees only where its own code happens to print it.
Outcomes
We have shown how a step asks for the tools it needs, and how the workflow provides them.
| The usual way | With Itinera |
|---|---|
| A method often creates its clients, or is handed them by whoever builds it, so which bank an order uses is decided wherever that happens to be. | A step asks for a service by the interface it needs, and the workflow's provider decides which one it gets. |
| A client is often opened and closed by hand, around the calls that use it. | Each journey gets services of its own, made the first time a step asks and let go when the journey ends. |