camel-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tenderice <>
Subject Re: camel-netty bug and the need of best practice for creating referenced parameter object on looking-up
Date Tue, 10 Apr 2012 06:10:07 GMT
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.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

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

	private Processor processor = new P();

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

		// START SNIPPET: registry-beans
		LengthFieldBasedFrameDecoder lengthDecoder = new
				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;

	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))
		byte[] body9999 = (new String(lengthHeader) + new String(s9999))

		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);


		mock.await(10, TimeUnit.SECONDS);


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

			public void configure() throws Exception {

	private static Socket getSocket(String host, int port)
			throws UnknownHostException, IOException {
		Socket s = new Socket(host, port);
		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);
		} catch (Exception e) {
			throw (e);
		} finally {

	class P implements Processor {

		public void process(Exchange exchange) throws Exception {
			System.out.println(new String(
					((BigEndianHeapChannelBuffer) exchange.getIn().getBody())
					new String(((BigEndianHeapChannelBuffer) exchange.getIn()


View this message in context:
Sent from the Camel Development mailing list archive at

View raw message