浅析Tomcat之AprEndpoint

APR是Apache可移植运行库(Apache portable Run-time libraries)的简称,这是一个可以跨越多操作系统平台使用的底层支持接口库.在Tomcat的实现中就有使用该库的地方,AprEndpoint就是将Socket方面的操作直接用JNI替代而非Java元素的API.这使得其有较高的性能要求.同样AprEndpoint也是继承了AbstractEndpoint在实现的具体细节有点差别而已.只是另一种JIoEndpoint.提供了Acceptor线程,Socket轮询线程,Worker线程池和文件发送线程.

先看看Acceptor的实现.于JIoEndpoint有着极其相似的代码结构.

public void run() {

	int errorDelay = 0;

	// Loop until we receive a shutdown command
	while (running) {

		// Loop if endpoint is paused
		while (paused && running) {
			state = AcceptorState.PAUSED;
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				// Ignore
			}
		}

		if (!running) {
			break;
		}
		state = AcceptorState.RUNNING;

		try {
			//if we have reached max connections, wait
			countUpOrAwaitConnection();

			long socket = 0;
			try {
				// Accept the next incoming connection from the server
				// socket
				socket = Socket.accept(serverSock);
			} catch (Exception e) {
				//we didn't get a socket
				countDownConnection();
				// Introduce delay if necessary
				errorDelay = handleExceptionWithDelay(errorDelay);
				// re-throw
				throw e;
			}
			// Successful accept, reset the error delay
			errorDelay = 0;

			if (running && !paused) {
				// Hand this socket off to an appropriate processor
				if (!processSocketWithOptions(socket)) {
					// Close socket and pool right away
					destroySocket(socket);
				}
			} else {
				// Close socket and pool right away
				destroySocket(socket);
			}
		} catch (Throwable t) {
			....
		}
		// The processor will recycle itself when it finishes
	}
	state = AcceptorState.ENDED;
}

   其中的socket = Socket.accept(serverSocket)就是使用的JNI,也就是APR库.而这个类的所有Socket的实现也是使用该库的.至于其他线程也是使用了该库.看下生命周期的bind方法的部分代码,里面用了更多的APR的函数.大体的内容也是一致的.只是在Socket实现的API不一样而已.至于NIoEndpoint也是类似的实现.只不过实现方式是ServerSockeChannel,SockeChannel和Selector的就绪选择.

 

 

首发于泛泛之辈 - http://www.lihongkun.com/archives/116

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值