Skip to content

Commit 380373a

Browse files
committed
Add WorkerInterceptorBase and improve WorkerInterceptor docs to help new interceptor users
1 parent bdee762 commit 380373a

File tree

4 files changed

+167
-93
lines changed

4 files changed

+167
-93
lines changed

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

+51-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,57 @@
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+
* You may want to start your implementation with this initial structure:
32+
*
33+
* <pre>{@code
34+
* private static class CustomWorkerInterceptor extends WorkerInterceptorBase {
35+
* // remove if you don't need to have a custom WorkflowInboundCallsInterceptor or WorkflowOutboundCallsInterceptor
36+
* @Override
37+
* public WorkflowInboundCallsInterceptor interceptWorkflow(WorkflowInboundCallsInterceptor next) {
38+
* return new CustomWorkflowInboundCallsInterceptor(next) {
39+
* // remove if you don't need to have a custom WorkflowOutboundCallsInterceptor
40+
* @Override
41+
* public void init(WorkflowOutboundCallsInterceptor outboundCalls) {
42+
* next.init(new CustomWorkflowOutboundCallsInterceptor(outboundCalls));
43+
* }
44+
* };
45+
* }
46+
*
47+
* // remove if you don't need to have a custom ActivityInboundCallsInterceptor
48+
* @Override
49+
* public ActivityInboundCallsInterceptor interceptActivity(ActivityInboundCallsInterceptor next) {
50+
* return new CustomActivityInboundCallsInterceptor(next);
51+
* }
52+
* }
53+
*
54+
* private static class CustomWorkflowInboundCallsInterceptor extends WorkflowInboundCallsInterceptorBase {
55+
* public CustomWorkflowInboundCallsInterceptor(WorkflowInboundCallsInterceptor next) {
56+
* super(next);
57+
* }
58+
*
59+
* // override only the methods you need
60+
* }
61+
*
62+
* private static class CustomWorkflowOutboundCallsInterceptor extends WorkflowOutboundCallsInterceptorBase {
63+
* public CustomWorkflowOutboundCallsInterceptor(WorkflowOutboundCallsInterceptor next) {
64+
* super(next);
65+
* }
66+
*
67+
* // override only the methods you need
68+
* }
69+
*
70+
* private static class CustomActivityInboundCallsInterceptor extends ActivityInboundCallsInterceptorBase {
71+
* public CustomActivityInboundCallsInterceptor(ActivityInboundCallsInterceptor next) {
72+
* super(next);
73+
* }
74+
*
75+
* // override only the methods you need
76+
* }
77+
* }</pre>
2878
*/
2979
@Experimental
3080
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

+84-41
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,8 @@
6964
import java.util.Map;
7065
import java.util.concurrent.CompletableFuture;
7166
import java.util.concurrent.ExecutionException;
67+
import java.util.function.Function;
68+
7269
import org.junit.After;
7370
import org.junit.Rule;
7471
import org.junit.Test;
@@ -261,19 +258,7 @@ public void testCorruptedSignalMetrics() throws InterruptedException {
261258
.setWorkerInterceptors(
262259
new CorruptedSignalWorkerInterceptor(),
263260
// 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-
})
261+
new WorkerInterceptorBase())
277262
.build());
278263

279264
Worker worker = testEnvironment.newWorker(TASK_QUEUE);
@@ -341,24 +326,7 @@ public void testTemporalFailureMetric() throws InterruptedException {
341326

342327
@Test
343328
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());
329+
setUp(WorkerFactoryOptions.getDefaultInstance());
362330

363331
Worker worker = testEnvironment.newWorker(TASK_QUEUE);
364332
worker.registerWorkflowImplementationTypes(TestActivityFailureCountersWorkflow.class);
@@ -603,12 +571,10 @@ public void execute() {
603571
}
604572

605573
public static class Signal {
606-
607574
public String value;
608575
}
609576

610-
private static class CorruptedSignalWorkerInterceptor implements WorkerInterceptor {
611-
577+
private static class CorruptedSignalWorkerInterceptor extends WorkerInterceptorBase {
612578
@Override
613579
public WorkflowInboundCallsInterceptor interceptWorkflow(WorkflowInboundCallsInterceptor next) {
614580
return new WorkflowInboundCallsInterceptorBase(next) {
@@ -627,10 +593,87 @@ public void init(WorkflowOutboundCallsInterceptor outboundCalls) {
627593
}
628594
};
629595
}
596+
}
597+
598+
private static class SignalWorkflowOutboundCallsInterceptor 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+
}
610+
611+
@Override
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)));
622+
}
623+
}
624+
625+
626+
627+
628+
629+
630+
631+
632+
633+
634+
635+
636+
private static class CustomWorkerInterceptor extends WorkerInterceptorBase {
637+
// remove if you don't need to have a custom WorkflowInboundCallsInterceptor or WorkflowOutboundCallsInterceptor
638+
@Override
639+
public WorkflowInboundCallsInterceptor interceptWorkflow(WorkflowInboundCallsInterceptor next) {
640+
return new CustomWorkflowInboundCallsInterceptor(next) {
641+
// remove if you don't need to have a custom WorkflowOutboundCallsInterceptor
642+
@Override
643+
public void init(WorkflowOutboundCallsInterceptor outboundCalls) {
644+
next.init(new CustomWorkflowOutboundCallsInterceptor(outboundCalls));
645+
}
646+
};
647+
}
630648

649+
// remove if you don't need to have a custom ActivityInboundCallsInterceptor
631650
@Override
632651
public ActivityInboundCallsInterceptor interceptActivity(ActivityInboundCallsInterceptor next) {
633-
return next;
652+
return new CustomActivityInboundCallsInterceptor(next);
634653
}
635654
}
655+
656+
private static class CustomWorkflowInboundCallsInterceptor extends WorkflowInboundCallsInterceptorBase {
657+
public CustomWorkflowInboundCallsInterceptor(WorkflowInboundCallsInterceptor next) {
658+
super(next);
659+
}
660+
661+
// override only the methods you need
662+
}
663+
664+
private static class CustomWorkflowOutboundCallsInterceptor extends WorkflowOutboundCallsInterceptorBase {
665+
public CustomWorkflowOutboundCallsInterceptor(WorkflowOutboundCallsInterceptor next) {
666+
super(next);
667+
}
668+
669+
// override only the methods you need
670+
}
671+
672+
private static class CustomActivityInboundCallsInterceptor extends ActivityInboundCallsInterceptorBase {
673+
public CustomActivityInboundCallsInterceptor(ActivityInboundCallsInterceptor next) {
674+
super(next);
675+
}
676+
677+
// override only the methods you need
678+
}
636679
}

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

-51
This file was deleted.

0 commit comments

Comments
 (0)