You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2014/07/27 15:37:11 UTC

svn commit: r1613789 - in /tomee/tomee/trunk/container/openejb-core/src: main/java/org/apache/openejb/core/timer/ test/java/org/apache/openejb/core/timer/

Author: rmannibucau
Date: Sun Jul 27 13:37:11 2014
New Revision: 1613789

URL: http://svn.apache.org/r1613789
Log:
getAllTimers

Added:
    tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/core/timer/
    tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/core/timer/GetAllTimersTest.java
Modified:
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/timer/TimerServiceImpl.java
    tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/timer/TimerServiceWrapper.java

Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/timer/TimerServiceImpl.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/timer/TimerServiceImpl.java?rev=1613789&r1=1613788&r2=1613789&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/timer/TimerServiceImpl.java (original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/timer/TimerServiceImpl.java Sun Jul 27 13:37:11 2014
@@ -98,4 +98,9 @@ public class TimerServiceImpl implements
 
         return scheduleExpressionCopy;
     }
+
+    @Override
+    public Collection<Timer> getAllTimers() throws IllegalStateException, EJBException {
+        throw new UnsupportedOperationException("not expecting to call this method from this class, see TimerServiceWrapper");
+    }
 }

Modified: tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/timer/TimerServiceWrapper.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/timer/TimerServiceWrapper.java?rev=1613789&r1=1613788&r2=1613789&view=diff
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/timer/TimerServiceWrapper.java (original)
+++ tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/timer/TimerServiceWrapper.java Sun Jul 27 13:37:11 2014
@@ -18,8 +18,12 @@
 package org.apache.openejb.core.timer;
 
 import org.apache.openejb.BeanContext;
+import org.apache.openejb.BeanType;
 import org.apache.openejb.MethodContext;
+import org.apache.openejb.ModuleContext;
 import org.apache.openejb.core.ThreadContext;
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.openejb.spi.ContainerSystem;
 import org.apache.openejb.util.LogCategory;
 import org.apache.openejb.util.Logger;
 
@@ -32,6 +36,7 @@ import java.io.Serializable;
 import java.lang.reflect.Method;
 import java.util.Collection;
 import java.util.Date;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Map;
 
@@ -59,6 +64,36 @@ public class TimerServiceWrapper impleme
         return getTimerService().getTimers();
     }
 
+    @Override
+    public Collection<Timer> getAllTimers() throws IllegalStateException, EJBException {
+        final ThreadContext threadContext = ThreadContext.getThreadContext();
+        final BeanContext beanContext = threadContext.getBeanContext();
+        final ModuleContext module = beanContext.getModuleContext();
+
+        final Collection<Timer> timers = new HashSet<>();
+        for (final BeanContext c : module.getAppContext().getBeanContexts()) {
+            if (c.getModuleContext() == module) { // filter by module
+                if (c.getComponentType() != BeanType.STATEFUL) {
+                    final TimerService timerService = getTimerService(null, c, true);
+                    if (timerService == null) {
+                        continue;
+                    }
+                    final Collection<Timer> beanTimers = timerService.getTimers();
+                    timers.addAll(beanTimers);
+                } else {
+                    // for all instances
+                    final TimerService timerService = getTimerService(null, c, true);
+                    if (timerService == null) {
+                        continue;
+                    }
+                    final Collection<Timer> beanTimers = timerService.getTimers();
+                    timers.addAll(beanTimers);
+                }
+            }
+        }
+        return timers;
+    }
+
     public Timer createSingleActionTimer(final long l, final TimerConfig timerConfig) throws IllegalArgumentException, IllegalStateException, EJBException {
         return getTimerService().createSingleActionTimer(l, timerConfig);
     }
@@ -86,27 +121,53 @@ public class TimerServiceWrapper impleme
     private TimerService getTimerService() throws IllegalStateException {
         final ThreadContext threadContext = ThreadContext.getThreadContext();
         final BeanContext beanContext = threadContext.getBeanContext();
+        return getTimerService(threadContext.getPrimaryKey(), beanContext, false);
+    }
+
+    private TimerService getTimerService(final Object pk, final BeanContext beanContext, final boolean nullIfNotRelevant) throws IllegalStateException {
         final EjbTimerService timerService = beanContext.getEjbTimerService();
         if (timerService == null) {
             throw new IllegalStateException("This ejb does not support timers " + beanContext.getDeploymentID());
         } else if (beanContext.getEjbTimeout() == null) {
 
+            HasSchedule hasSchedule = beanContext.get(HasSchedule.class);
+
             boolean hasSchedules = false;
 
-            for (final Iterator<Map.Entry<Method, MethodContext>> it = beanContext.iteratorMethodContext(); it.hasNext(); ) {
-                final Map.Entry<Method, MethodContext> entry = it.next();
-                final MethodContext methodContext = entry.getValue();
-                if (methodContext.getSchedules().size() > 0) {
-                    hasSchedules = true;
+            if (hasSchedule != null) {
+                hasSchedules = hasSchedule.value;
+            } else {
+                for (final Iterator<Map.Entry<Method, MethodContext>> it = beanContext.iteratorMethodContext(); it.hasNext(); ) {
+                    final Map.Entry<Method, MethodContext> entry = it.next();
+                    final MethodContext methodContext = entry.getValue();
+                    if (methodContext.getSchedules().size() > 0) {
+                        hasSchedules = true;
+                    }
+                }
+                synchronized (beanContext) { // surely not the best lock instance but works in this context
+                    if (beanContext.get(HasSchedule.class) == null) {
+                        beanContext.set(HasSchedule.class, new HasSchedule(hasSchedules));
+                    }
                 }
             }
 
             if (!hasSchedules) {
+                if (nullIfNotRelevant) {
+                    return null;
+                }
                 log.error("This ejb does not support timers " + beanContext.getDeploymentID() + " due to no timeout method nor schedules in methodContext is configured");
             }
 
         }
 
-        return new TimerServiceImpl(timerService, threadContext.getPrimaryKey(), beanContext.getEjbTimeout());
+        return new TimerServiceImpl(timerService, pk, beanContext.getEjbTimeout());
+    }
+
+    private static class HasSchedule {
+        private final boolean value;
+
+        private HasSchedule(final boolean value) {
+            this.value = value;
+        }
     }
 }

Added: tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/core/timer/GetAllTimersTest.java
URL: http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/core/timer/GetAllTimersTest.java?rev=1613789&view=auto
==============================================================================
--- tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/core/timer/GetAllTimersTest.java (added)
+++ tomee/tomee/trunk/container/openejb-core/src/test/java/org/apache/openejb/core/timer/GetAllTimersTest.java Sun Jul 27 13:37:11 2014
@@ -0,0 +1,132 @@
+/*
+ * 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.core.timer;
+
+import org.apache.openejb.jee.EjbJar;
+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.Resource;
+import javax.ejb.EJB;
+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 javax.ejb.TransactionAttribute;
+import javax.ejb.TransactionAttributeType;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+@RunWith(ApplicationComposer.class)
+public class GetAllTimersTest {
+    @Module
+    public EjbJar jar() {
+        return new EjbJar()
+            .enterpriseBean(new SingletonBean(Query.class).localBean())
+            .enterpriseBean(new SingletonBean(Bean1.class).localBean())
+            .enterpriseBean(new SingletonBean(Bean2.class).localBean());
+    }
+
+    @EJB
+    private Query query;
+
+    @Test
+    public void noTimerForABeanWhichDidntCreatedAny() {
+        assertNull(query.empty());
+    }
+
+    @Test
+    public void findAllTimersInATx() {
+        checkList(query.listInTx());
+    }
+
+    @Test
+    public void findAllTimersWithoutTx() {
+        checkList(query.list());
+    }
+
+    private void checkList(final Collection<Timer> in) {
+        final List<Timer> list = new ArrayList<>(in); Collections.sort(list, new Comparator<Timer>() {
+            @Override
+            public int compare(final Timer o1, final Timer o2) {
+                return o1.getInfo().toString().compareTo(o2.getInfo().toString());
+            }
+        });
+        assertEquals(2, list.size());
+        final Iterator<Timer> it = list.iterator();
+        assertEquals(Bean1.class.getSimpleName(), it.next().getInfo());
+        assertEquals(Bean2.class.getSimpleName(), it.next().getInfo());
+    }
+
+    @Startup
+    public static class TimerBean {
+        @Resource
+        private TimerService ts;
+
+        @PostConstruct
+        public void run() {
+            ts.createSingleActionTimer(new Date(System.currentTimeMillis() + 100000L), new TimerConfig(getClass().getSimpleName(), false));
+        }
+
+        @Timeout
+        public void timeout(final Timer t) {
+            System.out.println(t.getInfo());
+        }
+    }
+
+    @Singleton
+    public static class Bean1 extends TimerBean {
+    }
+
+    @Singleton
+    public static class Bean2 extends TimerBean {
+    }
+
+    @Singleton
+    public static class Query {
+        @Resource
+        private TimerService ts;
+
+        @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
+        public Collection<Timer> listInTx() {
+            return ts.getAllTimers();
+        }
+
+        @TransactionAttribute(TransactionAttributeType.NEVER)
+        public Collection<Timer> list() {
+            return ts.getAllTimers();
+        }
+
+        public Collection<Timer> empty() {
+            return ts.getTimers();
+        }
+    }
+}