• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

temporalio / sdk-java / #333

16 Oct 2024 07:28PM UTC coverage: 78.65% (+0.6%) from 78.085%
#333

push

github

web-flow
Fix code coverage (#2275)

22670 of 28824 relevant lines covered (78.65%)

0.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.68
/temporal-sdk/src/main/java/io/temporal/worker/WorkflowImplementationOptions.java
1
/*
2
 * Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
3
 *
4
 * Copyright (C) 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");
9
 * you may not use this material except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *   http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20

21
package io.temporal.worker;
22

23
import io.temporal.activity.ActivityOptions;
24
import io.temporal.activity.LocalActivityOptions;
25
import io.temporal.workflow.NexusServiceOptions;
26
import java.util.*;
27
import javax.annotation.Nonnull;
28
import javax.annotation.Nullable;
29

30
public final class WorkflowImplementationOptions {
31

32
  private static final WorkflowImplementationOptions DEFAULT_INSTANCE;
33

34
  static {
35
    DEFAULT_INSTANCE = WorkflowImplementationOptions.newBuilder().build();
1✔
36
  }
1✔
37

38
  public static WorkflowImplementationOptions getDefaultInstance() {
39
    return DEFAULT_INSTANCE;
1✔
40
  }
41

42
  public static Builder newBuilder() {
43
    return new Builder();
1✔
44
  }
45

46
  public static Builder newBuilder(WorkflowImplementationOptions options) {
47
    return new Builder(options);
×
48
  }
49

50
  public Builder toBuilder() {
51
    return new Builder(this);
1✔
52
  }
53

54
  public static final class Builder {
55

56
    private Class<? extends Throwable>[] failWorkflowExceptionTypes;
57
    private Map<String, ActivityOptions> activityOptions;
58
    private ActivityOptions defaultActivityOptions;
59
    private Map<String, LocalActivityOptions> localActivityOptions;
60
    private LocalActivityOptions defaultLocalActivityOptions;
61
    private Map<String, NexusServiceOptions> nexusServiceOptions;
62
    private NexusServiceOptions defaultNexusServiceOptions;
63

64
    private Builder() {}
65

66
    private Builder(WorkflowImplementationOptions options) {
1✔
67
      if (options == null) {
1✔
68
        return;
×
69
      }
70
      this.failWorkflowExceptionTypes = options.getFailWorkflowExceptionTypes();
1✔
71
      this.activityOptions = options.getActivityOptions();
1✔
72
      this.defaultActivityOptions = options.getDefaultActivityOptions();
1✔
73
      this.localActivityOptions = options.getLocalActivityOptions();
1✔
74
      this.defaultLocalActivityOptions = options.getDefaultLocalActivityOptions();
1✔
75
      this.nexusServiceOptions = options.getNexusServiceOptions();
1✔
76
      this.defaultNexusServiceOptions = options.getDefaultNexusServiceOptions();
1✔
77
    }
1✔
78

79
    /**
80
     * Optional: Sets how workflow worker deals with exceptions thrown from the workflow code which
81
     * include non-deterministic history events (presumably arising from non-deterministic workflow
82
     * definitions or non-backward compatible workflow definition changes).
83
     *
84
     * <p>The default behavior is to fail workflow on {@link io.temporal.failure.TemporalFailure} or
85
     * any of its subclasses. Any other exceptions thrown from the workflow code are treated as bugs
86
     * that can be fixed by a new deployment. So workflow is not failed, but it stuck in a retry
87
     * loop trying to execute the code that led to the unexpected exception.
88
     *
89
     * <p>This option allows to specify specific exception types which should lead to workflow
90
     * failure instead of blockage. Any exception that extends the configured type considered
91
     * matched. For example to fail workflow on any exception pass {@link Throwable} class to this
92
     * method.
93
     */
94
    @SafeVarargs
95
    public final Builder setFailWorkflowExceptionTypes(
96
        Class<? extends Throwable>... failWorkflowExceptionTypes) {
97
      this.failWorkflowExceptionTypes = failWorkflowExceptionTypes;
1✔
98
      return this;
1✔
99
    }
100

101
    /**
102
     * Set individual activity options per activityType. Will be merged with the map from {@link
103
     * io.temporal.workflow.Workflow#newActivityStub(Class, ActivityOptions, Map)} which has the
104
     * highest precedence.
105
     *
106
     * @param activityOptions map from activityType to ActivityOptions
107
     */
108
    public Builder setActivityOptions(Map<String, ActivityOptions> activityOptions) {
109
      this.activityOptions = new HashMap<>(Objects.requireNonNull(activityOptions));
1✔
110
      return this;
1✔
111
    }
112

113
    /**
114
     * These activity options have the lowest precedence across all activity options. Will be
115
     * overwritten entirely by {@link io.temporal.workflow.Workflow#newActivityStub(Class,
116
     * ActivityOptions)} and then by the individual activity options if any are set through {@link
117
     * #setActivityOptions(Map)}
118
     *
119
     * @param defaultActivityOptions ActivityOptions for all activities in the workflow.
120
     */
121
    public Builder setDefaultActivityOptions(ActivityOptions defaultActivityOptions) {
122
      this.defaultActivityOptions = Objects.requireNonNull(defaultActivityOptions);
1✔
123
      return this;
1✔
124
    }
125

126
    /**
127
     * Set individual local activity options per activityType. Will be merged with the map from
128
     * {@link io.temporal.workflow.Workflow#newLocalActivityStub(Class, LocalActivityOptions, Map)}
129
     * which has the highest precedence.
130
     *
131
     * @param localActivityOptions map from activityType to ActivityOptions
132
     */
133
    public Builder setLocalActivityOptions(Map<String, LocalActivityOptions> localActivityOptions) {
134
      this.localActivityOptions = new HashMap<>(Objects.requireNonNull(localActivityOptions));
1✔
135
      return this;
1✔
136
    }
137

138
    /**
139
     * These local activity options have the lowest precedence across all local activity options.
140
     * Will be overwritten entirely by {@link
141
     * io.temporal.workflow.Workflow#newLocalActivityStub(Class, LocalActivityOptions)} and then by
142
     * the individual local activity options if any are set through {@link
143
     * #setLocalActivityOptions(Map)}
144
     *
145
     * @param defaultLocalActivityOptions ActivityOptions for all activities in the workflow.
146
     */
147
    public Builder setDefaultLocalActivityOptions(
148
        LocalActivityOptions defaultLocalActivityOptions) {
149
      this.defaultLocalActivityOptions = Objects.requireNonNull(defaultLocalActivityOptions);
1✔
150
      return this;
1✔
151
    }
152

153
    /**
154
     * Set individual Nexus Service options per service. Will be merged with the map from {@link
155
     * io.temporal.workflow.Workflow#newNexusServiceStub(Class, NexusServiceOptions)} which has the
156
     * highest precedence.
157
     *
158
     * @param nexusServiceOptions map from service to NexusServiceOptions
159
     */
160
    public Builder setNexusServiceOptions(Map<String, NexusServiceOptions> nexusServiceOptions) {
161
      this.nexusServiceOptions = new HashMap<>(Objects.requireNonNull(nexusServiceOptions));
1✔
162
      return this;
1✔
163
    }
164

165
    /**
166
     * These nexus service options to use if no specific options are passed for a service. Will be
167
     * used for a stub created with {@link io.temporal.workflow.Workflow#newNexusServiceStub(Class)}
168
     *
169
     * @param defaultNexusServiceOptions default NexusServiceOptions for all services in the
170
     *     workflow.
171
     */
172
    public Builder setDefaultNexusServiceOptions(NexusServiceOptions defaultNexusServiceOptions) {
173
      this.defaultNexusServiceOptions = Objects.requireNonNull(defaultNexusServiceOptions);
1✔
174
      return this;
1✔
175
    }
176

177
    public WorkflowImplementationOptions build() {
178
      return new WorkflowImplementationOptions(
1✔
179
          failWorkflowExceptionTypes == null ? new Class[0] : failWorkflowExceptionTypes,
1✔
180
          activityOptions == null ? null : activityOptions,
1✔
181
          defaultActivityOptions,
182
          localActivityOptions == null ? null : localActivityOptions,
1✔
183
          defaultLocalActivityOptions,
184
          nexusServiceOptions == null ? null : nexusServiceOptions,
1✔
185
          defaultNexusServiceOptions);
186
    }
187
  }
