Previously, we introduced that iosessionrecycler is an interface used to recycle sessions that are no longer in use. expiringsessionrecycler is an implementation class used to recycle sessions with expired timeout.
Private expiringmap <object, iosession> sessionmap; // session set to be processed
Private expiringmap <object, iosession>. expirer mapexpirer; // recycles
The sessionmap key is composed of the local address and the remote address. The value is the session corresponding to the two addresses.
The expirer class implements the runnable interface. This thread monitors expiringmap and removes elements that exceed the threshold value in expiringmap from expiringmap. This thread calls setdaemon (true) and runs in the background as a daemon. The specific process is as follows:
Java code
- Private
Void
Processexpires ()
- {
- Long
Timenow = system. currenttimemillis ();
// Current time
- For
(Expiringobject O: Delegate. Values ())
- {
- If
(Timetolivemillis <=
0
)
- {
- Continue
;
- }
- Long
Timeidle = timenow-o. getlastaccesstime ();
// Time Difference
- If
(Timeidle> = timetolivemillis)
- {// Timeout
- Delegate. Remove (O. getkey ());
- For
(Expirationlistener <v> listener: expirationlisteners)
- {// Call listener
- Listener. Expired (O. getvalue ());
- }
- }
- }
- }
Private void processexpires () {long timenow = system. currenttimemillis (); // current time for (expiringobject O: Delegate. values () {If (timetolivemillis <= 0) {continue;} Long timeidle = timenow-o. getlastaccesstime (); // time difference if (timeidle> = timetolivemillis) {// time-out delegate. remove (O. getkey (); For (expirationlistener <v> listener: expirationlisteners) {// call listener. expired (O. getvalue ());}}}}
The lock mechanism is required to start/Close the timeout check thread. Here the read/write lock is used:
Java code
- Private
Final
Readwritelock statelock =
New
Reentrantreadwritelock ();
- Public
Void
Startexpiring ()
- {
- Statelock. writelock (). Lock ();
- Try
- {
- If
(! Running)
- {
- Running = true
;
- Expirerthread. Start ();
- }
- }
- Finally
- {
- Statelock. writelock (). Unlock ();
- }
- }
- Public
Void
Stopexpiring ()
- {
- Statelock. writelock (). Lock ();
- Try
- {
- If
(Running)
- {
- Running = false
;
- Expirerthread. Interrupt ();
- }
- }
- Finally
- {
- Statelock. writelock (). Unlock ();
- }
- }
private final ReadWriteLock stateLock = new ReentrantReadWriteLock(); public void startExpiring() { stateLock.writeLock().lock(); try { if (!running) { running = true; expirerThread.start(); } } finally { stateLock.writeLock().unlock(); } } public void stopExpiring() { stateLock.writeLock().lock(); try { if (running) { running = false; expirerThread.interrupt(); } } finally { stateLock.writeLock().unlock(); } }
Session Timeout listener:
Java code
- Private
Class
Defaultexpirationlistener
Implements
- Expirationlistener <iosession> {
- Public
Void
Expired (iosession expiredsession ){
- Expiredsession. Close (); // close the timeout session
- }
- }