You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@mina.apache.org by Harakiri <ha...@yahoo.com> on 2012/08/20 11:49:02 UTC

Forcing the closure of any open session when mina server shuts down

Hello,

i read the FAQ entry about closing sessions here mina.apache.org/faq.html and also through the mailling list.

However im not sure how i should apply it to my use case.

I've created a very simple policy service similar to this example
http://jglatre.blogspot.de/2009/03/implementing-greylist-for-postfix-with.html

The faq states something about

ConnectFuture cf = connector.connect(new InetSocketAddress("localhost", 8080));

but i dont see how this applies to an IoAcceptor - i start my service like in the above example

IoAcceptor acceptor = new NioSocketAcceptor(); 
      acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ApdCodecFactory())); 
      acceptor.setHandler( new DummyApdHandler() ); 
      acceptor.getSessionConfig().setReadBufferSize( 2048 ); 
      acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 10 ); 
      acceptor.bind( new InetSocketAddress(6666) ); 

i would expect that upon VM shutdown that all open sessions are closed - however they are not - my postfix server keeps a session open for quiet a while - so when i restart my java server (standalone service) i get the bind error - address already in use.

Any hints? I tried a shutdown hook and manually do a acceptor.unbind() but that didnt do the trick either!

Thanks

Re: Forcing the closure of any open session when mina server shuts down

Posted by Hardik <ku...@gmail.com>.
Emmanuel Lécharny <el...@...> writes:

