| Method from org.apache.tomcat.util.net.JIoEndpoint Detail: |
protected JIoEndpoint.Worker createWorkerThread() {
synchronized (workers) {
if (workers.size() > 0) {
curThreadsBusy++;
return workers.pop();
}
if ((maxThreads > 0) && (curThreads < maxThreads)) {
curThreadsBusy++;
return (newWorkerThread());
} else {
if (maxThreads < 0) {
curThreadsBusy++;
return (newWorkerThread());
} else {
return (null);
}
}
}
}
Create (or allocate) and return an available processor for use in
processing a specific HTTP request, if possible. If the maximum
allowed processors have already been created and are in use, return
null instead. |
public void destroy() throws Exception {
if (running) {
stop();
}
if (serverSocket != null) {
try {
if (serverSocket != null)
serverSocket.close();
} catch (Exception e) {
log.error(sm.getString("endpoint.err.close"), e);
}
serverSocket = null;
}
initialized = false ;
}
Deallocate APR memory pools, and close server socket. |
public int getAcceptorThreadCount() {
return acceptorThreadCount;
}
|
public InetAddress getAddress() {
return address;
}
|
public int getBacklog() {
return backlog;
}
|
public int getCurrentThreadCount() {
return curThreads;
}
|
public int getCurrentThreadsBusy() {
return workers!=null?curThreads - workers.size():0;
}
|
public boolean getDaemon() {
return daemon;
}
|
public Executor getExecutor() {
return executor;
}
|
public JIoEndpoint.Handler getHandler() {
return handler;
}
|
public int getMaxThreads() {
return maxThreads;
}
|
public String getName() {
return name;
}
|
public int getPort() {
return port;
}
|
public ServerSocketFactory getServerSocketFactory() {
return serverSocketFactory;
}
|
public int getSoLinger() {
return soLinger;
}
|
public int getSoTimeout() {
return soTimeout;
}
|
public boolean getTcpNoDelay() {
return tcpNoDelay;
}
|
public int getThreadPriority() {
return threadPriority;
}
|
protected JIoEndpoint.Worker getWorkerThread() {
// Allocate a new worker thread
Worker workerThread = createWorkerThread();
while (workerThread == null) {
try {
synchronized (workers) {
workers.wait();
}
} catch (InterruptedException e) {
// Ignore
}
workerThread = createWorkerThread();
}
return workerThread;
}
Return a new worker thread, and block while to worker is available. |
public void init() throws Exception {
if (initialized)
return;
// Initialize thread count defaults for acceptor
if (acceptorThreadCount == 0) {
acceptorThreadCount = 1;
}
if (serverSocketFactory == null) {
serverSocketFactory = ServerSocketFactory.getDefault();
}
if (serverSocket == null) {
try {
if (address == null) {
serverSocket = serverSocketFactory.createSocket(port, backlog);
} else {
serverSocket = serverSocketFactory.createSocket(port, backlog, address);
}
} catch (BindException be) {
throw new BindException(be.getMessage() + ":" + port);
}
}
//if( serverTimeout >= 0 )
// serverSocket.setSoTimeout( serverTimeout );
initialized = true;
}
|
public boolean isPaused() {
return paused;
}
|
public boolean isRunning() {
return running;
}
|
protected JIoEndpoint.Worker newWorkerThread() {
Worker workerThread = new Worker();
workerThread.start();
return (workerThread);
}
Create and return a new processor suitable for processing HTTP
requests and returning the corresponding responses. |
public void pause() {
if (running && !paused) {
paused = true;
unlockAccept();
}
}
|
protected boolean processSocket(Socket socket) {
try {
if (executor == null) {
getWorkerThread().assign(socket);
} else {
executor.execute(new SocketProcessor(socket));
}
} catch (Throwable t) {
// This means we got an OOM or similar creating a thread, or that
// the pool and its queue are full
log.error(sm.getString("endpoint.process.fail"), t);
return false;
}
return true;
}
|
protected void recycleWorkerThread(JIoEndpoint.Worker workerThread) {
synchronized (workers) {
workers.push(workerThread);
curThreadsBusy--;
workers.notify();
}
}
Recycle the specified Processor so that it can be used again. |
public void resume() {
if (running) {
paused = false;
}
}
|
public void setAcceptorThreadCount(int acceptorThreadCount) {
this.acceptorThreadCount = acceptorThreadCount;
}
|
public void setAddress(InetAddress address) {
this.address = address;
}
|
public void setBacklog(int backlog) {
if (backlog > 0) this.backlog = backlog;
}
|
public void setDaemon(boolean b) {
daemon = b;
}
|
public void setExecutor(Executor executor) {
this.executor = executor;
}
|
public void setHandler(JIoEndpoint.Handler handler) {
this.handler = handler;
}
|
public void setMaxThreads(int maxThreads) {
this.maxThreads = maxThreads;
}
|
public void setName(String name) {
this.name = name;
}
|
public void setPort(int port) {
this.port=port;
}
|
public void setServerSocketFactory(ServerSocketFactory factory) {
this.serverSocketFactory = factory;
}
|
public void setSoLinger(int soLinger) {
this.soLinger = soLinger;
}
|
public void setSoTimeout(int soTimeout) {
this.soTimeout = soTimeout;
}
|
protected boolean setSocketOptions(Socket socket) {
// Process the connection
int step = 1;
try {
// 1: Set socket options: timeout, linger, etc
if (soLinger >= 0) {
socket.setSoLinger(true, soLinger);
}
if (tcpNoDelay) {
socket.setTcpNoDelay(tcpNoDelay);
}
if (soTimeout > 0) {
socket.setSoTimeout(soTimeout);
}
// 2: SSL handshake
step = 2;
serverSocketFactory.handshake(socket);
} catch (Throwable t) {
if (log.isDebugEnabled()) {
if (step == 2) {
log.debug(sm.getString("endpoint.err.handshake"), t);
} else {
log.debug(sm.getString("endpoint.err.unexpected"), t);
}
}
// Tell to close the socket
return false;
}
return true;
}
Set the options for the current socket. |
public void setTcpNoDelay(boolean tcpNoDelay) {
this.tcpNoDelay = tcpNoDelay;
}
|
public void setThreadPriority(int threadPriority) {
this.threadPriority = threadPriority;
}
|
public void start() throws Exception {
// Initialize socket if not done before
if (!initialized) {
init();
}
if (!running) {
running = true;
paused = false;
// Create worker collection
if (executor == null) {
workers = new WorkerStack(maxThreads);
}
// Start acceptor threads
for (int i = 0; i < acceptorThreadCount; i++) {
Thread acceptorThread = new Thread(new Acceptor(), getName() + "-Acceptor-" + i);
acceptorThread.setPriority(threadPriority);
acceptorThread.setDaemon(daemon);
acceptorThread.start();
}
}
}
|
public void stop() {
if (running) {
running = false;
unlockAccept();
}
}
|
protected void unlockAccept() {
Socket s = null;
try {
// Need to create a connection to unlock the accept();
if (address == null) {
s = new Socket(InetAddress.getByName("localhost").getHostAddress(), port);
} else {
s = new Socket(address, port);
// setting soLinger to a small value will help shutdown the
// connection quicker
s.setSoLinger(true, 0);
}
} catch (Exception e) {
if (log.isDebugEnabled()) {
log.debug(sm.getString("endpoint.debug.unlock", "" + port), e);
}
} finally {
if (s != null) {
try {
s.close();
} catch (Exception e) {
// Ignore
}
}
}
}
Unlock the accept by using a local connection. |