|
| 1 | +package io.temporal.samples.workerversioning; |
| 2 | + |
| 3 | +import io.temporal.activity.ActivityOptions; |
| 4 | +import io.temporal.common.VersioningBehavior; |
| 5 | +import io.temporal.workflow.Workflow; |
| 6 | +import io.temporal.workflow.WorkflowVersioningBehavior; |
| 7 | +import java.time.Duration; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | +import org.slf4j.Logger; |
| 11 | + |
| 12 | +/** |
| 13 | + * This represents us having made *compatible* changes to AutoUpgradingWorkflowV1Impl. |
| 14 | + * |
| 15 | + * <p>The compatible changes we've made are: |
| 16 | + * |
| 17 | + * <ul> |
| 18 | + * <li>Altering the log lines |
| 19 | + * <li>Using the `Workflow.getVersion` API to properly introduce branching behavior while |
| 20 | + * maintaining compatibility |
| 21 | + * </ul> |
| 22 | + */ |
| 23 | +public class AutoUpgradingWorkflowV1bImpl implements AutoUpgradingWorkflow { |
| 24 | + |
| 25 | + private static final Logger logger = Workflow.getLogger(AutoUpgradingWorkflowV1bImpl.class); |
| 26 | + |
| 27 | + private final List<String> signals = new ArrayList<>(); |
| 28 | + private final Activities activities = |
| 29 | + Workflow.newActivityStub( |
| 30 | + Activities.class, |
| 31 | + ActivityOptions.newBuilder().setStartToCloseTimeout(Duration.ofSeconds(10)).build()); |
| 32 | + |
| 33 | + @Override |
| 34 | + @WorkflowVersioningBehavior(VersioningBehavior.AUTO_UPGRADE) |
| 35 | + public void run() { |
| 36 | + logger.info("Changing workflow v1b started. StartTime: {}", Workflow.currentTimeMillis()); |
| 37 | + |
| 38 | + while (true) { |
| 39 | + Workflow.await(() -> !signals.isEmpty()); |
| 40 | + String signal = signals.remove(0); |
| 41 | + |
| 42 | + if ("do-activity".equals(signal)) { |
| 43 | + logger.info("Changing workflow v1b running activity"); |
| 44 | + int version = Workflow.getVersion("DifferentActivity", Workflow.DEFAULT_VERSION, 1); |
| 45 | + if (version == 1) { |
| 46 | + activities.someIncompatibleActivity( |
| 47 | + new Activities.IncompatibleActivityInput("v1b", "hello!")); |
| 48 | + } else { |
| 49 | + // Note it is a valid compatible change to alter the input to an activity. |
| 50 | + // However, because we're using the getVersion API, this branch will never be |
| 51 | + // taken. |
| 52 | + activities.someActivity("v1b"); |
| 53 | + } |
| 54 | + } else { |
| 55 | + logger.info("Concluding workflow v1b"); |
| 56 | + break; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void doNextSignal(String signal) { |
| 63 | + signals.add(signal); |
| 64 | + } |
| 65 | +} |
0 commit comments