@Override public Task<?> call() throws Exception { return new BasicTask<Integer>(new Callable<Integer>() { @Override public Integer call() { log.info("task running ("+i+"): "+Tasks.current()+" "+Tasks.current().getStatusDetail(false)); ScheduledTask submitter = (ScheduledTask) ((BasicTask)Tasks.current()).getSubmittedByTask(); i.incrementAndGet(); if (i.get() >= 5) submitter.cancel(); return i.get(); }}); }});
@Override public Task<?> call() { return new BasicTask<Void>(new Runnable() { @Override public void run() { timestamps.add(System.currentTimeMillis()); }}); }}; ScheduledTask t = new ScheduledTask(taskFactory).period(period);
@Override public Task<?> call() throws Exception { return new BasicTask<Integer>(new Callable<Integer>() { @Override public Integer call() { log.debug("task running: "+Tasks.current()+" "+Tasks.current().getStatusDetail(false)); return i.incrementAndGet(); }}); }});
@Override public Task<?> call() throws Exception { return new BasicTask<>(new Callable<Integer>() { @Override public Integer call() { calls.incrementAndGet(); throw new RuntimeException("boo"); }}); }});
@Override public Task<?> call() { return new BasicTask<Void>(new Runnable() { @Override public void run() { latch.countDown(); }}); }}; ScheduledTask t = new ScheduledTask(taskFactory).delay(delay);
@Override public Task<?> call() throws Exception { return new BasicTask<>(new Callable<Integer>() { @Override public Integer call() { calls.incrementAndGet(); throw new RuntimeException("boo"); }}); }});
@Override public Task<?> call() throws Exception { return new BasicTask<Integer>(new Callable<Integer>() { @Override public Integer call() { ScheduledTask submitter = (ScheduledTask) ((BasicTask)Tasks.current()).getSubmittedByTask(); if (i.get() >= 4) submitter.period = null; log.info("task running ("+i+"): "+Tasks.current()+" "+Tasks.current().getStatusDetail(false)); return i.incrementAndGet(); }}); }});
@Override public Task<?> call() throws Exception { return new BasicTask<Void>(new Runnable() { @Override public void run() { execTimes.add(System.currentTimeMillis()); try { Thread.sleep(100); } catch (InterruptedException e) { throw Exceptions.propagate(e); } }}); }});
@Override public TaskAdaptable<String> newTask() { return new BasicTask<>(new Callable<String>() { public String call() { executing.set(true); try { Time.sleep(Duration.ONE_MINUTE); return "myval"; } finally { executing.set(false); } }}); } };
@Override public Task<?> call() { return new BasicTask<Void>(new Runnable() { @Override public void run() { timestamps.add(stopwatch.elapsed(TimeUnit.MILLISECONDS)); if (timestamps.size() >= numTimestamps) latch.countDown(); }}); }}; ScheduledTask t = new ScheduledTask(taskFactory).delay(1).period(period);
private BasicTask<String> taskReturning(final String val) { return new BasicTask<String>(new Callable<String>() { @Override public String call() { return val; } }); }
private BasicTask<String> slowTaskReturning(final String val, final Duration pauseTime) { return new BasicTask<String>(new Callable<String>() { @Override public String call() { Time.sleep(pauseTime); return val; } }); }
@SuppressWarnings("unchecked") protected void schedulePublish(long delay) { if (isRunning() && executorQueued.compareAndSet(false, true)) { long now = System.currentTimeMillis(); delay = Math.max(0, Math.max(delay, (executorTime + MIN_PERIOD_BETWEEN_EXECS_MILLIS) - now)); if (LOG.isTraceEnabled()) LOG.trace("{} scheduling publish in {}ms", this, delay); Runnable job = new PublishJob(); ScheduledTask task = new ScheduledTask(MutableMap.of("delay", Duration.of(delay, TimeUnit.MILLISECONDS)), new BasicTask<Void>(job)); ((EntityInternal)entity).getExecutionContext().submit(task); } }
@Test public void runSimpleBasicTask() throws Exception { BasicTask<Object> t = new BasicTask<Object>(newPutCallable(1, "b")); data.put(1, "a"); Task<Object> t2 = em.submit(MutableMap.of("tag", "A"), t); assertEquals("a", t.get()); assertEquals("a", t2.get()); assertEquals("b", data.get(1)); }
@Test public void testRetrievingTasksWithAllTagsWhenFirstNotMatched() throws Exception { Task<?> t = new BasicTask<Void>(newNoop()); em.submit(MutableMap.of("tags", ImmutableList.of("A")), t); t.get(); assertEquals(em.getTasksWithAllTags(ImmutableList.of("not_there","A")), ImmutableSet.of()); }
@Test public void testRetrievingTasksWithTagsExcludesNonMatchingTasks() throws Exception { Task<?> t = new BasicTask<Void>(newNoop()); em.submit(MutableMap.of("tag", "A"), t); t.get(); assertEquals(em.getTasksWithTag("B"), ImmutableSet.of()); assertEquals(em.getTasksWithAnyTag(ImmutableList.of("B")), ImmutableSet.of()); assertEquals(em.getTasksWithAllTags(ImmutableList.of("A", "B")), ImmutableSet.of()); }
@Test public void testCanExcludeNonEffectorTasks() throws Exception { ExecutionContext executionContext = mgmt.getExecutionContext(e); executionContext.submit(new BasicTask<Void>(new Runnable() { @Override public void run() {} })); Set<Task<?>> effectTasks = mgmt.getExecutionManager().getTasksWithAllTags(ImmutableList.of( BrooklynTaskTags.tagForContextEntity(e),ManagementContextInternal.EFFECTOR_TAG)); assertEquals(effectTasks.size(), 0); }
@Test public void testGetResultOfQueuedTaskBeforeItExecutesWithTimeout() throws Exception { final CountDownLatch latch = new CountDownLatch(1); em.submit(MutableMap.of("tag", "category1"), newLatchAwaiter(latch)); BasicTask<Integer> t = new BasicTask<Integer>(Callables.returning(123)); Future<Integer> future = em.submit(MutableMap.of("tag", "category1"), t); try { assertEquals(future.get(10, TimeUnit.MILLISECONDS), (Integer)123); fail(); } catch (TimeoutException e) { // success } }
@Test public void testTransform() throws Exception { Task<Integer> t = DependentConfiguration.transform( new BasicTask<Integer>(Callables.returning(2)), incrementerFunction()); submit(t); assertEquals(t.get(TIMEOUT_MS, TimeUnit.MILLISECONDS), Integer.valueOf(3)); }
@Test public void testFormatString() throws Exception { Task<String> t = DependentConfiguration.formatString("%s://%s:%d/", "http", new BasicTask<String>(Callables.returning("localhost")), DependentConfiguration.transform(new BasicTask<Integer>(Callables.returning(8080)), incrementerFunction())); submit(t); Assert.assertEquals(t.get(TIMEOUT_MS, TimeUnit.MILLISECONDS), "http://localhost:8081/"); }