Skip to content

Commit c7fcb4b

Browse files
authored
Add WorkerInterceptorBase and improve WorkerInterceptor docs to help new interceptor users (#1231)
1 parent 58f4d9d commit c7fcb4b

File tree

4 files changed

+118
-94
lines changed

4 files changed

+118
-94
lines changed

temporal-sdk/src/main/java/io/temporal/common/interceptors/WorkerInterceptor.java

+56-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,62 @@
2424
/**
2525
* Intercepts workflow and activity executions.
2626
*
27-
* <p>TODO(maxim): JavaDoc with sample
27+
* <p>Prefer extending {@link WorkerInterceptorBase} and overriding only the methods you need
28+
* instead of implementing this interface directly. {@link WorkerInterceptorBase} provides correct
29+
* default implementations to all the methods of this interface.
30+
*
31+
* <p>You may want to start your implementation with this initial structure:
32+
*
33+
* <pre>{@code
34+
* public class CustomWorkerInterceptor extends WorkerInterceptorBase {
35+
* // remove if you don't need to have a custom WorkflowInboundCallsInterceptor or
36+
* // WorkflowOutboundCallsInterceptor
37+
* @Override
38+
* public WorkflowInboundCallsInterceptor interceptWorkflow(WorkflowInboundCallsInterceptor next) {
39+
* return new CustomWorkflowInboundCallsInterceptor(next) {
40+
* // remove if you don't need to have a custom WorkflowOutboundCallsInterceptor
41+
* @Override
42+
* public void init(WorkflowOutboundCallsInterceptor outboundCalls) {
43+
* next.init(new CustomWorkflowOutboundCallsInterceptor(outboundCalls));
44+
* }
45+
* };
46+
* }
47+
*
48+
* // remove if you don't need to have a custom ActivityInboundCallsInterceptor
49+
* @Override
50+
* public ActivityInboundCallsInterceptor interceptActivity(ActivityInboundCallsInterceptor next) {
51+
* return new CustomActivityInboundCallsInterceptor(next);
52+
* }
53+
*
54+
* private static class CustomWorkflowInboundCallsInterceptor
55+
* extends WorkflowInboundCallsInterceptorBase {
56+
* public CustomWorkflowInboundCallsInterceptor(WorkflowInboundCallsInterceptor next) {
57+
* super(next);
58+
* }
59+
*
60+
* // override only the methods you need
61+
* }
62+
*
63+
* private static class CustomWorkflowOutboundCallsInterceptor
64+
* extends WorkflowOutboundCallsInterceptorBase {
65+
* public CustomWorkflowOutboundCallsInterceptor(WorkflowOutboundCallsInterceptor next) {
66+
* super(next);
67+
* }
68+
*
69+
* // override only the methods you need
70+
* }
71+
*
72+
* private static class CustomActivityInboundCallsInterceptor
73+
* extends ActivityInboundCallsInterceptorBase {
74+
* public CustomActivityInboundCallsInterceptor(ActivityInboundCallsInterceptor next) {
75+
* super(next);
76+
* }
77+
*
78+
* // override only the methods you need
79+
* }
80+
* }
81+
*
82+
* }</pre>
2883
*/
2984
@Experimental
3085
public interface WorkerInterceptor {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (C) 2020 Temporal Technologies, Inc. All Rights Reserved.
3+
*
4+
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5+
*
6+
* Modifications copyright (C) 2017 Uber Technologies, Inc.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
9+
* use this file except in compliance with the License. A copy of the License is
10+
* located at
11+
*
12+
* http://aws.amazon.com/apache2.0
13+
*
14+
* or in the "license" file accompanying this file. This file is distributed on
15+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
16+
* express or implied. See the License for the specific language governing
17+
* permissions and limitations under the License.
18+
*/
19+
20+
package io.temporal.common.interceptors;
21+
22+
public class WorkerInterceptorBase implements WorkerInterceptor {
23+
@Override
24+
public WorkflowInboundCallsInterceptor interceptWorkflow(WorkflowInboundCallsInterceptor next) {
25+
return next;
26+
}
27+
28+
@Override
29+
public ActivityInboundCallsInterceptor interceptActivity(ActivityInboundCallsInterceptor next) {
30+
return next;
31+
}
32+
}

temporal-sdk/src/test/java/io/temporal/workflow/MetricsTest.java

+30-42
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@
4343
import io.temporal.client.WorkflowOptions;
4444
import io.temporal.client.WorkflowStub;
4545
import io.temporal.common.RetryOptions;
46-
import io.temporal.common.interceptors.ActivityInboundCallsInterceptor;
47-
import io.temporal.common.interceptors.WorkerInterceptor;
48-
import io.temporal.common.interceptors.WorkflowInboundCallsInterceptor;
49-
import io.temporal.common.interceptors.WorkflowInboundCallsInterceptorBase;
50-
import io.temporal.common.interceptors.WorkflowOutboundCallsInterceptor;
46+
import io.temporal.common.interceptors.*;
5147
import io.temporal.common.reporter.TestStatsReporter;
5248
import io.temporal.serviceclient.MetricsTag;
5349
import io.temporal.serviceclient.WorkflowServiceStubs;
@@ -57,7 +53,6 @@
5753
import io.temporal.worker.Worker;
5854
import io.temporal.worker.WorkerFactoryOptions;
5955
import io.temporal.worker.WorkerMetricsTag;
60-
import io.temporal.workflow.interceptors.SignalWorkflowOutboundCallsInterceptor;
6156
import io.temporal.workflow.shared.TestActivities.TestActivitiesImpl;
6257
import io.temporal.workflow.shared.TestActivities.TestActivity3;
6358
import io.temporal.workflow.shared.TestActivities.VariousTestActivities;
@@ -69,6 +64,7 @@
6964
import java.util.Map;
7065
import java.util.concurrent.CompletableFuture;
7166
import java.util.concurrent.ExecutionException;
67+
import java.util.function.Function;
7268
import org.junit.After;
7369
import org.junit.Rule;
7470
import org.junit.Test;
@@ -261,19 +257,7 @@ public void testCorruptedSignalMetrics() throws InterruptedException {
261257
.setWorkerInterceptors(
262258
new CorruptedSignalWorkerInterceptor(),
263259
// Add noop just to test that list of interceptors is working.
264-
new WorkerInterceptor() {
265-
@Override
266-
public WorkflowInboundCallsInterceptor interceptWorkflow(
267-
WorkflowInboundCallsInterceptor next) {
268-
return next;
269-
}
270-
271-
@Override
272-
public ActivityInboundCallsInterceptor interceptActivity(
273-
ActivityInboundCallsInterceptor next) {
274-
return next;
275-
}
276-
})
260+
new WorkerInterceptorBase())
277261
.build());
278262

279263
Worker worker = testEnvironment.newWorker(TASK_QUEUE);
@@ -341,24 +325,7 @@ public void testTemporalFailureMetric() throws InterruptedException {
341325

342326
@Test
343327
public void testTemporalActivityFailureMetric() throws InterruptedException {
344-
setUp(
345-
WorkerFactoryOptions.newBuilder()
346-
.setWorkerInterceptors(
347-
// Add noop just to test that list of interceptors is working.
348-
new WorkerInterceptor() {
349-
@Override
350-
public WorkflowInboundCallsInterceptor interceptWorkflow(
351-
WorkflowInboundCallsInterceptor next) {
352-
return next;
353-
}
354-
355-
@Override
356-
public ActivityInboundCallsInterceptor interceptActivity(
357-
ActivityInboundCallsInterceptor next) {
358-
return next;
359-
}
360-
})
361-
.build());
328+
setUp(WorkerFactoryOptions.getDefaultInstance());
362329

363330
Worker worker = testEnvironment.newWorker(TASK_QUEUE);
364331
worker.registerWorkflowImplementationTypes(TestActivityFailureCountersWorkflow.class);
@@ -603,12 +570,10 @@ public void execute() {
603570
}
604571

605572
public static class Signal {
606-
607573
public String value;
608574
}
609575

610-
private static class CorruptedSignalWorkerInterceptor implements WorkerInterceptor {
611-
576+
private static class CorruptedSignalWorkerInterceptor extends WorkerInterceptorBase {
612577
@Override
613578
public WorkflowInboundCallsInterceptor interceptWorkflow(WorkflowInboundCallsInterceptor next) {
614579
return new WorkflowInboundCallsInterceptorBase(next) {
@@ -627,10 +592,33 @@ public void init(WorkflowOutboundCallsInterceptor outboundCalls) {
627592
}
628593
};
629594
}
595+
}
596+
597+
private static class SignalWorkflowOutboundCallsInterceptor
598+
extends WorkflowOutboundCallsInterceptorBase {
599+
private final Function<Object[], Object[]> overrideArgs;
600+
private final Function<String, String> overrideSignalName;
601+
602+
public SignalWorkflowOutboundCallsInterceptor(
603+
Function<Object[], Object[]> overrideArgs,
604+
Function<String, String> overrideSignalName,
605+
WorkflowOutboundCallsInterceptor next) {
606+
super(next);
607+
this.overrideArgs = overrideArgs;
608+
this.overrideSignalName = overrideSignalName;
609+
}
630610

631611
@Override
632-
public ActivityInboundCallsInterceptor interceptActivity(ActivityInboundCallsInterceptor next) {
633-
return next;
612+
public SignalExternalOutput signalExternalWorkflow(SignalExternalInput input) {
613+
Object[] args = input.getArgs();
614+
if (args != null && args.length > 0) {
615+
args = new Object[] {"corrupted signal"};
616+
}
617+
return super.signalExternalWorkflow(
618+
new SignalExternalInput(
619+
input.getExecution(),
620+
overrideSignalName.apply(input.getSignalName()),
621+
overrideArgs.apply(args)));
634622
}
635623
}
636624
}

temporal-sdk/src/test/java/io/temporal/workflow/interceptors/SignalWorkflowOutboundCallsInterceptor.java

-51
This file was deleted.

0 commit comments

Comments
 (0)