|
22 | 22 | package org.mobicents.servlet.sip.catalina;
|
23 | 23 |
|
24 | 24 | import org.apache.catalina.Context;
|
25 |
| -import org.apache.catalina.LifecycleException; |
26 | 25 | import org.apache.catalina.core.StandardContext;
|
27 | 26 | import org.apache.log4j.Logger;
|
| 27 | +import org.mobicents.javax.servlet.ContainerListener; |
| 28 | +import org.mobicents.javax.servlet.GracefulShutdownCheckEvent; |
28 | 29 | import org.mobicents.servlet.sip.core.SipContext;
|
29 | 30 |
|
30 | 31 | /**
|
31 | 32 | * @author jean.deruelle@gmail.com
|
32 | 33 | *
|
33 | 34 | */
|
34 | 35 | public class ContextGracefulStopTask implements Runnable {
|
35 |
| - private static final Logger logger = Logger.getLogger(ContextGracefulStopTask.class); |
36 |
| - Context sipContext; |
37 |
| - long timeToWait; |
38 |
| - long startTime; |
39 | 36 |
|
40 |
| - public ContextGracefulStopTask(Context context, long timeToWait) { |
41 |
| - sipContext = context; |
42 |
| - this.timeToWait = timeToWait; |
43 |
| - startTime = System.currentTimeMillis(); |
44 |
| - } |
| 37 | + private static final Logger logger = Logger.getLogger(ContextGracefulStopTask.class); |
| 38 | + Context sipContext; |
| 39 | + long timeToWait; |
| 40 | + long startTime; |
| 41 | + |
| 42 | + public ContextGracefulStopTask(Context context, long timeToWait) { |
| 43 | + sipContext = context; |
| 44 | + this.timeToWait = timeToWait; |
| 45 | + startTime = System.currentTimeMillis(); |
| 46 | + } |
| 47 | + |
| 48 | + private static final String PREVENT_PREMATURE_SHUTDOWN = "org.mobicents.servlet.sip.PREVENT_PREMATURE_SHUTDOWN"; |
| 49 | + |
| 50 | + @Override |
| 51 | + public void run() { |
| 52 | + int numberOfActiveSipApplicationSessions = ((SipContext) sipContext).getSipManager().getActiveSipApplicationSessions(); |
| 53 | + int numberOfActiveHttpSessions = sipContext.getManager().getActiveSessions(); |
| 54 | + if (logger.isTraceEnabled()) { |
| 55 | + logger.trace("ContextGracefulStopTask running for context " + sipContext.getName() + ", number of Sip Application Sessions still active " + numberOfActiveSipApplicationSessions + " number of HTTP Sessions still active " + numberOfActiveHttpSessions); |
| 56 | + } |
| 57 | + |
| 58 | + boolean stopPrematuraly = false; |
| 59 | + long currentTime = System.currentTimeMillis(); |
| 60 | + // if timeToWait is positive, then we check the time since the task started, if the time is greater than timeToWait we can safely stop the context |
| 61 | + long elapsedTime = currentTime - startTime; |
| 62 | + if (timeToWait > 0 && (elapsedTime > timeToWait)) { |
| 63 | + logger.info("Graceful TimeToWait Consumed."); |
| 64 | + stopContext(); |
| 65 | + } |
| 66 | + if (logger.isDebugEnabled()) { |
| 67 | + logger.debug("ContextGracefulStopTask running for context " + sipContext.getName() |
| 68 | + + ", number of Sip Application Sessions still active " + numberOfActiveSipApplicationSessions |
| 69 | + + " number of HTTP Sessions still active " + numberOfActiveHttpSessions |
| 70 | + + ", stopPrematurely " + stopPrematuraly); |
| 71 | + } |
| 72 | + if (numberOfActiveSipApplicationSessions <= 0 |
| 73 | + && numberOfActiveHttpSessions <= 0) { |
| 74 | + logger.info("No more active sessions, lets check with service"); |
| 75 | + boolean servicePremature = true; |
| 76 | + ContainerListener containerListener = ((SipContext) sipContext).getListeners().getContainerListener(); |
| 77 | + if (containerListener != null) { |
| 78 | + GracefulShutdownCheckEvent event = new GracefulShutdownCheckEvent(elapsedTime, timeToWait); |
| 79 | + ((SipContext) sipContext).getListeners().callbackContainerListener(event); |
| 80 | + servicePremature = sipContext.getServletContext().getAttribute(PREVENT_PREMATURE_SHUTDOWN) == null; |
| 81 | + logger.info("servicePremature=" + servicePremature); |
| 82 | + } |
| 83 | + if (servicePremature) { |
| 84 | + stopContext(); |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private void stopContext() { |
| 91 | + try { |
| 92 | + logger.info("About to stop the context."); |
| 93 | + ((StandardContext) sipContext).stop(); |
| 94 | + } catch (Exception e) { |
| 95 | + logger.error("Couldn't gracefully stop context " + sipContext.getName(), e); |
| 96 | + } |
| 97 | + } |
45 | 98 |
|
46 |
| - public void run() { |
47 |
| - int numberOfActiveSipApplicationSessions = ((SipContext)sipContext).getSipManager().getActiveSipApplicationSessions(); |
48 |
| - int numberOfActiveHttpSessions = sipContext.getManager().getActiveSessions(); |
49 |
| - if(logger.isTraceEnabled()) { |
50 |
| - logger.trace("ContextGracefulStopTask running for context " + sipContext.getName() + ", number of Sip Application Sessions still active " + numberOfActiveSipApplicationSessions + " number of HTTP Sessions still active " + numberOfActiveHttpSessions); |
51 |
| - } |
52 |
| - boolean stopPrematuraly = false; |
53 |
| - long currentTime = System.currentTimeMillis(); |
54 |
| - // if timeToWait is positive, then we check the time since the task started, if the time is greater than timeToWait we can safely stop the context |
55 |
| - if(timeToWait > 0 && ((currentTime - startTime) > timeToWait)) { |
56 |
| - stopPrematuraly = true; |
57 |
| - } |
58 |
| - if((numberOfActiveSipApplicationSessions <= 0 && numberOfActiveHttpSessions <= 0) || stopPrematuraly) { |
59 |
| - try { |
60 |
| - ((StandardContext)sipContext).stop(); |
61 |
| - } catch (LifecycleException e) { |
62 |
| - logger.error("Couldn't gracefully stop context " + sipContext.getName(), e); |
63 |
| - } |
64 |
| - } |
65 |
| - } |
66 | 99 | }
|
0 commit comments