> 
> Le 8/20/12 3:28 PM, Harakiri a écrit :
> >
> > --- On Mon, 8/20/12, Emmanuel Lécharny <el...@...> wrote:
> >
> >> From: Emmanuel Lécharny <el...@...>
> >> Subject: Re: Forcing the closure of any open session when mina server 
shuts down
> >> To: users@...
> >> Date: Monday, August 20, 2012, 8:54 AM
> >> Le 8/20/12 2:41 PM, Harakiri a écrit
> >> :
> >>> Hello,
> >>>
> >>> thanks for your input - i tried it with both
> >> close(true) and close(false) - but i would expect that
> >> close(false) blocks till all sessions are really closed -
> >> however the doc states something about flushing pending
> >> writes - i dont think there are any pending writes when the
> >> connection is just idle.
> >>
> >> You can also call the servise.dispose( boolean ) method. If
> >> the parameter is 'true', the service will try to flush all
> >> the pending writes. Otherwise, it will immediately close all
> >> the sessions.
> > I think most of my issues result in the rebinding of the port - in java 
there is setReuseAddress(boolean on)
> //         Enable/disable the SO_REUSEADDR socket option for a serversockt 
- is there something similar for mina?
> >
> > I found in the archive your post about
> >
> >   acceptor.getSessionConfig().setReuseAddress
> >
> > but that does not exist in 2.x
> 
> Of course it does !
> 
> public class DefaultSocketSessionConfig extends 
> AbstractSocketSessionConfig {
> ...
>      public void setReuseAddress(boolean reuseAddress) {
>          this.reuseAddress = reuseAddress;
>      }
> 
> The thing is that you have to cast the acceptor.getSessionConfig() to be 
> able to set the flag, because it implements an interface which is a bit 
> generic (IoSessionConfig) :
> 
> ((SocketSessionConfig)acceptor.getSessionConfig() ).setReuseAddress( true 
);
> 
> Or you can set it for the whole service :
> 
>          SocketAcceptor acceptor = new NioSocketAcceptor();
>          acceptor.setReuseAddress( true );
> 
> >
> >
> 

I have Implemented following code
     SocketAcceptor acceptor = new NioSocketAcceptor();
     acceptor.setReuseAddress( true );
though it throws
java.net.BindException: Address already in use Exception Also tried with 

following code
NioSocketAcceptor acceptor = new NioSocketAcceptor()
acceptor.getSessonConfig().setReuseAddress(true);
It also doesn't work



Re: Forcing the closure of any open session when mina server shuts down

Posted by Harakiri <ha...@yahoo.com>.

--- On Mon, 8/20/12, Emmanuel Lécharny <el...@gmail.com> wrote:

> From: Emmanuel Lécharny <el...@gmail.com>
> Subject: Re: Forcing the closure of any open session when mina server shuts down
> To: users@mina.apache.org
> Cc: elecharny@apache.org
> Date: Monday, August 20, 2012, 9:37 AM
> Le 8/20/12 3:28 PM, Harakiri a écrit
> :

> Or you can set it for the whole service :
> 
>          SocketAcceptor
> acceptor = new NioSocketAcceptor();
>      
>    acceptor.setReuseAddress( true );


Yes thank you - i figured as much - i just changed it from 
IoAcceptor acceptor; to NioSocketAcceptor acceptor;
 

Re: Forcing the closure of any open session when mina server shuts down

Posted by Emmanuel Lécharny <el...@gmail.com>.
Le 8/20/12 3:28 PM, Harakiri a écrit :
>
> --- On Mon, 8/20/12, Emmanuel Lécharny <el...@gmail.com> wrote:
>
>> From: Emmanuel Lécharny <el...@gmail.com>
>> Subject: Re: Forcing the closure of any open session when mina server shuts down
>> To: users@mina.apache.org
>> Date: Monday, August 20, 2012, 8:54 AM
>> Le 8/20/12 2:41 PM, Harakiri a écrit
>> :
>>> Hello,
>>>
>>> thanks for your input - i tried it with both
>> close(true) and close(false) - but i would expect that
>> close(false) blocks till all sessions are really closed -
>> however the doc states something about flushing pending
>> writes - i dont think there are any pending writes when the
>> connection is just idle.
>>
>> You can also call the servise.dispose( boolean ) method. If
>> the parameter is 'true', the service will try to flush all
>> the pending writes. Otherwise, it will immediately close all
>> the sessions.
> I think most of my issues result in the rebinding of the port - in java there is setReuseAddress(boolean on) //         Enable/disable the SO_REUSEADDR socket option for a serversockt - is there something similar for mina?
>
> I found in the archive your post about
>
>   acceptor.getSessionConfig().setReuseAddress
>
> but that does not exist in 2.x

Of course it does !

public class DefaultSocketSessionConfig extends 
AbstractSocketSessionConfig {
...
     public void setReuseAddress(boolean reuseAddress) {
         this.reuseAddress = reuseAddress;
     }

The thing is that you have to cast the acceptor.getSessionConfig() to be 
able to set the flag, because it implements an interface which is a bit 
generic (IoSessionConfig) :

((SocketSessionConfig)acceptor.getSessionConfig() ).setReuseAddress( true );

Or you can set it for the whole service :

         SocketAcceptor acceptor = new NioSocketAcceptor();
         acceptor.setReuseAddress( true );


>
>


-- 
Regards,
Cordialement,
Emmanuel Lécharny
www.iktek.com


Re: Forcing the closure of any open session when mina server shuts down

Posted by Harakiri <ha...@yahoo.com>.

--- On Mon, 8/20/12, Emmanuel Lécharny <el...@gmail.com> wrote:

> From: Emmanuel Lécharny <el...@gmail.com>
> Subject: Re: Forcing the closure of any open session when mina server shuts down
> To: users@mina.apache.org
> Date: Monday, August 20, 2012, 8:54 AM
> Le 8/20/12 2:41 PM, Harakiri a écrit
> :
> > Hello,
> > 
> > thanks for your input - i tried it with both
> close(true) and close(false) - but i would expect that
> close(false) blocks till all sessions are really closed -
> however the doc states something about flushing pending
> writes - i dont think there are any pending writes when the
> connection is just idle.
> 
> You can also call the servise.dispose( boolean ) method. If
> the parameter is 'true', the service will try to flush all
> the pending writes. Otherwise, it will immediately close all
> the sessions.

I think most of my issues result in the rebinding of the port - in java there is setReuseAddress(boolean on) //         Enable/disable the SO_REUSEADDR socket option for a serversockt - is there something similar for mina?

I found in the archive your post about

 acceptor.getSessionConfig().setReuseAddress

but that does not exist in 2.x



Re: Forcing the closure of any open session when mina server shuts down

Posted by Emmanuel Lécharny <el...@gmail.com>.
Le 8/20/12 2:41 PM, Harakiri a écrit :
> Hello,
>
> thanks for your input - i tried it with both close(true) and close(false) - but i would expect that close(false) blocks till all sessions are really closed - however the doc states something about flushing pending writes - i dont think there are any pending writes when the connection is just idle.

You can also call the servise.dispose( boolean ) method. If the 
parameter is 'true', the service will try to flush all the pending 
writes. Otherwise, it will immediately close all the sessions.


-- 
Regards,
Cordialement,
Emmanuel Lécharny
www.iktek.com


Re: Forcing the closure of any open session when mina server shuts down

Posted by Harakiri <ha...@yahoo.com>.
Hello,

thanks for your input - i tried it with both close(true) and close(false) - but i would expect that close(false) blocks till all sessions are really closed - however the doc states something about flushing pending writes - i dont think there are any pending writes when the connection is just idle.

The 30sec sleep is a workaround, but for me i would have to use 60sec maybe... or alternatively - when starting the server try for 60sec to open the used port again.

Anyway, there should be a better solution =/

--- On Mon, 8/20/12, Mike van Goor <mi...@probie.nl> wrote:

> From: Mike van Goor <mi...@probie.nl>
> Subject: Re: Forcing the closure of any open session when mina server shuts down
> To: users@mina.apache.org
> Date: Monday, August 20, 2012, 8:17 AM
> Hi,
> 
> I am (for now) ignoring this line as it exists before exit
> and after:
> tcp        0      0
> 127.0.0.1:56901     
>    127.0.0.1:10027 
> TIME_WAIT
> 
> Apart from that the acceptor tcp ports have been closed (as
> there is no 
> LISTEN in your second output.
> 
> As for the remaining session (the 2 listings are the same
> session):
> If you use close(true) then the traditional close handshake
> (FIN --> 
> FIN-ACK) does not take place in the correct way.
> Thus this would leave the port in the CLOSE_WAIT and other
> end FIN_WAIT2 
> state.
> 
> To solve this you normally use close(false) and wait for the
> session to 
> shutdown gracefully.
> You could built in a sleep of a few seconds to give the
> system the time 
> to close the sessions.
> 
> I haven't found a better way of doing this than doing a
> close(false) on 
> all sessions, wait for example 30 seconds, and then a
> close(true).
> If anyone has a better way of handling this let me know as I
> cannot find 
> a good way to handle these situations all over the
> internet.
> 
> Maybe Emnanuel has a good solution for this.
> 
> Hope my comments help you....
> 
> Regards,
> Mike
> 
> Op 20-8-2012 13:56, Harakiri schreef:
> > Hello,
> >
> > thanks for the suggestion - i tried it out but it does
> not seem to work - the server listener port is closed (this
> worked for me before) however the client who connects still
> has the sessions opened. After doing a close(true) on all
> sessions - and confirming with
> acceptor.getManagedSessions().size() that there are indeed 0
> sessions left - im puzzled who is to blame - the mina server
> or postfix ?
> >
> > Here is the netstat output after at least one session
> has been created
> > (there are 2 mina services running, at 10027 and
> 10028)
> >
> > tcp        0      0
> 127.0.0.1:10027     
>    0.0.0.0:*       
>        LISTEN
> > tcp        0      0
> 127.0.0.1:10028     
>    0.0.0.0:*       
>        LISTEN
> > tcp        0      0
> 127.0.0.1:54751     
>    127.0.0.1:10028     
>    ESTABLISHED
> > tcp        0      0
> 127.0.0.1:56901     
>    127.0.0.1:10027     
>    TIME_WAIT
> > tcp        0      0
> 127.0.0.1:10028     
>    127.0.0.1:54751     
>    ESTABLISHED
> >
> > after i shutdown the server and a debug output
> confirmed that the address was unbind, and 0 sessions are
> left - im left with this
> >
> > tcp        1      0
> 127.0.0.1:54751     
>    127.0.0.1:10028     
>    CLOSE_WAIT
> > tcp        0      0
> 127.0.0.1:56901     
>    127.0.0.1:10027     
>    TIME_WAIT
> > tcp        0      0
> 127.0.0.1:10028     
>    127.0.0.1:54751     
>    FIN_WAIT2
> >
> > the client is still connecting to 10027 and 28, and for
> some reason the server port for 10028 is still open but on
> fin_wait2.
> >
> > I also tried adding  acceptor.dispose(); after
> unbind - no dice.
> >
> > Any further ideas?
> >
> > Thanks
> >
> >
> 

Re: Forcing the closure of any open session when mina server shuts down

Posted by Mike van Goor <mi...@probie.nl>.
Hi,

I am (for now) ignoring this line as it exists before exit and after:
tcp        0      0 127.0.0.1:56901         127.0.0.1:10027 
TIME_WAIT

Apart from that the acceptor tcp ports have been closed (as there is no 
LISTEN in your second output.

As for the remaining session (the 2 listings are the same session):
If you use close(true) then the traditional close handshake (FIN --> 
FIN-ACK) does not take place in the correct way.
Thus this would leave the port in the CLOSE_WAIT and other end FIN_WAIT2 
state.

To solve this you normally use close(false) and wait for the session to 
shutdown gracefully.
You could built in a sleep of a few seconds to give the system the time 
to close the sessions.

I haven't found a better way of doing this than doing a close(false) on 
all sessions, wait for example 30 seconds, and then a close(true).
If anyone has a better way of handling this let me know as I cannot find 
a good way to handle these situations all over the internet.

Maybe Emnanuel has a good solution for this.

Hope my comments help you....

Regards,
Mike

Op 20-8-2012 13:56, Harakiri schreef:
> Hello,
>
> thanks for the suggestion - i tried it out but it does not seem to work - the server listener port is closed (this worked for me before) however the client who connects still has the sessions opened. After doing a close(true) on all sessions - and confirming with acceptor.getManagedSessions().size() that there are indeed 0 sessions left - im puzzled who is to blame - the mina server or postfix ?
>
> Here is the netstat output after at least one session has been created
> (there are 2 mina services running, at 10027 and 10028)
>
> tcp        0      0 127.0.0.1:10027         0.0.0.0:*               LISTEN
> tcp        0      0 127.0.0.1:10028         0.0.0.0:*               LISTEN
> tcp        0      0 127.0.0.1:54751         127.0.0.1:10028         ESTABLISHED
> tcp        0      0 127.0.0.1:56901         127.0.0.1:10027         TIME_WAIT
> tcp        0      0 127.0.0.1:10028         127.0.0.1:54751         ESTABLISHED
>
> after i shutdown the server and a debug output confirmed that the address was unbind, and 0 sessions are left - im left with this
>
> tcp        1      0 127.0.0.1:54751         127.0.0.1:10028         CLOSE_WAIT
> tcp        0      0 127.0.0.1:56901         127.0.0.1:10027         TIME_WAIT
> tcp        0      0 127.0.0.1:10028         127.0.0.1:54751         FIN_WAIT2
>
> the client is still connecting to 10027 and 28, and for some reason the server port for 10028 is still open but on fin_wait2.
>
> I also tried adding  acceptor.dispose(); after unbind - no dice.
>
> Any further ideas?
>
> Thanks
>
>

Re: Forcing the closure of any open session when mina server shuts down

Posted by Harakiri <ha...@yahoo.com>.
Hello,

thanks for the suggestion - i tried it out but it does not seem to work - the server listener port is closed (this worked for me before) however the client who connects still has the sessions opened. After doing a close(true) on all sessions - and confirming with acceptor.getManagedSessions().size() that there are indeed 0 sessions left - im puzzled who is to blame - the mina server or postfix ?

Here is the netstat output after at least one session has been created
(there are 2 mina services running, at 10027 and 10028)

tcp        0      0 127.0.0.1:10027         0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:10028         0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:54751         127.0.0.1:10028         ESTABLISHED
tcp        0      0 127.0.0.1:56901         127.0.0.1:10027         TIME_WAIT
tcp        0      0 127.0.0.1:10028         127.0.0.1:54751         ESTABLISHED

after i shutdown the server and a debug output confirmed that the address was unbind, and 0 sessions are left - im left with this

tcp        1      0 127.0.0.1:54751         127.0.0.1:10028         CLOSE_WAIT
tcp        0      0 127.0.0.1:56901         127.0.0.1:10027         TIME_WAIT
tcp        0      0 127.0.0.1:10028         127.0.0.1:54751         FIN_WAIT2

the client is still connecting to 10027 and 28, and for some reason the server port for 10028 is still open but on fin_wait2.

I also tried adding  acceptor.dispose(); after unbind - no dice.

Any further ideas?

Thanks

Re: Forcing the closure of any open session when mina server shuts down

Posted by Mike van Goor <mi...@probie.nl>.
Hello,

In short: You are responsible for every session that has been created 
after MINA accepted it.
If you do not close them upon shutdown, they will remain in a 
to-be-closed state for 2 minutes (I thought this was FIN-Wait, not sure).

To make sure the sessions are closed you should close them and then 
unbind the port.
You can accomplish this by using a shutdownhook added to runtime.
Like before declaring the acceptor you do:
Runtime.getRuntime().addShutdownHook(new Thread() {
    @Override
    public void run() {
      for(IoSession sess : _nioAcceptor.getManagedSessions().values())
      {
         sess.close(false); // Make this true if you want to force the 
closure
      }
      nioAcceptor.unbind();
    }
   });

Op 20-8-2012 11:49, Harakiri schreef:
> Hello,
>
> i read the FAQ entry about closing sessions here mina.apache.org/faq.html and also through the mailling list.
>
> However im not sure how i should apply it to my use case.
>
> I've created a very simple policy service similar to this example
> http://jglatre.blogspot.de/2009/03/implementing-greylist-for-postfix-with.html
>
> The faq states something about
>
> ConnectFuture cf = connector.connect(new InetSocketAddress("localhost", 8080));
>
> but i dont see how this applies to an IoAcceptor - i start my service like in the above example
>
> IoAcceptor acceptor = new NioSocketAcceptor();
>        acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ApdCodecFactory()));
>        acceptor.setHandler( new DummyApdHandler() );
>        acceptor.getSessionConfig().setReadBufferSize( 2048 );
>        acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 10 );
>        acceptor.bind( new InetSocketAddress(6666) );
>
> i would expect that upon VM shutdown that all open sessions are closed - however they are not - my postfix server keeps a session open for quiet a while - so when i restart my java server (standalone service) i get the bind error - address already in use.
>
> Any hints? I tried a shutdown hook and manually do a acceptor.unbind() but that didnt do the trick either!
>
> Thanks
>
>