Search in sources :

Example 1 with LastExecution

use of javax.enterprise.concurrent.LastExecution in project tomee by apache.

the class ManagedScheduledExecutorServiceTest method triggerCallableSchedule.

@Test
public void triggerCallableSchedule() throws Exception {
    final ManagedScheduledExecutorService es = new ManagedScheduledExecutorServiceImplFactory().create();
    final CountDownLatch counter = new CountDownLatch(5);
    final FutureAwareCallable callable = new FutureAwareCallable(counter);
    final Future<Long> future = es.schedule((Callable<Long>) callable, new Trigger() {

        @Override
        public Date getNextRunTime(final LastExecution lastExecutionInfo, final Date taskScheduledTime) {
            if (lastExecutionInfo == null) {
                return new Date();
            }
            return new Date(System.currentTimeMillis() + 100);
        }

        @Override
        public boolean skipRun(final LastExecution lastExecutionInfo, final Date scheduledRunTime) {
            return false;
        }
    });
    assertFalse(future.isDone());
    assertFalse(future.isCancelled());
    counter.await(1, TimeUnit.SECONDS);
    assertEquals("Future was not called", 0L, future.get().longValue());
    future.cancel(true);
    assertEquals("Counter did not count down in time", 0L, counter.getCount());
    assertTrue(future.isDone());
    assertTrue(future.isCancelled());
}
Also used : Trigger(javax.enterprise.concurrent.Trigger) LastExecution(javax.enterprise.concurrent.LastExecution) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedScheduledExecutorServiceImplFactory(org.apache.openejb.resource.thread.ManagedScheduledExecutorServiceImplFactory) ManagedScheduledExecutorService(javax.enterprise.concurrent.ManagedScheduledExecutorService) Date(java.util.Date) Test(org.junit.Test)

Example 2 with LastExecution

use of javax.enterprise.concurrent.LastExecution in project tomee by apache.

the class ManagedScheduledExecutorServiceTest method triggerRunnableSchedule.

@Test
public void triggerRunnableSchedule() throws Exception {
    final ManagedScheduledExecutorService es = new ManagedScheduledExecutorServiceImplFactory().create();
    final CountDownLatch counter = new CountDownLatch(5);
    final FutureAwareCallable callable = new FutureAwareCallable(counter);
    final ScheduledFuture<?> future = es.schedule(Runnable.class.cast(callable), new Trigger() {

        @Override
        public Date getNextRunTime(final LastExecution lastExecutionInfo, final Date taskScheduledTime) {
            if (lastExecutionInfo == null) {
                return new Date();
            }
            return new Date(lastExecutionInfo.getRunEnd().getTime() + 200);
        }

        @Override
        public boolean skipRun(final LastExecution lastExecutionInfo, final Date scheduledRunTime) {
            return false;
        }
    });
    assertFalse(future.isDone());
    assertFalse(future.isCancelled());
    //Should easily get 5 invocations within 1 second
    counter.await(2, TimeUnit.SECONDS);
    future.cancel(true);
    assertEquals("Counter did not count down in time", 0L, counter.getCount());
    final boolean done = future.isDone();
    assertTrue(done);
    final boolean cancelled = future.isCancelled();
    assertTrue(cancelled);
}
Also used : Trigger(javax.enterprise.concurrent.Trigger) LastExecution(javax.enterprise.concurrent.LastExecution) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedScheduledExecutorServiceImplFactory(org.apache.openejb.resource.thread.ManagedScheduledExecutorServiceImplFactory) ManagedScheduledExecutorService(javax.enterprise.concurrent.ManagedScheduledExecutorService) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Date (java.util.Date)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 LastExecution (javax.enterprise.concurrent.LastExecution)2 ManagedScheduledExecutorService (javax.enterprise.concurrent.ManagedScheduledExecutorService)2 Trigger (javax.enterprise.concurrent.Trigger)2 ManagedScheduledExecutorServiceImplFactory (org.apache.openejb.resource.thread.ManagedScheduledExecutorServiceImplFactory)2 Test (org.junit.Test)2