解决 SIP 和 HTTP 会话到期时间不同的最佳方法是,启动时设置足够长的 SAS 失效时间,大小相当于应用程序会话预期存活的总时间(包含几个 HTTP 请求)。甚至,还可以将 SAS 生命周期设置为无限长,尤其是在使用了 invalidateWhenReady 语义时;在这种情形下,SipApplicationSession 到最后一个协议子会话到期时才会失效。SAS 的初始失效时间可以在部署描述符中进行配置。
如果可以事先估计总持续时间的最大值,则无需使用更多代码,因为在 SAS 失效时 SIP 会话和 HTTP 会话也会同时失效。如果不能事先估计总持续时间的最大值,则可在到期时延长 SipApplicationSession,代码片段如下所示。
在 SipApplicationSessionListener 实施中,可使用类似如下的代码:
public void sessionExpired(SipApplicationSessionEvent sasEvent) { // check if the SAS needs to be extended first, if so: int granted = sasEvent.getApplicationSession().setExpires(2); if (granted <= 0) { System.out.println("extension rejected"); } else if (granted < 2) { System.out.println("extension granted with lower value " + granted); } // else allowed }