APR为JBoss加速主要通过基于APR和JNI(Java Native Interface)的Connector实现。具体包括:Connector、JNI和集成。
Connector
1、 基于APR的HTTP11 Connector实现
org.apache.coyote.http11. Http11NioProcessor
org.apache.coyote.http11. Http11AprProtocol
2、 基于APR的AJP Connector实现
org.apache.coyote.ajp. AjpAprProcessor
org.apache.coyote.ajp. AjpAprProtocol
JNI实现
1、 Connector
Java接口:org.apache.tomcat.jni.Library
C实现:native connector的src目录
2、 SSL
Java接口:org.apache.tomcat.jni.SSL
C实现:native connector的src目录
APR集成
1、 JNI库的初始化和清除
APR的初始化通过Server的LifecycleListener接口实现,配置在server.xml中,xml片断如下:
<Server>
<!--APR library loader. Documentation at /docs/apr.html -->
<Listener className=
"org.apache.catalina.core.AprLifecycleListener"
SSLEngine=
"off"
/>
……
JNI库在Server的Lifecycle的INI_EVENT时进行初始化,在AFTER_STOP_EVENT时进行清除,具体代码如下:
/**
* Primary entry point for startup and shutdown events.
*
*
@param event The event that has occurred
*/
public void
lifecycleEvent(LifecycleEvent event) {
if
(Lifecycle.INIT_EVENT.equals(event.getType())) {
aprInitialized = init();
if
(aprInitialized) {
try
{
initializeSSL();
}
catch
(Throwable t) {
if
(!log.isDebugEnabled()) {
log.info(sm.getString(
"aprListener.sslInit"
));
}
else
{
log.debug(sm.getString(
"aprListener.sslInit"
));
}
}
}
}
else if
(Lifecycle.AFTER_STOP_EVENT.equals(event.getType())) {
if
(!aprInitialized) {
return
;
}
try
{
terminateAPR();
}
catch
(Throwable t) {
if
(!log.isDebugEnabled()) {
log.info(sm.getString(
"aprListener.aprDestroy"
));
}
else
{
log.debug(sm.getString(
"aprListener.aprDestroy"
), t);
}
}
}
}
2、 Connector选用
public Connector(String protocol)
throws Exception {
setProtocol(protocol);
// Instantiate protocol handler
try {
Class clazz = Class.forName(protocolHandlerClassName);
this.protocolHandler = (ProtocolHandler) clazz.newInstance();
}
catch (Exception e) {
log.error
(sm.getString
(
"coyoteConnector.protocolHandlerInstantiationFailed", e));
}
}
/**
* Set the Coyote protocol which will be used by the connector.
*
*
@param
protocol The Coyote protocol name
*/
public void
setProtocol(String protocol) {
// Test APR support
initializeAPR();
if
(aprInitialized) {
if
(
"HTTP/1.1"
.equals(protocol)) {
setProtocolHandlerClassName
(
"org.apache.coyote.http11.Http11AprProtocol"
);
}
else if
(
"AJP/1.3"
.equals(protocol)) {
setProtocolHandlerClassName
(
"org.apache.coyote.ajp.AjpAprProtocol"
);
}
else if
(protocol !=
null
) {
setProtocolHandlerClassName(protocol);
}
else
{
setProtocolHandlerClassName
(
"org.apache.coyote.http11.Http11AprProtocol"
);
}
}
else
{
if
(
"HTTP/1.1"
.equals(protocol)) {
setProtocolHandlerClassName
(
"org.apache.coyote.http11.Http11Protocol"
);
}
else if
(
"AJP/1.3"
.equals(protocol)) {
setProtocolHandlerClassName
(
"org.apache.jk.server.JkCoyoteHandler"
);
}
else if
(protocol !=
null
) {
setProtocolHandlerClassName(protocol);
}
}
}
说明:Connector构造函数调用setProtocol函数设置Connector处理请求的Protocol Handler。在setProtocol函数中,只要能够成功地初始化APR,将使用基于APR的实现的
org.apache.coyote.http11.Http11AprProtocol和
org.apache.coyote.ajp.AjpAprProtocol作为请求处理的Protocol Handler。