clean server shutdown after requests have been answered

mineralko mineralko at yahoo.com
Wed Feb 4 12:18:01 EST 2009


if you're writing some sort of pull server, and traffic integrity is
important, then closing a channel should occur only when a message from
client is answered. so the order should be

request => response => close

since close event is added to the pipeline on shutdown (most likely from
some other thread), you need to keep the order of events. as described in
api, to shutdown the server you need to shutdown the parent channel (easy
part) and all open children channels. to keep track of open children
channels you'll need some handler that would register/unregister them on
channelOpen/channelClosed respectively and add it in the pipeline before
your business logic handler (don't add it with bootstrap.setParentHandler()
as parentHandler is used for other things).
to keep the order of events close should be called by adding a listener to
ChannelFuture in writeRequested

public void writeRequested(ChannelHandlerContext ctx, MessageEvent e)
			throws Exception {
	if (shutdown) e.getFuture().addListener(ChannelFutureListener.CLOSE);
	super.writeRequested(ctx, e);
}

this would close all channels eventually. for faster shutdown, keep track of
Channels and last ChannelFutures (ie in a HashMap) - on messageReceived
remove ChannelFuture from map (message is processing and it's response will
be available soon in writeRequested as it's a pull server). so on shutdown
add ChannelFutureListener.CLOSE to all Channels for which you have
ChannelFutures, and close the rest in writeRequested.

when all channels are closed, you need to shutdown ExecutorServices you gave
as parameters to ChannelFactory, and any Executors (ie
OrderedMemoryAwareThreadPoolExecutor) in your pipeline.

i'm sure this must be common knowledge for long time users, but it took me a
while to figure out (thanks trustin), so i hope this would help new users
get a hang of Netty.
-- 
View this message in context: http://n2.nabble.com/clean-server-shutdown-after-requests-have-been-answered-tp2270073p2270073.html
Sent from the Netty User Group mailing list archive at Nabble.com.




More information about the netty-users mailing list