📄 pooltcpendpoint.java
字号:
/*
* 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.tomcat.util.net;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.BindException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.security.AccessControlException;
import java.util.Stack;
import java.util.Vector;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.util.res.StringManager;
import org.apache.tomcat.util.threads.ThreadPool;
import org.apache.tomcat.util.threads.ThreadPoolRunnable;
/* Similar with MPM module in Apache2.0. Handles all the details related with
"tcp server" functionality - thread management, accept policy, etc.
It should do nothing more - as soon as it get a socket ( and all socket options
are set, etc), it just handle the stream to ConnectionHandler.processConnection. (costin)
*/
/**
* Handle incoming TCP connections.
*
* This class implement a simple server model: one listener thread accepts on a socket and
* creates a new worker thread for each incoming connection.
*
* More advanced Endpoints will reuse the threads, use queues, etc.
*
* @author James Duncan Davidson [duncan@eng.sun.com]
* @author Jason Hunter [jch@eng.sun.com]
* @author James Todd [gonzo@eng.sun.com]
* @author Costin@eng.sun.com
* @author Gal Shachor [shachor@il.ibm.com]
* @author Yoav Shapira <yoavs@apache.org>
*/
public class PoolTcpEndpoint implements Runnable { // implements Endpoint {
static Log log=LogFactory.getLog(PoolTcpEndpoint.class );
private StringManager sm =
StringManager.getManager("org.apache.tomcat.util.net.res");
private static final int BACKLOG = 100;
private static final int TIMEOUT = 1000;
private final Object threadSync = new Object();
private int backlog = BACKLOG;
private int serverTimeout = TIMEOUT;
private InetAddress inet;
private int port;
private ServerSocketFactory factory;
private ServerSocket serverSocket;
private volatile boolean running = false;
private volatile boolean paused = false;
private boolean initialized = false;
private boolean reinitializing = false;
static final int debug=0;
protected boolean tcpNoDelay=false;
protected int linger=100;
protected int socketTimeout=-1;
private boolean lf = true;
// ------ Leader follower fields
TcpConnectionHandler handler;
ThreadPoolRunnable listener;
ThreadPool tp;
// ------ Master slave fields
/* The background thread. */
private Thread thread = null;
/* Available processors. */
private Stack workerThreads = new Stack();
private int curThreads = 0;
private int maxThreads = 20;
/* All processors which have been created. */
private Vector created = new Vector();
public PoolTcpEndpoint() {
tp = new ThreadPool();
}
public PoolTcpEndpoint( ThreadPool tp ) {
this.tp=tp;
}
// -------------------- Configuration --------------------
public void setMaxThreads(int maxThreads) {
if( maxThreads > 0)
tp.setMaxThreads(maxThreads);
}
public int getMaxThreads() {
return tp.getMaxThreads();
}
public void setMaxSpareThreads(int maxThreads) {
if(maxThreads > 0)
tp.setMaxSpareThreads(maxThreads);
}
public int getMaxSpareThreads() {
return tp.getMaxSpareThreads();
}
public void setMinSpareThreads(int minThreads) {
if(minThreads > 0)
tp.setMinSpareThreads(minThreads);
}
public int getMinSpareThreads() {
return tp.getMinSpareThreads();
}
public void setThreadPriority(int threadPriority) {
tp.setThreadPriority(threadPriority);
}
public int getThreadPriority() {
return tp.getThreadPriority();
}
public int getPort() {
return port;
}
public void setPort(int port ) {
this.port=port;
}
public InetAddress getAddress() {
return inet;
}
public void setAddress(InetAddress inet) {
this.inet=inet;
}
public void setServerSocket(ServerSocket ss) {
serverSocket = ss;
}
public void setServerSocketFactory( ServerSocketFactory factory ) {
this.factory=factory;
}
ServerSocketFactory getServerSocketFactory() {
return factory;
}
public void setConnectionHandler( TcpConnectionHandler handler ) {
this.handler=handler;
}
public TcpConnectionHandler getConnectionHandler() {
return handler;
}
public boolean isRunning() {
return running;
}
public boolean isPaused() {
return paused;
}
/**
* Allows the server developer to specify the backlog that
* should be used for server sockets. By default, this value
* is 100.
*/
public void setBacklog(int backlog) {
if( backlog>0)
this.backlog = backlog;
}
public int getBacklog() {
return backlog;
}
/**
* Sets the timeout in ms of the server sockets created by this
* server. This method allows the developer to make servers
* more or less responsive to having their server sockets
* shut down.
*
* <p>By default this value is 1000ms.
*/
public void setServerTimeout(int timeout) {
this.serverTimeout = timeout;
}
public boolean getTcpNoDelay() {
return tcpNoDelay;
}
public void setTcpNoDelay( boolean b ) {
tcpNoDelay=b;
}
public int getSoLinger() {
return linger;
}
public void setSoLinger( int i ) {
linger=i;
}
public int getSoTimeout() {
return socketTimeout;
}
public void setSoTimeout( int i ) {
socketTimeout=i;
}
public int getServerSoTimeout() {
return serverTimeout;
}
public void setServerSoTimeout( int i ) {
serverTimeout=i;
}
public String getStrategy() {
if (lf) {
return "lf";
} else {
return "ms";
}
}
public void setStrategy(String strategy) {
if ("ms".equals(strategy)) {
lf = false;
} else {
lf = true;
}
}
public int getCurrentThreadCount() {
return curThreads;
}
public int getCurrentThreadsBusy() {
return curThreads - workerThreads.size();
}
// -------------------- Public methods --------------------
public void initEndpoint() throws IOException, InstantiationException {
try {
if(factory==null)
factory=ServerSocketFactory.getDefault();
if(serverSocket==null) {
try {
if (inet == null) {
serverSocket = factory.createSocket(port, backlog);
} else {
serverSocket = factory.createSocket(port, backlog, inet);
}
} catch ( BindException be ) {
throw new BindException(be.getMessage() + ":" + port);
}
}
if( serverTimeout >= 0 )
serverSocket.setSoTimeout( serverTimeout );
} catch( IOException ex ) {
throw ex;
} catch( InstantiationException ex1 ) {
throw ex1;
}
initialized = true;
}
public void startEndpoint() throws IOException, InstantiationException {
if (!initialized) {
initEndpoint();
}
if (lf) {
tp.start();
}
running = true;
paused = false;
if (lf) {
listener = new LeaderFollowerWorkerThread(this);
tp.runIt(listener);
} else {
maxThreads = getMaxThreads();
threadStart();
}
}
public void pauseEndpoint() {
if (running && !paused) {
paused = true;
unlockAccept();
}
}
public void resumeEndpoint() {
if (running) {
paused = false;
}
}
public void stopEndpoint() {
if (running) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -