Search in sources :

Example 6 with TimerConfig

use of javax.ejb.TimerConfig in project wildfly by wildfly.

the class ExpiredTimerTestCase method testInvocationOnExpiredTimer.

@Test
public void testInvocationOnExpiredTimer() throws Exception {
    final CountDownLatch timeoutNotifier = new CountDownLatch(1);
    final CountDownLatch timeoutWaiter = new CountDownLatch(1);
    this.bean.createSingleActionTimer(TIMER_TIMEOUT_TIME_MS, new TimerConfig(null, false), timeoutNotifier, timeoutWaiter);
    // wait for the timeout to be invoked
    final boolean timeoutInvoked = timeoutNotifier.await(TIMER_CALL_WAITING_S, TimeUnit.SECONDS);
    Assert.assertTrue("timeout method was not invoked (within " + TIMER_CALL_WAITING_S + " seconds)", timeoutInvoked);
    // the timer stays in timeout method - checking how the invoke of method getNext and getTimeRemaining behave
    try {
        bean.invokeTimeRemaining();
        Assert.fail("Expecting exception " + NoMoreTimeoutsException.class.getSimpleName());
    } catch (NoMoreTimeoutsException e) {
        log.trace("Expected exception " + e.getClass().getSimpleName() + " was thrown on method getTimeRemaining");
    }
    try {
        bean.invokeGetNext();
        Assert.fail("Expecting exception " + NoMoreTimeoutsException.class.getSimpleName());
    } catch (NoMoreTimeoutsException e) {
        log.trace("Expected exception " + e.getClass().getSimpleName() + " was thrown on method getNextTimeout");
    }
    // the timeout can finish
    timeoutWaiter.countDown();
    // as we can't be exactly sure when the timeout method is finished in this moment
    // we invoke in a loop, can check the exception type.
    int count = 0;
    boolean passed = false;
    while (count < 20 && !passed) {
        try {
            bean.invokeTimeRemaining();
            Assert.fail("Expected to fail on invoking on an expired timer");
        } catch (NoSuchObjectLocalException nsole) {
            // expected
            log.trace("Got the expected exception " + nsole);
            passed = true;
        } catch (NoMoreTimeoutsException e) {
            //this will be thrown if the timer is still active
            Thread.sleep(100);
            count++;
        }
    }
    if (!passed) {
        Assert.fail("Got NoMoreTimeoutsException rather than  NoSuchObjectLocalException");
    }
}
Also used : NoSuchObjectLocalException(javax.ejb.NoSuchObjectLocalException) NoMoreTimeoutsException(javax.ejb.NoMoreTimeoutsException) TimerConfig(javax.ejb.TimerConfig) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 7 with TimerConfig

use of javax.ejb.TimerConfig in project wildfly by wildfly.

the class TimerBeanInOtherModule method createTimerForNextDay.

public void createTimerForNextDay(final boolean persistent, final String info) {
    this.timerService.createSingleActionTimer(new Date(System.currentTimeMillis() + (60 * 60 * 24 * 1000)), new TimerConfig(info, persistent));
    logger.trace("Created a timer persistent = " + persistent + " info = " + info);
}
Also used : TimerConfig(javax.ejb.TimerConfig) Date(java.util.Date)

Example 8 with TimerConfig

use of javax.ejb.TimerConfig in project wildfly by wildfly.

the class NonPersistentTimerServiceBean method createTimer.

public void createTimer() {
    TimerConfig timerConfig = new TimerConfig();
    timerConfig.setPersistent(false);
    timerService.createIntervalTimer(100, 100, timerConfig);
}
Also used : TimerConfig(javax.ejb.TimerConfig)

Example 9 with TimerConfig

use of javax.ejb.TimerConfig in project tomee by apache.

the class FarmerBrown method construct.

