You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ga...@apache.org on 2008/03/11 21:34:58 UTC

svn commit: r636096 - in /geronimo/sandbox/concurrent: concurrent/src/main/plan/ geronimo-concurrent-core/ geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/hand...

Author: gawor
Date: Tue Mar 11 13:34:54 2008
New Revision: 636096

URL: http://svn.apache.org/viewvc?rev=636096&view=rev
Log:
rudimentary transaction support

Added:
    geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/handlers/
    geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/handlers/ClassLoaderContextHandler.java
      - copied, changed from r636063, geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ClassLoaderContextHandler.java
    geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/handlers/TransactionContextHandler.java   (with props)
    geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/handlers/TransactionContextHandler.java   (with props)
Removed:
    geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ClassLoaderContextHandler.java
Modified:
    geronimo/sandbox/concurrent/concurrent/src/main/plan/plan.xml
    geronimo/sandbox/concurrent/geronimo-concurrent-core/pom.xml
    geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedContextHandlerChain.java
    geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/executor/ServerManagedExecutorServiceWrapperGBean.java
    geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/executor/ServerManagedScheduledExecutorServiceWrapperGBean.java

Modified: geronimo/sandbox/concurrent/concurrent/src/main/plan/plan.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/concurrent/src/main/plan/plan.xml?rev=636096&r1=636095&r2=636096&view=diff
==============================================================================
--- geronimo/sandbox/concurrent/concurrent/src/main/plan/plan.xml (original)
+++ geronimo/sandbox/concurrent/concurrent/src/main/plan/plan.xml Tue Mar 11 13:34:54 2008
@@ -25,18 +25,20 @@
         <attribute name="hungTaskThreshold">30000</attribute>
         <attribute name="hungTaskMonitorFrequency">60000</attribute>
         <attribute name="contextHandlers">
-             org.apache.geronimo.concurrent.ClassLoaderContextHandler, 
+             org.apache.geronimo.concurrent.handlers.ClassLoaderContextHandler, 
              org.apache.geronimo.concurrent.impl.handlers.NamingContextHandler,
-             org.apache.geronimo.concurrent.impl.handlers.SecurityContextHandler
+             org.apache.geronimo.concurrent.impl.handlers.SecurityContextHandler,
+             org.apache.geronimo.concurrent.impl.handlers.TransactionContextHandler
         </attribute>
     </gbean>
 
    <gbean name="DefaultContextService" 
           class="org.apache.geronimo.concurrent.impl.context.ContextServiceGBean">
         <attribute name="contextHandlers">
-             org.apache.geronimo.concurrent.ClassLoaderContextHandler, 
+             org.apache.geronimo.concurrent.handlers.ClassLoaderContextHandler, 
              org.apache.geronimo.concurrent.impl.handlers.NamingContextHandler,
-             org.apache.geronimo.concurrent.impl.handlers.SecurityContextHandler
+             org.apache.geronimo.concurrent.impl.handlers.SecurityContextHandler,
+             org.apache.geronimo.concurrent.impl.handlers.TransactionContextHandler
         </attribute>
    </gbean>
 
@@ -49,9 +51,10 @@
             <name>DefaultManagedThreadFactory</name>
         </reference>
         <attribute name="contextHandlers">
-             org.apache.geronimo.concurrent.ClassLoaderContextHandler, 
+             org.apache.geronimo.concurrent.handlers.ClassLoaderContextHandler, 
              org.apache.geronimo.concurrent.impl.handlers.NamingContextHandler,
-             org.apache.geronimo.concurrent.impl.handlers.SecurityContextHandler
+             org.apache.geronimo.concurrent.impl.handlers.SecurityContextHandler,
+             org.apache.geronimo.concurrent.impl.handlers.TransactionContextHandler
         </attribute>
    </gbean>
 
@@ -62,9 +65,10 @@
             <name>DefaultManagedThreadFactory</name>
         </reference>
         <attribute name="contextHandlers">
-             org.apache.geronimo.concurrent.ClassLoaderContextHandler, 
+             org.apache.geronimo.concurrent.handlers.ClassLoaderContextHandler, 
              org.apache.geronimo.concurrent.impl.handlers.NamingContextHandler,
-             org.apache.geronimo.concurrent.impl.handlers.SecurityContextHandler
+             org.apache.geronimo.concurrent.impl.handlers.SecurityContextHandler,
+             org.apache.geronimo.concurrent.impl.handlers.TransactionContextHandler
         </attribute>
    </gbean>
 

Modified: geronimo/sandbox/concurrent/geronimo-concurrent-core/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/pom.xml?rev=636096&r1=636095&r2=636096&view=diff
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/pom.xml (original)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/pom.xml Tue Mar 11 13:34:54 2008
@@ -44,6 +44,11 @@
             <groupId>commons-logging</groupId>
             <artifactId>commons-logging</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.geronimo.modules</groupId>
