2
votes

I am getting messages over TCP in standard xml format, which I am listening to over my netty client. How should my pipeline be written so that I can parse strings starting and ending with <abc>....</abc> ?

I am guessing there should be a String decoder and another custom handler (netty defined or otherwise) that deals with the parsing of XML from the TCP stream.

Something like the following?

public Bootstrap createBootstrap(final Bootstrap b, EventLoopGroup eventLoop) {
    b.group(workerGroup);
    b.channel(NioSocketChannel.class);
    ReconnectionClient reconnectionClient = new ReconnectionClient(this);
    b.handler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast(reconnectionClient);
            pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));             
            pipeline.addLast(new ClientHandler()); // deals with String parsing                
        }
    });

    // Use command line arguments to pass the socket
    b.connect(serverIP, serverPort).addListener(new ConnectionListener(this));
    return b;
}

The second part of my question is how do I parse individual XML strings that are transmitted over TCP? As in, what code should be written in ClientHandler() that implements this functionality?

1

1 Answers

2
votes

If you just want to decode XML with netty you can use the inbuild XML-decoder (this handler doesn't need a StringDecoder). If you want to start process the XML-document after an <abc> tag you can simply wait for a XmlElementStart object whichs name is abc. The same thing goes for the close tag but with a XmlElementEnd object.