dain 2006/02/01 06:50:10
Modified: modules/core/src/java/org/openejb/timer
BasicTimerService.java BasicTimerServiceImpl.java
EJBTimeoutInvocationFactory.java TimerImpl.java
UnavailableTimerService.java
Log:
Major refactor
Split container into an object to represent a deployed ejb and a set of
shared containers which process invocations
Introduced interface between CMP container and CMP engine
Revision Changes Path
1.5 +2 -2
openejb/modules/core/src/java/org/openejb/timer/BasicTimerService.java
Index: BasicTimerService.java
===================================================================
RCS file:
/home/projects/openejb/scm/openejb/modules/core/src/java/org/openejb/timer/BasicTimerService.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- BasicTimerService.java 14 Feb 2005 18:32:32 -0000 1.4
+++ BasicTimerService.java 1 Feb 2006 11:50:10 -0000 1.5
@@ -68,5 +68,5 @@
Collection getTimers(Object id) throws IllegalStateException,
EJBException;
- TimerImpl getTimerById(Long id);
+ Timer getTimerById(Long id);
}
1.6 +59 -96
openejb/modules/core/src/java/org/openejb/timer/BasicTimerServiceImpl.java
Index: BasicTimerServiceImpl.java
===================================================================
RCS file:
/home/projects/openejb/scm/openejb/modules/core/src/java/org/openejb/timer/BasicTimerServiceImpl.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- BasicTimerServiceImpl.java 4 Dec 2005 02:37:53 -0000 1.5
+++ BasicTimerServiceImpl.java 1 Feb 2006 11:50:10 -0000 1.6
@@ -55,59 +55,64 @@
import javax.ejb.EJBException;
import javax.ejb.NoSuchObjectLocalException;
import javax.ejb.Timer;
+import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.transaction.RollbackException;
import javax.transaction.Status;
import javax.transaction.Synchronization;
import javax.transaction.SystemException;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.geronimo.core.service.Interceptor;
+import org.apache.geronimo.kernel.proxy.DeadProxyException;
import org.apache.geronimo.timer.PersistenceException;
import org.apache.geronimo.timer.PersistentTimer;
-import org.apache.geronimo.timer.ThreadPooledTimer;
import org.apache.geronimo.timer.UserTaskFactory;
import org.apache.geronimo.timer.WorkInfo;
import org.apache.geronimo.transaction.context.TransactionContext;
-import org.apache.geronimo.transaction.context.TransactionContextManager;
-import org.apache.geronimo.kernel.proxy.DeadProxyException;
-import org.openejb.EJBInvocation;
+import org.openejb.EjbContainer;
+import org.openejb.ExtendedEjbDeployment;
+import org.openejb.dispatch.InterfaceMethodSignature;
/**
- *
- *
* @version $Revision$ $Date$
- *
- * */
+ */
public class BasicTimerServiceImpl implements BasicTimerService {
-
- private static final Log log = LogFactory.getLog(EJBInvokeTask.class);
-
- private final EJBTimeoutInvocationFactory invocationFactory;
- private final Interceptor stack;
- private final PersistentTimer persistentTimer;
- private final String key;
- private final UserTaskFactory userTaskFactory;
+ private static final InterfaceMethodSignature EJB_TIMEOUT_SIGNATURE =
new InterfaceMethodSignature("ejbTimeout", new Class[]{Timer.class}, false);
+ private final ExtendedEjbDeployment deployment;
+ private final EjbContainer ejbContainer;
+ private final PersistentTimer timer;
private final String kernelName;
- private final ObjectName timerSourceName;
- private final TransactionContextManager transactionContextManager;
-// private final Map idToTimersMap = new HashMap();
-
- public BasicTimerServiceImpl(EJBTimeoutInvocationFactory
invocationFactory, Interceptor stack, ThreadPooledTimer timer, String key,
String kernelName, ObjectName timerSourceName, TransactionContextManager
transactionContextManager, ClassLoader classLoader) {
- this.invocationFactory = invocationFactory;
- this.stack = stack;
- this.persistentTimer = timer;
- this.key = key;
+ private final String containerId;
+ private final ObjectName containerObjectName;
+ private final UserTaskFactory userTaskFactory;
+ private final int ejbTimeoutIndex;
+
+ public BasicTimerServiceImpl(ExtendedEjbDeployment deployment,
EjbContainer ejbContainer, PersistentTimer timer, String kernelName, String
containerId) throws MalformedObjectNameException {
+ this.deployment = deployment;
+ this.ejbContainer = ejbContainer;
+ this.timer = timer;
this.kernelName = kernelName;
- this.timerSourceName = timerSourceName;
- this.transactionContextManager = transactionContextManager;
- userTaskFactory = new EJBInvokeTaskFactory(this, classLoader,
transactionContextManager);
+ this.containerId = containerId;
+ this.containerObjectName = new ObjectName(containerId);
+ userTaskFactory = new EJBInvokeTaskFactory(this);
+
+ int ejbTimeoutIndex = -1;
+ InterfaceMethodSignature[] signatures = deployment.getSignatures();
+ for (int i = 0; i < signatures.length; i++) {
+ InterfaceMethodSignature signature = signatures[i];
+ if (signature.equals(EJB_TIMEOUT_SIGNATURE)) {
+ ejbTimeoutIndex = i;
+ break;
+ }
+ }
+ if (ejbTimeoutIndex < 0) {
+ throw new IllegalArgumentException("EJB " +
deployment.getEjbName() + " does not implement ejbTimeout(javax.ejb.Timer
timer)");
+ }
+ this.ejbTimeoutIndex = ejbTimeoutIndex;
}
public void doStart() throws PersistenceException {
//reconstruct saved timers.
- Collection workInfos = persistentTimer.playback(key,
userTaskFactory);
+ Collection workInfos = timer.playback(containerId, userTaskFactory);
for (Iterator iterator = workInfos.iterator(); iterator.hasNext();) {
WorkInfo workInfo = (WorkInfo) iterator.next();
newTimer(workInfo);
@@ -115,14 +120,14 @@
}
public void doStop() throws PersistenceException {
- Collection ids = persistentTimer.getIdsByKey(key, null);
- persistentTimer.cancelTimerTasks(ids);
+ Collection ids = timer.getIdsByKey(containerId, null);
+ timer.cancelTimerTasks(ids);
}
public Timer createTimer(Object id, Date initialExpiration, long
intervalDuration, Serializable info) throws IllegalArgumentException,
IllegalStateException, EJBException {
try {
- WorkInfo workInfo = persistentTimer.scheduleAtFixedRate(key,
userTaskFactory, id, info, initialExpiration, intervalDuration);
+ WorkInfo workInfo = timer.scheduleAtFixedRate(containerId,
userTaskFactory, id, info, initialExpiration, intervalDuration);
return newTimer(workInfo);
} catch (PersistenceException e) {
throw new EJBException(e);
@@ -135,7 +140,7 @@
public Timer createTimer(Object id, Date expiration, Serializable info)
throws IllegalArgumentException, IllegalStateException, EJBException {
try {
- WorkInfo workInfo = persistentTimer.schedule(key,
userTaskFactory, id, info, expiration);
+ WorkInfo workInfo = timer.schedule(containerId, userTaskFactory,
id, info, expiration);
return newTimer(workInfo);
} catch (PersistenceException e) {
throw new EJBException(e);
@@ -148,7 +153,7 @@
public Timer createTimer(Object id, long initialDuration, long
intervalDuration, Serializable info) throws IllegalArgumentException,
IllegalStateException, EJBException {
try {
- WorkInfo workInfo = persistentTimer.scheduleAtFixedRate(key,
userTaskFactory, id, info, initialDuration, intervalDuration);
+ WorkInfo workInfo = timer.scheduleAtFixedRate(containerId,
userTaskFactory, id, info, initialDuration, intervalDuration);
return newTimer(workInfo);
} catch (PersistenceException e) {
throw new EJBException(e);
@@ -161,7 +166,7 @@
public Timer createTimer(Object id, long duration, Serializable info)
throws IllegalArgumentException, IllegalStateException, EJBException {
try {
- WorkInfo workInfo = persistentTimer.schedule(userTaskFactory,
key, id, info, duration);
+ WorkInfo workInfo = timer.schedule(userTaskFactory, containerId,
id, info, duration);
return newTimer(workInfo);
} catch (PersistenceException e) {
throw new EJBException(e);
@@ -173,13 +178,9 @@
}
public Collection getTimers(Object id) throws IllegalStateException,
EJBException {
-// synchronized(idToTimersMap) {
-// Set timers = (Set) idToTimersMap.get(id);
-// return timers == null? Collections.EMPTY_SET:
Collections.unmodifiableSet(timers);
-// }
Collection ids = null;
try {
- ids = persistentTimer.getIdsByKey(key, id);
+ ids = timer.getIdsByKey(containerId, id);
} catch (PersistenceException e) {
throw new EJBException(e);
}
@@ -187,10 +188,10 @@
for (Iterator iterator = ids.iterator(); iterator.hasNext();) {
Long timerId = (Long) iterator.next();
try {
- TimerImpl timer = getTimerById(timerId);
+ Timer timer = getTimerById(timerId);
timers.add(timer);
} catch (NoSuchObjectLocalException e) {
- System.out.println("could not find timer for timerId " +
timerId + "from key " + key + " and " + id);
+ System.out.println("could not find timer for timerId " +
timerId + "from key " + containerId + " and " + id);
}
}
return timers;
@@ -198,17 +199,18 @@
//TODO HACK SEE GERONIMO-623
private boolean notified = false;
- public TimerImpl getTimerById(Long id) {
+
+ public Timer getTimerById(Long id) {
WorkInfo workInfo = null;
try {
- workInfo = persistentTimer.getWorkInfo(id);
+ workInfo = timer.getWorkInfo(id);
} catch (DeadProxyException e) {
//TODO HACK SEE GERONIMO-623
notified = true;
if (notified) {
return null;
}
- throw new RuntimeException("Dead proxy for ejb " + key);
+ throw new RuntimeException("Dead proxy for ejb " + containerId);
}
if (workInfo != null) {
TimerImpl timer = (TimerImpl) workInfo.getClientHandle();
@@ -219,7 +221,7 @@
}
void registerCancelSynchronization(Synchronization
cancelSynchronization) throws RollbackException, SystemException {
- TransactionContext transactionContext =
transactionContextManager.getContext();
+ TransactionContext transactionContext =
ejbContainer.getTransactionContextManager().getContext();
if (transactionContext != null && transactionContext.isInheritable()
&& transactionContext.isActive()) {
transactionContext.registerSynchronization(cancelSynchronization);
return;
@@ -228,79 +230,40 @@
}
private Timer newTimer(WorkInfo workInfo) {
-// System.out.println("Created timer with timerId " +
workInfo.getId() + " for key " + key + " and id " + workInfo.getUserId());
- Timer timer = new TimerImpl(workInfo, this, kernelName,
timerSourceName);
+ Timer timer = new TimerImpl(workInfo, this, kernelName,
containerObjectName);
workInfo.setClientHandle(timer);
-// synchronized (idToTimersMap) {
-// Set timers = (Set) idToTimersMap.get(workInfo.getUserId());
-// if (timers == null) {
-// timers = new HashSet();
-// idToTimersMap.put(workInfo.getUserId(), timers);
-// }
-// timers.add(timer);
-// }
return timer;
}
- private Interceptor getStack() {
- return stack;
- }
-
private static class EJBInvokeTask implements Runnable {
private final BasicTimerServiceImpl timerService;
private final long timerId;
- private final ClassLoader classLoader;
- private final TransactionContextManager transactionContextManager;
- public EJBInvokeTask(BasicTimerServiceImpl timerService, long id,
ClassLoader classLoader, TransactionContextManager transactionContextManager) {
+ public EJBInvokeTask(BasicTimerServiceImpl timerService, long
timerId) {
this.timerService = timerService;
- this.timerId = id;
- this.classLoader = classLoader;
- this.transactionContextManager = transactionContextManager;
+ this.timerId = timerId;
}
public void run() {
- TimerImpl timerImpl = timerService.getTimerById(new
Long(timerId));
+ TimerImpl timerImpl = (TimerImpl) timerService.getTimerById(new
Long(timerId));
//TODO HACK SEE GERONIMO-623
if (timerImpl == null) {
return;
}
- EJBInvocation invocation =
timerService.invocationFactory.getEJBTimeoutInvocation(timerImpl.getUserId(),
timerImpl);
-
- // set the transaction context into the invocation object
- TransactionContext transactionContext =
transactionContextManager.getContext();
- if (transactionContext == null) {
- throw new IllegalStateException("Transaction context has not
been set");
- }
- invocation.setTransactionContext(transactionContext);
-
- Thread currentThread = Thread.currentThread();
- ClassLoader oldClassLoader =
currentThread.getContextClassLoader();
- try {
- currentThread.setContextClassLoader(classLoader);
- timerService.getStack().invoke(invocation);
- } catch (Throwable throwable) {
- log.warn("Timer invocation failed", throwable);
- } finally {
- currentThread.setContextClassLoader(oldClassLoader);
- }
+ timerService.ejbContainer.timeout(timerService.deployment,
timerImpl.getUserId(), timerImpl, timerService.ejbTimeoutIndex);
}
}
private static class EJBInvokeTaskFactory implements UserTaskFactory {
private final BasicTimerServiceImpl timerService;
- private final ClassLoader classLoader;
- private final TransactionContextManager transactionContextManager;
- public EJBInvokeTaskFactory(BasicTimerServiceImpl timerService,
ClassLoader classLoader, TransactionContextManager transactionContextManager) {
+ public EJBInvokeTaskFactory(BasicTimerServiceImpl timerService) {
this.timerService = timerService;
- this.classLoader = classLoader;
- this.transactionContextManager = transactionContextManager;
}
public Runnable newTask(long id) {
- return new EJBInvokeTask(timerService, id, classLoader,
transactionContextManager);
+ return new EJBInvokeTask(timerService, id);
}
}
1.3 +5 -3
openejb/modules/core/src/java/org/openejb/timer/EJBTimeoutInvocationFactory.java
Index: EJBTimeoutInvocationFactory.java
===================================================================
RCS file:
/home/projects/openejb/scm/openejb/modules/core/src/java/org/openejb/timer/EJBTimeoutInvocationFactory.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- EJBTimeoutInvocationFactory.java 20 Jul 2004 23:40:03 -0000 1.2
+++ EJBTimeoutInvocationFactory.java 1 Feb 2006 11:50:10 -0000 1.3
@@ -1,6 +1,8 @@
package org.openejb.timer;
-import org.openejb.EJBInvocation;
+import javax.ejb.Timer;
+
+import org.openejb.EjbInvocation;
/**
*
@@ -10,6 +12,6 @@
* */
public interface EJBTimeoutInvocationFactory {
- EJBInvocation getEJBTimeoutInvocation(Object id, TimerImpl timer);
+ EjbInvocation getEJBTimeoutInvocation(Object id, Timer timer);
}
1.4 +4 -6
openejb/modules/core/src/java/org/openejb/timer/TimerImpl.java
Index: TimerImpl.java
===================================================================
RCS file:
/home/projects/openejb/scm/openejb/modules/core/src/java/org/openejb/timer/TimerImpl.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- TimerImpl.java 14 Feb 2005 18:32:32 -0000 1.3
+++ TimerImpl.java 1 Feb 2006 11:50:10 -0000 1.4
@@ -65,10 +65,8 @@
* TODO keep track of state so after single-shot firing nothing works.
*
* @version $Revision$ $Date$
- *
- * */
+ */
public class TimerImpl implements Timer {
-
private final WorkInfo workInfo;
private final BasicTimerServiceImpl timerService;
private final String kernelName;
@@ -89,7 +87,7 @@
try {
timerService.registerCancelSynchronization(new
CancelSynchronization());
} catch (RollbackException e) {
- throw (IllegalStateException)new
IllegalStateException("Transaction is already rolled back").initCause(e);
+ throw (IllegalStateException) new
IllegalStateException("Transaction is already rolled back").initCause(e);
} catch (SystemException e) {
throw new EJBException(e);
}
@@ -109,7 +107,7 @@
public Serializable getInfo() throws EJBException,
IllegalStateException, NoSuchObjectLocalException {
checkState();
- return (Serializable)workInfo.getUserInfo();
+ return (Serializable) workInfo.getUserInfo();
}
public TimerHandle getHandle() throws EJBException,
IllegalStateException, NoSuchObjectLocalException {
1.2 +2 -2
openejb/modules/core/src/java/org/openejb/timer/UnavailableTimerService.java
Index: UnavailableTimerService.java
===================================================================
RCS file:
/home/projects/openejb/scm/openejb/modules/core/src/java/org/openejb/timer/UnavailableTimerService.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- UnavailableTimerService.java 14 Feb 2005 18:32:32 -0000 1.1
+++ UnavailableTimerService.java 1 Feb 2006 11:50:10 -0000 1.2
@@ -84,7 +84,7 @@
throw new IllegalStateException("Timer service is not available");
}
- public TimerImpl getTimerById(Long id) {
+ public Timer getTimerById(Long id) {
throw new IllegalStateException("Timer service is not available");
}
}