+            <artifactId>geronimo-transaction</artifactId>
+            <version>${version}</version>
+        </dependency>
    </dependencies>
    
 </project>

Modified: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedContextHandlerChain.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedContextHandlerChain.java?rev=636096&r1=636095&r2=636096&view=diff
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedContextHandlerChain.java (original)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ManagedContextHandlerChain.java Tue Mar 11 13:34:54 2008
@@ -17,6 +17,7 @@
 package org.apache.geronimo.concurrent;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
 import java.util.ListIterator;
 import java.util.Map;
@@ -36,7 +37,7 @@
     public ManagedContextHandlerChain() {        
     }
     
-    public ManagedContextHandlerChain(List<ManagedContextHandler> handlers) {
+    public ManagedContextHandlerChain(Collection<ManagedContextHandler> handlers) {
         if (handlers != null) {
             this.chain.addAll(handlers);
         }

Copied: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/handlers/ClassLoaderContextHandler.java (from r636063, geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ClassLoaderContextHandler.java)
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/handlers/ClassLoaderContextHandler.java?p2=geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/handlers/ClassLoaderContextHandler.java&p1=geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ClassLoaderContextHandler.java&r1=636063&r2=636096&rev=636096&view=diff
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/ClassLoaderContextHandler.java (original)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/handlers/ClassLoaderContextHandler.java Tue Mar 11 13:34:54 2008
@@ -14,7 +14,7 @@
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
-package org.apache.geronimo.concurrent;
+package org.apache.geronimo.concurrent.handlers;
 
 import java.util.Map;
 

Added: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/handlers/TransactionContextHandler.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/handlers/TransactionContextHandler.java?rev=636096&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/handlers/TransactionContextHandler.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/handlers/TransactionContextHandler.java Tue Mar 11 13:34:54 2008
@@ -0,0 +1,131 @@
+/**
+ *  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.geronimo.concurrent.handlers;
+
+import java.util.Map;
+
+import javax.transaction.Status;
+import javax.transaction.SystemException;
+import javax.transaction.Transaction;
+import javax.transaction.TransactionManager;
+import javax.util.concurrent.ContextService;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.geronimo.concurrent.ManagedContextHandler;
+
+/*
+ * Managed context handler that for handling transactions. Follows 
+ * Bean-Managed Transaction Demarcation rules as described in EJB 3.0 specification
+ * 13.6.1 section.
+ */
+public abstract class TransactionContextHandler implements ManagedContextHandler {
+
+    protected final static Log LOG = LogFactory.getLog(TransactionContextHandler.class);
+    
+    protected final static String TRANSACTION_MANAGER = 
+        TransactionContextHandler.class.getName() + ".transactionManager";
+    
+    protected final static String CLIENT_TRANSACTION = 
+        TransactionContextHandler.class.getName() + ".clientTransaction";
+               
+    private boolean isUseParentTransaction(Map<String, Object> threadContext) {
+        Object useParentTransaction = 
+            threadContext.get(ContextService.USE_PARENT_TRANSACTION);
+        return Boolean.TRUE.equals(useParentTransaction);
+    }
+    
+    public void setContext(Map<String, Object> threadContext) {
+        LOG.debug("setContext");
+        
+        if (!isUseParentTransaction(threadContext)) {                  
+            suspendTransaction(threadContext);
+        }
+    }
+    
+    public void unsetContext(Map<String, Object> threadContext) {
+        LOG.debug("unsetContext");
+        
+        if (!isUseParentTransaction(threadContext)) {                  
+            resumeTransaction(threadContext);
+        }
+    }
+    
+    protected TransactionManager getTransactionManager(Map<String, Object> threadContext) {
+        return (TransactionManager)threadContext.get(TRANSACTION_MANAGER);
+    }
+    
+    protected void suspendTransaction(Map<String, Object> threadContext) {
+        LOG.debug("suspendTransaction");
+        
+        TransactionManager manager = getTransactionManager(threadContext);
+        
+        try {
+            Transaction clientTransaction = manager.suspend();
+            if (clientTransaction != null) {
+                threadContext.put(CLIENT_TRANSACTION, clientTransaction);
+            }
+        } catch (SystemException e) {
+            LOG.warn("Failed to suspend transaction", e);
+        }        
+    }
+
+    public void resumeTransaction(Map<String, Object> threadContext) {
+        LOG.debug("resumeTransaction");
+        
+        TransactionManager manager = getTransactionManager(threadContext);
+                
+        try {
+            /*
+            * The Container must detect the case in which a transaction was started, but
+            * not completed, in the business method, and handle it as follows:
+            */
+            Transaction transaction = manager.getTransaction();
+
+            if (transaction == null) {
+                return;
+            }
+
+            if (transaction.getStatus() != Status.STATUS_ROLLEDBACK && 
+                transaction.getStatus() != Status.STATUS_COMMITTED) {
+                String message = "The task started a transaction but did not complete it.";
+                
+                LOG.error(message);
+
+                try {
+                    manager.rollback();
+                } catch (Throwable t) {
+                    // ignore
+                }
+            }
+
+        } catch (SystemException e) {
+            LOG.warn("Error handling transaction", e);
+        } finally {
+            Transaction clientTransaction = 
+                (Transaction)threadContext.get(CLIENT_TRANSACTION);
+            if (clientTransaction != null) {
+                try {
+                    manager.resume(clientTransaction);
+                } catch (Exception  e) {
+                    LOG.warn("Failed to resume transaction", e);
+                }
+            }
+        }
+    }    
+  
+}

Propchange: geronimo/sandbox/concurrent/geronimo-concurrent-core/src/main/java/org/apache/geronimo/concurrent/handlers/TransactionContextHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/executor/ServerManagedExecutorServiceWrapperGBean.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/executor/ServerManagedExecutorServiceWrapperGBean.java?rev=636096&r1=636095&r2=636096&view=diff
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/executor/ServerManagedExecutorServiceWrapperGBean.java (original)
+++ geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/executor/ServerManagedExecutorServiceWrapperGBean.java Tue Mar 11 13:34:54 2008
@@ -82,7 +82,7 @@
         GBeanData wrapperData = kernel.getGBeanData(this.name);
         
         executorServiceData.setReferencePatterns("threadFactory", 
-                wrapperData.getReferencePatterns("threadFactory"));
+                                                 wrapperData.getReferencePatterns("threadFactory"));
         
         addGBeanKernel(this.executorServiceName, executorServiceData);
 

Modified: geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/executor/ServerManagedScheduledExecutorServiceWrapperGBean.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/executor/ServerManagedScheduledExecutorServiceWrapperGBean.java?rev=636096&r1=636095&r2=636096&view=diff
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/executor/ServerManagedScheduledExecutorServiceWrapperGBean.java (original)
+++ geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/executor/ServerManagedScheduledExecutorServiceWrapperGBean.java Tue Mar 11 13:34:54 2008
@@ -70,7 +70,7 @@
         GBeanData wrapperData = kernel.getGBeanData(this.name);
         
         executorServiceData.setReferencePatterns("threadFactory", 
-                wrapperData.getReferencePatterns("threadFactory"));
+                                                 wrapperData.getReferencePatterns("threadFactory"));
         
         addGBeanKernel(this.executorServiceName, executorServiceData);
 

