/**
   * Test for JBAS-5404
   *
   * @throws Exception
   */
  public void testMaxInactiveIntervalReplication() throws Exception {
    log.info("Enter testMaxInactiveIntervalReplication");

    ++testCount;

    JBossCacheManager jbcm0 =
        SessionTestUtil.createManager("test" + testCount, 5, false, null, true, true, null, caches);

    JBossWebMetaData webMetaData = SessionTestUtil.createWebMetaData(2);
    jbcm0.init("test.war", webMetaData);

    jbcm0.start();

    JBossCacheManager jbcm1 =
        SessionTestUtil.createManager("test" + testCount, 5, false, null, true, true, null, caches);

    jbcm1.init("test.war", webMetaData);

    jbcm1.start();

    // Set up a session
    String id = "1";
    Session sess = jbcm0.findSession(id);
    assertNull("session does not exist", sess);

    sess = jbcm0.createSession(id);
    sess.access();
    sess.getSession().setAttribute("test", "test");
    jbcm0.storeSession(sess);
    sess.endAccess();

    assertEquals("Session count correct", 1, jbcm0.getActiveSessionCount());
    assertEquals("Local session count correct", 1, jbcm0.getLocalActiveSessionCount());
    assertEquals("Session count correct", 1, jbcm1.getActiveSessionCount());
    assertEquals("Local session count correct", 0, jbcm1.getLocalActiveSessionCount());

    // Confirm a session timeout clears space
    sess = jbcm0.findSession(id);
    sess.access();
    sess.setMaxInactiveInterval(1);
    jbcm0.storeSession(sess);
    sess.endAccess();

    SessionTestUtil.sleepThread(1005);

    jbcm1.backgroundProcess();

    assertEquals("Session count correct", 1, jbcm0.getActiveSessionCount());
    assertEquals("Local session count correct", 1, jbcm0.getLocalActiveSessionCount());
    assertEquals("Session count correct", 0, jbcm1.getActiveSessionCount());
    assertEquals("Local session count correct", 0, jbcm1.getLocalActiveSessionCount());

    jbcm0.backgroundProcess();

    assertEquals("Session count correct", 0, jbcm0.getActiveSessionCount());
    assertEquals("Local session count correct", 0, jbcm0.getLocalActiveSessionCount());
    assertEquals("Session count correct", 0, jbcm1.getActiveSessionCount());
    assertEquals("Local session count correct", 0, jbcm1.getLocalActiveSessionCount());
  }
Пример #2
0
  /**
   * Return the session associated with this Request, creating one if necessary and requested.
   *
   * @param create Create a new session if one does not exist
   */
  public HttpSession getSession(boolean create) {

    if (crossContext) {

      // There cannot be a session if no context has been assigned yet
      if (context == null) return (null);

      // Return the current session if it exists and is valid
      if (session != null && session.isValid()) {
        return (session.getSession());
      }

      HttpSession other = super.getSession(false);
      if (create && (other == null)) {
        // First create a session in the first context: the problem is
        // that the top level request is the only one which can
        // create the cookie safely
        other = super.getSession(true);
      }
      if (other != null) {
        Session localSession = null;
        try {
          localSession = context.getManager().findSession(other.getId());
          if (localSession != null && !localSession.isValid()) {
            localSession = null;
          }
        } catch (IOException e) {
          // Ignore
        }
        if (localSession == null && create) {
          localSession = context.getManager().createSession(other.getId());
        }
        if (localSession != null) {
          localSession.access();
          session = localSession;
          return session.getSession();
        }
      }
      return null;

    } else {
      return super.getSession(create);
    }
  }
 /**
  * This method is called by the received thread when a SessionMessage has been received from one
  * of the other nodes in the cluster.
  *
  * @param msg - the message received
  * @param sender - the sender of the message, this is used if we receive a EVT_GET_ALL_SESSION
  *     message, so that we only reply to the requesting node
  */
 protected void messageReceived(SessionMessage msg, Member sender) {
   try {
     if (log.isInfoEnabled()) {
       log.debug("Received SessionMessage of type=" + msg.getEventTypeString());
       log.debug("Received SessionMessage sender=" + sender);
     }
     switch (msg.getEventType()) {
       case SessionMessage.EVT_GET_ALL_SESSIONS:
         {
           // get a list of all the session from this manager
           Object[] sessions = findSessions();
           java.io.ByteArrayOutputStream bout = new java.io.ByteArrayOutputStream();
           java.io.ObjectOutputStream oout = new java.io.ObjectOutputStream(bout);
           oout.writeInt(sessions.length);
           for (int i = 0; i < sessions.length; i++) {
             ReplicatedSession ses = (ReplicatedSession) sessions[i];
             oout.writeUTF(ses.getIdInternal());
             byte[] data = writeSession(ses);
             oout.writeObject(data);
           } // for
           // don't send a message if we don't have to
           oout.flush();
           oout.close();
           byte[] data = bout.toByteArray();
           SessionMessage newmsg =
               new SessionMessageImpl(
                   name,
                   SessionMessage.EVT_ALL_SESSION_DATA,
                   data,
                   "SESSION-STATE",
                   "SESSION-STATE-" + getName());
           cluster.send(newmsg, sender);
           break;
         }
       case SessionMessage.EVT_ALL_SESSION_DATA:
         {
           java.io.ByteArrayInputStream bin = new java.io.ByteArrayInputStream(msg.getSession());
           java.io.ObjectInputStream oin = new java.io.ObjectInputStream(bin);
           int size = oin.readInt();
           for (int i = 0; i < size; i++) {
             String id = oin.readUTF();
             byte[] data = (byte[]) oin.readObject();
             Session session = readSession(data, id);
           } // for
           stateTransferred = true;
           break;
         }
       case SessionMessage.EVT_SESSION_CREATED:
         {
           Session session = this.readSession(msg.getSession(), msg.getSessionID());
           if (log.isDebugEnabled()) {
             log.debug("Received replicated session=" + session + " isValid=" + session.isValid());
           }
           break;
         }
       case SessionMessage.EVT_SESSION_EXPIRED:
         {
           Session session = findSession(msg.getSessionID());
           if (session != null) {
             session.expire();
             this.remove(session);
           } // end if
           break;
         }
       case SessionMessage.EVT_SESSION_ACCESSED:
         {
           Session session = findSession(msg.getSessionID());
           if (session != null) {
             session.access();
             session.endAccess();
           }
           break;
         }
       default:
         {
           // we didn't recognize the message type, do nothing
           break;
         }
     } // switch
   } catch (Exception x) {
     log.error("Unable to receive message through TCP channel", x);
   }
 }