Issue #1841 reduce ServletHolder contention

This commit is contained in:
Greg Wilkins 2017-09-26 16:40:49 +10:00
parent 4a70e84f24
commit a7891b2f52
1 changed files with 57 additions and 51 deletions

View File

@ -400,9 +400,11 @@ public class ServletHolder extends Holder<Servlet> implements UserIdentity.Scope
_config=new Config(); _config=new Config();
synchronized (this)
{
if (_class!=null && javax.servlet.SingleThreadModel.class.isAssignableFrom(_class)) if (_class!=null && javax.servlet.SingleThreadModel.class.isAssignableFrom(_class))
_servlet = new SingleThreadedWrapper(); _servlet = new SingleThreadedWrapper();
}
} }
@ -484,6 +486,12 @@ public class ServletHolder extends Holder<Servlet> implements UserIdentity.Scope
*/ */
public synchronized Servlet getServlet() public synchronized Servlet getServlet()
throws ServletException throws ServletException
{
Servlet servlet=_servlet;
if (servlet!=null && _unavailable==0)
return servlet;
synchronized(this)
{ {
// Handle previous unavailability // Handle previous unavailability
if (_unavailable!=0) if (_unavailable!=0)
@ -494,9 +502,23 @@ public class ServletHolder extends Holder<Servlet> implements UserIdentity.Scope
_unavailableEx=null; _unavailableEx=null;
} }
if (_servlet==null) servlet=_servlet;
if (servlet!=null)
return servlet;
if (isRunning())
{
if (_class == null)
throw new UnavailableException("Servlet Not Initialized");
if (_unavailable != 0 || !_initOnStartup)
initServlet(); initServlet();
return _servlet; servlet=_servlet;
if (servlet == null)
throw new UnavailableException("Could not instantiate " + _class);
}
return servlet;
}
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
@ -504,9 +526,15 @@ public class ServletHolder extends Holder<Servlet> implements UserIdentity.Scope
* @return The servlet or null * @return The servlet or null
*/ */
public Servlet getServletInstance() public Servlet getServletInstance()
{
Servlet servlet=_servlet;
if (servlet!=null)
return servlet;
synchronized(this)
{ {
return _servlet; return _servlet;
} }
}
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
/** /**
@ -604,7 +632,7 @@ public class ServletHolder extends Holder<Servlet> implements UserIdentity.Scope
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
private void initServlet() private synchronized void initServlet()
throws ServletException throws ServletException
{ {
Object old_run_as = null; Object old_run_as = null;
@ -767,36 +795,17 @@ public class ServletHolder extends Holder<Servlet> implements UserIdentity.Scope
protected void prepare (Request baseRequest, ServletRequest request, ServletResponse response) protected void prepare (Request baseRequest, ServletRequest request, ServletResponse response)
throws ServletException, UnavailableException throws ServletException, UnavailableException
{ {
ensureInstance(); getServlet();
MultipartConfigElement mpce = ((Registration)getRegistration()).getMultipartConfig(); MultipartConfigElement mpce = ((Registration)getRegistration()).getMultipartConfig();
if (mpce != null) if (mpce != null)
baseRequest.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, mpce); baseRequest.setAttribute(Request.__MULTIPART_CONFIG_ELEMENT, mpce);
} }
@Deprecated
public Servlet ensureInstance() public Servlet ensureInstance()
throws ServletException, UnavailableException throws ServletException, UnavailableException
{ {
if (!isStarted()) return getServlet();
throw new UnavailableException("Servlet not initialized", -1);
Servlet servlet=_servlet;
if (servlet!=null && _unavailable==0)
return servlet;
synchronized(this)
{
servlet=_servlet;
if (servlet!=null)
return servlet;
if (_class == null)
throw new UnavailableException("Servlet Not Initialized");
if (_unavailable != 0 || (!_initOnStartup && servlet == null))
servlet = getServlet();
if (servlet == null)
throw new UnavailableException("Could not instantiate " + _class);
return servlet;
}
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
@ -820,7 +829,7 @@ public class ServletHolder extends Holder<Servlet> implements UserIdentity.Scope
if (_class==null) if (_class==null)
throw new UnavailableException("Servlet Not Initialized"); throw new UnavailableException("Servlet Not Initialized");
Servlet servlet = ensureInstance(); Servlet servlet = getServlet();
// Service the request // Service the request
Object old_run_as = null; Object old_run_as = null;
@ -865,26 +874,23 @@ public class ServletHolder extends Holder<Servlet> implements UserIdentity.Scope
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
private boolean isJspServlet () protected boolean isJspServlet ()
{ {
if (_servlet == null) Servlet servlet = getServletInstance();
return false; Class<?> c = servlet==null?_class:servlet.getClass();
Class<?> c = _servlet.getClass(); while (c != null)
boolean result = false;
while (c != null && !result)
{ {
result = isJspServlet(c.getName()); if (isJspServlet(c.getName()))
return true;
c = c.getSuperclass(); c = c.getSuperclass();
} }
return false;
return result;
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
private boolean isJspServlet (String classname) protected boolean isJspServlet (String classname)
{ {
if (classname == null) if (classname == null)
return false; return false;