/** * Runs the given task. Task passed in as a parameter becomes a root on a new Plan. * All tasks created and started as a consequence of a root task will belong to that plan and will share a Trace. * <p> * This method throws {@code IllegalStateException} if Engine does not have capacity to run the task. * Engine's capacity is specified by a {@value #MAX_CONCURRENT_PLANS} configuration property. Use * {@link EngineBuilder#setEngineProperty(String, Object)} to set this property. * For the sake of backwards compatibility default value for a {@value #MAX_CONCURRENT_PLANS} is * {@value #DEFUALT_MAX_CONCURRENT_PLANS} which essentially means "unbounded capacity". * * @param task the task to run * @throws IllegalStateException */ public void run(final Task<?> task) { run(task, defaultPlanClass(task)); }
@Override protected void doRunExample(final Engine engine) throws Exception { final int[] toSort = createRandomArray(10, new Random()); final Task<int[]> mergeSort = mergeSort(toSort, new Range(0, toSort.length)); engine.run(mergeSort); mergeSort.await(); System.out.println("Before sort: " + Arrays.toString(toSort)); System.out.println("After sort: " + Arrays.toString(mergeSort.get())); Arrays.sort(toSort); System.out.println("Java sort: " + Arrays.toString(toSort)); ExampleUtil.printTracingResults(mergeSort); }
/** Task collections */ //--------------------------------------------------------------- @Override protected void doRunExample(final Engine engine) throws Exception { Task<?> task = createExtendedSummary(1); // Task<?> task = Task.par(createExtendedSummary(1), createExtendedSummary(2)); engine.run(task); runTaskAndPrintResults(engine, task); }
@Override public void runTask(Engine engine, Task<?> t) throws InterruptedException { initArrivalProcess(); if (lastNano == 0) { lastNano = System.nanoTime(); } long nextNano = lastNano + arrivalProcess.nanosToNextEvent(); long actualNano = waitUntil(nextNano); planExecutionAccuracy.recordValue(Math.abs(actualNano - nextNano)); engine.run(t); lastNano = nextNano; }
@Override protected void doRunExample(final Engine engine) throws Exception { final int[] toSort = createRandomArray(10, new Random()); final Task<int[]> mergeSort = new MergeSortPlan(toSort); engine.run(mergeSort); mergeSort.await(); System.out.println("Before sort: " + Arrays.toString(toSort)); System.out.println("After sort: " + Arrays.toString(mergeSort.get())); ExampleUtil.printTracingResults(mergeSort); }
private void runTaskAndPrintResults(final Engine engine, Task<?> task) throws InterruptedException { engine.run(task); task.await(); System.out.println(task.get()); ExampleUtil.printTracingResults(task); }
@Override protected void doRunExample(final Engine engine) throws Exception { final Task<?> nonBatchable = plan(this::nonBatchableTask); engine.run(nonBatchable); nonBatchable.await(); System.out.println("not batched:"); ExampleUtil.printTracingResults(nonBatchable); System.out.println("batched:"); final Task<?> batchable = plan(this::batchableTask); engine.run(batchable); batchable.await(); ExampleUtil.printTracingResults(batchable); }
@Override protected void doRunExample(final Engine engine) throws Exception { final Task<?> nonBatchable = plan(this::nonBatchableTask); engine.run(nonBatchable); nonBatchable.await(); System.out.println("not batched:"); ExampleUtil.printTracingResults(nonBatchable); System.out.println("batched:"); final Task<?> batchable = plan(this::batchableTask); engine.run(batchable); batchable.await(); ExampleUtil.printTracingResults(batchable); }
@Override protected void doRunExample(final Engine engine) throws Exception { final MockService<String> httpClient = getService(); final Task<Integer> fetchAndLength = fetch404Url(httpClient, "http://www.google.com") .map("length", x -> x.length()); engine.run(fetchAndLength); fetchAndLength.await(); System.out.println("Error while fetching url: " + fetchAndLength.getError()); ExampleUtil.printTracingResults(fetchAndLength); } }
@Override protected void doRunExample(final Engine engine) throws Exception { final MockService<String> httpClient = getService(); final Task<Integer> fetchAndLength = fetch404Url(httpClient, "http://www.google.com") .recover("default", t -> "") .map("length", s -> s.length()); engine.run(fetchAndLength); fetchAndLength.await(); System.out.println("Response length: " + fetchAndLength.get()); ExampleUtil.printTracingResults(fetchAndLength); } }
@Override protected void doRunExample(final Engine engine) throws Exception { final MockService<String> httpClient = getService(); final Task<Integer> fetchAndLength = fetchUrl(httpClient, "http://www.google.com", 100) .withTimeout(50, TimeUnit.MILLISECONDS) .recover("default", t -> "") .map("length", s -> s.length()); engine.run(fetchAndLength); fetchAndLength.await(); System.out.println("Response length: " + fetchAndLength.get()); ExampleUtil.printTracingResults(fetchAndLength); } }
@Override public void run() { engine.run(task); try { assertTrue(task.await(5, TimeUnit.SECONDS)); } catch (InterruptedException e) { // Ignore. } } });
@Override protected void doRunExample(final Engine engine) throws Exception { final MockService<String> httpClient = getService(); final Task<String> fetchBing = fetchUrl(httpClient, "http://www.bing.com"); final Task<String> fetchYahoo = fetchUrl(httpClient, "http://www.yahoo.com"); final Task<String> fetchGoogle = fetchUrl(httpClient, "http://www.google.com"); final Task<?> parFetch = Task.par(fetchBing, fetchGoogle, fetchYahoo); engine.run(parFetch); parFetch.await(); System.out.println("Bing => " + fetchBing.get()); System.out.println("Yahoo => " + fetchYahoo.get()); System.out.println("Google => " + fetchGoogle.get()); ExampleUtil.printTracingResults(parFetch); } }
@Test public void testRunWithinCapacity() throws InterruptedException { final AtomicInteger counter = new AtomicInteger(0); Task<?>[] tasks = new Task<?>[10]; for (int i = 0; i < 10; i++) { tasks[i] = Task.action(counter::incrementAndGet); } for (int i = 0; i < 10; i++) { _engine.run(tasks[i], (i % 2 == 0) ? "evenPlan" : "oddPlan"); } assertTrue(awaitAll(tasks)); assertEquals(10, counter.get()); }
@Test public void testRunWithinCapacity() throws InterruptedException { final AtomicInteger counter = new AtomicInteger(0); Task<?>[] tasks = new Task<?>[10]; for (int i = 0; i < 10; i++) { tasks[i] = Task.action(counter::incrementAndGet); } for (int i = 0; i < 10; i++) { _engine.run(tasks[i]); } assertTrue(awaitAll(tasks)); assertEquals(10, counter.get()); }
@Override protected void doRunExample(final Engine engine) throws Exception { final MockService<String> httpClient = getService(); final Task<String> fetchWithTimeout = fetchUrl(httpClient, "http://www.google.com") .withTimeout(50, TimeUnit.MILLISECONDS); engine.run(fetchWithTimeout); fetchWithTimeout.await(); System.out.println(!fetchWithTimeout.isFailed() ? "Received result: " + fetchWithTimeout.get() : "Error: " + fetchWithTimeout.getError()); ExampleUtil.printTracingResults(fetchWithTimeout); } }
@Override protected void doRunExample(final Engine engine) throws Exception { final MockService<String> httpClient = getService(); final Task<String> fetch = fetchUrl(httpClient, "http://www.google.com"); final Task<String> fetchWithTimeout = timeoutWithError(50, TimeUnit.MILLISECONDS, fetch); engine.run(fetchWithTimeout); fetchWithTimeout.await(); System.out.println(!fetchWithTimeout.isFailed() ? "Received result: " + fetchWithTimeout.get() : "Error: " + fetchWithTimeout.getError()); ExampleUtil.printTracingResults(fetchWithTimeout); } }
@Test public void testShutdownThenRunTask() throws InterruptedException { _engine.shutdown(); final Task<String> task = Task.value("task executed"); _engine.run(task); // Task should be cancelled immediately assertTrue(task.await(50, TimeUnit.MILLISECONDS)); assertTrue(task.isFailed()); }
@Test public void testTimeoutTaskWithError() throws InterruptedException { final Exception error = new Exception(); final Task<String> task = Task.callable("task", () -> { throw error; } ); final Task<String> timeoutTask = task.withTimeout(2000, TimeUnit.MILLISECONDS); getEngine().run(timeoutTask); // We should complete with an error almost immediately assertTrue(timeoutTask.await(100, TimeUnit.MILLISECONDS)); assertTrue(timeoutTask.isFailed()); assertEquals(error, timeoutTask.getError()); }
@Override protected void doRunExample(final Engine engine) throws Exception { final MockService<String> httpClient = getService(); Task<String> stage1 = Task.par(fetchAndMap("http://www.bing.com", httpClient), fetchAndMap("http://www.yahoo.com", httpClient)) .map((a, b) -> a + b); Task<String> stage2 = Task.par(fetchAndMap("http://www.google.com", httpClient), fetchAndMap("https://duckduckgo.com", httpClient)) .map((a, b) -> a + b); Task<String> plan = stage1.flatMap("combine", s1 -> stage2.map(s2 -> s1 + s2)); engine.run(plan); plan.await(); printTracingResults(plan); }