Created
March 11, 2014 12:58
-
-
Save jeanouii/9485093 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Licensed to the Apache Software Foundation (ASF) under one or more | |
| * contributor license agreements. See the NOTICE file distributed with | |
| * this work for additional information regarding copyright ownership. | |
| * The ASF licenses this file to You under the Apache License, Version 2.0 | |
| * (the "License"); you may not use this file except in compliance with | |
| * the License. You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| package org.apache.openejb.timer; | |
| import org.apache.openejb.jee.EnterpriseBean; | |
| import org.apache.openejb.jee.SingletonBean; | |
| import org.apache.openejb.junit.ApplicationComposer; | |
| import org.apache.openejb.testing.Module; | |
| import org.junit.Test; | |
| import org.junit.runner.RunWith; | |
| import javax.annotation.PostConstruct; | |
| import javax.annotation.PreDestroy; | |
| import javax.annotation.Resource; | |
| import javax.ejb.EJB; | |
| import javax.ejb.EJBException; | |
| import javax.ejb.Lock; | |
| import javax.ejb.LockType; | |
| import javax.ejb.ScheduleExpression; | |
| import javax.ejb.Singleton; | |
| import javax.ejb.Startup; | |
| import javax.ejb.Timeout; | |
| import javax.ejb.Timer; | |
| import javax.ejb.TimerConfig; | |
| import javax.ejb.TimerService; | |
| import java.io.Serializable; | |
| import static org.junit.Assert.assertEquals; | |
| @RunWith(ApplicationComposer.class) | |
| public class InitialIntervalTimerTest { | |
| @Module | |
| public EnterpriseBean bean() { | |
| return new SingletonBean(ScheduledBean.class).localBean(); | |
| } | |
| @EJB | |
| private TimerWithDelay bean; | |
| @EJB | |
| private ScheduledBean bean2; | |
| @Test | |
| public void test() throws InterruptedException { | |
| bean2.schedule(); | |
| Thread.sleep(100000); | |
| assertEquals(3, bean.getOk()); | |
| } | |
| @Singleton | |
| @Startup | |
| @Lock(LockType.READ) | |
| public static class TimerWithDelay { | |
| @Resource | |
| private TimerService ts; | |
| private Timer timer; | |
| private int ok = 0; | |
| @PostConstruct | |
| public void start() { | |
| timer = ts.createIntervalTimer(3000, 1000, new TimerConfig(System.currentTimeMillis(), false)); | |
| } | |
| @Timeout | |
| public void timeout(final Timer timer) { | |
| final long actual = System.currentTimeMillis() - ((Long) timer.getInfo() + 1000 * ok + 3000); | |
| assertEquals(0, actual, 500); | |
| ok++; | |
| System.out.println("==> Called " + ok); | |
| } | |
| public int getOk() { | |
| return ok; | |
| } | |
| @PreDestroy | |
| public void stop() { | |
| timer.cancel(); | |
| } | |
| } | |
| @Singleton | |
| public static class ScheduledBean implements Serializable { | |
| private static final long serialVersionUID = 1L; | |
| @Resource | |
| private transient TimerService timerService; | |
| private Timer oldTimer = null; | |
| private int times = 0; | |
| public void schedule() { | |
| if (oldTimer != null) { | |
| oldTimer.cancel(); | |
| } | |
| try { | |
| oldTimer = timerService.createCalendarTimer(new ScheduleExpression().second("*").minute("*").hour("*"), new TimerConfig("TIMER", true)); | |
| System.out.println("==> Bean schedule done."); | |
| } catch (IllegalArgumentException e) { | |
| throw new EJBException(e); | |
| } | |
| } | |
| @Timeout | |
| @Lock(LockType.READ) | |
| private void timeout(Timer timer) { | |
| System.out.println("==> Called " + ++times); | |
| } | |
| @PreDestroy | |
| public void stop() { | |
| if (oldTimer != null) { | |
| oldTimer.cancel(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment