Si nuestro servlet tiene peticiones de servicio potencialmente largas, debemos utilizar las técnicas de esta lección para:
public ShutdownExample extends HttpServlet {
private int serviceCounter = 0;
...
//Access methods for serviceCounter
protected synchronized void enteringServiceMethod() {
serviceCounter++;
}
protected synchronized void leavingServiceMethod() {
serviceCounter--;
}
protected synchronized int numServices() {
return serviceCounter;
}
}
El método service debería incrementar el contador de servicios cada vez que se entre en él y decrementarlo cada vez que se salga de él. Esta es una de las pocas veces que al subclasificar la clase
HttpServlet debamos sobreescribir el método service. El nuevo método debería llamar al super.service para preservar la funcionalidad del método HttpServlet.service original.
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
enteringServiceMethod();
try {
super.service(req, resp);
} finally {
leavingServiceMethod();
}
}
public ShutdownExample extends HttpServlet {
private Boolean shuttingDown;
...
//Access methods for shuttingDown
protected setShuttingDown(Boolean flag) {
shuttingDown = flag;
}
protected Boolean isShuttingDown() {
return shuttingDown;
}
}
Abajo podemos ver un método destroy que utiliza estos campos para proporcionar una limpieza de desconexión:
public void destroy() {
/* Check to see whether there are still service methods running,
* and if there are, tell them to stop. */
if (numServices() > 0) {
setShuttingDown(true);
}
/* Wait for the service methods to stop. */
while(numServices() > 0) {
try {
Thread.sleep(interval);
} catch (InterruptedException e) {
}
}
}
public void doPost(...) {
...
for(i = 0; ((i < lotsOfStuffToDo) && !isShuttingDown()); i++) {
try {
partOfLongRunningOperation(i);
} catch (InterruptedException e) {
}
}
}