forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicActivitiesClientImpl.java
More file actions
164 lines (133 loc) · 6.15 KB
/
DynamicActivitiesClientImpl.java
File metadata and controls
164 lines (133 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.amazonaws.services.simpleworkflow.flow;
import com.amazonaws.services.simpleworkflow.flow.core.Functor;
import com.amazonaws.services.simpleworkflow.flow.core.Promise;
import com.amazonaws.services.simpleworkflow.flow.core.Settable;
import com.amazonaws.services.simpleworkflow.flow.core.TryCatchFinally;
import com.amazonaws.services.simpleworkflow.flow.generic.ExecuteActivityParameters;
import com.amazonaws.services.simpleworkflow.flow.generic.GenericActivityClient;
import com.amazonaws.services.simpleworkflow.model.ActivityType;
public class DynamicActivitiesClientImpl implements DynamicActivitiesClient {
protected DataConverter dataConverter;
protected ActivitySchedulingOptions schedulingOptions;
protected GenericActivityClient genericClient;
protected DecisionContextProvider decisionContextProvider = new DecisionContextProviderImpl();
public DynamicActivitiesClientImpl() {
this(null, null, null);
}
public DynamicActivitiesClientImpl(ActivitySchedulingOptions schedulingOptions) {
this(schedulingOptions, null, null);
}
public DynamicActivitiesClientImpl(ActivitySchedulingOptions schedulingOptions, DataConverter dataConverter) {
this(schedulingOptions, dataConverter, null);
}
public DynamicActivitiesClientImpl(ActivitySchedulingOptions schedulingOptions, DataConverter dataConverter,
GenericActivityClient genericClient) {
this.genericClient = genericClient;
if (schedulingOptions == null) {
this.schedulingOptions = new ActivitySchedulingOptions();
}
else {
this.schedulingOptions = schedulingOptions;
}
if (dataConverter == null) {
this.dataConverter = new JsonDataConverter();
}
else {
this.dataConverter = dataConverter;
}
}
@Override
public DataConverter getDataConverter() {
return dataConverter;
}
public void setDataConverter(DataConverter dataConverter) {
this.dataConverter = dataConverter;
}
@Override
public ActivitySchedulingOptions getSchedulingOptions() {
return schedulingOptions;
}
public void setSchedulingOptions(ActivitySchedulingOptions schedulingOptions) {
this.schedulingOptions = schedulingOptions;
}
@Override
public GenericActivityClient getGenericClient() {
return genericClient;
}
public void setGenericClient(GenericActivityClient genericClient) {
this.genericClient = genericClient;
}
public <T> Promise<T> scheduleActivity(final ActivityType activityType, final Promise<?>[] arguments,
final ActivitySchedulingOptions optionsOverride, final Class<T> returnType, final Promise<?>... waitFor) {
return new Functor<T>(arguments) {
@Override
protected Promise<T> doExecute() throws Throwable {
Object[] input = new Object[arguments.length];
for (int i = 0; i < arguments.length; i++) {
Promise<?> argument = arguments[i];
input[i] = argument.get();
}
return scheduleActivity(activityType, input, optionsOverride, returnType, waitFor);
}
};
}
public <T> Promise<T> scheduleActivity(final ActivityType activityType, final Object[] arguments,
final ActivitySchedulingOptions optionsOverride, final Class<T> returnType, Promise<?>... waitFor) {
final Settable<T> result = new Settable<T>();
new TryCatchFinally(waitFor) {
Promise<String> stringOutput;
@Override
protected void doTry() throws Throwable {
ExecuteActivityParameters parameters = new ExecuteActivityParameters();
parameters.setActivityType(activityType);
String stringInput = dataConverter.toData(arguments);
parameters.setInput(stringInput);
final ExecuteActivityParameters _scheduleParameters_ = parameters.createExecuteActivityParametersFromOptions(
schedulingOptions, optionsOverride);
GenericActivityClient client;
if (genericClient == null) {
client = decisionContextProvider.getDecisionContext().getActivityClient();
} else {
client = genericClient;
}
stringOutput = client.scheduleActivityTask(_scheduleParameters_);
result.setDescription(stringOutput.getDescription());
}
@Override
protected void doCatch(Throwable e) throws Throwable {
if (e instanceof ActivityTaskFailedException) {
ActivityTaskFailedException taskFailedException = (ActivityTaskFailedException) e;
try {
String details = taskFailedException.getDetails();
if (details != null) {
Throwable cause = dataConverter.fromData(details, Throwable.class);
if (cause != null && taskFailedException.getCause() == null) {
taskFailedException.initCause(cause);
}
}
}
catch (DataConverterException dataConverterException) {
if (dataConverterException.getCause() == null) {
dataConverterException.initCause(taskFailedException);
}
throw dataConverterException;
}
}
throw e;
}
@Override
protected void doFinally() throws Throwable {
if (stringOutput != null && stringOutput.isReady()) {
if (returnType.equals(Void.class)) {
result.set(null);
}
else {
T output = dataConverter.fromData(stringOutput.get(), returnType);
result.set(output);
}
}
}
};
return result;
}
}