Skip to content

Commit 900bd57

Browse files
committed
ported from ts8. gracefulShutdown and dispatcherFSM for AS10
1 parent a6e4ef3 commit 900bd57

File tree

6 files changed

+184
-53
lines changed

6 files changed

+184
-53
lines changed

Diff for: containers/sip-servlets-catalina-7/src/main/java/org/mobicents/servlet/sip/catalina/ContextGracefulStopTask.java

+63-30
Original file line numberDiff line numberDiff line change
@@ -22,45 +22,78 @@
2222
package org.mobicents.servlet.sip.catalina;
2323

2424
import org.apache.catalina.Context;
25-
import org.apache.catalina.LifecycleException;
2625
import org.apache.catalina.core.StandardContext;
2726
import org.apache.log4j.Logger;
27+
import org.mobicents.javax.servlet.ContainerListener;
28+
import org.mobicents.javax.servlet.GracefulShutdownCheckEvent;
2829
import org.mobicents.servlet.sip.core.SipContext;
2930

3031
/**
3132
* @author jean.deruelle@gmail.com
3233
*
3334
*/
3435
public class ContextGracefulStopTask implements Runnable {
35-
private static final Logger logger = Logger.getLogger(ContextGracefulStopTask.class);
36-
Context sipContext;
37-
long timeToWait;
38-
long startTime;
3936

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+
}
4598

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-
}
6699
}

Diff for: containers/sip-servlets-catalina-7/src/main/java/org/mobicents/servlet/sip/catalina/SipProtocolHandler.java

+36
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,42 @@ public void setUseLoadBalancer(boolean useLoadBalancer) {
535535
sipConnector.setUseLoadBalancer(useLoadBalancer);
536536
}
537537

538+
public String getLoadBalancerAddress() {
539+
return sipConnector.getLoadBalancerAddress();
540+
}
541+
542+
/**
543+
* @param loadBalancerAddress the loadBalancerAddress to set
544+
*/
545+
public void setLoadBalancerAddress(String loadBalancerAddress) {
546+
sipConnector.setLoadBalancerAddress(loadBalancerAddress);
547+
}
548+
549+
/**
550+
* @return the loadBalancerRmiPort
551+
*/
552+
public int getLoadBalancerRmiPort() {
553+
return sipConnector.getLoadBalancerRmiPort();
554+
}
555+
556+
/**
557+
* @param loadBalancerRmiPort the loadBalancerRmiPort to set
558+
*/
559+
public void setLoadBalancerRmiPort(int loadBalancerRmiPort) {
560+
sipConnector.setLoadBalancerRmiPort(loadBalancerRmiPort);
561+
}
562+
563+
public int getLoadBalancerSipPort() {
564+
return sipConnector.getLoadBalancerSipPort();
565+
}
566+
567+
/**
568+
* @param loadBalancerSipPort the loadBalancerSipPort to set
569+
*/
570+
public void setLoadBalancerSipPort(int loadBalancerSipPort) {
571+
sipConnector.setLoadBalancerSipPort(loadBalancerSipPort);
572+
}
573+
538574
/**
539575
* @return the useStun
540576
*/

Diff for: containers/sip-servlets-catalina-7/src/main/java/org/mobicents/servlet/sip/catalina/SipStandardService.java

+6
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ public class SipStandardService extends StandardService implements CatalinaSipSe
142142
protected boolean dialogPendingRequestChecking = false;
143143
protected int callIdMaxLength;
144144
protected int tagHashMaxLength;
145+
private long gracefulInterval = 30000;
145146

