com.linkedin.parseq.Task.async()方法的使用及代码示例

x33g5p2x  于2022-01-30 转载在 其他  
字(8.8k)|赞(0)|评价(0)|浏览(193)

本文整理了Java中com.linkedin.parseq.Task.async()方法的一些代码示例,展示了Task.async()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Task.async()方法的具体详情如下:
包路径:com.linkedin.parseq.Task
类名称:Task
方法名:async

Task.async介绍

[英]Equivalent to async("async", func).
[中]相当于async(“async”,func)。

代码示例

代码示例来源:origin: linkedin/parseq

/**
 * Equivalent to {@code async("async", callable)}.
 * @see #async(String, Callable)
 */
public static <T> Task<T> async(final Callable<Promise<? extends T>> callable) {
 return async("async: " + _taskDescriptor.getDescription(callable.getClass().getName()), callable);
}

代码示例来源:origin: linkedin/parseq

/**
 * Equivalent to {@code async("async", func)}.
 * @see #async(String, Function1)
 */
public static <T> Task<T> async(final Function1<Context, Promise<? extends T>> func) {
 return async("async: " + _taskDescriptor.getDescription(func.getClass().getName()), func);
}

代码示例来源:origin: linkedin/parseq

@Override
Task<String> getFailureTask() {
 return Task.async("failure", () -> {
  throw new RuntimeException(TASK_ERROR_MESSAGE);
 });
}

代码示例来源:origin: linkedin/parseq

public Task<Response> task(final String desc) {
 return Task.async(desc, () -> {
  final SettablePromise<Response> result = Promises.settable();
  _delegate.execute(new AsyncCompletionHandler<Response>() {
   @Override
   public Response onCompleted(final Response response) throws Exception {
    result.done(response);
    return response;
   }
   @Override
   public void onThrowable(Throwable t) {
    result.fail(t);
   }
  });
  return result;
 });
}

代码示例来源:origin: linkedin/parseq

@Override
 Task<String> getCancelledTask() {
  return Task.async("cancelled", () -> {
   throw new CancellationException(new TimeoutException());
  });
 }
}

代码示例来源:origin: linkedin/parseq

public static <K, RES> Task<RES> callService(final String name, final MockService<RES> service,
  final MockRequest<RES> request, K key) {
 if (service instanceof BatchableMockService) {
  BatchableMockService<RES> batchableService = (BatchableMockService<RES>)service;
  return batchableService.task(name, new MockRequestWithKey<K, RES>(key, request));
 } else {
  return Task.async(name, () -> service.call(request));
 }
}

代码示例来源:origin: linkedin/parseq

@Override
Task<String> getSuccessTask() {
 return Task.async("success", () -> Promises.value(TASK_VALUE));
}

代码示例来源:origin: linkedin/parseq

/**
 * A helper for creating task wrapper with associated retry policy.
 *
 * @param name A name of the task that needs to be retried.
 * @param policy Retry policy that will control this task's behavior.
 * @param taskFunction A task generator function. It will receive a zero-based attempt number as a parameter.
 * @param <U> Type of a task result.
 */
public static <U> Task<U> withRetryPolicy(String name, RetryPolicy policy, Function1<Integer, Task<U>> taskFunction) {
 RetriableTask<U> retriableTask = new RetriableTask<>(name, policy, taskFunction);
 Task<U> retryTaskWrapper = Task.async(name + " retriableTask", retriableTask::run);
 retryTaskWrapper.getShallowTraceBuilder().setTaskType(TaskType.WITH_RETRY.getName());
 return retryTaskWrapper;
}

代码示例来源:origin: linkedin/parseq

@Test
public void testAsyncMap0() {
 Task<Integer> task = Task.async("value", () -> {
  SettablePromise<Integer> p = Promises.settable();
  p.done(1);
  return p;
 });
 runAndWait("FusionTaskTraceTest.testAsyncMap0", task);
}

代码示例来源:origin: linkedin/parseq

@Test
public void testAsyncWithContext() {
 final Task<String> t = Task.callable(() -> "done");
 Task<String> task = Task.async(ctx -> {
  ctx.run(t);
  return t;
 });
 String value = runAndWait("TestTaskFactoryMethods.testAsyncWithContext", task);
 assertEquals(value, "done");
 assertEquals(countTasks(task.getTrace()), 2);
}

代码示例来源:origin: linkedin/parseq

@Test
public void testAsync() {
 final SettablePromise<String> promise = Promises.settable();
 Task<String> task = Task.async(() -> promise);
 getScheduler().schedule(() -> promise.done("done"), 10, TimeUnit.MILLISECONDS);
 String value = runAndWait("TestTaskFactoryMethods.testAsync", task);
 assertEquals(value, "done");
 assertEquals(countTasks(task.getTrace()), 1);
}

代码示例来源:origin: linkedin/parseq

private <T> Task<Response<T>> createTaskWithTimeout(final String name, final Request<T> request,
  final RequestContext requestContext, RequestConfig config) {
 ConfigValue<Long> timeout = config.getTimeoutMs();
 Task<Response<T>> requestTask;
 if (RequestGroup.isBatchable(request, config)) {
  requestTask = createBatchableTask(name, request, requestContext, config);
 } else {
  requestTask = Task.async(name, () -> sendRequest(request, requestContext));
 }
 if (!needApplyTaskTimeout(requestContext, timeout)) {
  return requestTask;
 } else {
  return withTimeout(requestTask, timeout);
 }
}