Added: geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/handlers/TransactionContextHandler.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/handlers/TransactionContextHandler.java?rev=636096&view=auto
==============================================================================
--- geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/handlers/TransactionContextHandler.java (added)
+++ geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/handlers/TransactionContextHandler.java Tue Mar 11 13:34:54 2008
@@ -0,0 +1,60 @@
+/**
+ *  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.geronimo.concurrent.impl.handlers;
+
+import java.util.Map;
+import java.util.Set;
+
+import javax.transaction.TransactionManager;
+
+import org.apache.geronimo.gbean.AbstractName;
+import org.apache.geronimo.gbean.AbstractNameQuery;
+import org.apache.geronimo.kernel.Kernel;
+import org.apache.geronimo.kernel.KernelRegistry;
+
+/*
+ * Geronimo-specific managed context handler that for handling transactions. 
+ */
+public class TransactionContextHandler extends org.apache.geronimo.concurrent.handlers.TransactionContextHandler {
+
+    private TransactionManager transactionManager;
+    
+    public void saveContext(Map<String, Object> context) {
+        context.put(TRANSACTION_MANAGER, getTransactionManager());
+    }
+    
+    private TransactionManager getTransactionManager() {
+        if (this.transactionManager == null) {
+            try {
+                Kernel kernel = KernelRegistry.getSingleKernel();
+                AbstractNameQuery query = new AbstractNameQuery(TransactionManager.class.getName());
+                Set gbeans = kernel.listGBeans(query);
+                if (gbeans != null && !gbeans.isEmpty()) {
+                    AbstractName name = (AbstractName)gbeans.iterator().next();
+                    this.transactionManager = (TransactionManager)kernel.getGBean(name); 
+                    LOG.debug("TransactionManager: " + this.transactionManager);
+                } else {
+                    LOG.debug("TransactionManager not found");
+                }
+            } catch (Exception e) {
+                LOG.warn("Failed to find TransactionManager", e);
+            }
+        }
+        return this.transactionManager;
+    }
+   
+}

Propchange: geronimo/sandbox/concurrent/geronimo-concurrent/src/main/java/org/apache/geronimo/concurrent/impl/handlers/TransactionContextHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native