Mina2.0 framework source code analysis (5)

Source: Internet
Author: User

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
  1. Private
     
    Void
    Processexpires ()
  2. {
  3. Long
    Timenow = system. currenttimemillis ();
    // Current time

  4. For
    (Expiringobject O: Delegate. Values ())
  5. {
  6. If
    (Timetolivemillis <=
    0
    )
  7. {
  8. Continue
    ;
  9. }
  10. Long
    Timeidle = timenow-o. getlastaccesstime ();
    // Time Difference

  11. If
    (Timeidle> = timetolivemillis)
  12. {// Timeout

  13. Delegate. Remove (O. getkey ());
  14. For
    (Expirationlistener <v> listener: expirationlisteners)
  15. {// Call listener

  16. Listener. Expired (O. getvalue ());
  17. }
  18. }
  19. }
  20. }
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
  1. Private
     
    Final
    Readwritelock statelock =
    New
    Reentrantreadwritelock ();
  2. Public
     
    Void
    Startexpiring ()
  3. {
  4. Statelock. writelock (). Lock ();
  5. Try

  6. {
  7. If
    (! Running)
  8. {
  9. Running = true
    ;
  10. Expirerthread. Start ();
  11. }
  12. }
  13. Finally

  14. {
  15. Statelock. writelock (). Unlock ();
  16. }
  17. }
  18. Public
     
    Void
    Stopexpiring ()
  19. {
  20. Statelock. writelock (). Lock ();
  21. Try

  22. {
  23. If
    (Running)
  24. {
  25. Running = false
    ;
  26. Expirerthread. Interrupt ();
  27. }
  28. }
  29. Finally

  30. {
  31. Statelock. writelock (). Unlock ();
  32. }
  33. }
      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
  1. Private
     
    Class
    Defaultexpirationlistener
    Implements

  2. Expirationlistener <iosession> {
  3. Public
     
    Void
    Expired (iosession expiredsession ){
  4. Expiredsession. Close (); // close the timeout session

  5. }
  6. }

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.