@PostConstruct
private void construct() {
    final TimerConfig plantTheCorn = new TimerConfig("plantTheCorn", false);
    timerService.createCalendarTimer(new ScheduleExpression().month(5).dayOfMonth("20-Last").minute(0).hour(8), plantTheCorn);
    timerService.createCalendarTimer(new ScheduleExpression().month(6).dayOfMonth("1-10").minute(0).hour(8), plantTheCorn);
    final TimerConfig harvestTheCorn = new TimerConfig("harvestTheCorn", false);
    timerService.createCalendarTimer(new ScheduleExpression().month(9).dayOfMonth("20-Last").minute(0).hour(8), harvestTheCorn);
    timerService.createCalendarTimer(new ScheduleExpression().month(10).dayOfMonth("1-10").minute(0).hour(8), harvestTheCorn);
    final TimerConfig checkOnTheDaughters = new TimerConfig("checkOnTheDaughters", false);
    timerService.createCalendarTimer(new ScheduleExpression().second("*").minute("*").hour("*"), checkOnTheDaughters);
}
Also used : ScheduleExpression(javax.ejb.ScheduleExpression) TimerConfig(javax.ejb.TimerConfig) PostConstruct(javax.annotation.PostConstruct)

Example 10 with TimerConfig

use of javax.ejb.TimerConfig in project wildfly by wildfly.

the class SimpleTimerServiceTestCase method testIntervalTimer.

@Test
@InSequence(3)
public void testIntervalTimer() throws NamingException {
    InitialContext ctx = new InitialContext();
    TimerConfig timerConfig = new TimerConfig();
    timerConfig.setInfo(INFO_MSG_FOR_CHECK);
    AnnotationTimerServiceBean bean1 = (AnnotationTimerServiceBean) ctx.lookup("java:module/" + AnnotationTimerServiceBean.class.getSimpleName());
    bean1.resetTimerServiceCalled();
    long ts = (new Date()).getTime() + TIMER_INIT_TIME_MS;
    Timer timer1 = bean1.getTimerService().createIntervalTimer(new Date(ts), TIMER_TIMEOUT_TIME_MS, timerConfig);
    Assert.assertTrue(AnnotationTimerServiceBean.awaitTimerCall());
    bean1.resetTimerServiceCalled();
    Assert.assertTrue(AnnotationTimerServiceBean.awaitTimerCall());
    timer1.cancel();
    TimedObjectTimerServiceBean bean2 = (TimedObjectTimerServiceBean) ctx.lookup("java:module/" + TimedObjectTimerServiceBean.class.getSimpleName());
    bean2.resetTimerServiceCalled();
    Timer timer2 = bean2.getTimerService().createIntervalTimer(TIMER_INIT_TIME_MS, TIMER_TIMEOUT_TIME_MS, timerConfig);
    Assert.assertTrue(TimedObjectTimerServiceBean.awaitTimerCall());
    bean2.resetTimerServiceCalled();
    Assert.assertTrue(TimedObjectTimerServiceBean.awaitTimerCall());
    timer2.cancel();
}
Also used : Timer(javax.ejb.Timer) TimerConfig(javax.ejb.TimerConfig) InitialContext(javax.naming.InitialContext) Date(java.util.Date) Test(org.junit.Test) InSequence(org.jboss.arquillian.junit.InSequence)

Aggregations

TimerConfig (javax.ejb.TimerConfig)16 Test (org.junit.Test)7 Date (java.util.Date)6 InitialContext (javax.naming.InitialContext)6 ScheduleExpression (javax.ejb.ScheduleExpression)4 Timer (javax.ejb.Timer)4 ModelNode (org.jboss.dmr.ModelNode)3 InSequence (org.jboss.arquillian.junit.InSequence)2 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 PostConstruct (javax.annotation.PostConstruct)1 NoMoreTimeoutsException (javax.ejb.NoMoreTimeoutsException)1 NoSuchObjectLocalException (javax.ejb.NoSuchObjectLocalException)1 NamingException (javax.naming.NamingException)1 ScheduleData (org.apache.openejb.core.timer.ScheduleData)1