|
| 1 | +# Java SDK Advanced Features |
| 2 | + |
| 3 | +## Schedules |
| 4 | + |
| 5 | +Create recurring workflow executions. |
| 6 | + |
| 7 | +```java |
| 8 | +import io.temporal.client.schedules.*; |
| 9 | + |
| 10 | +ScheduleClient scheduleClient = ScheduleClient.newInstance(service); |
| 11 | + |
| 12 | +// Create a schedule |
| 13 | +String scheduleId = "daily-report"; |
| 14 | +ScheduleHandle handle = scheduleClient.createSchedule( |
| 15 | + scheduleId, |
| 16 | + Schedule.newBuilder() |
| 17 | + .setAction( |
| 18 | + ScheduleActionStartWorkflow.newBuilder() |
| 19 | + .setWorkflowType(DailyReportWorkflow.class) |
| 20 | + .setOptions( |
| 21 | + WorkflowOptions.newBuilder() |
| 22 | + .setWorkflowId("daily-report") |
| 23 | + .setTaskQueue("reports") |
| 24 | + .build() |
| 25 | + ) |
| 26 | + .build() |
| 27 | + ) |
| 28 | + .setSpec( |
| 29 | + ScheduleSpec.newBuilder() |
| 30 | + .setIntervals( |
| 31 | + List.of(new ScheduleIntervalSpec(Duration.ofDays(1))) |
| 32 | + ) |
| 33 | + .build() |
| 34 | + ) |
| 35 | + .build(), |
| 36 | + ScheduleOptions.newBuilder().build() |
| 37 | +); |
| 38 | + |
| 39 | +// Manage schedules |
| 40 | +ScheduleHandle scheduleHandle = scheduleClient.getHandle(scheduleId); |
| 41 | +scheduleHandle.pause("Maintenance window"); |
| 42 | +scheduleHandle.unpause(); |
| 43 | +scheduleHandle.trigger(); // Run immediately |
| 44 | +scheduleHandle.delete(); |
| 45 | +``` |
| 46 | + |
| 47 | +## Async Activity Completion |
| 48 | + |
| 49 | +For activities that complete asynchronously (e.g., human tasks, external callbacks). |
| 50 | +If you configure a heartbeat timeout on this activity, the external completer is responsible for sending heartbeats via the async handle. |
| 51 | + |
| 52 | +**Note:** If the external system can reliably Signal back with the result and doesn't need to Heartbeat or receive Cancellation, consider using **signals** instead. |
| 53 | + |
| 54 | +```java |
| 55 | +public class ApprovalActivitiesImpl implements ApprovalActivities { |
| 56 | + @Override |
| 57 | + public String requestApproval(String requestId) { |
| 58 | + ActivityExecutionContext ctx = Activity.getExecutionContext(); |
| 59 | + |
| 60 | + // Get task token for async completion |
| 61 | + byte[] taskToken = ctx.getTaskToken(); |
| 62 | + |
| 63 | + // Store task token for later completion (e.g., in database) |
| 64 | + storeTaskToken(requestId, taskToken); |
| 65 | + |
| 66 | + // Mark this activity as waiting for external completion |
| 67 | + ctx.doNotCompleteOnReturn(); |
| 68 | + |
| 69 | + return null; // Return value is ignored |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// Later, complete the activity from another process |
| 74 | +public void completeApproval(String requestId, boolean approved) { |
| 75 | + WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs(); |
| 76 | + WorkflowClient client = WorkflowClient.newInstance(service); |
| 77 | + |
| 78 | + ActivityCompletionClient completionClient = client.newActivityCompletionClient(); |
| 79 | + |
| 80 | + byte[] taskToken = getTaskToken(requestId); |
| 81 | + |
| 82 | + if (approved) { |
| 83 | + completionClient.complete(taskToken, "approved"); |
| 84 | + } else { |
| 85 | + completionClient.completeExceptionally( |
| 86 | + taskToken, |
| 87 | + new RuntimeException("Rejected") |
| 88 | + ); |
| 89 | + } |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +## Worker Tuning |
| 94 | + |
| 95 | +Configure worker performance settings. |
| 96 | + |
| 97 | +```java |
| 98 | +WorkerOptions workerOptions = WorkerOptions.newBuilder() |
| 99 | + // Max concurrent workflow task executions (default: 200) |
| 100 | + .setMaxConcurrentWorkflowTaskExecutionSize(200) |
| 101 | + // Max concurrent activity executions (default: 200) |
| 102 | + .setMaxConcurrentActivityExecutionSize(200) |
| 103 | + // Max concurrent local activity executions (default: 200) |
| 104 | + .setMaxConcurrentLocalActivityExecutionSize(200) |
| 105 | + // Max workflow task pollers (default: 5) |
| 106 | + .setMaxConcurrentWorkflowTaskPollers(5) |
| 107 | + // Max activity task pollers (default: 5) |
| 108 | + .setMaxConcurrentActivityTaskPollers(5) |
| 109 | + .build(); |
| 110 | + |
| 111 | +WorkerFactory factory = WorkerFactory.newInstance(client); |
| 112 | +Worker worker = factory.newWorker("my-queue", workerOptions); |
| 113 | +worker.registerWorkflowImplementationTypes(MyWorkflowImpl.class); |
| 114 | +worker.registerActivitiesImplementations(new MyActivitiesImpl()); |
| 115 | +factory.start(); |
| 116 | +``` |
| 117 | + |
| 118 | +## Workflow Failure Exception Types |
| 119 | + |
| 120 | +Control which exceptions cause workflow failures vs workflow task failures. |
| 121 | + |
| 122 | +By default, only `ApplicationFailure` (and its subclasses) fail the workflow execution. All other exceptions fail the **workflow task**, causing the task to retry indefinitely until the code is fixed or the workflow is terminated. |
| 123 | + |
| 124 | +### Per-Workflow Configuration |
| 125 | + |
| 126 | +Use `WorkflowImplementationOptions` to specify which exception types should fail the workflow: |
| 127 | + |
| 128 | +```java |
| 129 | +Worker worker = factory.newWorker("my-queue"); |
| 130 | +worker.registerWorkflowImplementationTypes( |
| 131 | + WorkflowImplementationOptions.newBuilder() |
| 132 | + .setFailWorkflowExceptionTypes( |
| 133 | + IllegalArgumentException.class, |
| 134 | + CustomBusinessException.class |
| 135 | + ) |
| 136 | + .build(), |
| 137 | + MyWorkflowImpl.class |
| 138 | +); |
| 139 | +``` |
| 140 | + |
| 141 | +With this configuration, `IllegalArgumentException` and `CustomBusinessException` thrown from the workflow will fail the workflow execution instead of just the workflow task. |
| 142 | + |
| 143 | +### Worker-Level Configuration |
| 144 | + |
| 145 | +Apply to all workflows registered on the worker: |
| 146 | + |
| 147 | +```java |
| 148 | +WorkerFactoryOptions factoryOptions = WorkerFactoryOptions.newBuilder() |
| 149 | + .setWorkflowHostLocalTaskQueueScheduleToStartTimeout(Duration.ofSeconds(10)) |
| 150 | + .build(); |
| 151 | +WorkerFactory factory = WorkerFactory.newInstance(client, factoryOptions); |
| 152 | + |
| 153 | +Worker worker = factory.newWorker("my-queue"); |
| 154 | +// Register each workflow type with its own failure exception types |
| 155 | +worker.registerWorkflowImplementationTypes( |
| 156 | + WorkflowImplementationOptions.newBuilder() |
| 157 | + .setFailWorkflowExceptionTypes( |
| 158 | + IllegalArgumentException.class, |
| 159 | + CustomBusinessException.class |
| 160 | + ) |
| 161 | + .build(), |
| 162 | + MyWorkflowImpl.class, |
| 163 | + AnotherWorkflowImpl.class |
| 164 | +); |
| 165 | +``` |
| 166 | + |
| 167 | +- **Tip for testing:** Set `setFailWorkflowExceptionTypes(Throwable.class)` so any unhandled exception fails the workflow immediately rather than retrying the workflow task forever. This surfaces bugs faster. |
0 commit comments