146147
protected boolean httpFollowsSip = false;
147148
protected String jvmRoute;
@@ -1354,6 +1355,7 @@ public void stopGracefully(long timeToWait) {
13541355
Iterator<SipContext> sipContexts = sipApplicationDispatcher.findSipApplications();
13551356
while (sipContexts.hasNext()) {
13561357
SipContext sipContext = sipContexts.next();
1358+
sipContext.setGracefulInterval(gracefulInterval);
13571359
sipContext.stopGracefully(timeToWait);
13581360
}
13591361
gracefulStopFuture = sipApplicationDispatcher.getAsynchronousScheduledExecutor().scheduleWithFixedDelay(new ServiceGracefulStopTask(this), 30000, 30000, TimeUnit.MILLISECONDS);
@@ -1403,4 +1405,8 @@ public String getSasTimerServiceImplementationType() {
14031405
public void setSasTimerServiceImplementationType(String sasTimerServiceImplementationType) {
14041406
this.sasTimerServiceImplementationType = sasTimerServiceImplementationType;
14051407
}
1408+
1409+
public void setGracefulInterval(long gracefulStopTaskInterval) {
1410+
this.gracefulInterval = gracefulStopTaskInterval;
1411+
}
14061412
}

Diff for: containers/sip-servlets-catalina-8/src/main/java/org/mobicents/servlet/sip/catalina/ContextGracefulStopTask.java

+54-22
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
package org.mobicents.servlet.sip.catalina;
2323

2424
import org.apache.catalina.Context;
25-
import org.apache.catalina.LifecycleException;
2625
import org.apache.catalina.core.StandardContext;
2726
import org.apache.log4j.Logger;
27+
import org.mobicents.javax.servlet.ContainerListener;
28+
import org.mobicents.javax.servlet.GracefulShutdownCheckEvent;
2829
import org.mobicents.servlet.sip.core.SipContext;
2930

3031
/**
@@ -43,24 +44,55 @@ public ContextGracefulStopTask(Context context, long timeToWait) {
4344
startTime = System.currentTimeMillis();
4445
}
4546

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-
}
47+
private static final String PREVENT_PREMATURE_SHUTDOWN = "org.mobicents.servlet.sip.PREVENT_PREMATURE_SHUTDOWN";
48+
49+
@Override
50+
public void run() {
51+
int numberOfActiveSipApplicationSessions = ((SipContext) sipContext).getSipManager().getActiveSipApplicationSessions();
52+
int numberOfActiveHttpSessions = sipContext.getManager().getActiveSessions();
53+
if (logger.isTraceEnabled()) {
54+
logger.trace("ContextGracefulStopTask running for context " + sipContext.getName() + ", number of Sip Application Sessions still active " + numberOfActiveSipApplicationSessions + " number of HTTP Sessions still active " + numberOfActiveHttpSessions);
55+
}
56+
57+
boolean stopPrematuraly = false;
58+
long currentTime = System.currentTimeMillis();
59+
// 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
60+
long elapsedTime = currentTime - startTime;
61+
if (timeToWait > 0 && (elapsedTime > timeToWait)) {
62+
logger.info("Graceful TimeToWait Consumed.");
63+
stopContext();
64+
}
65+
if (logger.isDebugEnabled()) {
66+
logger.debug("ContextGracefulStopTask running for context " + sipContext.getName()
67+
+ ", number of Sip Application Sessions still active " + numberOfActiveSipApplicationSessions
68+
+ " number of HTTP Sessions still active " + numberOfActiveHttpSessions
69+
+ ", stopPrematurely " + stopPrematuraly);
70+
}
71+
if (numberOfActiveSipApplicationSessions <= 0
72+
&& numberOfActiveHttpSessions <= 0) {
73+
logger.info("No more active sessions, lets check with service");
74+
boolean servicePremature = true;
75+
ContainerListener containerListener = ((SipContext) sipContext).getListeners().getContainerListener();
76+
if (containerListener != null) {
77+
GracefulShutdownCheckEvent event = new GracefulShutdownCheckEvent(elapsedTime, timeToWait);
78+
((SipContext) sipContext).getListeners().callbackContainerListener(event);
79+
servicePremature = sipContext.getServletContext().getAttribute(PREVENT_PREMATURE_SHUTDOWN) == null;
80+
logger.info("servicePremature=" + servicePremature);
81+
}
82+
if (servicePremature) {
83+
stopContext();
84+
}
85+
86+
}
87+
}
88+
89+
private void stopContext() {
90+
try {
91+
logger.info("About to stop the context.");
92+
((StandardContext) sipContext).stop();
93+
} catch (Exception e) {
94+
logger.error("Couldn't gracefully stop context " + sipContext.getName(), e);
95+
}
96+
}
97+
98+
}

Diff for: containers/sip-servlets-catalina-8/src/main/java/org/mobicents/servlet/sip/catalina/SipStandardService.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -1328,10 +1328,13 @@ public void closeReliableConnection(SipConnector sipConnector, String clientAddr
13281328
clientAddress, clientPort);
13291329
}
13301330

1331+
private long gracefulInterval = 30000;
1332+
13311333
/*
13321334
* (non-Javadoc)
13331335
* @see org.mobicents.servlet.sip.core.SipService#stopGracefully(long)
13341336
*/
1337+
@Override
13351338
public void stopGracefully(long timeToWait) {
13361339
if(logger.isInfoEnabled()) {
13371340
logger.info("Stopping the Server Gracefully in " + timeToWait + " ms");
@@ -1350,9 +1353,10 @@ public void stopGracefully(long timeToWait) {
13501353
Iterator<SipContext> sipContexts = sipApplicationDispatcher.findSipApplications();
13511354
while (sipContexts.hasNext()) {
13521355
SipContext sipContext = sipContexts.next();
1356+
sipContext.setGracefulInterval(gracefulInterval);
13531357
sipContext.stopGracefully(timeToWait);
13541358
}
1355-
gracefulStopFuture = sipApplicationDispatcher.getAsynchronousScheduledExecutor().scheduleWithFixedDelay(new ServiceGracefulStopTask(this), 30000, 30000, TimeUnit.MILLISECONDS);
1359+
gracefulStopFuture = sipApplicationDispatcher.getAsynchronousScheduledExecutor().scheduleWithFixedDelay(new ServiceGracefulStopTask(this), gracefulInterval, gracefulInterval, TimeUnit.MILLISECONDS);
13561360
if(timeToWait > 0) {
13571361
gracefulStopFuture = sipApplicationDispatcher.getAsynchronousScheduledExecutor().schedule(
13581362
new Runnable() {
@@ -1399,4 +1403,10 @@ public String getSasTimerServiceImplementationType() {
13991403
public void setSasTimerServiceImplementationType(String sasTimerServiceImplementationType) {
14001404
this.sasTimerServiceImplementationType = sasTimerServiceImplementationType;
14011405
}
1406+
1407+
public void setGracefulInterval(long gracefulInterval) {
1408+
this.gracefulInterval = gracefulInterval;
1409+
}
1410+
1411+
14021412
}

Diff for: sip-servlets-impl/src/main/resources/org/mobicents/servlet/sip/core/mbeans-descriptors.xml

+14
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,20 @@
153153
154154
<operation name="stop" description="Stop" impact="ACTION"
155155
returnType="void" />
156+
157+
<operation name="stopGracefully" description="Stop Gracefully" impact="ACTION"
158+
returnType="void">
159+
<parameter name="timeToWait"
160+
description="the container will wait for the time specified in this parameter before forcefully killing the remaining sessions (HTTP and SIP) for each application deployed, if a negative value is provided the container will wait until there is no remaining Session before shutting down"
161+
type="long" />
162+
</operation>
163+
164+
<operation name="stopGracefully" description="Stop Gracefully" impact="ACTION"
165+
returnType="void">
166+
<parameter name="timeToWait"
167+
description="the container will wait for the time specified in this parameter before forcefully killing the remaining sessions (HTTP and SIP) for each application deployed, if a negative value is provided the container will wait until there is no remaining Session before shutting down"
168+
type="java.lang.String" />
169+
</operation>
156170
157171
<operation name="stopGracefully" description="Stop Gracefully" impact="ACTION"
158172
returnType="void">

0 commit comments

Comments
 (0)