PageRenderTime 143ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/servers/sip-servlets/sip-servlets-tomcat-jboss4/src/main/java/org/jboss/web/tomcat/service/session/IntervalConvergedSnapshotManager.java

http://mobicents.googlecode.com/
Java | 219 lines | 134 code | 19 blank | 66 comment | 10 complexity | 6f0c2459b37b18c1b4bbccbdc21bfb59 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.web.tomcat.service.session;
  23. import java.util.LinkedHashSet;
  24. import java.util.Set;
  25. /**
  26. * @author <A HREF="mailto:jean.deruelle@gmail.com">Jean Deruelle</A>
  27. *
  28. */
  29. public class IntervalConvergedSnapshotManager extends IntervalSnapshotManager implements SnapshotSipManager {
  30. // the modified sessions
  31. protected Set sipSessions = new LinkedHashSet();
  32. protected Set sipApplicationSessions = new LinkedHashSet();
  33. /**
  34. * @param manager
  35. * @param path
  36. */
  37. public IntervalConvergedSnapshotManager(AbstractJBossManager manager, String path) {
  38. super(manager, path);
  39. }
  40. /**
  41. * @param manager
  42. * @param path
  43. * @param interval
  44. */
  45. public IntervalConvergedSnapshotManager(AbstractJBossManager manager,
  46. String path, int interval) {
  47. super(manager, path, interval);
  48. }
  49. /**
  50. * Store the modified session in a hashmap for the distributor thread
  51. */
  52. public void snapshot(ClusteredSipSession session) {
  53. try {
  54. // Don't hold a ref to the session for a long time
  55. synchronized (sipSessions) {
  56. sipSessions.add(session);
  57. }
  58. } catch (Exception e) {
  59. log.error(
  60. "Failed to queue session " + session + " for replication",
  61. e);
  62. }
  63. }
  64. /**
  65. * Store the modified session in a hashmap for the distributor thread
  66. */
  67. public void snapshot(ClusteredSipApplicationSession session) {
  68. try {
  69. // Don't hold a ref to the session for a long time
  70. synchronized (sipApplicationSessions) {
  71. sipApplicationSessions.add(session);
  72. }
  73. } catch (Exception e) {
  74. log.error(
  75. "Failed to queue session " + session + " for replication",
  76. e);
  77. }
  78. }
  79. /**
  80. * Distribute all modified sessions
  81. */
  82. protected void processSipSessions() {
  83. ClusteredSipSession[] toProcess = null;
  84. synchronized (sipSessions) {
  85. toProcess = new ClusteredSipSession[sipSessions.size()];
  86. toProcess = (ClusteredSipSession[]) sipSessions.toArray(toProcess);
  87. sipSessions.clear();
  88. }
  89. JBossCacheSipManager mgr = (JBossCacheSipManager) getManager();
  90. for (int i = 0; i < toProcess.length; i++) {
  91. // Confirm we haven't been stopped
  92. if (!processingAllowed)
  93. break;
  94. try {
  95. mgr.storeSipSession(toProcess[i]);
  96. } catch (Exception e) {
  97. getLog().error(
  98. "Caught exception processing session "
  99. + toProcess[i].getId(), e);
  100. }
  101. }
  102. }
  103. /**
  104. * Distribute all modified sessions
  105. */
  106. protected void processSipApplicationSessions() {
  107. ClusteredSipApplicationSession[] toProcess = null;
  108. synchronized (sipApplicationSessions) {
  109. toProcess = new ClusteredSipApplicationSession[sipApplicationSessions
  110. .size()];
  111. toProcess = (ClusteredSipApplicationSession[]) sipApplicationSessions
  112. .toArray(toProcess);
  113. sipApplicationSessions.clear();
  114. }
  115. JBossCacheSipManager mgr = (JBossCacheSipManager) getManager();
  116. for (int i = 0; i < toProcess.length; i++) {
  117. // Confirm we haven't been stopped
  118. if (!processingAllowed)
  119. break;
  120. try {
  121. mgr.storeSipApplicationSession(toProcess[i]);
  122. } catch (Exception e) {
  123. getLog().error(
  124. "Caught exception processing session "
  125. + toProcess[i].getId(), e);
  126. }
  127. }
  128. }
  129. /**
  130. * Start the snapshot manager
  131. */
  132. public void start() {
  133. processingAllowed = true;
  134. startThread();
  135. }
  136. /**
  137. * Stop the snapshot manager
  138. */
  139. public void stop() {
  140. processingAllowed = false;
  141. stopThread();
  142. synchronized (sessions) {
  143. sessions.clear();
  144. }
  145. synchronized (sipSessions) {
  146. sipSessions.clear();
  147. }
  148. synchronized (sipApplicationSessions) {
  149. sipApplicationSessions.clear();
  150. }
  151. }
  152. /**
  153. * Start the distributor thread
  154. */
  155. protected void startThread() {
  156. if (thread != null) {
  157. return;
  158. }
  159. thread = new Thread(this, "ClusteredConvergedSessionDistributor["
  160. + getContextPath() + "]");
  161. thread.setDaemon(true);
  162. thread.setContextClassLoader(getManager().getContainer().getLoader()
  163. .getClassLoader());
  164. threadDone = false;
  165. thread.start();
  166. }
  167. /**
  168. * Stop the distributor thread
  169. */
  170. protected void stopThread() {
  171. if (thread == null) {
  172. return;
  173. }
  174. threadDone = true;
  175. thread.interrupt();
  176. try {
  177. thread.join();
  178. } catch (InterruptedException e) {
  179. }
  180. thread = null;
  181. }
  182. /**
  183. * Thread-loop
  184. */
  185. public void run() {
  186. while (!threadDone) {
  187. try {
  188. Thread.sleep(interval);
  189. processSessions();
  190. processSipApplicationSessions();
  191. processSipSessions();
  192. } catch (InterruptedException ie) {
  193. if (!threadDone)
  194. getLog().error("Caught exception processing sessions", ie);
  195. } catch (Exception e) {
  196. getLog().error("Caught exception processing sessions", e);
  197. }
  198. }
  199. }
  200. }