You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by SteveR <sr...@vonage.com> on 2015/10/09 15:40:02 UTC

Upgrading to camel-netty4: How to migrate my UdpPacketDecoder?

I'm upgrading from *camel-netty* to *camel-netty4* and I see there are lots
of API changes.  Currently, with *camel-netty*, I have a route that consumes
from *netty:udp* and a *ServerPipelineFactory *that has a *UdpPacketDecoder
*that is working fine.  The idea is to detect and fire a message upstream
when netty has received each UDP datagram.  My *camel-netty* version of the
*UdpPacketDecoder* is shown below.  The idea for this *UdpPacketDecoder*
came from  Nicholas Hagen's Netty: Using Handlers
<http://www.znetdevelopment.com/blogs/2009/04/21/netty-using-handlers/>  
blog post.

I'm trying to understand how to migrate my *UdpPacketDecoder* to
*camel-netty4*?  I've just purchased and downloaded *Netty in Action Version
11* and will certainly be reading Chapter 6 first!  Also shown below is
where I'm currently at wrt my *UdpPacketDecoder* migration to
*camel-netty4*.

The basic idea of my *camel-netty* version of *UdpPacketDecoder* is to
invoke *Channels.fireMessageReceived(ctx, message, me.getRemoteAddress())*
when the received *MessageEvent* is an instance of a *ChannelBuffer* to send
it upstream to be treated as a separate Camel Exchange, else invoke
*ctx.sendUpstream(ce)*.

Any thoughts on how to finish this *UdpPacketDecoder* migration to
*camel-netty4* is much appreciated. 

  Thanks, SteveR

*camel-netty3 version:*

	package com.mission.mplr.multiprotocollistenerrouter;

	import org.jboss.netty.buffer.ChannelBuffer;
	import org.jboss.netty.buffer.ChannelBuffers;
	import org.jboss.netty.channel.ChannelEvent;
	import org.jboss.netty.channel.ChannelHandlerContext;
	import org.jboss.netty.channel.ChannelUpstreamHandler;
	import org.jboss.netty.channel.Channels;
	import org.jboss.netty.channel.MessageEvent;
	import org.slf4j.Logger;
	import org.slf4j.LoggerFactory;

	/**
	 * Upstream handler to grab each UDP datagram and fire it
	 * upstream to be treated as a separate Camel Exchange.
	 * @see
http://www.znetdevelopment.com/blogs/2009/04/21/netty-using-handlers/
	 */
	public class UdpPacketDecoder_ORIG implements ChannelUpstreamHandler {
		private final static Logger logger =
LoggerFactory.getLogger(UdpPacketDecoder_ORIG.class);

		// -------------------------------------------------
		// Upstream is incoming (i.e. from client to server)
		// -------------------------------------------------
		@Override
		public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent ce)
throws Exception {

			if(!(ce instanceof MessageEvent))
			{
				// -------------------------------------------------------
				// We're only interested in a MessageEvent that represents
				// a received UDP packet. Send this ChannelEvent to the
				// ChannelUpstreamHandler which is placed in the closest
				// upstream from the handler associated with this context.
				// -------------------------------------------------------
				ctx.sendUpstream(ce);
				logger.trace("handleUpstream(): EXIT: ChannelEvent is not a
MessageEvent");
				return;
			}

			final MessageEvent me = (MessageEvent) ce;
			if(!(me.getMessage() instanceof ChannelBuffer))
			{
				// -------------------------------------------------------
				// We're only interested in a MessageEvent that represents
				// a received UDP packet. Send this ChannelEvent to the
				// ChannelUpstreamHandler which is placed in the closest
				// upstream from the handler associated with this context.
				// -------------------------------------------------------
				ctx.sendUpstream(ce);
				logger.trace("handleUpstream(): EXIT: MessageEvent is not a
ChannelBuffer");
				return;
			}

			// -----------------------------------------------------
			// Process this MessageEvent as a ChannelBuffer and fire
			// it upstream to next handler in the server pipeline
			// -----------------------------------------------------
			final Object message = me.getMessage();
			final ChannelBuffer buffer = (ChannelBuffer) message;

			if(buffer == null || buffer.readableBytes() <= 0) {
				logger.error("ChannelBuffer is null or has no readable bytes");
				return;
			}

*			// -------------------------------------------------------------
			// Sends a "messageReceived" event to the ChannelUpstreamHandler
			// which is placed in the closest upstream from the handler
			// associated with the specified ChannelHandlerContext.
			// -------------------------------------------------------------
			Channels.fireMessageReceived(ctx, message, me.getRemoteAddress());*
		}
	}
	// ------------- end --------------


*camel-netty4 version (a work-in-progress):*

	package com.mission.mplr.multiprotocollistenerrouter;

	import io.netty.channel.ChannelHandlerContext;
	import io.netty.channel.ChannelInboundHandler; // ChannelHandler which adds
callbacks for state changes.

	/**
	 * Upstream handler to grab each UDP datagram and fire it
	 * upstream to be treated as a separate Camel Exchange.
	 * @see
https://blog.twitter.com/2013/netty-4-at-twitter-reduced-gc-overhead
	 * @see https://forums.manning.com/posts/list/33560.page
	 * @see
http://stackoverflow.com/questions/19383261/netty-channelbuffer-in-ver-4-0
	 * 
	 * @see
http://www.znetdevelopment.com/blogs/2009/04/21/netty-using-handlers/
	 */
	public class UdpPacketDecoder implements ChannelInboundHandler {
		@Override
		public void channelRegistered(ChannelHandlerContext chc) throws Exception
{
		}

		@Override
		public void channelUnregistered(ChannelHandlerContext chc) throws
Exception {
		}

		@Override
		public void channelActive(ChannelHandlerContext chc) throws Exception {
		}

		@Override
		public void channelInactive(ChannelHandlerContext chc) throws Exception
{);
		}

*		@Override
		public void channelRead(ChannelHandlerContext chc, Object o) throws
Exception {
			chc.fireChannelRead(o);
		}*

		@Override
		public void channelReadComplete(ChannelHandlerContext chc) throws
Exception {
		}

		@Override
		public void userEventTriggered(ChannelHandlerContext chc, Object o) throws
Exception {
		}

		@Override
		public void channelWritabilityChanged(ChannelHandlerContext chc) throws
Exception {
		}

		@Override
		public void exceptionCaught(ChannelHandlerContext chc, Throwable thrwbl)
throws Exception {
		}

		@Override
		public void handlerAdded(ChannelHandlerContext chc) throws Exception {
		}

		@Override
		public void handlerRemoved(ChannelHandlerContext chc) throws Exception {
		}
	}
	// ------------- end --------------






--
View this message in context: http://camel.465427.n5.nabble.com/Upgrading-to-camel-netty4-How-to-migrate-my-UdpPacketDecoder-tp5772465.html
Sent from the Camel - Users mailing list archive at Nabble.com.