You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@directory.apache.org by Srikanth Veeramachaneni <sv...@gmail.com> on 2005/12/22 03:00:46 UTC

[MINA] 0.9.0 - Need advice on implementing graceful shutdown

I would like to implement graceful shutdown of a MINA server.

The features I would like to support for the graceful shutdown are

 1. Stop listening for new connections
 2. Suspend read on all open IO sessions
 3. Wait until all pending requests are processed for open IO sessions
 4. Send a message to the clients indicating the session is being closed
    due to server shutdown
 5. Close all IO sessions

Assuming that sessions are added and removed from a list using the
sessionOpened()
and sessionClosed() methods of the IO handler and information as to whether a
session has pending requests is maintained as a session attribute,
would an approach like below work?

-------------------------------------------------------
  private ServiceRegistry registry;
  private List<IoSession> ioSessionsList;

  /**
   * pseudo code for supporting MINA server graceful shutdown
   */
  public void shutdown() {
    registry.unbindAll();
    while (!ioSessionsList.isEmpty()) {
      IoSession[] ioSessions = ioSessionsList.toArray(new IoSession[0]);
      for (int i = 0; i < ioSessions.length; i++) {
        IoSession session = ioSessions[i];
        if (!session.isClosing()) {
          session.suspendRead();
          if (!(Boolean) session.getAttribute("HAS_PENDING_REQUESTS")) {
            session.write("Server closing connection due to shutdown");
            session.close();
          }
        }
      }
    }
  }
-------------------------------------------------------

I would appreciate any advice/feedback/suggestions from MINA experts
or anyone who has already implemented something similar.

I probably need to add some synchronization to the above approach if
it is a reasonable approach.

thanks,
Srikanth

Re: [MINA] 0.9.0 - Need advice on implementing graceful shutdown

Posted by Trustin Lee <tr...@gmail.com>.
Hi Srikanth,

2005/12/22, Srikanth Veeramachaneni <sv...@gmail.com>:
>
> I would like to implement graceful shutdown of a MINA server.
>
> The features I would like to support for the graceful shutdown are
>
> 1. Stop listening for new connections
> 2. Suspend read on all open IO sessions
> 3. Wait until all pending requests are processed for open IO sessions
> 4. Send a message to the clients indicating the session is being closed
>     due to server shutdown
> 5. Close all IO sessions
>
> Assuming that sessions are added and removed from a list using the
> sessionOpened()
> and sessionClosed() methods of the IO handler and information as to
> whether a
> session has pending requests is maintained as a session attribute,
> would an approach like below work?
>
> -------------------------------------------------------
>   private ServiceRegistry registry;
>   private List<IoSession> ioSessionsList;
>
>   /**
>    * pseudo code for supporting MINA server graceful shutdown
>    */
>   public void shutdown() {
>     registry.unbindAll();
>     while (!ioSessionsList.isEmpty()) {
>       IoSession[] ioSessions = ioSessionsList.toArray(new IoSession[0]);
>       for (int i = 0; i < ioSessions.length; i++) {
>         IoSession session = ioSessions[i];
>         if (!session.isClosing()) {
>           session.suspendRead();
>           if (!(Boolean) session.getAttribute("HAS_PENDING_REQUESTS")) {
>             session.write("Server closing connection due to shutdown");
>             session.close();
>           }
>         }
>       }
>     }
>   }
> -------------------------------------------------------
>
> I would appreciate any advice/feedback/suggestions from MINA experts
> or anyone who has already implemented something similar.


Your approach looks reasonable, but please test enough.

I probably need to add some synchronization to the above approach if
> it is a reasonable approach.


Actually we're going to add an option which enables automatic client
disconnection on unbind.  It is not yet implemented, but we'll ship this
feature before 1.0 is released.

HTH,
Trustin
--
what we call human nature is actually human habit
--
http://gleamynode.net/
PGP Key ID: 0x854B996C