You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2010/10/01 18:32:31 UTC

svn commit: r1003572 - in /tomcat/trunk/java/org/apache/catalina/tribes: group/interceptors/MessageDispatch15Interceptor.java util/TcclThreadFactory.java

Author: markt
Date: Fri Oct  1 16:32:31 2010
New Revision: 1003572

URL: http://svn.apache.org/viewvc?rev=1003572&view=rev
Log:
Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=49905
Prevent memory leak when using aysnc session replication

Added:
    tomcat/trunk/java/org/apache/catalina/tribes/util/TcclThreadFactory.java   (with props)
Modified:
    tomcat/trunk/java/org/apache/catalina/tribes/group/interceptors/MessageDispatch15Interceptor.java

Modified: tomcat/trunk/java/org/apache/catalina/tribes/group/interceptors/MessageDispatch15Interceptor.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/tribes/group/interceptors/MessageDispatch15Interceptor.java?rev=1003572&r1=1003571&r2=1003572&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/tribes/group/interceptors/MessageDispatch15Interceptor.java (original)
+++ tomcat/trunk/java/org/apache/catalina/tribes/group/interceptors/MessageDispatch15Interceptor.java Fri Oct  1 16:32:31 2010
@@ -24,6 +24,7 @@ import org.apache.catalina.tribes.Channe
 import org.apache.catalina.tribes.Member;
 import org.apache.catalina.tribes.group.InterceptorPayload;
 import org.apache.catalina.tribes.transport.bio.util.LinkObject;
+import org.apache.catalina.tribes.util.TcclThreadFactory;
 
 /**
  * 
@@ -80,7 +81,9 @@ public class MessageDispatch15Intercepto
     @Override
     public void startQueue() {
         if ( run ) return;
-        executor = new ThreadPoolExecutor(maxSpareThreads,maxThreads,keepAliveTime,TimeUnit.MILLISECONDS,runnablequeue);
+        executor = new ThreadPoolExecutor(maxSpareThreads, maxThreads,
+                keepAliveTime, TimeUnit.MILLISECONDS, runnablequeue,
+                new TcclThreadFactory());
         run = true;
     }
 

Added: tomcat/trunk/java/org/apache/catalina/tribes/util/TcclThreadFactory.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/tribes/util/TcclThreadFactory.java?rev=1003572&view=auto
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/tribes/util/TcclThreadFactory.java (added)
+++ tomcat/trunk/java/org/apache/catalina/tribes/util/TcclThreadFactory.java Fri Oct  1 16:32:31 2010
@@ -0,0 +1,66 @@
+/*
+ * 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.catalina.tribes.util;
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * ThreadFactory implementation that creates threads with the thread context
+ * class loader set to the class loader that loaded this factory. It is intended
+ * to be used when tasks may be passed to executors when the web application
+ * class loader is set as the thread context class loader, such as in async
+ * session replication.
+ */
+public class TcclThreadFactory implements ThreadFactory {
+    
+    private static final AtomicInteger poolNumber = new AtomicInteger(1);
+    private static final boolean IS_SECURITY_ENABLED =
+        (System.getSecurityManager() != null);
+    
+    private final ThreadGroup group;
+    private final AtomicInteger threadNumber = new AtomicInteger(1);
+    private final String namePrefix;
+    
+    public TcclThreadFactory() {
+        SecurityManager s = System.getSecurityManager();
+        group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
+        namePrefix = "pool-" + poolNumber.getAndIncrement() + "-thread-";
+    }
+
+    @Override
+    public Thread newThread(Runnable r) {
+        final Thread t = new Thread(group, r, namePrefix +
+                threadNumber.getAndIncrement());
+        
+        if (IS_SECURITY_ENABLED) {
+            AccessController.doPrivileged(new PrivilegedAction<Void>() {
+                @Override
+                public Void run() {
+                    t.setContextClassLoader(this.getClass().getClassLoader());
+                    return null;
+                }
+            });
+        } else {
+            t.setContextClassLoader(this.getClass().getClassLoader());
+        }
+        return t;
+    }
+
+}

Propchange: tomcat/trunk/java/org/apache/catalina/tribes/util/TcclThreadFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org