Self-written code implements the Session, and the code implements the session

Source: Internet
Author: User

Self-written code implements the Session, and the code implements the session

Package com. zq. web. context. windows;

Import java. util. HashMap;
Import java. util. Map;

Import org. apache. log4j. Logger;

Import com. zq. utils. DateUtils;
Import com. zq. utils. string. StringUtils;

/**
*
* Windows desktop application session management container SessionContainer
*
* Created by MyEclipse. Author: ChenBin E-mail: chenbin_2008@126.com Date:
* December 4, 2016 Time: 9:53:03 AM
*/
Public class SC {
Private static Logger log = Logger. getLogger (SC. class );
/**
* Timeout in milliseconds. If the session object in the container is not updated after this time, it will be cleared.
*/
Private static long timeOut = 3600000L;

Private static Map <String, Session> container = new HashMap <String, Session> ();

// Private container Constructor
Private SC (){
}

/**
* Description: obtains the corresponding session entity through sessionId.
*
* @ Author: ChenBin
* @ Date: 2:33:24, January 1, December 13, 2016
*/
Public static Session get (String sessionId ){
If (! StringUtils. compareTrim (sessionId ))
Return null;
Session session = container. get (sessionId );
If (session! = Null)
Session. updateLastTime ();
Return session;
}

/**
* Description: adds a session object to the session container.
*
* @ Author: ChenBin
* @ Date: December 4, 2016 10:47:15 AM
*/
Public static Session create (){
Session session = Session. me ();
SC. put (session );
Return session;
}

Public static Session put (Session sen ){
If (sen = null)
Return sen;
If (! StringUtils. compareTrim (sen. getSessionId ())){
Log. debug ("sessionid cannot be blank! ");
Return sen;
}
Return container. put (sen. getSessionId (), sen );
}

/**
* Description: removes the specified Session object.
*
* @ Author: ChenBin
* @ Date: December 4, 2016 10:06:40 AM
*/
Public static void remove (Session sen ){
If (sen! = Null)
SC. remove (sen. getSessionId ());
}

/**
* Description: removes the specified sessionId through sessionId.
*
* @ Author: ChenBin (E-Mail: chenbin_2008@126.com)
* @ Date: 4:30:12, January 1, March 9, 2017
*/
Public static void remove (String sessionId ){
If (StringUtils. compareTrim (sessionId ))
Container. remove (sessionId );
}

Public static long getTimeOut (){
Return timeOut;
}

Public static void setTimeOut (long timeOut ){
SC. timeOut = timeOut;
}

/**
* Description: process the session object in the session container. If it has expired, the session object corresponding to the sessionId will be destroyed.
*
* @ Author: ChenBin (E-Mail: chenbin_2008@126.com)
* @ Date: 5:24:05, January 1, March 9, 2017
*/
Public static void handleTimeOutSession (String sessionId ){
Session sen = SC. get (sessionId );
If (sen = null)
Return;
If (DateUtils. timeDifference (sen. getLastUpdateTime ()> SC. timeOut)
SC. remove (sessionId );
}

}

/*************************************** ************************** *******************/

Package com. zq. web. context. windows;

Import java. util. HashMap;
Import java. util. Map;

Import com. zq. utils. DateUtils;
Import com. zq. utils. system. WindowsAppUtils;

/**
*
* Simple custom Session Object
*
* Created by MyEclipse. Author: ChenBin E-mail: chenbin_2008@126.com Date:
* December 4, 2016 Time: 9:57:12 AM
*/
Public class Session {
/**
* Session id
*/
Private String sessionId;
/**
* Creation time in milliseconds
*/
Private long createTime;
/**
* Last Update Time
*/
Private long lastUpdateTime;
/**
* Content saved in the Session Object
*/
Private Map <Object, Object> sessionContent;
/**
* Session type: windows-10; browser-20; Android-30; IOS-40;
*/
Private int sessionType;
/**
* Whether the session has expired (invalid-true; valid-false)
*/
Private boolean invalid;

/**
* Description: defines the constructor. sessionId must be input.
*
* @ Author: ChenBin
* @ Date: December 4, 2016 10:23:05 AM
*/
Private Session (String sessionId ){
This. sessionId = sessionId;
This. createTime = DateUtils. getTimeMillis ();
This. lastUpdateTime = createTime;
This. sessionContent = new HashMap <Object, Object> ();
}

/**
* Description: Get the specified parameter using the key.
*
* @ Author: ChenBin
* @ Date: December 4, 2016 10:38:16 AM
*/
Public Object getAttribute (Object key ){
UpdateLastTime ();
Return this. sessionContent. get (key );
}

/**
* Description: Save parameters to the session.
*
* @ Author: ChenBin
* @ Date: December 4, 2016 10:39:26 AM
*/
Public void setAttribute (Object k, Object v ){
UpdateLastTime ();
This. sessionContent. put (k, v );
}

/**
* Description: method for obtaining the Session object
*
* @ Author: ChenBin
* @ Date: December 4, 2016 10:24:17 AM
*/
Public static Session me (){
Return new Session (WindowsAppUtils. sessionIDGenerator ());
}

/**
* Description: The last update timestamp.
*
* @ Author: ChenBin
* @ Date: December 4, 2016 10:43:32 AM
*/
Public void updateLastTime (){
This. lastUpdateTime = DateUtils. getTimeMillis ();
}

Public String getSessionId (){
Return sessionId;
}

Public long getLastUpdateTime (){
Return lastUpdateTime;
}

Public int getSessionType (){
Return sessionType;
}

Public void setSessionType (int sessionType ){
This. sessionType = sessionType;
}

Public boolean isInvalid (){
Return invalid;
}

Public void setInvalid (boolean invalid ){
This. invalid = invalid;
}

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.