开发者

Force MINA niosocketacceptor to clean up

开发者 https://www.devze.com 2023-02-18 05:53 出处:网络
I started to using mina to do async writes 开发者_如何学Goto 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 wh

I started to using mina to do async writes 开发者_如何学Goto 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


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

        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消