Vinh
Vinh

Reputation: 141

Force MINA niosocketacceptor to clean up

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

Upvotes: 3

Views: 2238

Answers (1)

Ricardo Cristian Ramirez
Ricardo Cristian Ramirez

Reputation: 1234

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);
            }

        }
    }
}

Upvotes: 1

Related Questions