APR为JBoss AS加速:实现分析

  •  
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。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值