Skip to content

Commit c458bfc

Browse files
authored
Fixed doNotRetry RetryOptions (#182)
* Added doNotRetry to RetryOptions in testAsyncActivityRetryOptionsChange * Added type adapter for Class objects to DataConverter * Added JsonDataConverter.testClass and fixed the bug in JsonDataConverter
1 parent 02df12b commit c458bfc

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

src/main/java/com/uber/cadence/converter/JsonDataConverter.java

+31-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public final class JsonDataConverter implements DataConverter {
7676
private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
7777
public static final String TYPE_FIELD_NAME = "type";
7878
public static final String JSON_CONVERTER_TYPE = "JSON";
79+
public static final String CLASS_NAME_FIELD_NAME = "className";
7980
private final Gson gson;
8081
private final JsonParser parser = new JsonParser();
8182

@@ -195,7 +196,7 @@ public void write(JsonWriter out, T value) throws IOException {
195196
@SuppressWarnings("unchecked")
196197
public T read(JsonReader in) throws IOException {
197198
in.beginObject();
198-
if (!in.nextName().equals("type")) {
199+
if (!in.nextName().equals(TYPE_FIELD_NAME)) {
199200
throw new IOException("Cannot deserialize DataConverter. Missing type field");
200201
}
201202
String value = in.nextString();
@@ -208,6 +209,35 @@ public T read(JsonReader in) throws IOException {
208209
}
209210
};
210211
}
212+
if (Class.class.isAssignableFrom(typeToken.getRawType())) {
213+
return new TypeAdapter<T>() {
214+
@Override
215+
public void write(JsonWriter out, T value) throws IOException {
216+
out.beginObject();
217+
String className = ((Class) value).getName();
218+
out.name(CLASS_NAME_FIELD_NAME).value(className);
219+
out.endObject();
220+
}
221+
222+
@Override
223+
public T read(JsonReader in) throws IOException {
224+
in.beginObject();
225+
if (!in.nextName().equals(CLASS_NAME_FIELD_NAME)) {
226+
throw new IOException(
227+
"Cannot deserialize class. Missing " + CLASS_NAME_FIELD_NAME + " field");
228+
}
229+
String className = in.nextString();
230+
try {
231+
@SuppressWarnings("unchecked")
232+
T result = (T) Class.forName(className);
233+
in.endObject();
234+
return result;
235+
} catch (ClassNotFoundException e) {
236+
throw new RuntimeException(e);
237+
}
238+
}
239+
};
240+
}
211241
if (!Throwable.class.isAssignableFrom(typeToken.getRawType())) {
212242
return null; // this class only serializes 'Throwable' and its subtypes
213243
}

src/test/java/com/uber/cadence/converter/JsonDataConverterTest.java

+9
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,13 @@ public void testUUIDList() throws NoSuchMethodException {
7474
List<UUID> result = (List<UUID>) converter.fromDataArray(data, arg)[0];
7575
assertEquals(result.toString(), list, result);
7676
}
77+
78+
@Test
79+
public void testClass() {
80+
DataConverter converter = JsonDataConverter.getInstance();
81+
byte[] data = converter.toData(this.getClass());
82+
@SuppressWarnings("unchecked")
83+
Class result = converter.fromData(data, Class.class, Class.class);
84+
assertEquals(result.toString(), this.getClass(), result);
85+
}
7786
}

src/test/java/com/uber/cadence/workflow/WorkflowTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ public String execute(String taskList) {
560560
.setMinimumAttempts(1)
561561
.setMaximumInterval(Duration.ofSeconds(1))
562562
.setInitialInterval(Duration.ofSeconds(1))
563+
.setDoNotRetry(NullPointerException.class)
563564
.setMaximumAttempts(3)
564565
.build());
565566
} else {
@@ -569,6 +570,7 @@ public String execute(String taskList) {
569570
.setMaximumInterval(Duration.ofSeconds(1))
570571
.setInitialInterval(Duration.ofSeconds(1))
571572
.setMaximumAttempts(2)
573+
.setDoNotRetry(NullPointerException.class)
572574
.build());
573575
}
574576
this.activities = Workflow.newActivityStub(TestActivities.class, options.build());

0 commit comments

Comments
 (0)