Skip to content

Commit 2569b4d

Browse files
authored
Refactored WorkflowExternalInvocationHandler to initialize UntypedWorkflowStubImpl once (#128)
* Refactored WorkflowExternalInvocationHandler to initialize UntypedWorkflowStubImpl once * Changed WorkflowClient to use Optional for runId
1 parent 97219e2 commit 2569b4d

16 files changed

+373
-195
lines changed

src/main/java/com/uber/cadence/client/DuplicateWorkflowException.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.uber.cadence.client;
1919

2020
import com.uber.cadence.WorkflowExecution;
21+
import java.util.Optional;
2122

2223
/**
2324
* This exception is thrown in the following cases:
@@ -51,6 +52,6 @@ public final class DuplicateWorkflowException extends WorkflowException {
5152

5253
public DuplicateWorkflowException(
5354
WorkflowExecution execution, String workflowType, String message) {
54-
super(message, execution, workflowType, null);
55+
super(message, execution, Optional.of(workflowType), null);
5556
}
5657
}

src/main/java/com/uber/cadence/client/UntypedWorkflowStub.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.uber.cadence.client;
1919

2020
import com.uber.cadence.WorkflowExecution;
21+
import java.util.Optional;
2122
import java.util.concurrent.CompletableFuture;
2223
import java.util.concurrent.TimeUnit;
2324
import java.util.concurrent.TimeoutException;
@@ -28,7 +29,7 @@ public interface UntypedWorkflowStub {
2829

2930
WorkflowExecution start(Object... args);
3031

31-
String getWorkflowType();
32+
Optional<String> getWorkflowType();
3233

3334
WorkflowExecution getExecution();
3435

@@ -63,4 +64,6 @@ public interface UntypedWorkflowStub {
6364

6465
/** Request cancellation. */
6566
void cancel();
67+
68+
Optional<WorkflowOptions> getOptions();
6669
}

src/main/java/com/uber/cadence/client/WorkflowClient.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.uber.cadence.workflow.Functions.Proc4;
3232
import com.uber.cadence.workflow.Functions.Proc5;
3333
import com.uber.cadence.workflow.Functions.Proc6;
34+
import java.util.Optional;
3435
import java.util.concurrent.CompletableFuture;
3536

3637
/**
@@ -174,11 +175,22 @@ static WorkflowClient newInstance(
174175
* Creates workflow client stub for a known execution. Use it to send signals or queries to a
175176
* running workflow. Do not call methods annotated with @WorkflowMethod.
176177
*
177-
* @param workflowInterface interface that given workflow implements
178-
* @param execution workflow id and optional run id for execution
178+
* @param workflowInterface interface that given workflow implements.
179+
* @param workflowId Workflow id.
180+
* @return Stub that implements workflowInterface and can be used to signal or query it.
181+
*/
182+
<T> T newWorkflowStub(Class<T> workflowInterface, String workflowId);
183+
184+
/**
185+
* Creates workflow client stub for a known execution. Use it to send signals or queries to a
186+
* running workflow. Do not call methods annotated with @WorkflowMethod.
187+
*
188+
* @param workflowInterface interface that given workflow implements.
189+
* @param workflowId Workflow id.
190+
* @param runId Run id of the workflow execution.
179191
* @return Stub that implements workflowInterface and can be used to signal or query it.
180192
*/
181-
<T> T newWorkflowStub(Class<T> workflowInterface, WorkflowExecution execution);
193+
<T> T newWorkflowStub(Class<T> workflowInterface, String workflowId, Optional<String> runId);
182194

183195
/**
184196
* Creates workflow untyped client stub that can be used to start a single workflow execution.
@@ -191,14 +203,29 @@ static WorkflowClient newInstance(
191203
*/
192204
UntypedWorkflowStub newUntypedWorkflowStub(String workflowType, WorkflowOptions options);
193205

206+
/**
207+
* Creates workflow untyped client stub for a known execution. Use it to send signals or queries
208+
* to a running workflow. Do not call methods annotated with @WorkflowMethod.
209+
*
210+
* @param workflowId workflow id and optional run id for execution
211+
* @param runId runId of the workflow execution. If not provided the last workflow with the given
212+
* workflowId is assumed.
213+
* @param workflowType type of the workflow. Optional as it is used for error reporting only.
214+
* @return Stub that can be used to start workflow and later to signal or query it.
215+
*/
216+
UntypedWorkflowStub newUntypedWorkflowStub(
217+
String workflowId, Optional<String> runId, Optional<String> workflowType);
218+
194219
/**
195220
* Creates workflow untyped client stub for a known execution. Use it to send signals or queries
196221
* to a running workflow. Do not call methods annotated with @WorkflowMethod.
197222
*
198223
* @param execution workflow id and optional run id for execution
224+
* @param workflowType type of the workflow. Optional as it is used for error reporting only.
199225
* @return Stub that can be used to start workflow and later to signal or query it.
200226
*/
201-
UntypedWorkflowStub newUntypedWorkflowStub(WorkflowExecution execution);
227+
UntypedWorkflowStub newUntypedWorkflowStub(
228+
WorkflowExecution execution, Optional<String> workflowType);
202229

