Ejemplo n.º 1
0
    @Override
    protected Promise<?> run(final Context context) {
      try {
        if (_contextIndex != -1) {
          // we can now supply the context
          _arguments[_contextIndex] = context;
        }
        Object applicationResult = _method.invoke(_resource, _arguments);
        if (applicationResult == null) {
          return Promises.error(
              new RestLiServiceException(
                  HttpStatus.S_500_INTERNAL_SERVER_ERROR,
                  "Error in application code: null Promise"));
        }
        // TODO Should we guard against incorrectly returning a task that has no way of
        // starting?
        return (Promise<?>) applicationResult;
      } catch (Throwable t) {
        // Method runtime exceptions ar expected to fail with a top level
        // InvocationTargetException wrapped around the root cause.
        if (t instanceof InvocationTargetException && t.getCause() != null) {
          // Unbury the exception thrown from the resource method if it's there.
          return Promises.error(
              t.getCause() instanceof RestLiServiceException
                  ? t.getCause()
                  : new RestLiServiceException(
                      HttpStatus.S_500_INTERNAL_SERVER_ERROR, t.getCause()));
        }

        return Promises.error(
            t instanceof RestLiServiceException
                ? t
                : new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, t));
      }
    }
Ejemplo n.º 2
0
 public static <V> Task<V> get(String key, Class<V> valueType) {
   return Task.async(
       "Get PlanLocal",
       context -> {
         long id = context.getPlanId();
         final Map<String, Object> map = _planLocalMap.get(id);
         return Promises.value(map != null ? (V) map.get(key) : null);
       });
 }
Ejemplo n.º 3
0
 /**
  * Constructs a base task with a name.
  *
  * @param name the name for this task.
  */
 public BaseTask(final String name) {
   super(Promises.<T>settable());
   _name = truncate(name);
   final State state = State.INIT;
   _shallowTraceBuilder = new ShallowTraceBuilder(_id);
   _shallowTraceBuilder.setName(getName());
   _shallowTraceBuilder.setResultType(ResultType.UNFINISHED);
   _stateRef = new AtomicReference<State>(state);
 }
Ejemplo n.º 4
0
 public static <V> Task<Void> put(String key, V value) {
   return Task.async(
       "Put PlanLocal",
       context -> {
         long id = context.getPlanId();
         final Map<String, Object> map = _planLocalMap.computeIfAbsent(id, k -> new HashMap());
         map.put(key, value);
         return Promises.value(null);
       });
 }
Ejemplo n.º 5
0
 public static Task<Void> remove(String key) {
   return Task.async(
       "Remove PlanLocal",
       context -> {
         long id = context.getPlanId();
         _planLocalMap.computeIfPresent(
             id,
             (k, v) -> {
               v.remove(key);
               return v.isEmpty() ? null : v;
             });
         return Promises.value(null);
       });
 }
Ejemplo n.º 6
0
  @Override
  protected Promise<List<T>> run(final Context context) throws Exception {
    final SettablePromise<List<T>> result = Promises.settable();

    final PromiseListener<?> listener =
        new PromiseListener<Object>() {
          @Override
          public void onResolved(Promise<Object> resolvedPromise) {
            boolean allEarlyFinish = true;
            final List<T> taskResult = new ArrayList<T>();
            final List<Throwable> errors = new ArrayList<Throwable>();

            for (Task<? extends T> task : _tasks) {
              if (task.isFailed()) {
                if (allEarlyFinish && ResultType.fromTask(task) != ResultType.EARLY_FINISH) {
                  allEarlyFinish = false;
                }
                errors.add(task.getError());
              } else {
                taskResult.add(task.get());
              }
            }
            if (!errors.isEmpty()) {
              result.fail(
                  allEarlyFinish
                      ? errors.get(0)
                      : new MultiException("Multiple errors in 'ParTask' task.", errors));
            } else {
              result.done(taskResult);
            }
          }
        };

    InternalUtil.after(listener, _tasks.toArray(new Task<?>[_tasks.size()]));

    for (Task<?> task : _tasks) {
      context.run(task);
    }

    return result;
  }