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

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

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

Task.callable介绍

[英]Creates a new task that's value will be set to the value returned from the supplied callable. This task is useful when doing basic computation that does not require asynchrony. It is not appropriate for long running or blocking callables. If callable is long running or blocking use #blocking(String,Callable,Executor) method.

// this task will complete with  
 String representing current time 
Task 
  task = Task.callable("current time", ()  
 -> new Date().toString());

Returned task will fail if callable passed in as a parameter throws an exception.

// this task will fail with java.lang.ArithmeticException 
Task 
  task = Task.callable("division", ()  
 -> 2 / 0);

[中]创建一个新任务,该任务的值将设置为从提供的可调用函数返回的值。当执行不需要异步的基本计算时,此任务非常有用。它不适合长时间运行或阻塞可调用项。如果callable是长时间运行或阻塞的,请使用#阻塞(String,callable,Executor)方法

// this task will complete with  
 String representing current time 
Task 
  task = Task.callable("current time", ()  
 -> new Date().toString());

如果作为参数传入的callable引发异常,则返回的任务将失败

// this task will fail with java.lang.ArithmeticException 
Task 
  task = Task.callable("division", ()  
 -> 2 / 0);

代码示例

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

Task<Map<Long, Try<String>>> batchGet(Collection<Long> keys) {
  return Task.callable("batchGet",
    () -> keys.stream().collect(Collectors.toMap(Function.identity(), key -> Success.of(Long.toString(key)))));
 }
}

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

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

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

@Override
public Task<Map<K, Try<T>>> taskForBatch(final G group, final Set<K> keys) {
 return Task.callable("taskForBatch", () -> {
  if (keys.size() == 1) {
   _singletons.add(keys);
  } else {
   _batches.add(keys);
  }
  return keys.stream().collect(Collectors.toMap(Function.identity(), _completer));
 });
}

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

@Override
 public Task<Map<Integer, Try<String>>> taskForBatch(Set<Integer> keys) {
  return Task.callable("taskForBatch", () -> {
   return keys.stream().collect(Collectors.toMap(Function.identity(), key -> Success.of(Integer.toString(key))));
  });
 }
}

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

@Test
public void testExecuteMultipleTimes() {
 final AtomicInteger counter = new AtomicInteger();
 Task<String> task = Task.callable(() -> {
  counter.incrementAndGet();
  return "hello";
 } );
 runAndWait("TestTaskReuse.testExecuteMultipleTimes-1", task);
 runAndWait("TestTaskReuse.testExecuteMultipleTimes-2", task);
 runAndWait("TestTaskReuse.testExecuteMultipleTimes-3", task);
 assertEquals(counter.get(), 1);
}

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

@Test
public void testCallable() {
 Task<UUID> task = Task.callable(UUID::randomUUID);
 runAndWait("TestTaskFactoryMethods.testCallable", task);
 assertNotNull(task.get());
 assertEquals(countTasks(task.getTrace()), 1);
}

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

@Test
public void testFlattenFailure() {
 Task<Task<String>> nested = Task.callable(() -> getFailureTask());
 Task<String> flat = Task.flatten(nested);
 try {
  runAndWait("AbstractTaskTest.testFlattenFailure", flat);
  fail("should have failed");
 } catch (Exception ex) {
  assertTrue(flat.isFailed());
 }
 assertEquals(flat.getError().getMessage(), TASK_ERROR_MESSAGE);
}

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

public void testFlatMap(int expectedNumberOfTasks) {
 Task<String> task = getSuccessTask().flatMap(str -> Task.callable("strlenstr", () -> String.valueOf(str.length())));
 runAndWait("AbstractTaskTest.testFlatMap", task);
 assertEquals(task.get(), String.valueOf(TASK_VALUE.length()));
 assertEquals(countTasks(task.getTrace()), expectedNumberOfTasks);
}

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

