mina关闭TCP连接

参考:关闭mina服务_Mina 解决请求后直接关闭连接 | 学步园_weixin_39949506的博客-CSDN博客

mina开启服务后,建立TCP连接,但释放资源后,TCP连接一直没有断,会持续一段时间。关闭连接:

@Override

publicvoidsessionCreated(IoSession session)throwsException {undefined

// TODO Auto-generated method stub

SocketSessionConfig cfg = (SocketSessionConfig) session.getConfig();

cfg.setReceiveBufferSize(2*1024*1024);

cfg.setReadBufferSize(2*1024*1024);

cfg.setKeepAlive(true);

cfg.setSoLinger(0);//这个是根本解决问题的设置

}


或者:
  ioSession = cf.awaitUninterruptibly().getSession();
        SocketSessionConfig  iscf=(SocketSessionConfig)ioSession.getConfig();
       iscf.setSoLinger(0);//这个是根本解决问题的设置

  public synchronized void closeSession() {
    if(cf!=null && ioSession.isConnected()){
        ioSession.close(true);
    }

        if (cf != null) {
           boolean flag=cf.isCanceled();
            if(!flag){
                cf.cancel();
            }
        
        }
        cf = null;

        if (connector != null) {
            connector.dispose();
        }
        connector = null;
        this.setOmsStatus(false);
        try {
            es.awaitTermination(1, TimeUnit.MILLISECONDS); //额外添加
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        es.shutdown();
        
    }

public class TCPTest {

    public static ConnectFuture cf;
    protected SocketConnector connector;
    public static IoSession ioSession;

xxxxxxxx

    protected IoHandler handler = null;

    public boolean clientStatus = false;
    
    ExecutorService es; //额外添加  zhou
  //  ExecutorFilter efr;
    public synchronized boolean connect() throws TimeoutException {
        InetSocketAddress ia=  new InetSocketAddress(hostname, port);
        cf = connector.connect(ia);
        
        boolean completed = cf.awaitUninterruptibly(connectTimeoutMillis);
        // 30秒未连接上,抛出超时异常
        if (!completed) {
            throw new TimeoutException();
        }
        if (!cf.isConnected()) {
            // 即getValue() instanceof IoSession == false,也就是说出现异常或Canceled
            throw new TimeoutException();
        }
        
        if (cf.isDone()) {
            if (!cf.isConnected()) {
                connector.dispose();
                connector = null;
                cf.cancel();
                cf = null;
                return false;
            }
        }

        ioSession = cf.awaitUninterruptibly().getSession();
        SocketSessionConfig  iscf=(SocketSessionConfig)ioSession.getConfig();
       iscf.setSoLinger(0);//这个是根本解决问题的设置
        return true;
    }

    public static boolean isConnected() {
      //  return cf != null && cf.isConnected();
        boolean flag=cf!=null;
        if(cf!=null){
            boolean flag2=cf.isConnected();
            return flag && flag2;
        }else{
            return flag;
        }
       // return flag && flag2;
    }

    public synchronized void close() {
        
        Set<String> keys = PKGClient.pkgTable.keySet();
        for (String key : keys) {
            PKG pkg = PKGClient.pkgTable.get(key);
            synchronized (pkg) {
                pkg.notifyAll();
            }
        }
        PKGClient.pkgTable = new Hashtable(); //修改 从 PKGClient.pkgTable = null,修改为 new Hashtable()
        closeSession();
      //  connector.dispose();
        
    }

    public synchronized void closeSession() {
    if(cf!=null && ioSession.isConnected()){
        ioSession.close(true);
    }

        if (cf != null) {
           boolean flag=cf.isCanceled();
            if(!flag){
                cf.cancel();
            }
        
        }
        cf = null;

        if (connector != null) {
            connector.dispose();
        }
        connector = null;
        this.setOmsStatus(false);
        try {
            es.awaitTermination(1, TimeUnit.MILLISECONDS); //额外添加
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        es.shutdown();
        
    }

    protected void initParas(){
                  xxx
    }

   
    public void initConnection() throws Exception {
        
        if (!isConnected()) {
            this.initParas();
            setIoHandler();

            if (cf == null || !cf.isConnected()) {
                connector = new NioSocketConnector();
                es=Executors.newCachedThreadPool();
                
                // Configure the service.
                ProtocolCodecFilter filter = new ProtocolCodecFilter(new ObjectSerializationCodecFactory());// 设定这个过滤器将以对象为单位读取数据
                connector.getFilterChain().addLast("OMS_codec", filter);
               // connector.getFilterChain().addLast("OMS_threadPool", new ExecutorFilter(Executors.newCachedThreadPool()));
                connector.getFilterChain().addLast("OMS_threadPool", new ExecutorFilter(es)); //
                // 读写通道30秒内无操作进入空闲状态
                connector.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, idleTime);
                connector.setHandler(getIoHandler());
                boolean flag = connect();

                if (!flag) {
                    this.setOmsStatus(false);
                    throw new Exception("SERVER未连接成功!");
                }
            }
        }
    }

  

    /**
     * 断开 server连接
     */
    public static void closeConnection() throws Exception{
        if (Client.isConnected()){    xxx
           lient.cf.cancel();            xxxxxxxxxx
        }
    }
    
 
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值