public static Future<TaskHandle> addAsync(Queue selfQueue, Map<String, Object> params) {
return selfQueue.addAsync(buildTaskOptions(params));
}
@SuppressWarnings("unchecked") private static TaskOptions buildTaskOptions(Map<String, Object> params) {
TaskOptions options = TaskOptions.Builder.withDefaults();
for(Entry<String, Object> e : params.entrySet()) {
String key = e.getKey();
Object value = e.getValue();
switch (key) {
case "countdownMillis":
options = options.countdownMillis(((Number)value).longValue());
break;
case "etaMillis":
options = options.etaMillis(((Number)value).longValue());
break;
case "taskName":
options = options.taskName(String.valueOf(value));
break;
case "url":
options = options.url(String.valueOf(value));
break;
case "headers":
if (value instanceof Map) {
for (Entry<String, String> header : ((Map<String, String>)value).entrySet()) {
options = options.header(header.getKey(), header.getValue());
}
} else {
throw new RuntimeException("The headers key/value pairs should be passed as a map.");
}
break;
case "retryOptions":
if (value instanceof Map) {
RetryOptions retryOptions = RetryOptions.Builder.withDefaults();
for (Entry<String, Object> option : ((Map<String, Object>)value).entrySet()) {
switch (option.getKey()) {
case "taskRetryLimit":
retryOptions.taskRetryLimit(((Number)option.getValue()).intValue());
break;
case "taskAgeLimitSeconds":
retryOptions.taskAgeLimitSeconds(((Number)option.getValue()).intValue());
break;
case "minBackoffSeconds":
retryOptions.minBackoffSeconds(((Number)option.getValue()).intValue());
break;
case "maxBackoffSeconds":
retryOptions.maxBackoffSeconds(((Number)option.getValue()).intValue());
break;
case "maxDoublings":
retryOptions.maxDoublings(((Number)option.getValue()).intValue());
break;
default:
throw new RuntimeException(option.getKey() + " is not a valid retry option parameter.");
}
}
options.retryOptions(retryOptions);
} else if (value instanceof RetryOptions) {
options.retryOptions((RetryOptions) value);
} else {
throw new RuntimeException("The retry options parameter should either be a map or an instance of RetryOptions.");
}
break;
case "method":
if (value instanceof TaskOptions.Method) {
options = options.method((Method) value);
} else if(ALLOWED_METHODS.contains(value)) {
options = options.method(TaskOptions.Method.valueOf((String) value));
} else {
throw new RuntimeException("Not a valid method: " + value);
}
break;
case "params":
if (value instanceof Map) {
for (Entry<String, Object> option : ((Map<String, Object>)value).entrySet()) {
options = options.param(option.getKey(), String.valueOf(option.getValue()));
}
} else {
throw new RuntimeException("The params key/value pairs should be passed as a map.");
}
break;
case "payload":
if (value instanceof List<?>) {
List<?> list = (List<?>) value;
options = options.payload(String.valueOf(list.get(0)), String.valueOf(list.get(1)));
} else if (value instanceof String) {
options = options.payload((String)value);
} else if (value instanceof Closure) {
options = options.payload(DefaultGroovyMethods.asType((Closure<?>)value, DeferredTask.class));
} else if (value instanceof DeferredTask) {
options = options.payload((DeferredTask)value);
} else {
options = options.payload(value.toString());
}
break;
default:
throw new RuntimeException(key + " is not a valid task option.\n" +
"Allowed: countdownMillis, etaMillis, taskName, url, headers, methods, params and payload");