代码示例来源:origin: linkedin/parseq

@Test
public void testAsyncMap1() {
 Task<Integer> task = Task.async("value", () -> {
  SettablePromise<Integer> p = Promises.settable();
  p.done(1);
  return p;
 }).map("m1", x -> x);
 runAndWait("FusionTaskTraceTest.testAsyncMa1p", task);
}

代码示例来源:origin: linkedin/parseq

@Test
public void testTaskCancellationTimeout() throws InterruptedException {
 final AtomicReference<Throwable> cancelActionValue = new AtomicReference<>();
 final CountDownLatch runLatch = new CountDownLatch(1);
 final CountDownLatch listenerLatch = new CountDownLatch(1);
 Task<Integer> uncompleted = Task.async(() -> {
  runLatch.countDown();
  return Promises.settable();
 });
 uncompleted.addListener(p -> {
  if (p.isFailed() && Exceptions.isCancellation(p.getError())) {
   cancelActionValue.set(p.getError().getCause());
  }
  listenerLatch.countDown();
 } );
 Task<?> task = uncompleted.withTimeout(10, TimeUnit.MILLISECONDS).recover(e -> 0);
 runAndWait("TestTaskCancellation.testTaskCancellationTimeout", task);
 assertTrue(listenerLatch.await(5, TimeUnit.SECONDS));
 assertTrue(cancelActionValue.get() instanceof EarlyFinishException);
}

代码示例来源:origin: linkedin/parseq

@Test
public void testTaskCancellationBeforeRun() throws InterruptedException {
 final AtomicReference<Throwable> cancelActionValue = new AtomicReference<>();
 Task<?> uncompleted = Task.async(() -> Promises.settable());
 uncompleted.addListener(p -> {
  if (p.isFailed() && Exceptions.isCancellation(p.getError())) {
   cancelActionValue.set(p.getError().getCause());
  }
 } );
 Exception cancelReason = new Exception();
 assertTrue(uncompleted.cancel(cancelReason));
 getEngine().run(uncompleted);
 uncompleted.await(5, TimeUnit.SECONDS);
 logTracingResults("TestTaskCancellation.testTaskCancellationBeforeRun", uncompleted);
 assertEquals(cancelActionValue.get(), cancelReason);
}

代码示例来源:origin: linkedin/parseq

@Test
public void testAsync2() {
 Task<Integer> task = Task.async("value", () -> {
  SettablePromise<Integer> p = Promises.settable();
  p.done(1);
  return p;
 }).map("m1", x -> x).map("m2", x -> x);
 runAndWait("FusionTaskTraceTest.testAsyncMap2", task);
}

代码示例来源:origin: linkedin/parseq

@Test
public void testAsync3() {
 Task<Integer> task = Task.async("value", () -> {
  SettablePromise<Integer> p = Promises.settable();
  p.done(1);
  return p;
 }).map("m1", x -> x).map("m2", x -> x).map("m3", x -> x);
 runAndWait("FusionTaskTraceTest.testAsyncMap3", task);
}

代码示例来源:origin: linkedin/parseq

@Test
public void testAsyncFork() {
 Task<Integer> base = Task.async("value", () -> {
  SettablePromise<Integer> p = Promises.settable();
  p.done(1);
  return p;
 });
 Task<Integer> task = Task.par(base.map("m1", x -> x), base.map("m2", x -> x))
 .map("sum", (x, y) -> x + y);
 runAndWait("FusionTaskTraceTest.testAsyncFork", task);
}

代码示例来源:origin: linkedin/parseq

@Test
public void testAsync4() {
 Task<Integer> task = Task.async("value", () -> {
  SettablePromise<Integer> p = Promises.settable();
  p.done(1);
  return p;
 }).map("m1", x -> x).map("m2", x -> x).map("m3", x -> x).map("m4", x -> x);
 runAndWait("FusionTaskTraceTest.testAsyncMap4", task);
}

代码示例来源:origin: linkedin/parseq

@Test
public void testShutdownWithSideEffectTask2() throws InterruptedException {
 final SettablePromise<String> sideEffectPromise = Promises.settable();
 final String mainValue = "main task executed";
 final String sideEffectValue = "side-effect task executed";
 Task<String> sideEffect = Task.async(context -> sideEffectPromise);
 Task<String> task = Task.value(mainValue).withSideEffect(v -> sideEffect);
 _engine.run(task);
 _engine.shutdown();
 assertFalse(_engine.awaitTermination(50, TimeUnit.MILLISECONDS));
 assertTrue(_engine.isShutdown());
 assertFalse(_engine.isTerminated());
 sideEffectPromise.done(sideEffectValue);
 assertTrue(_engine.awaitTermination(50, TimeUnit.MILLISECONDS));
 assertTrue(_engine.isShutdown());
 assertTrue(_engine.isTerminated());
 assertEquals(mainValue, task.get());
 assertEquals(sideEffectValue, sideEffect.get());
}

相关文章