@Test
public void testFlatten() {
 Task<Task<String>> nested = Task.callable(() -> getSuccessTask());
 Task<String> flat = Task.flatten(nested);
 runAndWait("AbstractTaskTest.testFlatten", flat);
 assertEquals(flat.get(), TASK_VALUE);
}

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

@Test
public void testRunWithSyncError() throws InterruptedException {
 final Exception exception = new Exception();
 final Task<String> task = Task.callable("task", () -> {
  throw exception;
 } );
 runTask(task);
 assertTrue(task.await(5, TimeUnit.SECONDS));
 assertFailed(task, exception);
}

代码示例来源: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 testRun() throws InterruptedException {
 final String result = "result";
 final Task<String> task = Task.callable("task", () -> result);
 runTask(task);
 assertTrue(task.await(5, TimeUnit.SECONDS));
 assertDone(task, result);
}

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

public void testRecoverWithSuccess(int expectedNumberOfTasks) {
 Task<String> success = getSuccessTask().recoverWith(e -> Task.callable("recover failure", () -> {
  throw new RuntimeException("recover failed!");
 } ));
 runAndWait("AbstractTaskTest.testRecoverWithSuccess", success);
 assertEquals(success.get(), TASK_VALUE);
 assertEquals(countTasks(success.getTrace()), expectedNumberOfTasks);
}

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

@Test
public void testHappyPath() throws InterruptedException {
 final Task<Try<String>> task = Task.callable("test", () -> "hello").toTry();
 runAndWait("TestWithTry.testHappyPath", task);
 assertFalse(task.get().isFailed());
 assertEquals("hello", task.get().get());
}

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

public void testRecoverWithCancelled(int expectedNumberOfTasks) {
 Task<String> cancelled = getCancelledTask().recoverWith(e -> Task.callable("recover success", () -> "recovered"));
 try {
  runAndWait("AbstractTaskTest.testRecoverWithCancelled", cancelled);
  fail("should have failed");
 } catch (Exception ex) {
  assertTrue(cancelled.isFailed());
  assertTrue(Exceptions.isCancellation(cancelled.getError()));
 }
 assertEquals(countTasks(cancelled.getTrace()), expectedNumberOfTasks);
}

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

public void testRecoverWithRecoverd(int expectedNumberOfTasks) {
 Task<String> recovered = getFailureTask().recoverWith(e -> Task.callable("recover success", () -> "recovered"));
 runAndWait("AbstractTaskTest.testRecoverWithRecoverd", recovered);
 assertEquals(recovered.get(), "recovered");
 assertEquals(countTasks(recovered.getTrace()), expectedNumberOfTasks);
}

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

@Test
public void testFlatMapTaskType() {
 Task<String> task = Task.value("Welcome");
 Task<String> flatMap = task.flatMap("+earth", s -> Task.callable(() -> s + " on earth!"));
 runAndWait("flatMapTaskType", flatMap);
 assertEquals(flatMap.getShallowTrace().getTaskType(), TaskType.FLATTEN.getName());
}

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

public void testAndThenTask(int expectedNumberOfTasks) {
 Task<Integer> task = getSuccessTask().andThen(Task.callable("life", () -> 42));
 runAndWait("AbstractTaskTest.testAndThenTask", task);
 assertEquals((int) task.get(), 42);
 assertEquals(countTasks(task.getTrace()), expectedNumberOfTasks);
}

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

@Test
public void testCancelAfterDone() throws InterruptedException {
 final String result = "result";
 final Task<String> task = Task.callable("task", () -> result);
 runTask(task);
 assertTrue(task.await(5, TimeUnit.SECONDS));
 assertDone(task, result);
 assertFalse(task.cancel(new Exception()));
 assertDone(task, result);
}

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

@Test
public void testSetPriorityAfterRun() throws InterruptedException {
 final String result = "result";
 final Task<String> task = Task.callable("task", () -> result);
 runTask(task);
 assertTrue(task.await(5, TimeUnit.SECONDS));
 assertFalse(task.setPriority(5));
 assertEquals(0, task.getPriority());
 assertDone(task, result);
}

相关文章