3

I started to using mina to do async writes to the socket, but now I can't seem to close the sessions. Is there a way to force mina to close all the managed sessions or clean up? There what i have for the clean right now:

if(this.acceptor.isActive()) {
  for(IoSession session : this.acceptor.getManagedSessions().values()) {
      session.close(true);
  }

  this.acceptor.unbind();
  this.acceptor.dispose();
}

Thanks

Vinh
  • 141
  • 2
  • 14
  • Long shoot: could it be that isActive() is returning false because it could be in another state while also not closed? – Zecas May 23 '12 at 09:47

1 Answers1

1

Where did you put that code?

I just used for loop like below and all sessions were closed. First, run the server and start 3 clients in 10 seconds. You will see all clients' sessions will be closed when 10 secs is up.

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

public class MinaServer {

    public static void main(String[] args) throws Exception {

        IoAcceptor acceptor = new NioSocketAcceptor();
        acceptor.getFilterChain().addLast("logger", new LoggingFilter());
        acceptor.getFilterChain().addLast(
                "codec",
        new ProtocolCodecFilter(new TextLineCodecFactory(Charset
                .forName("UTF-8"))));

        acceptor.setHandler(new ServerHandler());
        acceptor.bind(new InetSocketAddress(1071));

        Thread.sleep(10000);

        if (acceptor.isActive()) {
            for (IoSession ss : acceptor.getManagedSessions().values()) {
                ss.close(true);
            }

        }
    }
}
Ricardo Cristian Ramirez
  • 1,194
  • 3
  • 20
  • 42