参考:关闭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
}
}
}