ChannelHandler instance

Bob Bucy bob.bucy at gmail.com
Thu Feb 26 16:25:12 EST 2009


Great job on Netty, loving this framework.  I am in the process of
porting some .Net services that act as a specialized proxy (e.g.
receives requests from many short-lived connections and passes the
requests through to a finite number of persistent "back end"
connections).

Usually, the messages are one-one (e.g. request -response), but some
type of messages are many-to-one (e.g. many requests received on a
channel will result in a single backend response).  The messages
consist of a Header record, followed by many Detail Records, followed
by a Trailer.  In this many-to-one case, as messages come in I need to
make sure the message is sent to the same back-end channel.

To accomplish this, I was planning on saving off the back end channel
that a message was forwarded too in my ChannelHandler , thus follow-on
messages can be sent to the same backend channel.  This is based on
the assumption that the same instance of ChannelHandler always
processes the messages decoded for a given Channel.

Based on my tests/debugging this statement is true, but I wanted to
verify before making this assumption.  Below is my
ChannelPipelineFactory source.

thanks, bob

package com.rdb.nettypoc.server;

import static org.jboss.netty.channel.Channels.pipeline;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.handler.execution.ExecutionHandler;
import org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor;

public class NPOCRelayServicePipelineFactory implements ChannelPipelineFactory {

	private RelayService _service;

	// OrderedMemoryAwareThreadPoolExecutor will single thread the
	// requests read from the client channel.
	private ExecutionHandler execHandler = new ExecutionHandler(
			new OrderedMemoryAwareThreadPoolExecutor(1000, 1048576, 1048576));

	// MemoryAwareThreadPoolExecutor will multi thread the request
	// that are read from the client channel.
	// private ExecutionHandler execHandler =
	// new ExecutionHandler(new MemoryAwareThreadPoolExecutor(1000, 1048576,
	// 1048576));

	public NPOCRelayServicePipelineFactory(RelayService service) {
		this._service = service;
	}

	public ChannelPipeline getPipeline() throws Exception {
		ChannelPipeline pipeline = pipeline();

		// Add the number codec first,
		pipeline.addLast("decoder", new LengthPrefixDecoder());
		pipeline.addLast("encoder", new LengthPrefixEncoder());

		// pipeline.addLast("executor", new ExecutionHandler(new
		// MemoryAwareThreadPoolExecutor(16, 1048576, 1048576)));
		pipeline.addLast("executor", execHandler);

		// and then business logic.
		pipeline.addLast("handler", new NPOCRelayServiceHandler(_service));

		return pipeline;
	}
}



More information about the netty-users mailing list