tomcat源码阅读_代码篇10
2010-03-30 22:28
323 查看
JIoEndpoint类处理TCP连接,实现一个简单的服务器模型:一个线程监听套接字并为每个传入的连接创建一个新的工作线程。
该类的init方法在上一篇日志中已经介绍,接下来是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,启动acceptorThreadCount个线程用于处理请求。
for (int i = 0; i < acceptorThreadCount; i++) {
Thread acceptorThread = new Thread(new Acceptor(), getName() + "-Acceptor-" + i);
acceptorThread.setPriority(threadPriority);
acceptorThread.setDaemon(daemon);
acceptorThread.start();
}
}
}
Acceptor类为该类的内部类:
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 = 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使用的是processSocket(socket))方法。该方法如下:
protected boolean processSocket(Socket socket) {
try {
if (executor == null) {
getWorkerThread().assign(socket);//获得一个Worker并将socket分配给它,由它来处理。Worker类也是本类的内部类,使用堆栈结构来进行存储。
} 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;
}
在worker中的run方法中,使用handler.process(socket)来对socket进行处理,而handler就是Http11Processor类。
该类用来处理HTTP请求。
该类的init方法在上一篇日志中已经介绍,接下来是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,启动acceptorThreadCount个线程用于处理请求。
for (int i = 0; i < acceptorThreadCount; i++) {
Thread acceptorThread = new Thread(new Acceptor(), getName() + "-Acceptor-" + i);
acceptorThread.setPriority(threadPriority);
acceptorThread.setDaemon(daemon);
acceptorThread.start();
}
}
}
Acceptor类为该类的内部类:
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 = 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使用的是processSocket(socket))方法。该方法如下:
protected boolean processSocket(Socket socket) {
try {
if (executor == null) {
getWorkerThread().assign(socket);//获得一个Worker并将socket分配给它,由它来处理。Worker类也是本类的内部类,使用堆栈结构来进行存储。
} 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;
}
在worker中的run方法中,使用handler.process(socket)来对socket进行处理,而handler就是Http11Processor类。
该类用来处理HTTP请求。
相关文章推荐
- tomcat源码阅读_代码篇3
- tomcat源码阅读10
- tomcat源码阅读_代码篇8
- tomcat源码阅读_代码篇4
- tomcat源码阅读_代码篇1
- tomcat源码阅读_代码篇7
- spark源码阅读一-spark读写文件代码分析
- Tomcat连接器运行过程(源码阅读)
- tomcat源码阅读-2
- Tomcat源码阅读之Engine启动过程
- C#远程开机代码 【局域网测试通过】 分类: .NET 2011-10-26 11:45 2415人阅读 评论(1) 收藏
- Tomcat源码阅读之Server.xml文件的处理与Catalina启动流程
- python源码分析阅读理解chapter01~05 (纯阅读书籍,代码未看)
- tomcat源码阅读步骤五
- tomcat源码阅读步骤十一
- tensorflow cifar_10 代码阅读与理解
- Tomcat源码阅读之Mapper分析
- tomcat源码阅读笔记一
- tomcat源码阅读26
- tomcat阅读源码中涉及的知识点---自定义类加载器