三个类加载器
//三个类加载器如何初始化 为啥要定义这三个类加载器
protected ClassLoader commonLoader = null;
protected ClassLoader catalinaLoader = null;
protected ClassLoader sharedLoader = null;
initClassLoaders方法()
private void initClassLoaders() {
try {
commonLoader = createClassLoader("common", null);
if( commonLoader == null ) {
// no config file, default to this loader - we might be in a 'single' env.
commonLoader=this.getClass().getClassLoader();
}
catalinaLoader = createClassLoader("server", commonLoader);
sharedLoader = createClassLoader("shared", commonLoader);
} catch (Throwable t) {
log.error("Class loader creation threw exception", t);
System.exit(1);
}
}
catalina.properties配置
common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar
server.loader=
shared.loader=
从不同配置加载jar包创建加载器 用于不同用途
ClassLoaderFactory.java 类加载器工厂
StandardClassLoader
main方法片段
String command = "start";
if (args.length > 0) {
command = args[args.length - 1];
}
if (command.equals("startd")) {
args[args.length - 1] = "start";
daemon.load(args);
//启动方法入口
daemon.start();
}
start()方法调用
public void start()
throws Exception {
if( catalinaDaemon==null ) init();
//catalinaDaemon是通过反射生成的Catalinal类对象
//再通过反射调用Catalina.start()
Method method = catalinaDaemon.getClass().getMethod("start", (Class [] )null);
method.invoke(catalinaDaemon, (Object [])null);
}
Bootstrap.main() -> start() -> Catalina.start() -> load()
->load()
--> createStartDigester() 生成默认server.xml xml配置结构
--> 读取server.xml配置文件
--> getServer()).start() -> StandServer.start()
-> StandServer.start() -> StandService.start()
-> StandService.start()
--> container[i].start() StandardEngine.start() -> StandardHost.start() - > 启动背景线程ContainerBackgroundProcessor --> executors[i].start() StandardThreadExecutor.start() --> connectors[i].start() Http11Protocol.start()
Http11Protocol.start()启动流程 Http11Protocol.start() -> JIoEndpoint.start() ->
JIoEndpoint.start()方法
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++) {
//创建监听socket请求线程 等待请求 Acceptor是个内部类
Thread acceptorThread = new Thread(new Acceptor(), getName() + "-Acceptor-" + i);
acceptorThread.setPriority(threadPriority);
acceptorThread.setDaemon(daemon);
acceptorThread.start();
}
}
}
protected class Acceptor implements Runnable {
/**
* The background thread that listens for incoming TCP/IP connections and
* hands them off to an appropriate processor.
*/
public void run() {
// Loop until we receive a shutdown command
while (running) {
// Loop if endpoint is paused
while (paused) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// Ignore
}
}
// Accept the next incoming connection from the server socket
try {
//Socket处理逻辑
Socket socket = serverSocketFactory.acceptSocket(serverSocket);
serverSocketFactory.initSocket(socket);
// Hand this socket off to an appropriate processor
if (!processSocket(socket)) {
// Close socket right away
try {
socket.close();
} catch (IOException e) {
// Ignore
}
}
}catch ( IOException x ) {
if ( running ) log.error(sm.getString("endpoint.accept.fail"), x);
} catch (Throwable t) {
log.error(sm.getString("endpoint.accept.fail"), t);
}
// The processor will recycle itself when it finishes
}
}
}
Socket处理实现线程
protected class SocketProcessor implements Runnable {
protected Socket socket = null;
public SocketProcessor(Socket socket) {
this.socket = socket;
}
public void run() {
// Process the request from this socket
//具体处理实现由Http11ConnectionHandler来完成
if (!setSocketOptions(socket) || !handler.process(socket)) {
// Close socket
try {
socket.close();
} catch (IOException e) {
}
}
// Finish up this request
socket = null;
}
}
到此 整个tomcat启动流程代码阅读完毕 ,以上贴的代码只是为我自己方便做个记录