Permalink
Browse files

AS7-6561 Implemented support for EJB 3.2's TimerService#getAllTimers(…

…) API
  • Loading branch information...
1 parent 5530f06 commit aa000f55e8bb95f6c73750de334fa5d9b1e286c4 @jaikiran jaikiran committed with dmlloyd Mar 1, 2013
@@ -70,7 +70,7 @@
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
- <artifactId>jboss-ejb-api_3.1_spec</artifactId>
+ <artifactId>jboss-ejb-api_3.2_spec</artifactId>
<scope>test</scope>
</dependency>
<dependency>
@@ -82,7 +82,7 @@
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
- <artifactId>jboss-ejb-api_3.1_spec</artifactId>
+ <artifactId>jboss-ejb-api_3.2_spec</artifactId>
<scope>test</scope>
</dependency>
<dependency>
View
@@ -428,7 +428,7 @@
</module-def>
<module-def name="javax.ejb.api">
- <maven-resource group="org.jboss.spec.javax.ejb" artifact="jboss-ejb-api_3.1_spec"/>
+ <maven-resource group="org.jboss.spec.javax.ejb" artifact="jboss-ejb-api_3.2_spec"/>
</module-def>
<module-def name="javax.el.api">
View
@@ -1497,7 +1497,7 @@
<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
- <artifactId>jboss-ejb-api_3.1_spec</artifactId>
+ <artifactId>jboss-ejb-api_3.2_spec</artifactId>
</dependency>
<dependency>
@@ -1961,7 +1961,7 @@
<include>org.jboss.resteasy:jaxrs-api</include>
<include>org.jboss.spec.javax.annotation:jboss-annotations-api_1.2_spec</include>
- <include>org.jboss.spec.javax.ejb:jboss-ejb-api_3.1_spec</include>
+ <include>org.jboss.spec.javax.ejb:jboss-ejb-api_3.2_spec</include>
<include>org.jboss.spec.javax.el:jboss-el-api_2.2_spec</include>
<include>org.jboss.spec.javax.faces:jboss-jsf-api_2.1_spec</include>
<include>org.jboss.spec.javax.interceptor:jboss-interceptors-api_1.1_spec</include>
@@ -2875,7 +2875,7 @@
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
- <artifactId>jboss-ejb-api_3.1_spec</artifactId>
+ <artifactId>jboss-ejb-api_3.2_spec</artifactId>
<licenses>
<license>
<name>GNU Lesser General Public License, Version 2.1</name>
@@ -5,7 +5,7 @@ This jar contains the classes required for remote JMS and EJB usage, and consist
org.jboss.spec.javax.jms:jboss-jms-api_1.1_spec
org.jboss.spec.javax.transaction:jboss-transaction-api_1.1_spec
-org.jboss.spec.javax.ejb:jboss-ejb-api_3.1_spec
+org.jboss.spec.javax.ejb:jboss-ejb-api_3.2_spec
org.jboss:jboss-ejb-client
org.jboss:jboss-remote-naming
View
@@ -89,7 +89,7 @@
<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
- <artifactId>jboss-ejb-api_3.1_spec</artifactId>
+ <artifactId>jboss-ejb-api_3.2_spec</artifactId>
</dependency>
<dependency>
View
@@ -156,7 +156,7 @@ projects that depend on this project.-->
<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
- <artifactId>jboss-ejb-api_3.1_spec</artifactId>
+ <artifactId>jboss-ejb-api_3.2_spec</artifactId>
</dependency>
<dependency>
@@ -0,0 +1,71 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2013, Red Hat, Inc., and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.as.ejb3.component;
+
+import javax.ejb.Timer;
+import javax.ejb.TimerService;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * A registry to which individual {@link javax.ejb.TimerService timer services} can register to (and un-register from). The main purpose
+ * of this registry is to provide an implementation of {@link #getAllActiveTimers()} which returns all
+ * {@link javax.ejb.TimerService#getTimers() active timers} after querying each of the {@link javax.ejb.TimerService timer services} registered
+ * with this {@link TimerServiceRegistry registry}.
+ * <p/>
+ * Typical use of this registry is to maintain one instance of this registry, per deployment unit (also known as EJB module) and register the timer
+ * services of all EJB components that belong to that deployment unit. Effectively, such an instance can then be used to fetch all active timers
+ * that are applicable to that deployment unit (a.k.a EJB module).
+ *
+ * @author Jaikiran Pai
+ */
+public class TimerServiceRegistry {
+
+ private final Set<TimerService> timerServices = Collections.synchronizedSet(new HashSet<TimerService>());
+
+ public void registerTimerService(final TimerService timerService) {
+ if (timerService == null) {
+ return;
+ }
+ timerServices.add(timerService);
+ }
+
+ public boolean unRegisterTimerService(final TimerService timerService) {
+ if (timerService == null) {
+ return false;
+ }
+ return timerServices.remove(timerService);
+ }
+
+ public Collection<Timer> getAllActiveTimers() {
+ final Collection<Timer> activeTimers = new HashSet<>();
+ synchronized (timerServices) {
+ for (final TimerService timerService : timerServices) {
+ activeTimers.addAll(timerService.getTimers());
+ }
+ }
+ return activeTimers;
+ }
+}
@@ -36,6 +36,7 @@
import org.jboss.as.ee.component.EEModuleDescription;
import org.jboss.as.ejb3.component.EJBComponent;
import org.jboss.as.ejb3.component.EJBComponentDescription;
+import org.jboss.as.ejb3.component.TimerServiceRegistry;
import org.jboss.as.ejb3.deployment.EjbDeploymentAttachmentKeys;
import org.jboss.as.ejb3.timerservice.NonFunctionalTimerService;
import org.jboss.as.ejb3.timerservice.TimedObjectInvokerImpl;
@@ -47,6 +48,7 @@
import org.jboss.as.server.deployment.DeploymentUnit;
import org.jboss.as.server.deployment.DeploymentUnitProcessingException;
import org.jboss.as.server.deployment.DeploymentUnitProcessor;
+import org.jboss.as.server.deployment.EjbDeploymentMarker;
import org.jboss.metadata.ejb.spec.EjbJarMetaData;
import org.jboss.modules.Module;
import org.jboss.msc.service.ServiceBuilder;
@@ -82,6 +84,10 @@ public void deploy(final DeploymentPhaseContext phaseContext) throws DeploymentU
ServiceName defaultTimerPersistenceService = TimerPersistence.SERVICE_NAME.append(defaultTimerDataStore);
final Map<String, ServiceName> timerPersistenceServices = new HashMap<String, ServiceName>();
+ // if this is a EJB deployment then create a EJB module level TimerServiceRegistry which can be used by the timer services
+ // of all EJB components that belong to this EJB module.
+ final TimerServiceRegistry timerServiceRegistry = EjbDeploymentMarker.isEjbDeployment(deploymentUnit) ? new TimerServiceRegistry() : null;
+
if (ejbJarMetaData != null && ejbJarMetaData.getAssemblyDescriptor() != null) {
List<TimerServiceMetaData> timerService = ejbJarMetaData.getAssemblyDescriptor().getAny(TimerServiceMetaData.class);
if (timerService != null) {
@@ -124,7 +130,7 @@ public void configure(final DeploymentPhaseContext context, final ComponentDescr
//install the timer create service
final ServiceName serviceName = component.getServiceName().append(TimerServiceImpl.SERVICE_NAME);
- final TimerServiceImpl service = new TimerServiceImpl(ejbComponentDescription.getScheduleMethods(), serviceName);
+ final TimerServiceImpl service = new TimerServiceImpl(ejbComponentDescription.getScheduleMethods(), serviceName, timerServiceRegistry);
final ServiceBuilder<javax.ejb.TimerService> createBuilder = context.getServiceTarget().addService(serviceName, service);
createBuilder.addDependency(TIMER_SERVICE_NAME, Timer.class, service.getTimerInjectedValue());
createBuilder.addDependency(component.getCreateServiceName(), EJBComponent.class, service.getEjbComponentInjectedValue());
@@ -21,37 +21,8 @@
*/
package org.jboss.as.ejb3.timerservice;
-import java.io.Serializable;
-import java.lang.reflect.Method;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.ListIterator;
-import java.util.Map;
-import java.util.Set;
-import java.util.UUID;
-import java.util.concurrent.ExecutorService;
-
-import javax.ejb.EJBException;
-import javax.ejb.ScheduleExpression;
-import javax.ejb.Timer;
-import javax.ejb.TimerConfig;
-import javax.ejb.TimerHandle;
-import javax.ejb.TimerService;
-import javax.transaction.RollbackException;
-import javax.transaction.Status;
-import javax.transaction.Synchronization;
-import javax.transaction.SystemException;
-import javax.transaction.Transaction;
-import javax.transaction.TransactionManager;
-import javax.transaction.TransactionSynchronizationRegistry;
-
import org.jboss.as.ejb3.component.EJBComponent;
+import org.jboss.as.ejb3.component.TimerServiceRegistry;
import org.jboss.as.ejb3.component.allowedmethods.AllowedMethodsInformation;
import org.jboss.as.ejb3.component.allowedmethods.MethodType;
import org.jboss.as.ejb3.component.entity.EntityBeanComponent;
@@ -71,6 +42,35 @@
import org.jboss.msc.service.StopContext;
import org.jboss.msc.value.InjectedValue;
+import javax.ejb.EJBException;
+import javax.ejb.ScheduleExpression;
+import javax.ejb.Timer;
+import javax.ejb.TimerConfig;
+import javax.ejb.TimerHandle;
+import javax.ejb.TimerService;
+import javax.transaction.RollbackException;
+import javax.transaction.Status;
+import javax.transaction.Synchronization;
+import javax.transaction.SystemException;
+import javax.transaction.Transaction;
+import javax.transaction.TransactionManager;
+import javax.transaction.TransactionSynchronizationRegistry;
+import java.io.Serializable;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.ExecutorService;
+
import static org.jboss.as.ejb3.EjbLogger.ROOT_LOGGER;
import static org.jboss.as.ejb3.EjbMessages.MESSAGES;
@@ -134,6 +134,7 @@
private TransactionManager transactionManager;
private TransactionSynchronizationRegistry tsr;
+ private final TimerServiceRegistry timerServiceRegistry;
private volatile boolean started = false;
@@ -151,10 +152,25 @@
* @param autoTimers
* @param serviceName
* @throws IllegalArgumentException If either of the passed param is null
+ * @deprecated Use {@link #TimerServiceImpl(java.util.Map, org.jboss.msc.service.ServiceName, org.jboss.as.ejb3.component.TimerServiceRegistry)} instead
*/
+ @Deprecated
public TimerServiceImpl(final Map<Method, List<AutoTimer>> autoTimers, final ServiceName serviceName) {
+ this(autoTimers, serviceName, null);
+ }
+
+ /**
+ * Creates a {@link TimerServiceImpl}
+ *
+ * @param autoTimers The auto timers associated with this timer service
+ * @param serviceName The service name of this timer service
+ * @param registry The {@link TimerServiceRegistry} which has the knowledge of other timer services belonging to the EJB module to which this
+ * timer service belongs.
+ */
+ public TimerServiceImpl(final Map<Method, List<AutoTimer>> autoTimers, final ServiceName serviceName, final TimerServiceRegistry registry) {
this.autoTimers = autoTimers;
this.serviceName = serviceName;
+ this.timerServiceRegistry = registry;
}
@Override
@@ -178,10 +194,18 @@ public synchronized void start(final StartContext context) throws StartException
// restore the timers
started = true;
restoreTimers(timers);
+ // register ourselves to the TimerServiceRegistry (if any)
+ if (timerServiceRegistry != null) {
+ timerServiceRegistry.registerTimerService(this);
+ }
}
@Override
public synchronized void stop(final StopContext context) {
+ // un-register ourselves to the TimerServiceRegistry (if any)
+ if (timerServiceRegistry != null) {
+ timerServiceRegistry.unRegisterTimerService(this);
+ }
suspendTimers();
timerPersistence.getValue().timerUndeployed(timedObjectInvoker.getValue().getTimedObjectId());
started = false;
@@ -378,6 +402,20 @@ public TimerImpl loadAutoTimer(ScheduleExpression schedule,
}
/**
+ * {@inheritDoc}
+ */
+ @Override
+ public Collection<Timer> getAllTimers() throws IllegalStateException, EJBException {
+ // query the registry
+ if (this.timerServiceRegistry != null) {
+ return this.timerServiceRegistry.getAllActiveTimers();
+ }
+ // if we don't have the registry (shouldn't really happen) which stores the timer services applicable for the EJB module to which
+ // this timer service belongs, then let's at least return the active timers that are applicable only for this timer service
+ return this.getTimers();
+ }
+
+ /**
* Create a {@link javax.ejb.Timer}
*
* @param initialExpiration The {@link java.util.Date} at which the first timeout should occur.
View
@@ -82,7 +82,7 @@
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
- <artifactId>jboss-ejb-api_3.1_spec</artifactId>
+ <artifactId>jboss-ejb-api_3.2_spec</artifactId>
<!-- TODO: move EJB Embeddable to a separate artifact -->
<optional>true</optional>
</dependency>
View
@@ -86,7 +86,7 @@
<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
- <artifactId>jboss-ejb-api_3.1_spec</artifactId>
+ <artifactId>jboss-ejb-api_3.2_spec</artifactId>
</dependency>
<dependency>
Oops, something went wrong.

0 comments on commit aa000f5

Please sign in to comment.