Netty 客户端连接关闭太慢
Netty client side connection close too slow
这是我的客户端源代码:
public class Client
{
Server server;
Logger logger;
ChannelHandlerContext responseCtx;
public Client(String host, int port, int mode, String fileName)
{
server=null;
EventLoopGroup group = new NioEventLoopGroup();
try
{
Bootstrap b = new Bootstrap();
b.group(group);
b.channel(NioSocketChannel.class);
b.remoteAddress(new InetSocketAddress(host, port));
b.handler(new MyChannelInitializer(server, mode,fileName));
ChannelFuture f = b.connect().sync();
f.channel().closeFuture().sync();
System.out.println("client started");
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
try {
group.shutdownGracefully().sync();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Calendar endTime=Calendar.getInstance();
System.out.println("client stopped "+endTime.getTimeInMillis());
}
}
public static void main(String[] args) throws Exception
{
@SuppressWarnings("unused")
Client c=new Client("localhost",1234,MyFtpServer.SENDFILE,"D:\SITO3\Documents\Xmas-20141224-310.jpg");
}
}
这是我的文件传输完整监听器源代码:
public class FileTransferCompleteListener implements ChannelFutureListener
{
Server server;
public FileTransferCompleteListener(Server server)
{
this.server=server;
}
@Override
public void operationComplete(ChannelFuture cf) throws Exception
{
Calendar endTime=Calendar.getInstance();
System.out.println("File transfer completed! "+endTime.getTimeInMillis());
if(server!=null)
server.stop();
else
cf.channel().close();
}
}
执行结果如下:
File transfer completed! 1451446521041
client started
client stopped 1451446523244
我想知道为什么关闭连接需要大约2秒。
有什么办法可以减少吗?
非常感谢
看看shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit),你可以指定quietPeriod,默认是几秒。
我不确定如果你缩短它会有什么影响。
这是我的客户端源代码:
public class Client
{
Server server;
Logger logger;
ChannelHandlerContext responseCtx;
public Client(String host, int port, int mode, String fileName)
{
server=null;
EventLoopGroup group = new NioEventLoopGroup();
try
{
Bootstrap b = new Bootstrap();
b.group(group);
b.channel(NioSocketChannel.class);
b.remoteAddress(new InetSocketAddress(host, port));
b.handler(new MyChannelInitializer(server, mode,fileName));
ChannelFuture f = b.connect().sync();
f.channel().closeFuture().sync();
System.out.println("client started");
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
try {
group.shutdownGracefully().sync();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Calendar endTime=Calendar.getInstance();
System.out.println("client stopped "+endTime.getTimeInMillis());
}
}
public static void main(String[] args) throws Exception
{
@SuppressWarnings("unused")
Client c=new Client("localhost",1234,MyFtpServer.SENDFILE,"D:\SITO3\Documents\Xmas-20141224-310.jpg");
}
}
这是我的文件传输完整监听器源代码:
public class FileTransferCompleteListener implements ChannelFutureListener
{
Server server;
public FileTransferCompleteListener(Server server)
{
this.server=server;
}
@Override
public void operationComplete(ChannelFuture cf) throws Exception
{
Calendar endTime=Calendar.getInstance();
System.out.println("File transfer completed! "+endTime.getTimeInMillis());
if(server!=null)
server.stop();
else
cf.channel().close();
}
}
执行结果如下:
File transfer completed! 1451446521041
client started
client stopped 1451446523244
我想知道为什么关闭连接需要大约2秒。 有什么办法可以减少吗?
非常感谢
看看shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit),你可以指定quietPeriod,默认是几秒。
我不确定如果你缩短它会有什么影响。