欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MSM源代码分析

程序员文章站 2022-06-21 10:44:47
...
memcached session manager(MSM) 用集中式缓存方式解决了集群环境的session共享
MSM用MemcachedBackupSessionManager类替换了StandardManager

1 MSM通过两个Valve拦截Request请求实现了对Session的操作
RequestTrackingHostValve
RequestTrackingContextValve

这两个Valve是在startInternal()方法中加载的 如下代码
        _trackingHostValve = new RequestTrackingHostValve(_requestUriIgnorePattern, sessionCookieName, this, _statistics, _enabled, _currentRequest);
_manager.getContainer().getParent().getPipeline().addValve(_trackingHostValve);
_trackingContextValve = new RequestTrackingContextValve(sessionCookieName, this);
_manager.getContainer().getPipeline().addValve( _trackingContextValve );


先看RequestTrackingHostValve做了什么?
    @Override
public void invoke( final Request request, final Response response ) throws IOException, ServletException {

final String requestId = getURIWithQueryString( request );
if(!_enabled.get() || !_msmContext.equals(request.getContext())) {
getNext().invoke( request, response );
} else if ( _ignorePattern != null && _ignorePattern.matcher( requestId ).matches() ) {
if(_log.isDebugEnabled()) {
_log.debug( ">>>>>> Ignoring: " + requestId + " (requestedSessionId "+ request.getRequestedSessionId() +") ==================" );
}

try {
storeRequestThreadLocal( request );
request.setNote(REQUEST_IGNORED, Boolean.TRUE);
getNext().invoke( request, response );
} finally {
if(request.getNote(REQUEST_PROCESSED) == Boolean.TRUE) {
final String sessionId = getSessionId(request, response);
if(sessionId != null) {
_sessionBackupService.requestFinished(sessionId, requestId);
}
}
resetRequestThreadLocal();
}
if(_log.isDebugEnabled()) {
_log.debug( "<<<<<< Ignored: " + requestId + " ==================" );
}
} else {

request.setNote(REQUEST_PROCESS, Boolean.TRUE);

if ( _log.isDebugEnabled() ) {
_log.debug( ">>>>>> Request starting: " + requestId + " (requestedSessionId "+ request.getRequestedSessionId() +") ==================" );
}
//请求开始,注意这里并没有从memcached获取session,而是在用户调用request.getSession()才从memcached获取
try {
storeRequestThreadLocal( request ); //保存request到ThreadLocal
getNext().invoke( request, response ); //调用下一个Valve
} finally {
final Boolean sessionIdChanged = (Boolean) request.getNote(SESSION_ID_CHANGED);
//在请求结束时保存session到memcached
backupSession( request, response, sessionIdChanged == null ? false : sessionIdChanged.booleanValue() );
resetRequestThreadLocal();
}

if ( _log.isDebugEnabled() ) {
logDebugRequestSessionCookie( request );
logDebugResponseCookie( response );
_log.debug( "<<<<<< Request finished: " + requestId + " ==================" );
}

}
}