Search in sources :

Example 1 with ManagedScheduledExecutorServiceImplFactory

use of org.apache.openejb.resource.thread.ManagedScheduledExecutorServiceImplFactory in project tomee by apache.

the class ManagedScheduledExecutorServiceImplFactoryTest method createServiceTest.

@Test
public void createServiceTest() {
    final ManagedScheduledExecutorServiceImplFactory factory = new ManagedScheduledExecutorServiceImplFactory();
    factory.setThreadFactory(MyThreadFactory.class.getName());
    final ManagedScheduledExecutorServiceImpl executorService = factory.create();
    final ScheduledThreadPoolExecutor poolExecutor = (ScheduledThreadPoolExecutor) executorService.getDelegate();
    Assert.assertEquals(poolExecutor.getThreadFactory().getClass(), ManagedThreadFactoryImpl.class);
}
Also used : ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) ManagedScheduledExecutorServiceImpl(org.apache.openejb.threads.impl.ManagedScheduledExecutorServiceImpl) ManagedScheduledExecutorServiceImplFactory(org.apache.openejb.resource.thread.ManagedScheduledExecutorServiceImplFactory) Test(org.junit.Test)

Example 2 with ManagedScheduledExecutorServiceImplFactory

use of org.apache.openejb.resource.thread.ManagedScheduledExecutorServiceImplFactory 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 3 with ManagedScheduledExecutorServiceImplFactory

use of org.apache.openejb.resource.thread.ManagedScheduledExecutorServiceImplFactory 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)

Example 4 with ManagedScheduledExecutorServiceImplFactory

use of org.apache.openejb.resource.thread.ManagedScheduledExecutorServiceImplFactory in project tomee by apache.

the class ManagedScheduledExecutorServiceTest method simpleSchedule.

@Test
public void simpleSchedule() throws Exception {
    final ManagedScheduledExecutorService es = new ManagedScheduledExecutorServiceImplFactory().create();
    final long start = System.currentTimeMillis();
    final ScheduledFuture<Long> future = es.schedule(new Callable<Long>() {

        @Override
        public Long call() throws Exception {
            Thread.sleep(4000);
            return System.currentTimeMillis();
        }
    }, 2, TimeUnit.SECONDS);
    assertEquals(6, TimeUnit.MILLISECONDS.toSeconds(future.get() - start), 1);
}
Also used : ManagedScheduledExecutorServiceImplFactory(org.apache.openejb.resource.thread.ManagedScheduledExecutorServiceImplFactory) ManagedScheduledExecutorService(javax.enterprise.concurrent.ManagedScheduledExecutorService) Test(org.junit.Test)

Aggregations

ManagedScheduledExecutorServiceImplFactory (org.apache.openejb.resource.thread.ManagedScheduledExecutorServiceImplFactory)4 Test (org.junit.Test)4 ManagedScheduledExecutorService (javax.enterprise.concurrent.ManagedScheduledExecutorService)3 Date (java.util.Date)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 LastExecution (javax.enterprise.concurrent.LastExecution)2 Trigger (javax.enterprise.concurrent.Trigger)2 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 ManagedScheduledExecutorServiceImpl (org.apache.openejb.threads.impl.ManagedScheduledExecutorServiceImpl)1