Quarkus Qlue is a framework which wires up independent steps into an overall ordered chain of tasks to run. The ordering uses a produces/consumes model based on items.
The chain is assembled using a chain builder which, as one might expect, follows a builder pattern to provide initial items, express final item requirements, and add steps which can produce and/or consume items.
In this basic example, a step method executes which produces a simple item that can be accessed at the end of the chain's execution.
public final class MessageItem extends SimpleItem {
private final String message;
public MessageItem(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
// later...
public final class Main {
private Main() {}
public static void main(String[] args) {
// Build a new chain
final ChainBuilder builder = Chain.builder();
builder.addStepObject(new Object() {
@Step
public MessageItem produceHelloWorld() {
return new MessageItem("Hello World");
}
});
// make sure the step is run
builder.addFinal(MessageItem.class);
// create the reusable chain
Chain chain = builder.build();
// now create a new execution of the chain
ExecutionBuilder executionBuilder = chain.createExecutionBuilder();
// run the chain using a trivial executor
final Result result = executionBuilder.execute(Runnable::run);
if (result.isSuccess()) {
String message = result.asSuccess().consume(MessageItem.class).getMessage();
System.out.println(message);
} else {
System.err.println("Oh no :(");
}
}
The chain builder is used to assemble the list of step objects, step classes, and raw steps. Additionally, it can be used to specify initial input items and final output items (as shown in the trivial example above).
There are three essential kinds of item which can be used with Qlue:
-
Simple items, which are
final
classes which extend theSimpleItem
base class; such items can be produced by one step and consumed by many steps -
Multi items, which are
final
classes which extend theMultiItem
base class; such items can be produced by many steps and consumed by many steps -
Empty items, which are used only for ordering and cannot be constructed; such items can only be used in ordering constraints (e.g. annotations such as
@BeforeConsume
or@AfterProduce
)
Steps are scheduled such that any step which consumes an item executes after any step which produces the item.
A step method is an accessible method which is annotated with the @Step
annotation. The parameters of a step method must be one of the following types:
-
A simple item, indicating that the item of that type is consumed by the step
-
A
List
of a multi item, indicating that all items of the item's type are consumed by the step -
An
Optional
of a simple item, indicating that the item of the `Optional’s type is optionally consumed by the step -
A
Consumer
of a simple item or multi item, indicating that the item is produced by the step by passing the produced item into theConsumer
The return type of the method must be one of the following:
-
void
-
A simple item, indicating that the item is produced by the method
-
A multi item, indicating that the item is produced by the method
In cases where an item is returned, returning a null
will cause consumers of the item to receive a null
value for that item.
@Step
public MessageItem assembleMessage(NameItem name, GreetingItem) {
return new MessageItem(greeting.getGreeting() + ", " + name.getName());
}
A step object is any Java object which contains zero or more accessible methods which are annotated with the @Step
annotation.
Such objects can be passed in to the chain builder directly. The object will be used as-is, with the object’s step methods being invoked as needed during the build process.
A step class is a Java class with a single accessible constructor which contains zero or more accessible methods which are annotated with the @Step
annotation.
When a step class is added to the chain builder, an instance of the class will be instantiated using the accessible constructor. This instance will be used to receive invocations of the step methods as needed during the build process.