1 45 package org.openejb.server; 46 47 import java.io.*; 48 import java.net.*; 49 import java.util.*; 50 import org.openejb.*; 51 import org.openejb.util.Logger; 52 import edu.emory.mathcs.backport.java.util.concurrent.Executor; 53 import edu.emory.mathcs.backport.java.util.concurrent.LinkedBlockingQueue; 54 import edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor; 55 import edu.emory.mathcs.backport.java.util.concurrent.ThreadFactory; 56 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit; 57 58 71 public class ServicePool implements ServerService { 72 73 private Logger log = Logger.getInstance(ServicePool.class.getName(), ServicePool.class.getName()); 74 private ServerService next; 75 private Executor executor; 76 77 78 public ServicePool(ServerService next){ 79 this.next = next; 80 } 81 82 89 public void init(Properties props) throws Exception { 90 91 String threadsString = props.getProperty("threads", "200"); 93 int threads = Integer.parseInt(threadsString); 94 final String name = props.getProperty("name", "unknown"); 95 96 ThreadPoolExecutor p = new ThreadPoolExecutor(threads, threads, 5000, TimeUnit.MILLISECONDS, new LinkedBlockingQueue()); 97 p.setThreadFactory(new ThreadFactory() { 98 private volatile int id = 0; 99 100 public Thread newThread(Runnable arg0) { 101 Thread thread = new Thread (arg0, name + " " + getNextID()); 102 return thread; 103 } 104 105 private int getNextID() { 106 return id++; 107 } 108 109 }); 110 executor = p; 111 112 next.init(props); 114 } 115 116 public void start() throws ServiceException{ 117 119 next.start(); 121 } 122 123 public void stop() throws ServiceException{ 124 126 next.stop(); 128 } 129 130 public void service(final Socket socket) throws ServiceException, IOException{ 131 final Runnable service = new Runnable () { 132 public void run() { 133 try { 134 next.service(socket); 135 } catch (SecurityException e) { 136 log.error("Security error: " + e.getMessage(), e); 137 } catch (Throwable e) { 138 log.error("Unexpected error", e); 139 } finally { 140 try { 141 if (socket != null) { 142 socket.close(); 143 } 144 } catch (Throwable t) { 145 log.warning("Error while closing connection with client", t); 146 } 147 } 148 } 149 }; 150 151 final ClassLoader tccl = Thread.currentThread().getContextClassLoader(); 152 Runnable ctxCL = new Runnable () { 153 public void run() { 154 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 155 Thread.currentThread().setContextClassLoader(tccl); 156 try { 157 service.run(); 158 } finally { 159 Thread.currentThread().setContextClassLoader(cl); 160 } 161 } 162 }; 163 164 executor.execute(ctxCL); 165 } 166 167 public void service(InputStream in, OutputStream out) throws ServiceException, IOException { 168 throw new UnsupportedOperationException ("service(in,out)"); 169 } 170 171 175 public String getName(){ 176 return next.getName(); 177 } 178 179 183 public String getIP(){ 184 return next.getIP(); 185 } 186 187 191 public int getPort(){ 192 return next.getPort(); 193 } 194 195 } 196
| Popular Tags
|