Search in sources :

Example 1 with Timer

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

the class TimerServiceSuspendTestCase method testIntervalTimersDoNotBackUp.

/**
     * This test makes sure that interval timers that are scheduled while a timer is suspended do not back up, and only a single
     * run will occur when the container is resumed
     */
@Test
public void testIntervalTimersDoNotBackUp() throws NamingException, IOException, InterruptedException {
    SuspendTimerServiceBean.resetTimerServiceCalled();
    InitialContext ctx = new InitialContext();
    SuspendTimerServiceBean bean = (SuspendTimerServiceBean) ctx.lookup("java:module/" + SuspendTimerServiceBean.class.getSimpleName());
    Timer timer = null;
    try {
        long start = 0;
        ModelNode op = new ModelNode();
        try {
            op.get(ModelDescriptionConstants.OP).set("suspend");
            managementClient.getControllerClient().execute(op);
            //create the timer while the container is suspended
            start = System.currentTimeMillis();
            timer = bean.getTimerService().createIntervalTimer(100, 100, new TimerConfig("", false));
            Thread.sleep(5000);
            Assert.assertEquals(0, SuspendTimerServiceBean.getTimerServiceCount());
        } finally {
            op = new ModelNode();
            op.get(ModelDescriptionConstants.OP).set("resume");
            managementClient.getControllerClient().execute(op);
        }
        //if they were backed up we give them some time to run
        Thread.sleep(300);
        int timerServiceCount = SuspendTimerServiceBean.getTimerServiceCount();
        Assert.assertTrue("Interval " + (System.currentTimeMillis() - start) + " count " + timerServiceCount, timerServiceCount < 40);
    } finally {
        if (timer != null) {
            timer.cancel();
            Thread.sleep(100);
        }
    }
}
Also used : Timer(javax.ejb.Timer) TimerConfig(javax.ejb.TimerConfig) ModelNode(org.jboss.dmr.ModelNode) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 2 with Timer

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

the class TimerServiceSuspendTestCase method testSuspendWithIntervalTimer.

@Test
public void testSuspendWithIntervalTimer() throws NamingException, IOException, InterruptedException {
    SuspendTimerServiceBean.resetTimerServiceCalled();
    InitialContext ctx = new InitialContext();
    SuspendTimerServiceBean bean = (SuspendTimerServiceBean) ctx.lookup("java:module/" + SuspendTimerServiceBean.class.getSimpleName());
    ModelNode op = new ModelNode();
    Timer timer = null;
    try {
        try {
            timer = bean.getTimerService().createIntervalTimer(100, 100, new TimerConfig("", false));
            Assert.assertTrue(SuspendTimerServiceBean.awaitTimerServiceCount() > 0);
            op.get(ModelDescriptionConstants.OP).set("suspend");
            managementClient.getControllerClient().execute(op);
            SuspendTimerServiceBean.resetTimerServiceCalled();
            Thread.sleep(200);
            Assert.assertEquals(0, SuspendTimerServiceBean.getTimerServiceCount());
        } finally {
            op = new ModelNode();
            op.get(ModelDescriptionConstants.OP).set("resume");
            managementClient.getControllerClient().execute(op);
        }
        Assert.assertTrue(SuspendTimerServiceBean.awaitTimerServiceCount() > 0);
    } finally {
        if (timer != null) {
            timer.cancel();
            Thread.sleep(100);
        }
    }
}
Also used : Timer(javax.ejb.Timer) TimerConfig(javax.ejb.TimerConfig) ModelNode(org.jboss.dmr.ModelNode) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 3 with Timer

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

the class ActiveTimerServiceCountTestCase method removeSheduleOneTimerOfOtherTimerBean.

private boolean removeSheduleOneTimerOfOtherTimerBean(final Collection<Timer> timers) {
    for (final Timer activeTimer : timers) {
        // see if it's @Schedule one in OtherTimerBeanInSameModule
        final Serializable info = activeTimer.getInfo();
        if (OtherTimerBeanInSameModule.SCHEDULE_ONE_INFO.equals(info)) {
            Assert.assertTrue("Unexpected timer type of timer on " + OtherTimerBeanInSameModule.class.getSimpleName(), activeTimer.isCalendarTimer());
            Assert.assertFalse("Unexpected persistence type of timer on " + OtherTimerBeanInSameModule.class.getSimpleName(), activeTimer.isPersistent());
            // remove this matched timer from the collection
            return timers.remove(activeTimer);
        }
    }
    return false;
}
Also used : Serializable(java.io.Serializable) Timer(javax.ejb.Timer)

Example 4 with Timer

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

the class ActiveTimerServiceCountTestCase method removeSheduleTwoTimerOfSimpleTimerBean.

private boolean removeSheduleTwoTimerOfSimpleTimerBean(final Collection<Timer> timers) {
    for (final Timer activeTimer : timers) {
        // see if it's @Schedule two in SimpleTimerBean
        final Serializable info = activeTimer.getInfo();
        if (SimpleTimerBean.SCHEDULE_TWO_INFO.equals(info)) {
            Assert.assertTrue("Unexpected timer type of timer on " + SimpleTimerBean.class.getSimpleName(), activeTimer.isCalendarTimer());
            Assert.assertTrue("Unexpected persistence type of timer on " + SimpleTimerBean.class.getSimpleName(), activeTimer.isPersistent());
            // remove this matched timer from the collection
            return timers.remove(activeTimer);
        }
    }
    return false;
}
Also used : Serializable(java.io.Serializable) Timer(javax.ejb.Timer)

Example 5 with Timer

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

the class TimerAttributeDefinition method addTimers.

public static void addTimers(final EJBComponent ejb, final ModelNode response) {
    response.setEmptyList();
    final String name = ejb.getComponentName();
    TimerService ts = ejb.getTimerService();
    if (ts != null) {
        for (Timer timer : ts.getTimers()) {
            ModelNode timerNode = response.add();
            addTimeRemaining(timer, timerNode, name);
            addNextTimeout(timer, timerNode, name);
            addCalendarTimer(timer, timerNode, name);
            addPersistent(timer, timerNode, name);
            addSchedule(timer, timerNode, name);
        }
    }
}
Also used : Timer(javax.ejb.Timer) ModelNode(org.jboss.dmr.ModelNode) TimerService(javax.ejb.TimerService)

Aggregations

Timer (javax.ejb.Timer)18 BeanContext (org.apache.openejb.BeanContext)5 Test (org.junit.Test)5 Serializable (java.io.Serializable)4 TimerConfig (javax.ejb.TimerConfig)4 InitialContext (javax.naming.InitialContext)4 ModelNode (org.jboss.dmr.ModelNode)4 EjbTimerService (org.apache.openejb.core.timer.EjbTimerService)3 IOException (java.io.IOException)2 EJBLocalObject (javax.ejb.EJBLocalObject)2 EJBObject (javax.ejb.EJBObject)2 TimerService (javax.ejb.TimerService)2 OpenEJBException (org.apache.openejb.OpenEJBException)2 EjbTimerServiceImpl (org.apache.openejb.core.timer.EjbTimerServiceImpl)2 Method (java.lang.reflect.Method)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 EJBException (javax.ejb.EJBException)1 NoSuchObjectLocalException (javax.ejb.NoSuchObjectLocalException)1 NamingException (javax.naming.NamingException)1