188

189
  private final Class<? extends Throwable>[] failWorkflowExceptionTypes;
190
  private final @Nullable Map<String, ActivityOptions> activityOptions;
191
  private final ActivityOptions defaultActivityOptions;
192
  private final @Nullable Map<String, LocalActivityOptions> localActivityOptions;
193
  private final LocalActivityOptions defaultLocalActivityOptions;
194
  private final @Nullable Map<String, NexusServiceOptions> nexusServiceOptions;
195
  private final NexusServiceOptions defaultNexusServiceOptions;
196

197
  public WorkflowImplementationOptions(
198
      Class<? extends Throwable>[] failWorkflowExceptionTypes,
199
      @Nullable Map<String, ActivityOptions> activityOptions,
200
      ActivityOptions defaultActivityOptions,
201
      @Nullable Map<String, LocalActivityOptions> localActivityOptions,
202
      LocalActivityOptions defaultLocalActivityOptions,
203
      @Nullable Map<String, NexusServiceOptions> nexusServiceOptions,
204
      NexusServiceOptions defaultNexusServiceOptions) {
1✔
205
    this.failWorkflowExceptionTypes = failWorkflowExceptionTypes;
1✔
206
    this.activityOptions = activityOptions;
1✔
207
    this.defaultActivityOptions = defaultActivityOptions;
1✔
208
    this.localActivityOptions = localActivityOptions;
1✔
209
    this.defaultLocalActivityOptions = defaultLocalActivityOptions;
1✔
210
    this.nexusServiceOptions = nexusServiceOptions;
1✔
211
    this.defaultNexusServiceOptions = defaultNexusServiceOptions;
1✔
212
  }
