Hi,

If producerPoolEnabled=false and there are two concurrent requests, the
netty producer doesn't know which response is from what request and it
assumes both responses are from the last request sent. This is because when
producerPoolEnabled is false, the channel is used concurrenty, as opposed to
when there is a pool and each request has its own channel. When used
concurrently, the attachment set to the channel is overwritten by later
outgoing requests.

Here is a test.

public class CamelNettyConcurrencyTest extends CamelTestSupport {

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

                        @Override
                        public void configure() throws Exception {
                                from("direct:out")
                                                
.to("netty:tcp://localhost:12345?producerPoolEnabled=false");

                                
from("netty:tcp://localhost:12345").to("log:log");
                        }
                };
        }

        @Test
        public void test() throws InterruptedException {
                Thread[] threads = new Thread[2];
                final int[] results = new int[threads.length];
                for (int i = 0; i < threads.length; i++) {
                        final int current = i;
                        threads[i] = new Thread() {
                                @Override
                                public void run() {
                                        int result = 
template.requestBody("direct:out", current,
                                                        Integer.class);
                                        results[current] = result;
                                        System.out.println(current + " 
finished");
                                }
                        };
                }
                for (Thread thread : threads) {
                        thread.start();
                }
                for (Thread thread : threads) {
                        thread.join();
                }
                for (int i = 0; i < threads.length; i++) {
                        Assert.assertEquals(i, results[i]);
                }

        }
}


I think this problems goes beyond this particular case. When using a pool,
it might happen too. This is because the channel is returned to the pool as
soon as write is completed, not when the response is processed. In that
case, the attachment set to the channel can be overriden by other outcoming
request. 

For example:

- Request with body "0" is going to be sent so a channel is reserved
- Request with body "0" is sent through the channel
- The channel is returned to the pool
- Request with body "1" is going to be sent so the same channel is reserved
- Request with body "1" is sent through the same channel and the new
attachment overrides the previous one
- Response from "0" comes and Camel assumes it is from last request

Any ideas?

Thanks.




--
View this message in context: 
http://camel.465427.n5.nabble.com/Concurrency-problem-with-Netty-and-producerPoolEnabled-false-tp5739679.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Reply via email to