<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div>Hi Guatam,</div><div><br></div><div>At first sight, this looks like it should work -- your (large) number of</div><div>tasks, will get gated through the 20 threads, which will use the 50</div><div>channels. It ought to be the case that there are always more than enough</div><div>channels to be 'in use' at any one time. The thread pool will dispatch</div><div>the tasks to threads as they become available....</div><div><br></div><div>I'll experiment to see if I can reproduce your experience, and get back</div><div>to you next week (after the bank holiday w/e here).</div><div><br></div><div>
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-variant: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div style="font-style: normal; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div style="font-family: Georgia; font-size: medium; font-weight: normal; font-style: normal; "><div><font class="Apple-style-span" size="3"><span class="Apple-style-span" style="font-size: 12px; ">Steve Powell &nbsp;</span><span class="Apple-style-span" style="font-size: 11px; ">(<i>a happy bunny</i>)</span></font></div></div><div><font class="Apple-style-span"><div style="font-weight: normal; font-style: normal; font-family: Helvetica; font-size: 12px; "><i><font class="Apple-style-span" face="Georgia" size="2"><span class="Apple-style-span" style="font-size: 10px; ">----------some more definitions from the SPD----------</span></font></i></div></font></div></div></div><span class="Apple-style-span" style="font-style: normal; font-weight: normal; font-family: Helvetica; font-size: 12px; "><font class="Apple-style-span" face="Georgia" size="2" style="font-style: normal; "><span class="Apple-style-span" style="font-size: 10px; "><b>chinchilla</b>&nbsp;(</span></font><font class="Apple-style-span" face="Georgia" size="2"><span class="Apple-style-span" style="font-size: 10px; "><i>n.</i>) Cooling device for the lower jaw.</span></font></span><div style="font-style: normal; font-family: Georgia; font-size: medium; font-weight: normal; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Georgia; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "></span><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-variant: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span class="Apple-style-span" style="font-family: Georgia; font-weight: normal; font-style: normal; font-size: 10px; "><b>socialcast</b></span><span class="Apple-style-span" style="font-family: Georgia; font-weight: normal; font-style: normal; font-size: 10px; ">&nbsp;(</span><span class="Apple-style-span" style="font-family: Georgia; font-weight: normal; font-size: 10px; "><i>n.</i>)</span><span class="Apple-style-span" style="font-family: Georgia; font-weight: normal; font-style: normal; font-size: 10px; ">&nbsp;Someone to whom everyone is speaking but nobody likes.</span></span></div><div style="font-family: Georgia; font-size: medium; "><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-variant: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; "><span class="Apple-style-span" style="font-family: Georgia; font-style: normal; font-size: 10px; "><b>literacy</b></span><span class="Apple-style-span" style="font-family: Georgia; font-weight: normal; font-style: normal; font-size: 10px; "><span class="Apple-converted-space">&nbsp;</span>(</span><span class="Apple-style-span" style="font-family: Georgia; font-weight: normal; font-size: 10px; "><i>n.</i></span><span class="Apple-style-span" style="font-family: Georgia; font-weight: normal; font-style: normal; font-size: 10px; ">) A textually transmitted disease usually contracted in childhood.</span></span></div></div></span></div>
</div>
<br><div><div>On 3 May 2012, at 04:34, Gautam Bakshi wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">Hi Everyone,<div><br></div><div>I'm playing around with Rabbitmq(sorry for flooding the list lately) and am having some weird problems in my tests. &nbsp;I have a multi-threaded application that I was considering using rabbitmq to manage the queue but I'm getting alot of blocking between my channels. &nbsp;Is there a&nbsp;preferred&nbsp;way to setup pools?
</div><div><br></div><div>I'm a bit new to Java so I used to the pools from apache commons but when I profile the channels are all blocking each other. &nbsp;To test if dedicated connections per thread worked better I made an example of it as well and it does not block when the same program has a single channel per connection. &nbsp;</div>
<div><br></div><div>So my question, do channels block each other or am I doing something wrong(using java api wrong or misunderstanding connection/channels)? &nbsp;Is there a more&nbsp;preferred&nbsp;way? Unrelated to the question, but I was also wondering is there any difference between channels and connections in terms of throughput(i.e. would there be any&nbsp;benefit&nbsp;of using dedicated connections ignoring the overhead in&nbsp;establishing/maintaining the connection)?</div>
<div><br></div><div>Here's the test code(it simply sends to a thread a bunch of data to write the queue). If it helps I'm using v2.7.1, installed from MacPorts):</div><div><br></div><div><br></div><div><div>import org.apache.commons.pool.BasePoolableObjectFactory;</div>
<div>import org.apache.commons.pool.ObjectPool;</div><div>import org.apache.commons.pool.PoolableObjectFactory;</div><div>import org.apache.commons.pool.impl.GenericObjectPool;</div><div><br></div><div>import java.io.IOException;</div>
<div>import java.util.NoSuchElementException;</div><div>import java.util.Set;</div><div>import java.util.concurrent.ExecutorService;</div><div>import java.util.concurrent.Executors;</div><div>import java.util.concurrent.LinkedBlockingDeque;</div>
<div>import java.util.concurrent.ThreadPoolExecutor;</div><div>import java.util.concurrent.TimeUnit;</div><div><br></div><div><br></div><div>import com.rabbitmq.client.ConnectionFactory;</div><div>import com.rabbitmq.client.Connection;</div>
<div>import com.rabbitmq.client.Channel;</div><div>import com.rabbitmq.client.MessageProperties;</div><div><br></div><div>public class PoolExample {</div><div>&nbsp; &nbsp;&nbsp;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>private static ExecutorService executor_worker;</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>static {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; final int numberOfThreads_ThreadPoolExecutor = 20;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; executor_worker = Executors.newFixedThreadPool(numberOfThreads_ThreadPoolExecutor);</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">        </span></div>
<div>&nbsp; &nbsp; public static void main(String[] args) throws Exception {</div><div>&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">        </span>System.out.println("starting.."); <span class="Apple-tab-span" style="white-space:pre">        </span> &nbsp; <span class="Apple-tab-span" style="white-space:pre">                </span></div>
<div>&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">        </span>ObjectPool&lt;Channel&gt; pool =</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new GenericObjectPool&lt;Channel&gt;(</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new ConnectionPoolableObjectFactory(), 50);</div>
<div>&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">        </span>for (int x = 0; x&lt;500000000; x++) {</div><div>&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">                </span>executor_worker.submit(new MyRunnable(x, pool));</div>
<div>&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div>&nbsp; &nbsp; }</div><div>}</div><div><br></div><div>/*</div><div>&nbsp;//this is a version that creates its own connection per channel</div><div>&nbsp;class ConnectionPoolableObjectFactory</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; extends BasePoolableObjectFactory&lt;Channel&gt; {</div><div><br></div><div>&nbsp; &nbsp; private static final ConnectionFactory factory = newConnectionFactory();</div><div><br></div><div>&nbsp; &nbsp; private static ConnectionFactory newConnectionFactory() {</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; ConnectionFactory factory = new ConnectionFactory();</div><div>&nbsp; &nbsp; &nbsp; &nbsp; factory.setHost("localhost");</div><div>&nbsp; &nbsp; &nbsp; &nbsp; return factory;</div><div>&nbsp; &nbsp; }</div><div><br></div><div>&nbsp; &nbsp; @Override</div><div>
&nbsp; &nbsp; public Channel makeObject() throws Exception {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("new channel");</div><div>&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">        </span>return factory.newConnection().createChannel();</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; }</div><div><br></div><div>&nbsp; &nbsp; @Override</div><div>&nbsp; &nbsp; public boolean validateObject(Channel channel) {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; return channel.isOpen();</div><div>&nbsp; &nbsp; }</div><div><br></div><div>&nbsp; &nbsp; @Override</div>
<div>&nbsp; &nbsp; public void destroyObject(Channel channel) throws Exception {</div><div>&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">        </span>System.out.println("closing");</div><div>&nbsp; &nbsp; &nbsp; &nbsp; channel.close();</div>
<div>&nbsp; &nbsp; }</div><div><br></div><div>&nbsp; &nbsp; @Override</div><div>&nbsp; &nbsp; public void passivateObject(Channel channel) throws Exception {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; //System.out.println("sent back to queue");</div><div>&nbsp; &nbsp; }</div>
<div>}</div><div>*/</div><div><br></div><div>//this version pools channels&nbsp;</div><div>class ConnectionPoolableObjectFactory extends BasePoolableObjectFactory&lt;Channel&gt; {</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>private final Connection connection;</div>
<div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>ConnectionPoolableObjectFactory() throws IOException {</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>ConnectionFactory factory = new ConnectionFactory();</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>factory.setHost("localhost");</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>connection = factory.newConnection();</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>@Override</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>public Channel makeObject() throws Exception {</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>return connection.createChannel();</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>@Override</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>public boolean validateObject(Channel channel) {</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>return channel.isOpen();</div><div>
<span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>@Override</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>public void destroyObject(Channel channel) throws Exception {</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>channel.close();</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>@Override</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>public void passivateObject(Channel channel) throws Exception {</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>//do nothing</div><div>
<span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div>}</div><div><br></div><div><br></div><div>class MyRunnable implements Runnable{ &nbsp;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>protected int x = 0;</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>protected ObjectPool&lt;Channel&gt; pool;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span></div><div>&nbsp; &nbsp; public MyRunnable(int x, ObjectPool&lt;Channel&gt; pool) {</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>// TODO Auto-generated constructor stub</div><div>&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">        </span>this.x = x;</div><div>&nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">        </span>this.pool = pool;</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>public void run(){</div><div>&nbsp; &nbsp; &nbsp; &nbsp; try {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Channel channel = pool.borrowObject();</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">        </span>String message = Integer.toString(x);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">        </span>channel.basicPublish( "", "task_queue",&nbsp;</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MessageProperties.PERSISTENT_TEXT_PLAIN,</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message.getBytes());</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="Apple-tab-span" style="white-space:pre">        </span>pool.returnObject(channel);</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>} catch (NoSuchElementException e) {</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>// TODO Auto-generated catch block</div><div>
<span class="Apple-tab-span" style="white-space:pre">                        </span>e.printStackTrace();</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>} catch (IllegalStateException e) {</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>// TODO Auto-generated catch block</div>
<div><span class="Apple-tab-span" style="white-space:pre">                        </span>e.printStackTrace();</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>} catch (Exception e) {</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>// TODO Auto-generated catch block</div>
<div><span class="Apple-tab-span" style="white-space:pre">                        </span>e.printStackTrace();</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>}&nbsp;</div><div>&nbsp; &nbsp; }</div><div>}</div><div><br></div></div>
_______________________________________________<br>rabbitmq-discuss mailing list<br><a href="mailto:rabbitmq-discuss@lists.rabbitmq.com">rabbitmq-discuss@lists.rabbitmq.com</a><br><a href="https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss">https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss</a><br></blockquote></div><br></div></body></html>