You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by "manoj.sahu" <ma...@yahoo.com> on 2011/03/04 19:39:46 UTC

Netty for TCP communication

Hi -
I am using netty to send a request over a TCP socket.  When I send the
request I need to send 2 bytes lengh of the message and then followed by an
XML payload.  On response I am getting 2 bytes length and then XML payload. 
I am assuming that I need to write a custom codec for it.  Can someone give
me some pointer / sample code example.  I am using netty in conjunction with
Apache Camel.   

I am using this as in my route :
netty:tcp://host:port/?sync=true&textline=true 

I am adding 2 bytes length by hand to the message payload. Using ByteBuffer. 
I need to send using Big Endian network byte order.

Thanks! 

--
View this message in context: http://camel.465427.n5.nabble.com/Netty-for-TCP-communication-tp3409969p3409969.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Netty for TCP communication

Posted by xiangqiuzhao <xi...@gmail.com>.
if i use registry.put("myDecoder", new MyDecoder); 

And then I also need to use the beans configure file?




--
View this message in context: http://camel.465427.n5.nabble.com/Netty-for-TCP-communication-tp3409969p5107705.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Netty for TCP communication

Posted by Claus Ibsen <cl...@gmail.com>.
On Thu, Dec 29, 2011 at 1:38 AM, xiangqiuzhao <xi...@gmail.com> wrote:
> can't use the encoder or decoder in URI? must use bean id ? i wan't to use
> Camel API only. how to ?
>

The bean must be looked up in the registry. The registry is also
available in pure Java mode (which I assume you mean by Camel API
only).

SimpleRegistry registry = new SimpleRegistry();
registry.put("myDecoder", new MyDecoder);

CamelContext context = new DefaultCamelContext(registry);


> --
> View this message in context: http://camel.465427.n5.nabble.com/Netty-for-TCP-communication-tp3409969p5106449.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Re: Netty for TCP communication

Posted by Willem Jiang <wi...@gmail.com>.
You can not pass an object instance throw the URI. That is why camel 
support to look up the instance through the registry with the bean id.

On Thu Dec 29 08:38:30 2011, xiangqiuzhao wrote:
> can't use the encoder or decoder in URI? must use bean id ? i wan't to use
> Camel API only. how to ?
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Netty-for-TCP-communication-tp3409969p5106449.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



-- 
Willem
----------------------------------
FuseSource
Web: http://www.fusesource.com
Blog:    http://willemjiang.blogspot.com (English)
         http://jnn.javaeye.com (Chinese)
Twitter: willemjiang 
Weibo: willemjiang 


Re: Netty for TCP communication

Posted by xiangqiuzhao <xi...@gmail.com>.
can't use the encoder or decoder in URI? must use bean id ? i wan't to use
Camel API only. how to ?

--
View this message in context: http://camel.465427.n5.nabble.com/Netty-for-TCP-communication-tp3409969p5106449.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Netty for TCP communication

Posted by Claus Ibsen <cl...@gmail.com>.
On Wed, Dec 28, 2011 at 2:50 PM, xiangqiuzhao <xi...@gmail.com> wrote:
> i had implements my Decoder named MessageDecoder. and use by
> from("host:port?decoder=com.test.MessageDecoder"), but where i need to put
> the class?

Hi

You need to use the # syntax to refer to a bean id, from the Registry,
which is your custom decoder.

See for example the hl7 wiki page with the hl7 codec
http://camel.apache.org/hl7



>
> second, why need use decoders and encoders? if i need implements some
> decoder, why not to implements
> a big decoder?
>
> public class MessageDecoder extends FrameDecoder {
>
>    @Override
>    protected Object decode(
>            ChannelHandlerContext ctx, Channel channel, ChannelBuffer
> buffer) throws Exception {
>        if (buffer.readableBytes() < 4) {
>            return null;//(1)
>        }
>        int dataLength = buffer.getInt(buffer.readerIndex());
>        if (buffer.readableBytes() < dataLength + 4) {
>            return null;//(2)
>        }
>
>        buffer.skipBytes(4);//(3)
>        byte[] decoded = new byte[dataLength];
>        buffer.readBytes(decoded);
>        String msg = new String(decoded);//(4)
>        return msg;
>    }
> }
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Netty-for-TCP-communication-tp3409969p5105286.html
> Sent from the Camel - Users mailing list archive at Nabble.com.



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus, fusenews
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/

Re: Netty for TCP communication

Posted by xiangqiuzhao <xi...@gmail.com>.
i had implements my Decoder named MessageDecoder. and use by
from("host:port?decoder=com.test.MessageDecoder"), but where i need to put
the class?

second, why need use decoders and encoders? if i need implements some
decoder, why not to implements
a big decoder?

public class MessageDecoder extends FrameDecoder {
 
    @Override
    protected Object decode(
            ChannelHandlerContext ctx, Channel channel, ChannelBuffer
buffer) throws Exception {
        if (buffer.readableBytes() < 4) {
            return null;//(1)
        }
        int dataLength = buffer.getInt(buffer.readerIndex());
        if (buffer.readableBytes() < dataLength + 4) {
            return null;//(2)
        }
 
        buffer.skipBytes(4);//(3)
        byte[] decoded = new byte[dataLength];
        buffer.readBytes(decoded);
        String msg = new String(decoded);//(4)
        return msg;
    }
}

--
View this message in context: http://camel.465427.n5.nabble.com/Netty-for-TCP-communication-tp3409969p5105286.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Netty for TCP communication

Posted by "manoj.sahu" <ma...@yahoo.com>.
Thanks for the guidance Claus!

I dug further on the capabilities of netty and realized that they already
have what I need.

I configured LengthFieldBasedFrameDecoder  for decoding the message and
LengthFieldPrepender for encoding the message.  It worked like a charm.  I
configured the service endpoint like you had mentioned. 




--
View this message in context: http://camel.465427.n5.nabble.com/Netty-for-TCP-communication-tp3409969p3413344.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Netty for TCP communication

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

You need to develop a custom encoder and decoded to your custom codec.
And then configure this on the Camel netty endpoint.

For example

<bean id="myEncoder" class="xxx"/>
<bean id="myDecoder" class="xxx"/>

And in Camel route

<from uri="netty:tcp:0.0.0.0:9123?encoder=#myEncoder&amp;decoder=#myDecoder&amp;sync=true"/>

And check JBoss Netty documentation for how to write custom codecs
http://www.jboss.org/netty/documentation.html


On Fri, Mar 4, 2011 at 7:39 PM, manoj.sahu <ma...@yahoo.com> wrote:
> Hi -
> I am using netty to send a request over a TCP socket.  When I send the
> request I need to send 2 bytes lengh of the message and then followed by an
> XML payload.  On response I am getting 2 bytes length and then XML payload.
> I am assuming that I need to write a custom codec for it.  Can someone give
> me some pointer / sample code example.  I am using netty in conjunction with
> Apache Camel.
>
> I am using this as in my route :
> netty:tcp://host:port/?sync=true&textline=true
>
> I am adding 2 bytes length by hand to the message payload. Using ByteBuffer.
> I need to send using Big Endian network byte order.
>
> Thanks!
>
> --
> View this message in context: http://camel.465427.n5.nabble.com/Netty-for-TCP-communication-tp3409969p3409969.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>



-- 
Claus Ibsen
-----------------
FuseSource
Email: cibsen@fusesource.com
Web: http://fusesource.com
Twitter: davsclaus
Blog: http://davsclaus.blogspot.com/
Author of Camel in Action: http://www.manning.com/ibsen/