|
11 | 11 |
|
12 | 12 | import java.util.concurrent.*; |
13 | 13 | import java.util.*; |
| 14 | +import org.apache.log4j.Logger; |
14 | 15 |
|
15 | 16 | public class MemCacheThreadPoolExecutor extends ThreadPoolExecutor { |
16 | 17 |
|
| 18 | + // logger |
| 19 | + private static Logger log = |
| 20 | + Logger.getLogger( MemCacheThreadPoolExecutor.class.getName() ); |
| 21 | + |
17 | 22 | private String poolName; |
18 | 23 | private String host; |
19 | 24 | private SockIOPool.SockIO socket; |
@@ -61,34 +66,40 @@ public MemCacheThreadPoolExecutor( int corePoolSize, int maximumPoolSize, |
61 | 66 | } |
62 | 67 |
|
63 | 68 | protected void beforeExecute( Thread t, Runnable r ) { |
64 | | - super.beforeExecute( t, r ); |
65 | 69 |
|
66 | 70 | // make sure we have a valid connected socket |
67 | 71 | // if not, then get a new connection |
68 | | - if ( socketOnDemand |
69 | | - || socket == null |
70 | | - || !socket.isConnected() ) |
71 | | - socket = SockIOPool.getInstance( poolName ).getConnection( host ); |
72 | | - |
73 | | - ((CacheTask)r).setSocket( socket ); |
| 72 | + if ( socketOnDemand || this.socket == null || !this.socket.isConnected() ) { |
| 73 | + ((CacheTask)r).setSocket( SockIOPool.getInstance( poolName ).getConnection( host ) ); |
| 74 | + } |
| 75 | + else { |
| 76 | + ((CacheTask)r).setSocket( this.socket ); |
| 77 | + } |
74 | 78 |
|
| 79 | + super.beforeExecute( t, r ); |
75 | 80 | } |
76 | 81 |
|
77 | 82 | protected void afterExecute( Runnable r, Throwable t ) { |
78 | 83 | super.afterExecute( r, t ); |
79 | 84 |
|
80 | | - socket = ((CacheTask)r).getSocket(); |
81 | 85 | if ( socketOnDemand ) { |
82 | 86 | // return to pool |
83 | | - if ( socket != null ) |
84 | | - socket.close(); |
| 87 | + if ( ((CacheTask)r).getSocket() != null ) |
| 88 | + ((CacheTask)r).getSocket().close(); |
85 | 89 | } |
86 | 90 | else { |
87 | 91 | // freshen the socket so we have it around |
88 | | - if ( socket != null ) |
89 | | - socket.touch(); |
| 92 | + if ( this.socket != null ) |
| 93 | + this.socket.touch(); |
90 | 94 | else |
91 | 95 | this.socket = null; |
92 | 96 | } |
93 | 97 | } |
| 98 | + |
| 99 | + protected void terminated() { |
| 100 | + if ( this.socket != null && this.socket.isConnected() ) |
| 101 | + this.socket.close(); |
| 102 | + |
| 103 | + super.terminated(); |
| 104 | + } |
94 | 105 | } |
0 commit comments