|
33 | 33 | import io.temporal.worker.WorkerFactoryOptions; |
34 | 34 |
|
35 | 35 | public class Starter { |
36 | | - static final String TASK_QUEUE = "AutoheartbeatTaskQueue"; |
37 | | - static final String WORKFLOW_ID = "AutoHeartbeatWorkflow"; |
38 | | - |
39 | | - public static void main(String[] args) { |
40 | | - WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs(); |
41 | | - WorkflowClient client = WorkflowClient.newInstance(service); |
42 | | - |
43 | | - // Configure our auto heartbeat workflow interceptor which will apply |
44 | | - // AutoHeartbeaterUtil to each activity workflow schedules which has a heartbeat |
45 | | - // timeout configured |
46 | | - WorkerFactoryOptions wfo = |
47 | | - WorkerFactoryOptions.newBuilder() |
48 | | - .setWorkerInterceptors(new AutoHeartbeatWorkerInterceptor()) |
49 | | - .build(); |
50 | | - |
51 | | - WorkerFactory factory = WorkerFactory.newInstance(client, wfo); |
52 | | - Worker worker = factory.newWorker(TASK_QUEUE); |
53 | | - |
54 | | - worker.registerWorkflowImplementationTypes(AutoWorkflowImpl.class); |
55 | | - worker.registerActivitiesImplementations(new AutoActivitiesImpl()); |
56 | | - |
57 | | - factory.start(); |
58 | | - |
59 | | - // first run completes execution with autoheartbeat utils |
60 | | - firstRun(client); |
61 | | - // second run cancels running (pending) activity via signal (specific scope cancel) |
62 | | - secondRun(client); |
63 | | - // third run cancels running execution which cancels activity as well |
64 | | - thirdRun(client); |
65 | | - // fourth run turns off autoheartbeat for activities and lets activity time out on heartbeat |
66 | | - // timeout |
67 | | - fourthRun(client); |
68 | | - |
69 | | - System.exit(0); |
| 36 | + static final String TASK_QUEUE = "AutoheartbeatTaskQueue"; |
| 37 | + static final String WORKFLOW_ID = "AutoHeartbeatWorkflow"; |
| 38 | + |
| 39 | + public static void main(String[] args) { |
| 40 | + WorkflowServiceStubs service = WorkflowServiceStubs.newLocalServiceStubs(); |
| 41 | + WorkflowClient client = WorkflowClient.newInstance(service); |
| 42 | + |
| 43 | + // Configure our auto heartbeat workflow interceptor which will apply |
| 44 | + // AutoHeartbeaterUtil to each activity workflow schedules which has a heartbeat |
| 45 | + // timeout configured |
| 46 | + WorkerFactoryOptions wfo = |
| 47 | + WorkerFactoryOptions.newBuilder() |
| 48 | + .setWorkerInterceptors(new AutoHeartbeatWorkerInterceptor()) |
| 49 | + .build(); |
| 50 | + |
| 51 | + WorkerFactory factory = WorkerFactory.newInstance(client, wfo); |
| 52 | + Worker worker = factory.newWorker(TASK_QUEUE); |
| 53 | + |
| 54 | + worker.registerWorkflowImplementationTypes(AutoWorkflowImpl.class); |
| 55 | + worker.registerActivitiesImplementations(new AutoActivitiesImpl()); |
| 56 | + |
| 57 | + factory.start(); |
| 58 | + |
| 59 | + // first run completes execution with autoheartbeat utils |
| 60 | + firstRun(client); |
| 61 | + // second run cancels running (pending) activity via signal (specific scope cancel) |
| 62 | + secondRun(client); |
| 63 | + // third run cancels running execution which cancels activity as well |
| 64 | + thirdRun(client); |
| 65 | + // fourth run turns off autoheartbeat for activities and lets activity time out on heartbeat |
| 66 | + // timeout |
| 67 | + fourthRun(client); |
| 68 | + |
| 69 | + System.exit(0); |
| 70 | + } |
| 71 | + |
| 72 | + @SuppressWarnings("unused") |
| 73 | + private static void firstRun(WorkflowClient client) { |
| 74 | + System.out.println("**** First Run: run workflow to completion"); |
| 75 | + AutoWorkflow firstRun = |
| 76 | + client.newWorkflowStub( |
| 77 | + AutoWorkflow.class, |
| 78 | + WorkflowOptions.newBuilder() |
| 79 | + .setWorkflowId(WORKFLOW_ID) |
| 80 | + .setTaskQueue(TASK_QUEUE) |
| 81 | + .build()); |
| 82 | + |
| 83 | + try { |
| 84 | + String firstRunResult = firstRun.exec("Auto heartbeating is cool"); |
| 85 | + System.out.println("First run result: " + firstRunResult); |
| 86 | + } catch (Exception e) { |
| 87 | + System.out.println("First run - Workflow exec exception: " + e.getClass().getName()); |
70 | 88 | } |
71 | | - |
72 | | - @SuppressWarnings("unused") |
73 | | - private static void firstRun(WorkflowClient client) { |
74 | | - System.out.println("**** First Run: run workflow to completion"); |
75 | | - AutoWorkflow firstRun = |
76 | | - client.newWorkflowStub( |
77 | | - AutoWorkflow.class, |
78 | | - WorkflowOptions.newBuilder() |
79 | | - .setWorkflowId(WORKFLOW_ID) |
80 | | - .setTaskQueue(TASK_QUEUE) |
81 | | - .build()); |
82 | | - |
83 | | - try { |
84 | | - String firstRunResult = firstRun.exec("Auto heartbeating is cool"); |
85 | | - System.out.println("First run result: " + firstRunResult); |
86 | | - } catch (Exception e) { |
87 | | - System.out.println("First run - Workflow exec exception: " + e.getClass().getName()); |
88 | | - } |
| 89 | + } |
| 90 | + |
| 91 | + @SuppressWarnings("unused") |
| 92 | + private static void secondRun(WorkflowClient client) { |
| 93 | + System.out.println("\n\n**** Second Run: cancel activities via signal"); |
| 94 | + AutoWorkflow secondRun = |
| 95 | + client.newWorkflowStub( |
| 96 | + AutoWorkflow.class, |
| 97 | + WorkflowOptions.newBuilder() |
| 98 | + .setWorkflowId(WORKFLOW_ID) |
| 99 | + .setTaskQueue(TASK_QUEUE) |
| 100 | + .build()); |
| 101 | + WorkflowClient.start(secondRun::exec, "Auto heartbeating is cool"); |
| 102 | + doSleeps(4); |
| 103 | + secondRun.cancelActivity(); |
| 104 | + |
| 105 | + try { |
| 106 | + String secondRunResult = WorkflowStub.fromTyped(secondRun).getResult(String.class); |
| 107 | + System.out.println("Second run result: " + secondRunResult); |
| 108 | + } catch (Exception e) { |
| 109 | + System.out.println("Second run - Workflow exec exception: " + e.getClass().getName()); |
89 | 110 | } |
90 | | - |
91 | | - @SuppressWarnings("unused") |
92 | | - private static void secondRun(WorkflowClient client) { |
93 | | - System.out.println("\n\n**** Second Run: cancel activities via signal"); |
94 | | - AutoWorkflow secondRun = |
95 | | - client.newWorkflowStub( |
96 | | - AutoWorkflow.class, |
97 | | - WorkflowOptions.newBuilder() |
98 | | - .setWorkflowId(WORKFLOW_ID) |
99 | | - .setTaskQueue(TASK_QUEUE) |
100 | | - .build()); |
101 | | - WorkflowClient.start(secondRun::exec, "Auto heartbeating is cool"); |
102 | | - doSleeps(4); |
103 | | - secondRun.cancelActivity(); |
104 | | - |
105 | | - try { |
106 | | - String secondRunResult = WorkflowStub.fromTyped(secondRun).getResult(String.class); |
107 | | - System.out.println("Second run result: " + secondRunResult); |
108 | | - } catch (Exception e) { |
109 | | - System.out.println("Second run - Workflow exec exception: " + e.getClass().getName()); |
110 | | - } |
| 111 | + } |
| 112 | + |
| 113 | + @SuppressWarnings("unused") |
| 114 | + private static void thirdRun(WorkflowClient client) { |
| 115 | + System.out.println("\n\n**** Third Run: cancel workflow execution"); |
| 116 | + AutoWorkflow thirdRun = |
| 117 | + client.newWorkflowStub( |
| 118 | + AutoWorkflow.class, |
| 119 | + WorkflowOptions.newBuilder() |
| 120 | + .setWorkflowId(WORKFLOW_ID) |
| 121 | + .setTaskQueue(TASK_QUEUE) |
| 122 | + .build()); |
| 123 | + WorkflowClient.start(thirdRun::exec, "Auto heartbeating is cool"); |
| 124 | + doSleeps(10); |
| 125 | + try { |
| 126 | + WorkflowStub.fromTyped(thirdRun).cancel(); |
| 127 | + String thirdRunResult = WorkflowStub.fromTyped(thirdRun).getResult(String.class); |
| 128 | + System.out.println("Third run result: " + thirdRunResult); |
| 129 | + } catch (Exception e) { |
| 130 | + // we are expecting workflow cancelation |
| 131 | + if (e.getCause() instanceof CanceledFailure) { |
| 132 | + System.out.println("Third run - Workflow execution canceled."); |
| 133 | + } else { |
| 134 | + System.out.println("Third run - Workflow exec exception: " + e.getMessage()); |
| 135 | + } |
111 | 136 | } |
112 | | - |
113 | | - @SuppressWarnings("unused") |
114 | | - private static void thirdRun(WorkflowClient client) { |
115 | | - System.out.println("\n\n**** Third Run: cancel workflow execution"); |
116 | | - AutoWorkflow thirdRun = |
117 | | - client.newWorkflowStub( |
118 | | - AutoWorkflow.class, |
119 | | - WorkflowOptions.newBuilder() |
120 | | - .setWorkflowId(WORKFLOW_ID) |
121 | | - .setTaskQueue(TASK_QUEUE) |
122 | | - .build()); |
123 | | - WorkflowClient.start(thirdRun::exec, "Auto heartbeating is cool"); |
124 | | - doSleeps(10); |
125 | | - try { |
126 | | - WorkflowStub.fromTyped(thirdRun).cancel(); |
127 | | - String thirdRunResult = WorkflowStub.fromTyped(thirdRun).getResult(String.class); |
128 | | - System.out.println("Third run result: " + thirdRunResult); |
129 | | - } catch (Exception e) { |
130 | | - // we are expecting workflow cancelation |
131 | | - if (e.getCause() instanceof CanceledFailure) { |
132 | | - System.out.println("Third run - Workflow execution canceled."); |
133 | | - } else { |
134 | | - System.out.println("Third run - Workflow exec exception: " + e.getMessage()); |
135 | | - } |
136 | | - } |
137 | | - } |
138 | | - |
139 | | - @SuppressWarnings("unused") |
140 | | - private static void fourthRun(WorkflowClient client) { |
141 | | - System.out.println("\n\n**** Fourth Run: cause heartbeat timeout"); |
142 | | - // we disable autoheartbeat via env var |
143 | | - System.setProperty("sample.disableAutoHeartbeat", "true"); |
144 | | - AutoWorkflow fourth = |
145 | | - client.newWorkflowStub( |
146 | | - AutoWorkflow.class, |
147 | | - WorkflowOptions.newBuilder() |
148 | | - .setWorkflowId(WORKFLOW_ID) |
149 | | - .setTaskQueue(TASK_QUEUE) |
150 | | - .build()); |
151 | | - |
152 | | - try { |
153 | | - String fourthRunResult = fourth.exec("Auto heartbeating is cool"); |
154 | | - System.out.println("Fourth run result: " + fourthRunResult); |
155 | | - } catch (Exception e) { |
156 | | - System.out.println("Fourth run - Workflow exec exception: " + e.getClass().getName()); |
157 | | - } |
| 137 | + } |
| 138 | + |
| 139 | + @SuppressWarnings("unused") |
| 140 | + private static void fourthRun(WorkflowClient client) { |
| 141 | + System.out.println("\n\n**** Fourth Run: cause heartbeat timeout"); |
| 142 | + // we disable autoheartbeat via env var |
| 143 | + System.setProperty("sample.disableAutoHeartbeat", "true"); |
| 144 | + AutoWorkflow fourth = |
| 145 | + client.newWorkflowStub( |
| 146 | + AutoWorkflow.class, |
| 147 | + WorkflowOptions.newBuilder() |
| 148 | + .setWorkflowId(WORKFLOW_ID) |
| 149 | + .setTaskQueue(TASK_QUEUE) |
| 150 | + .build()); |
| 151 | + |
| 152 | + try { |
| 153 | + String fourthRunResult = fourth.exec("Auto heartbeating is cool"); |
| 154 | + System.out.println("Fourth run result: " + fourthRunResult); |
| 155 | + } catch (Exception e) { |
| 156 | + System.out.println("Fourth run - Workflow exec exception: " + e.getClass().getName()); |
158 | 157 | } |
| 158 | + } |
159 | 159 |
|
160 | | - private static void doSleeps(int seconds) { |
161 | | - try { |
162 | | - Thread.sleep(seconds * 1000L); |
163 | | - } catch (Exception e) { |
164 | | - System.out.println(e.getMessage()); |
165 | | - } |
| 160 | + private static void doSleeps(int seconds) { |
| 161 | + try { |
| 162 | + Thread.sleep(seconds * 1000L); |
| 163 | + } catch (Exception e) { |
| 164 | + System.out.println(e.getMessage()); |
166 | 165 | } |
| 166 | + } |
167 | 167 | } |
0 commit comments