How to use a Side Effect in Java
To use a Side Effect in Java, set the sideEffect()
function in your Workflow Execution and return the nondeterministic code.
int random = Workflow.sideEffect(Integer.class, () -> random.nextInt(100));
if random < 50 {
....
} else {
....
}
Here's another example that uses sideEffect()
.
// implementation of the @WorkflowMethod
public void execute() {
int randomInt = Workflow.sideEffect( int.class, () -> {
Random random = new SecureRandom();
return random.nextInt();
});
String userHome = Workflow.sideEffect(String.class, () -> System.getenv("USER_HOME"));
if(randomInt % 2 == 0) {
// ...
} else {
// ...
}
}
Java also provides a deterministic method to generate random numbers or random UUIDs.
To generate random numbers in a deterministic method, use newRandom()
// implementation of the @WorkflowMethod
public void execute() {
int randomInt = Workflow.newRandom().nextInt();
// ...
}
To generate a random UUID in a deterministic method, use randomUUID()
.
// implementation of the @WorkflowMethod
public void execute() {
String randomUUID = Workflow.randomUUID().toString();
// ...
}