Class SimpleAsyncJpaServlet
- java.lang.Object
-
- jakarta.servlet.GenericServlet
-
- jakarta.servlet.http.HttpServlet
-
- pl.morgwai.base.servlet.guiced.jpa.JpaServlet
-
- pl.morgwai.base.servlet.guiced.jpa.SimpleAsyncJpaServlet
-
- All Implemented Interfaces:
Servlet
,ServletConfig
,Serializable
public abstract class SimpleAsyncJpaServlet extends JpaServlet
Dispatches request handling tothe executor
associated with the configured persistence unit. This prevents requests awaiting for an available JDBC connection from blocking server threads. This way the total number of server's threads can remain constant and small regardless of the number of concurrent requests, while the JDBC connection pool will be optimally utilized.Base class for servlets that do not perform synchronous time consuming operations other than JPA related calls.
- See Also:
- Serialized Form
-
-
Field Summary
-
Fields inherited from class pl.morgwai.base.servlet.guiced.jpa.JpaServlet
containerCallContextTracker, entityManagerProvider, jpaExecutor
-
-
Constructor Summary
Constructors Constructor Description SimpleAsyncJpaServlet()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected long
getAsyncContextTimeout()
Returns timeout forAsyncContext.setTimeout(long)
; Negative number indicates thatAsyncContext.setTimeout(long)
should not be called in which case container default will take effect.protected void
service(HttpServletRequest request, HttpServletResponse response)
Dispatches request handling toJpaServlet.jpaExecutor
.protected boolean
shouldCallAsyncContextComplete(HttpServletRequest request)
WhetherAsyncContext.complete()
should be called automatically at the end of request processing.protected AsyncContext
startAsync(HttpServletRequest request, HttpServletResponse response)
StartsAsyncContext
.-
Methods inherited from class pl.morgwai.base.servlet.guiced.jpa.JpaServlet
executeWithinTx, executeWithinTx, getPersistenceUnitBindingName, init, removeEntityManagerFromRequestScope
-
Methods inherited from class jakarta.servlet.http.HttpServlet
doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service
-
Methods inherited from class jakarta.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
-
-
-
-
Method Detail
-
service
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
Dispatches request handling toJpaServlet.jpaExecutor
. Closes the obtainedEntityManager
at the end. By default also callsAsyncContext.complete()
: if a subclass wants to dispatch processing back to the container viaAsyncContext.dispatch(String)
or to another executor, thenshouldCallAsyncContextComplete(HttpServletRequest)
should be overridden to returnfalse
.If the invoked
doXXX
method throws an exception, then, unless it's anIOException
(indicating broken connection), it's logged at levelERROR
and an attempt to sendHttpServletResponse.SC_INTERNAL_SERVER_ERROR
is made.
IOException
s are logged at levelDEBUG
.
Error
s are additionally re-thrown after being logged.- Overrides:
service
in classHttpServlet
- Throws:
ServletException
IOException
-
getAsyncContextTimeout
protected long getAsyncContextTimeout()
Returns timeout forAsyncContext.setTimeout(long)
; Negative number indicates thatAsyncContext.setTimeout(long)
should not be called in which case container default will take effect. By default0
(meaning no timeout).NOTE: combining non-container threads with
AsyncContext
timeout mechanism requires proper synchronization between main request processing code (running onJpaServlet.jpaExecutor
thread andAsyncListener.onTimeout(AsyncEvent)
(running on container thread) and may result in the main processing code throwing harmless exceptions in case of timeouts even if the processing was discontinued (for example as of Jetty 10.0.x, if response output was obtained, an interceptor at the end of a givendoXXX()
method will throw an exception when trying to close it for the 2nd time).
-
shouldCallAsyncContextComplete
protected boolean shouldCallAsyncContextComplete(HttpServletRequest request)
WhetherAsyncContext.complete()
should be called automatically at the end of request processing. By defaulttrue
. Should be overridden if processing is dispatched back to the container viaAsyncContext.dispatch(String)
or to another executor,
-
startAsync
protected AsyncContext startAsync(HttpServletRequest request, HttpServletResponse response)
StartsAsyncContext
. By default callsServletRequest.startAsync()
. Can be overridden ifServletRequest.startAsync(jakarta.servlet.ServletRequest, jakarta.servlet.ServletResponse)
needs to be used.
-
-