📄 sessionfactory.java
字号:
package com.gctech.sms.sp.dwzb;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import EDU.oswego.cs.dl.util.concurrent.ReentrantWriterPreferenceReadWriteLock;
import EDU.oswego.cs.dl.util.concurrent.Sync;
import com.gctech.sms.sp.cms.util.IDGenerator;
/**
* 用户会话工厂类
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: gctech</p>
* @author lijz@gctech.com.cn
* @version 1.0
*/
public class SessionFactory
{
LRUHashMap map = null;
int defaultTTL;
ReentrantWriterPreferenceReadWriteLock lock = null;
Sync readLock = null;
Sync writeLock = null;
IDGenerator idg = new IDGenerator(0,999999);
/**
*
* @param sessionSize 能存放多少session,数了超过,那最不常用的sesion将自动被删除
* @param defaultTTL session的存活时间(分钟)
* @throws java.lang.Exception
*/
public SessionFactory(int sessionSize,int defaultTTL) throws Exception
{
map = new LRUHashMap();
map.setMax(sessionSize);
this.defaultTTL = defaultTTL;
//以下并没有使用,如果性能不好,或者同步有问题,可采用ReentrantWriterPreferenceReadWriteLock来解决
// lock = new ReentrantWriterPreferenceReadWriteLock();
// readLock =lock.readLock();
// writeLock = lock.writeLock();
startCleanThead();
}
public SessionFactory() throws Exception
{
this(100,10);
}
/**
* 产生一个没人占用的sessionid标识
* @return
*/
// public String getFreeId()
// {
// String temp = "" + idg.nextId();
// String str = "000000";
// str = (str + temp).substring(temp.length());
// return Config.instance().getServiceNo().substring(5) + str;
//
// }
public Session getSession(String id)
{
Object o = map.get(id);
if(o != null)
{
return(Session) o;
}
else
{
Session session = new Session(id,defaultTTL);
synchronized(map)
{
map.put(id,session);
}
return session;
}
}
public void clear()
{
synchronized(map)
{
map.clear();
}
}
protected void remove(List keys)
{
synchronized(map)
{
System.out.println("remove sessionId:"+keys);
try
{
Object primaryKey = null;
for(int i = 0;i < keys.size();i++)
{
primaryKey = keys.get(i);
map.remove(primaryKey);
}
}
catch(Exception ex)
{
}
}
}
void startCleanThead() throws Exception
{
try
{
Thread threadCleanerUpper = new Thread(
new Runnable()
{
int milliSecondSleepTime = 1000*120;
public void run()
{
try
{
while (true)
{
Date now = new Date();
ArrayList list = new ArrayList();
Set keySet = map.keySet();
Iterator keys = keySet.iterator();
while(keys.hasNext())
{
Object key = keys.next();
Session value = (Session)map.get(key);
if(value.isExpired(now))
{
list.add(key);
}
}
if(list.size()!=0)
{
remove(list);
}
Thread.sleep(milliSecondSleepTime);
}
}
catch (Exception e)
{
e.printStackTrace();
}
return;
} /* End run method */
}); /* End class definition and close new thread definition */
// Sets the thread's priority to the NORM_PRIORITY-1 value.
threadCleanerUpper.setPriority(Thread.NORM_PRIORITY-1);
threadCleanerUpper.start();
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
public static void main(String[] args) throws Exception
{
SessionFactory f = new SessionFactory();
// System.out.println(f.getFreeId());
// System.out.println(f.getFreeId());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -