/servers/sip-servlets/sip-servlets-tomcat-jboss4/src/main/java/org/jboss/web/tomcat/service/session/IntervalConvergedSnapshotManager.java
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
- /*
- * JBoss, Home of Professional Open Source
- * Copyright 2011, Red Hat, Inc. and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
- package org.jboss.web.tomcat.service.session;
- import java.util.LinkedHashSet;
- import java.util.Set;
- /**
- * @author <A HREF="mailto:jean.deruelle@gmail.com">Jean Deruelle</A>
- *
- */
- public class IntervalConvergedSnapshotManager extends IntervalSnapshotManager implements SnapshotSipManager {
- // the modified sessions
- protected Set sipSessions = new LinkedHashSet();
- protected Set sipApplicationSessions = new LinkedHashSet();
- /**
- * @param manager
- * @param path
- */
- public IntervalConvergedSnapshotManager(AbstractJBossManager manager, String path) {
- super(manager, path);
- }
- /**
- * @param manager
- * @param path
- * @param interval
- */
- public IntervalConvergedSnapshotManager(AbstractJBossManager manager,
- String path, int interval) {
- super(manager, path, interval);
- }
- /**
- * Store the modified session in a hashmap for the distributor thread
- */
- public void snapshot(ClusteredSipSession session) {
- try {
- // Don't hold a ref to the session for a long time
- synchronized (sipSessions) {
- sipSessions.add(session);
- }
- } catch (Exception e) {
- log.error(
- "Failed to queue session " + session + " for replication",
- e);
- }
- }
- /**
- * Store the modified session in a hashmap for the distributor thread
- */
- public void snapshot(ClusteredSipApplicationSession session) {
- try {
- // Don't hold a ref to the session for a long time
- synchronized (sipApplicationSessions) {
- sipApplicationSessions.add(session);
- }
- } catch (Exception e) {
- log.error(
- "Failed to queue session " + session + " for replication",
- e);
- }
- }
- /**
- * Distribute all modified sessions
- */
- protected void processSipSessions() {
- ClusteredSipSession[] toProcess = null;
- synchronized (sipSessions) {
- toProcess = new ClusteredSipSession[sipSessions.size()];
- toProcess = (ClusteredSipSession[]) sipSessions.toArray(toProcess);
- sipSessions.clear();
- }
- JBossCacheSipManager mgr = (JBossCacheSipManager) getManager();
- for (int i = 0; i < toProcess.length; i++) {
- // Confirm we haven't been stopped
- if (!processingAllowed)
- break;
- try {
- mgr.storeSipSession(toProcess[i]);
- } catch (Exception e) {
- getLog().error(
- "Caught exception processing session "
- + toProcess[i].getId(), e);
- }
- }
- }
- /**
- * Distribute all modified sessions
- */
- protected void processSipApplicationSessions() {
- ClusteredSipApplicationSession[] toProcess = null;
- synchronized (sipApplicationSessions) {
- toProcess = new ClusteredSipApplicationSession[sipApplicationSessions
- .size()];
- toProcess = (ClusteredSipApplicationSession[]) sipApplicationSessions
- .toArray(toProcess);
- sipApplicationSessions.clear();
- }
- JBossCacheSipManager mgr = (JBossCacheSipManager) getManager();
- for (int i = 0; i < toProcess.length; i++) {
- // Confirm we haven't been stopped
- if (!processingAllowed)
- break;
- try {
- mgr.storeSipApplicationSession(toProcess[i]);
- } catch (Exception e) {
- getLog().error(
- "Caught exception processing session "
- + toProcess[i].getId(), e);
- }
- }
- }
- /**
- * Start the snapshot manager
- */
- public void start() {
- processingAllowed = true;
- startThread();
- }
- /**
- * Stop the snapshot manager
- */
- public void stop() {
- processingAllowed = false;
- stopThread();
- synchronized (sessions) {
- sessions.clear();
- }
- synchronized (sipSessions) {
- sipSessions.clear();
- }
- synchronized (sipApplicationSessions) {
- sipApplicationSessions.clear();
- }
- }
- /**
- * Start the distributor thread
- */
- protected void startThread() {
- if (thread != null) {
- return;
- }
- thread = new Thread(this, "ClusteredConvergedSessionDistributor["
- + getContextPath() + "]");
- thread.setDaemon(true);
- thread.setContextClassLoader(getManager().getContainer().getLoader()
- .getClassLoader());
- threadDone = false;
- thread.start();
- }
- /**
- * Stop the distributor thread
- */
- protected void stopThread() {
- if (thread == null) {
- return;
- }
- threadDone = true;
- thread.interrupt();
- try {
- thread.join();
- } catch (InterruptedException e) {
- }
- thread = null;
- }
- /**
- * Thread-loop
- */
- public void run() {
- while (!threadDone) {
- try {
- Thread.sleep(interval);
- processSessions();
- processSipApplicationSessions();
- processSipSessions();
- } catch (InterruptedException ie) {
- if (!threadDone)
- getLog().error("Caught exception processing sessions", ie);
- } catch (Exception e) {
- getLog().error("Caught exception processing sessions", e);
- }
- }
- }
- }