I wanted to see how much time difference would it make to use a multi-threaded producer instead of a single threaded one. I setup an ActiveMQ on my local machine, wrote a producer class that would initialise and start a JMS connection in its constructor. I set the message limit to 3M and it took about 50 seconds to push all the messages over to ActiveMQ. I sent just one string "hello world" 3M times.
Then I used the same producer object (one connection but multiple session) and ran it with an ExecutorService of thread size eight. In the run
method, I would divide 3M by 8 just to make sure each thread sends no more than 375000 messages. It took about 60 seconds to push all the messages in this case.
ExecutorService service = Executors.newFixedThreadPool(8);
for (int i = 0; i < 8; i++) {
service.execute(producer);
}
Then I created eight producers with each producing having it's own connection and ran them with ExecutorService or thread size eight. This time it took about 68 seconds to push all 3M messages.
for (int i = 0; i < 8; i++) {
service.execute(new Producer());
}
I'm wondering, why would a single threaded producer perform better here? I ran each scenario about 10 times but the results remained the same.