Java client lib: up to 5 threads created in client when server publishes new message

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Java client lib: up to 5 threads created in client when server publishes new message

sharonbn
Hello,

I created a simple client in Java, which consumes messages from a queue in a loop.
what I see in the debugger, is that whenever I publish a new message into queue "q0"
a new thread is opened in the client.  until 5 threads are open and running.
(this is besides the single thread named "AMQP Connection")
the threads begin when the messages are published, not when they are consumed...

is this expected behavior? can this be changed?

here is the stack trace, the client code is below.

Test at localhost:62105
        Thread [main] (Suspended (breakpoint at line 19 in Test))
                Test.main(String...) line: 19
        Thread [AMQP Connection 204.236.192.179:1883] (Running)
        Thread [pool-1-thread-1] (Running)
        Thread [pool-1-thread-2] (Running)
        Thread [pool-1-thread-3] (Running)
        Thread [pool-1-thread-4] (Running)
        Thread [pool-1-thread-5] (Running)

thanks,
Sharon.


import com.rabbitmq.client.*;

public class Test
{
        static Connection connection;
        static Channel channel;
        static QueueingConsumer consumer;

        public static void main (String... args) throws Exception
        {
                ConnectionFactory factory = new ConnectionFactory();
                factory.setHost(Constants.server);
                factory.setPort(Constants.port);
                connection = factory.newConnection();
                channel = connection.createChannel();
                consumer = new QueueingConsumer(channel);
                channel.basicConsume("q0", true, consumer);

                for (int i = 0 ; i < 10 ; i++) {
                        QueueingConsumer.Delivery d = consumer.nextDelivery();
                        System.out.println(" got: " + new String(d.getBody()));
                        try { Thread.sleep(1000); } catch (InterruptedException e) {}
                }
        }
}
Reply | Threaded
Open this post in threaded view
|

Re: Java client lib: up to 5 threads created in client when server publishes new message

Simon MacMullen-2
On 02/09/12 14:06, sharonbn wrote:
> Hello,

Hi.

> I created a simple client in Java, which consumes messages from a queue in a
> loop.
> what I see in the debugger, is that whenever I publish a new message into
> queue "q0"
> a new thread is opened in the client.  until 5 threads are open and running.
> (this is besides the single thread named "AMQP Connection")
> the threads begin when the messages are published, not when they are
> consumed...
>
> is this expected behavior?

Yes. This is the thread pool for consumer callbacks to get executed in.

> can this be changed?

Yes, invoke ConnectionFactory.newConnection(ExecutorService, Address[])
with a different ExecutorService.

Cheers, Simon

> here is the stack trace, the client code is below.
>
> Test at localhost:62105
> Thread [main] (Suspended (breakpoint at line 19 in Test))
> Test.main(String...) line: 19
> Thread [AMQP Connection 204.236.192.179:1883] (Running)
> Thread [pool-1-thread-1] (Running)
> Thread [pool-1-thread-2] (Running)
> Thread [pool-1-thread-3] (Running)
> Thread [pool-1-thread-4] (Running)
> Thread [pool-1-thread-5] (Running)
>
> thanks,
> Sharon.
>
>
> import com.rabbitmq.client.*;
>
> public class Test
> {
> static Connection connection;
> static Channel channel;
> static QueueingConsumer consumer;
>
> public static void main (String... args) throws Exception
> {
> ConnectionFactory factory = new ConnectionFactory();
> factory.setHost(Constants.server);
> factory.setPort(Constants.port);
> connection = factory.newConnection();
> channel = connection.createChannel();
> consumer = new QueueingConsumer(channel);
> channel.basicConsume("q0", true, consumer);
>
> for (int i = 0 ; i<  10 ; i++) {
> QueueingConsumer.Delivery d = consumer.nextDelivery();
> System.out.println(" got: " + new String(d.getBody()));
> try { Thread.sleep(1000); } catch (InterruptedException e) {}
> }
> }
> }
>
>
>
>
> --
> View this message in context: http://rabbitmq.1065348.n5.nabble.com/Java-client-lib-up-to-5-threads-created-in-client-when-server-publishes-new-message-tp21751.html
> Sent from the RabbitMQ mailing list archive at Nabble.com.
> _______________________________________________
> rabbitmq-discuss mailing list
> [hidden email]
> https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss


--
Simon MacMullen
RabbitMQ, VMware
_______________________________________________
rabbitmq-discuss mailing list
[hidden email]
https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Java client lib: up to 5 threads created in client when server publishes new message

Simon MacMullen-2
On 03/09/12 17:40, Simon MacMullen wrote:

> On 02/09/12 14:06, sharonbn wrote:
>> I created a simple client in Java, which consumes messages from a
>> queue in a
>> loop.
>> what I see in the debugger, is that whenever I publish a new message into
>> queue "q0"
>> a new thread is opened in the client. until 5 threads are open and
>> running.
>> (this is besides the single thread named "AMQP Connection")
>> the threads begin when the messages are published, not when they are
>> consumed...
>>
>> is this expected behavior?

Ah, I missed "the threads begin when the messages are published, not
when they are consumed."

The threads are started up when the message is consumed by the client
(which is likely to be very soon after you published!) But this could be
some time before you invoke QueueingConsumer.nextDelivery() - since
QueueingConsumer will of course itself queue these messages.

> Yes. This is the thread pool for consumer callbacks to get executed in.
>
>> can this be changed?
>
> Yes, invoke ConnectionFactory.newConnection(ExecutorService, Address[])
> with a different ExecutorService.

...or ConnectionFactory.newConnection(ExecutorService). See:

http://www.rabbitmq.com/api-guide.html#advanced-connection

Cheers, Simon

--
Simon MacMullen
RabbitMQ, VMware
_______________________________________________
rabbitmq-discuss mailing list
[hidden email]
https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Java client lib: up to 5 threads created in client when server publishes new message

sharonbn
thanks, your answer cleared the issue for me.