1✔
213

214
  public Class<? extends Throwable>[] getFailWorkflowExceptionTypes() {
215
    return failWorkflowExceptionTypes;
1✔
216
  }
217

218
  public @Nonnull Map<String, ActivityOptions> getActivityOptions() {
219
    return activityOptions != null
1✔
220
        ? Collections.unmodifiableMap(activityOptions)
1✔
221
        : Collections.emptyMap();
1✔
222
  }
223

224
  public ActivityOptions getDefaultActivityOptions() {
225
    return defaultActivityOptions;
1✔
226
  }
227

228
  public @Nonnull Map<String, LocalActivityOptions> getLocalActivityOptions() {
229
    return localActivityOptions != null
1✔
230
        ? Collections.unmodifiableMap(localActivityOptions)
1✔
231
        : Collections.emptyMap();
1✔
232
  }
233

234
  public LocalActivityOptions getDefaultLocalActivityOptions() {
235
    return defaultLocalActivityOptions;
1✔
236
  }
237

238
  public @Nonnull Map<String, NexusServiceOptions> getNexusServiceOptions() {
239
    return nexusServiceOptions != null
1✔
240
        ? Collections.unmodifiableMap(nexusServiceOptions)
1✔
241
        : Collections.emptyMap();
1✔
242
  }
243

244
  public NexusServiceOptions getDefaultNexusServiceOptions() {
245
    return defaultNexusServiceOptions;
1✔
246
  }
247

248
  @Override
249
  public String toString() {
250
    return "WorkflowImplementationOptions{"
×
251
        + "failWorkflowExceptionTypes="
252
        + Arrays.toString(failWorkflowExceptionTypes)
×
253
        + ", activityOptions="
254
        + activityOptions
255
        + ", defaultActivityOptions="
256
        + defaultActivityOptions
257
        + ", localActivityOptions="
258
        + localActivityOptions
259
        + ", defaultLocalActivityOptions="
260
        + defaultLocalActivityOptions
261
        + ", nexusServiceOptions="
262
        + nexusServiceOptions
263
        + ", defaultNexusServiceOptions="
264
        + defaultNexusServiceOptions
265
        + '}';
266
  }
267

268
  @Override
269
  public boolean equals(Object o) {
270
    if (this == o) return true;
×
271
    if (o == null || getClass() != o.getClass()) return false;
×
272
    WorkflowImplementationOptions that = (WorkflowImplementationOptions) o;
×
273
    return Arrays.equals(failWorkflowExceptionTypes, that.failWorkflowExceptionTypes)
×
274
        && Objects.equals(activityOptions, that.activityOptions)
×
275
        && Objects.equals(defaultActivityOptions, that.defaultActivityOptions)
×
276
        && Objects.equals(localActivityOptions, that.localActivityOptions)
×
277
        && Objects.equals(defaultLocalActivityOptions, that.defaultLocalActivityOptions)
×
278
        && Objects.equals(nexusServiceOptions, that.nexusServiceOptions)
×
279
        && Objects.equals(defaultNexusServiceOptions, that.defaultNexusServiceOptions);
×
280
  }
281

282
  @Override
283
  public int hashCode() {
284
    int result =
×
285
        Objects.hash(
×
286
            activityOptions,
287
            defaultActivityOptions,
288
            localActivityOptions,
289
            defaultLocalActivityOptions,
290
            nexusServiceOptions,
291
            defaultNexusServiceOptions);
292
    result = 31 * result + Arrays.hashCode(failWorkflowExceptionTypes);
×
293
    return result;
×
294
  }
295
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc