Search in sources :

Example 26 with TimerConfig

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

the class OtherTimerBeanInSameModule 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 27 with TimerConfig

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

the class DatabaseTimerServiceTestCase method testTimedObjectTimeoutMethod.

@Test
public void testTimedObjectTimeoutMethod() throws NamingException {
    InitialContext ctx = new InitialContext();
    TimedObjectTimerServiceBean bean = (TimedObjectTimerServiceBean) ctx.lookup("java:module/" + TimedObjectTimerServiceBean.class.getSimpleName());
    bean.resetTimerServiceCalled();
    bean.getTimerService().createTimer(TIMER_TIMEOUT_TIME_MS, INFO_MSG_FOR_CHECK);
    Assert.assertTrue(TimedObjectTimerServiceBean.awaitTimerCall());
    bean.resetTimerServiceCalled();
    long ts = (new Date()).getTime() + TIMER_INIT_TIME_MS;
    TimerConfig timerConfig = new TimerConfig();
    timerConfig.setInfo(INFO_MSG_FOR_CHECK);
    bean.getTimerService().createSingleActionTimer(new Date(ts), timerConfig);
    Assert.assertTrue(TimedObjectTimerServiceBean.awaitTimerCall());
    Assert.assertEquals(INFO_MSG_FOR_CHECK, bean.getTimerInfo());
    Assert.assertFalse(bean.isCalendar());
    Assert.assertTrue(bean.isPersistent());
}
Also used : TimerConfig(javax.ejb.TimerConfig) InitialContext(javax.naming.InitialContext) Date(java.util.Date) Test(org.junit.Test)

Example 28 with TimerConfig

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

the class TimerServiceSuspendTestCase method testSingleActionTimerWhenSuspended.

/**
 * Tests that a single action timer that executes when the container is suspended will run as normal once it is resumed
 */
@Test
public void testSingleActionTimerWhenSuspended() 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().createSingleActionTimer(1, new TimerConfig("", false));
            Thread.sleep(1000);
            Assert.assertEquals(0, SuspendTimerServiceBean.getTimerServiceCount());
        } finally {
            op = new ModelNode();
            op.get(ModelDescriptionConstants.OP).set("resume");
            managementClient.getControllerClient().execute(op);
        }
        Assert.assertEquals(1, SuspendTimerServiceBean.awaitTimerServiceCount());
    } finally {
        if (timer != null) {
            try {
                timer.cancel();
                Thread.sleep(100);
            } catch (Exception e) {
            // as the timer has already expired this may throw an exception
            }
        }
    }
}
Also used : Timer(javax.ejb.Timer) TimerConfig(javax.ejb.TimerConfig) ModelNode(org.jboss.dmr.ModelNode) InitialContext(javax.naming.InitialContext) IOException(java.io.IOException) NamingException(javax.naming.NamingException) Test(org.junit.Test)

Example 29 with TimerConfig

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

the class CancelledTimerServiceBean method createTimer.

public TimerHandle createTimer() {
    ScheduleExpression expression = new ScheduleExpression();
    expression.second("*");
    expression.minute("*");
    expression.hour("*");
    expression.dayOfMonth("*");
    expression.year("*");
    TimerConfig timerConfig = new TimerConfig();
    timerConfig.setInfo(new String("info"));
    return timerService.createCalendarTimer(expression, timerConfig).getHandle();
}
Also used : ScheduleExpression(javax.ejb.ScheduleExpression) TimerConfig(javax.ejb.TimerConfig)

Example 30 with TimerConfig

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

the class SimpleScheduleBean method verifyTimezone.

/**
 * Verifies that changing timezone in a schedule expression after the timer creation
 * should not affect the previously created timer.
 */
public void verifyTimezone() {
    final String[] zoneIds = { "Europe/Andorra", "Asia/Dubai", "Asia/Kabul", "America/Antigua", "America/Anguilla", "Africa/Johannesburg", "Africa/Lusaka", "Africa/Harare" };
    final ScheduleExpression exp = new ScheduleExpression().year(9999);
    final ArrayList<Timer> timers = new ArrayList<>();
    for (String z : zoneIds) {
        exp.timezone(z);
        timers.add(timerService.createCalendarTimer(exp, new TimerConfig(z, false)));
    }
    RuntimeException e = null;
    for (Timer t : timers) {
        final Serializable info = t.getInfo();
        final String timezone = t.getSchedule().getTimezone();
        if (!info.equals(timezone)) {
            e = new RuntimeException(String.format("Expecting schedule expression timezone: %s, but got: %s", info, timezone));
            break;
        }
    }
    for (Timer t : timers) {
        try {
            t.cancel();
        } catch (Exception ignore) {
        }
    }
    if (e != null) {
        throw e;
    }
}
Also used : ScheduleExpression(javax.ejb.ScheduleExpression) Serializable(java.io.Serializable) Timer(javax.ejb.Timer) ArrayList(java.util.ArrayList) TimerConfig(javax.ejb.TimerConfig)

Aggregations

TimerConfig (javax.ejb.TimerConfig)37 ScheduleExpression (javax.ejb.ScheduleExpression)13 Date (java.util.Date)8 PostConstruct (javax.annotation.PostConstruct)7 Test (org.junit.Test)7 Timer (javax.ejb.Timer)6 InitialContext (javax.naming.InitialContext)6 ModelNode (org.jboss.dmr.ModelNode)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 InSequence (org.jboss.arquillian.junit.InSequence)2 MethodDescriptor (com.sun.enterprise.deployment.MethodDescriptor)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Serializable (java.io.Serializable)1 Method (java.lang.reflect.Method)1 Calendar (java.util.Calendar)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1