below is a very simple testcase for this problem, due to the random nature of
the bug, you can get different result each time you run the program.
However, if you create a new decoder in the pipelinefactory each time, you
get the correct result.

The idea of the test is: create two endpoint, listening on two port, send
data to these ports, first send port of data to port1, the send some thing
to port two, then send rest data to port1. we expect that port1 receive all
data correctly.
/////////////////////////////////////////////////////////////////////////////////////////////


package org.apache.camel.component.netty;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.impl.JndiRegistry;
import org.jboss.netty.buffer.BigEndianHeapChannelBuffer;
import org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder;
import org.jboss.netty.handler.codec.frame.LengthFieldPrepender;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UnsharableCodecsConflictsTest extends BaseNettyTest {
        private static final Logger logger = LoggerFactory
                        .getLogger(UnsharableCodecsConflictsTest.class);

        static final byte[] lengthHeader = { 0x00, 0x00, 0x40, 0x00 }; // 4096
bytes

        private Processor processor = new P();

        @Override
        protected JndiRegistry createRegistry() throws Exception {
                JndiRegistry registry = super.createRegistry();

                // START SNIPPET: registry-beans
                LengthFieldBasedFrameDecoder lengthDecoder = new
LengthFieldBasedFrameDecoder(
                                1048576, 0, 4, 0, 4);
                registry.bind("length-decoder", lengthDecoder);

                LengthFieldPrepender lengthEncoder = new 
LengthFieldPrepender(4);
                registry.bind("length-encoder", lengthEncoder);

                // END SNIPPET: registry-beans
                return registry;
        }

        @Test
        public void canSupplyMultipleCodecsToEndpointPipeline() throws 
Exception {
                byte[] s8888 = new byte[8192];
                byte[] s9999 = new byte[16383];
                Arrays.fill(s8888, (byte) 0x38);
                Arrays.fill(s9999, (byte) 0x39);
                byte[] body8888 = (new String(lengthHeader) + new String(s8888))
                                .getBytes();
                byte[] body9999 = (new String(lengthHeader) + new String(s9999))
                                .getBytes();

                MockEndpoint mock = getMockEndpoint("mock:result");
                mock.expectedBodiesReceived(new String(s9999)+"9");

                Socket server1 = getSocket("localhost", 8888);
                Socket server2 = getSocket("localhost", 9999);

                try {
                        sendSopBuffer(body9999, server2);
                        sendSopBuffer(body8888, server1);
                        sendSopBuffer(new String("9").getBytes(), server2);
                } catch (Exception e) {
                        logger.error("", e);
                }

                server1.close();
                server2.close();

                mock.await(10, TimeUnit.SECONDS);
                mock.assertIsSatisfied();

        }

        protected RouteBuilder createRouteBuilder() throws Exception {
                return new RouteBuilder() {

                        public void configure() throws Exception {
                                from(
                                                
"netty:tcp://localhost:8888?decoder=#length-decoder&sync=false")
                                                .process(processor);
                                from(
                                                
"netty:tcp://localhost:9999?decoder=#length-decoder&sync=false")
                                                
.process(processor).to("mock:result");
                        }
                };
        }

        private static Socket getSocket(String host, int port)
                        throws UnknownHostException, IOException {
                Socket s = new Socket(host, port);
                s.setSoTimeout(60000);
                return s;
        }

        public static void sendSopBuffer(byte[] buf, Socket server)
                        throws Exception {

                OutputStream netOut = server.getOutputStream();
                OutputStream dataOut = new BufferedOutputStream(netOut);

                try {
                        dataOut.write(buf, 0, buf.length);
                        dataOut.flush();
                } catch (Exception e) {
                        server.close();
                        throw (e);
                } finally {
                }
        }

        class P implements Processor {

                @Override
                public void process(Exchange exchange) throws Exception {
                        System.out.println(exchange.getIn().getHeaders());
                        System.out.println(new String(
                                        ((BigEndianHeapChannelBuffer) 
exchange.getIn().getBody())
                                                        .array()));
                        exchange.getOut().setBody(
                                        new 
String(((BigEndianHeapChannelBuffer) exchange.getIn()
                                                        .getBody()).array()));

                }
        }
}


--
View this message in context: 
http://camel.465427.n5.nabble.com/camel-netty-bug-and-the-need-of-best-practice-for-creating-referenced-parameter-object-on-looking-up-tp5627926p5629063.html
Sent from the Camel Development mailing list archive at Nabble.com.

Reply via email to