Reputation: 146
Iv been doing network programming for a while and recently converted one of my projects to netty. Iv been bothered a lot by the fact that unlike with my original program, the client will freeze for about 3-5 seconds before closing, enough to make me end up force terminating it everytime because I don't want to wait for it. Is this normal for netty? am I doing something wrong?
main method:
public static void main(String[] args) throws Exception {
final SslContext sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.handler(new NetworkInitializer(sslCtx));
// Start the connection attempt.
ch = b.connect(HOST, PORT).sync().channel();
//sends the clients name to the server
askname();
//loop just does some simple gl stuff and gameplay updating, posted below
while(running){loop();
if (Display.isCloseRequested()){
running = false;
if (lastWriteFuture != null) {
lastWriteFuture.sync();
} //tells the server to shut down
write("9");
ch.closeFuture().sync();
group.shutdownGracefully();
System.out.println("herro der");
break;
}
}
} finally {
group.shutdownGracefully();
// The connection is closed automatically on shutdown.
}
}
loop class:
private static void loop() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
//main update method for my game. does calculations and stuff. I can post it if neccisary but its kinda big.
SpaceState.update();
Display.update();
Display.sync(60);
}
Upvotes: 0
Views: 345
Reputation: 12351
group.shutdownGracefully();
by default waits for about 3 seconds until all pending tasks are executed completely before it terminates itself. You can specify a different timeout value, but too small value has the risk of getting RejectedExecutionException
. If you are sure your application has no business left with the event loop group, you can specify a very small value to terminate it immediately.
Upvotes: 2