The Itinera bookJava edition

Part 2: Getting started, Chapter 11

Naming a journey

What you want to do

Every event says who decided what, and when, and which journey it belongs to, by the journey's ID. You want that ID to be one the shop's people already know, the order's number, so that every decision in the log and at the desk is tied to its order.

The code

The workflow names each journey with a method of its class marked @IdGenerator, from org.itineradev.itinera.workflow. It reads the order's number from the journey's data, and returns it as the journey's ID. The providers are chapter 8's, and Payments and Carrier are the interfaces chapter 7 shows.

Java, from the program ch11_naming_a_journey
/**
 * The shop's workflow: it charges, then ships, every journey goes to the log and the desk, and each
 * is named after its order.
 */
@Workflow(
    name = "orders",
    reporters = {Log.class, Desk.class})
@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() {
    return provide(new Bank());
  }

  /** The main carrier, as the carrier of the journey. */
  @Provider(key = "carrier")
  public Provided<Carrier> mainCarrier() {
    return provide(new MainCarrier(20));
  }

  /** Names each journey after its order. */
  @IdGenerator
  public String orderNumber(DataBag data) {
    String order = data.read("order", TypeToken.of(String.class)).orNull();
    if (order == null) throw new IllegalStateException("an order without a number");
    return order;
  }
}

The log now writes the workflow's name and the journey ID at the start of every line:

Java, from the program ch11_naming_a_journey
/**
 * The shop's log: it writes a line for every event of a journey, starting with the workflow's name
 * and the journey's ID.
 */
final class Log implements Reporter {
  @Override
  public void report(Event event) {
    String line =
        "log "
            + event.getWorkflow()
            + " "
            + event.getJourneyId()
            + " #"
            + event.getSequence()
            + " "
            + event.getKind();
    System.out.println(line + detail(event).map(text -> " " + text).orElse(""));
  }

  /**
   * What the log says after an event's kind, for the kinds it says more about: the step the event
   * is about, and what it names.
   */
  static Optional<String> detail(Event event) {
    if (event instanceof JourneyStarted started) {
      return Optional.of(String.join(", ", started.initialKeys()));
    }
    if (event instanceof AttemptStarted started) return Optional.of(started.step().toString());
    if (event instanceof ServiceProvided provided) {
      return Optional.of(
          provided.step()
              + " gets "
              + provided.key()
              + " from "
              + provided.provider()
              + ", a "
              + provided.implementation());
    }
    if (event instanceof StepSucceeded succeeded) return Optional.of(succeeded.step().toString());
    if (event instanceof StepFailed failed) {
      return Optional.of(failed.step() + ": " + failed.reason().code());
    }
    if (event instanceof ContributionCommitted committed) return Optional.of(committed.key());
    if (event instanceof StepGivenUp givenUp) return Optional.of(givenUp.step().toString());
    if (event instanceof JourneyFailed failed) return Optional.of(failed.step().toString());
    if (event instanceof StepInfo info) return Optional.of(info.step() + ": " + info.message());
    return Optional.empty();
  }
}

detail is the method chapter 8 shows, with the test for the step's message that chapter 9 added.

And the desk says which order failed:

Java, from the program ch11_naming_a_journey
/** The customer service desk: of all the events of a journey, it keeps only a failed journey's. */
final class Desk implements Reporter {
  @Override
  public void report(Event event) {
    if (event instanceof JourneyFailed failed) {
      System.out.println("desk: order " + failed.journeyId() + " failed at " + failed.step());
    }
  }
}

The steps the workflow lists are the shipping of chapter 9 and the charge of chapter 7, and the shop runs Ann's and Gil's orders again. It prints:

What ch11_naming_a_journey prints, recorded in ch11_naming_a_journey.txt
Ann's order:
log orders A-1 #1 journey_started order, price, weight
log orders A-1 #2 attempt_started charge
log orders A-1 #3 service_provided charge gets payments from bank, a ch11_naming_a_journey.Bank
the bank charges 40 for order A-1
log orders A-1 #4 step_succeeded charge
log orders A-1 #5 contribution_committed receipt
log orders A-1 #6 attempt_started ship
log orders A-1 #7 service_provided ship gets carrier from mainCarrier, a ch11_naming_a_journey.MainCarrier
the carrier books order A-1
log orders A-1 #8 step_info ship: the parcel leaves with receipt R-A-1
log orders A-1 #9 step_succeeded ship
log orders A-1 #10 contribution_committed tracking
log orders A-1 #11 journey_succeeded
the journey succeeded, with receipt R-A-1 and tracking T-A-1
Gil's order:
log orders A-7 #1 journey_started order, price, weight
log orders A-7 #2 attempt_started charge
log orders A-7 #3 service_provided charge gets payments from bank, a ch11_naming_a_journey.Bank
the bank charges 45 for order A-7
log orders A-7 #4 step_succeeded charge
log orders A-7 #5 contribution_committed receipt
log orders A-7 #6 attempt_started ship
log orders A-7 #7 service_provided ship gets carrier from mainCarrier, a ch11_naming_a_journey.MainCarrier
the carrier books order A-7
log orders A-7 #8 step_failed ship: too-heavy
log orders A-7 #9 step_given_up ship
log orders A-7 #10 journey_failed ship
desk: order A-7 failed at ship
the journey failed, with receipt R-A-7 and tracking none

How it works

  1. Every journey has a journey ID. It is made once, when the instance is created, and every event of the journey carries it, as getJourneyId() on an Event, or journeyId() on the record of its kind, as the desk reads it from a JourneyFailed: a JourneyId that prints as the ID itself. The executor never makes one of its own, and you never hand one to it.
  2. By default, it is random. Unless the workflow names its journeys, each journey's ID is a new random UUID, different on every run and meaningless to people, which is why the log of chapter 8 left it out.
  3. Naming journeys is the workflow's decision. An ID generator is part of the workflow, like its steps and its providers: a method of the workflow's class marked @IdGenerator. It takes the journey's data, a DataBag from org.itineradev.itinera.journey, or nothing at all when it needs none, and returns the ID as a String. A workflow has at most one. No step and no reporter decides what a journey is called.
  4. It reads the initial data. The generator runs when the instance is created, before any step, so the data bag holds only the initial data. data.read("order", TypeToken.of(String.class)) reads the value under order as a String, and gives back a Read, which says whether the value was there, and of that type. Its orNull() gives the value, or null when it is missing or of another type, and then the generator throws an IllegalStateException with its message. Ann's journey becomes A-1, and Gil's A-7.
  5. The workflow's name says what it does; the ID says which journey. Every event also carries the name of its workflow, as event.getWorkflow(), which chapter 8 listed among its fields: the one in @Workflow(name = "orders", ...), the same for every journey of the workflow. The name should say what the workflow does, as orders says it handles the shop's orders, and the ID should name one journey of it, as an order's number names one order, so the events of each journey can be told apart from every other's.

Now the desk says "order A-7 failed at ship", and the log shows why: the main carrier won't take Gil's parcel of 60 kg. The shop has a backup carrier too, which takes heavier parcels than the main one. But the shipping gets one attempt, with the one carrier its provider makes for the journey, so a parcel the main carrier refuses is never offered to the other.

Outcomes

We have shown how a workflow names its journeys, so every event says which order it is about.

The usual way With Itinera
A log line usually carries an order's number only where the code that writes it remembers to add it. Every event, and so every decision, carries the journey's ID, made once by the workflow from the order's data.