203230
/**
204231
* Creates new {@link ActivityCompletionClient} that can be used to complete activities

src/main/java/com/uber/cadence/client/WorkflowException.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.uber.cadence.WorkflowExecution;
2121
import com.uber.cadence.workflow.ChildWorkflowException;
22+
import java.util.Optional;
2223

2324
/**
2425
* Base exception for all workflow failures returned by an external client. Note that inside a
@@ -27,22 +28,22 @@
2728
public class WorkflowException extends RuntimeException {
2829

2930
private final WorkflowExecution execution;
30-
private final String workflowType;
31+
private final Optional<String> workflowType;
3132

3233
protected WorkflowException(
33-
String message, WorkflowExecution execution, String workflowType, Throwable cause) {
34+
String message, WorkflowExecution execution, Optional<String> workflowType, Throwable cause) {
3435
super(getMessage(message, execution, workflowType), cause);
3536
this.execution = execution;
3637
this.workflowType = workflowType;
3738
}
3839

3940
private static String getMessage(
40-
String message, WorkflowExecution execution, String workflowType) {
41+
String message, WorkflowExecution execution, Optional<String> workflowType) {
4142
StringBuilder result = new StringBuilder();
4243
result.append(message);
43-
if (workflowType != null) {
44+
if (workflowType.isPresent()) {
4445
result.append(", WorkflowType=\"");
45-
result.append(workflowType);
46+
result.append(workflowType.get());
4647
result.append("\"");
4748
}
4849
result.append(", WorkflowExecution=\"");
@@ -54,7 +55,7 @@ public WorkflowExecution getExecution() {
5455
return execution;
5556
}
5657

57-
public String getWorkflowType() {
58+
public Optional<String> getWorkflowType() {
5859
return workflowType;
5960
}
6061
}

src/main/java/com/uber/cadence/client/WorkflowFailureException.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.uber.cadence.client;
1919

2020
import com.uber.cadence.WorkflowExecution;
21+
import java.util.Optional;
2122

2223
/**
2324
* Indicates that a workflow failed. An original cause of the workflow failure can be retrieved
@@ -29,7 +30,7 @@ public final class WorkflowFailureException extends WorkflowException {
2930

3031
public WorkflowFailureException(
3132
WorkflowExecution execution,
32-
String workflowType,
33+
Optional<String> workflowType,
3334
long decisionTaskCompletedEventId,
3435
Throwable failure) {
3536
super(

src/main/java/com/uber/cadence/client/WorkflowNotFoundException.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.uber.cadence.client;
1919

2020
import com.uber.cadence.WorkflowExecution;
21+
import java.util.Optional;
2122

2223
/**
2324
* Thrown when workflow with the given id is not known to the cadence service. It could be because
@@ -26,7 +27,7 @@
2627
public final class WorkflowNotFoundException extends WorkflowException {
2728

2829
public WorkflowNotFoundException(
29-
WorkflowExecution execution, String workflowType, String message) {
30+
WorkflowExecution execution, Optional<String> workflowType, String message) {
3031
super(message, execution, workflowType, null);
3132
}
3233
}

src/main/java/com/uber/cadence/client/WorkflowQueryException.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
package com.uber.cadence.client;
1919

2020
import com.uber.cadence.WorkflowExecution;
21+
import java.util.Optional;
2122

2223
public final class WorkflowQueryException extends WorkflowException {
2324

2425
public WorkflowQueryException(WorkflowExecution execution, String message) {
25-
super(message, execution, null, null);
26+
super(message, execution, Optional.empty(), null);
2627
}
2728
}

src/main/java/com/uber/cadence/client/WorkflowServiceException.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
package com.uber.cadence.client;
1919

2020
import com.uber.cadence.WorkflowExecution;
21+
import java.util.Optional;
2122

2223
public final class WorkflowServiceException extends WorkflowException {
2324

2425
public WorkflowServiceException(WorkflowExecution execution, String message) {
25-
super(message, execution, null, null);
26+
super(message, execution, Optional.empty(), null);
2627
}
2728
}

src/main/java/com/uber/cadence/client/WorkflowTerminatedException.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.uber.cadence.client;
1919

2020
import com.uber.cadence.WorkflowExecution;
21+
import java.util.Optional;
2122

2223
/**
2324
* Indicates that a workflow was forcefully terminated by an external command to Cadence service.
@@ -29,7 +30,7 @@ public final class WorkflowTerminatedException extends WorkflowException {
2930

3031
public WorkflowTerminatedException(
3132
WorkflowExecution execution,
32-
String workflowType,
33+
Optional<String> workflowType,
3334
String reason,
3435
String identity,
3536
byte[] details) {

src/main/java/com/uber/cadence/client/WorkflowTimedOutException.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.uber.cadence.TimeoutType;
2121
import com.uber.cadence.WorkflowExecution;
22+
import java.util.Optional;
2223

2324
/**
2425
* Indicates that a workflow exceeded its execution timeout and was forcefully terminated by the
@@ -29,7 +30,7 @@ public final class WorkflowTimedOutException extends WorkflowException {
2930
private final TimeoutType timeoutType;
3031

3132
public WorkflowTimedOutException(
32-
WorkflowExecution execution, String workflowType, TimeoutType timeoutType) {
33+
WorkflowExecution execution, Optional<String> workflowType, TimeoutType timeoutType) {
3334
super(String.valueOf(timeoutType) + " timeout type", execution, workflowType, null);
3435
this.timeoutType = timeoutType;
3536
}

0 commit comments

Comments
 (0)