本文介绍了单线程与多线程JMS生产者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看看使用多线程生产者而不是单线程生产者会花费多少时间.我在本地计算机上设置了ActiveMQ,编写了生产者类,该类将初始化并在其构造函数中启动JMS连接.我将消息限制设置为3M,花了大约50秒钟才将所有消息推送到ActiveMQ.我只发送了一个字符串"hello world" 3M次.

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.

然后,我使用相同的生产者对象(一个连接但有多个会话),并使用线程大小为8的ExecutorService运行它.在run方法中,我将3M除以8只是为了确保每个线程发送不超过375000条消息.在这种情况下,推送所有消息大约需要60秒.

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

然后,我创建了八个生产者,每个生产者都有自己的连接,并使用ExecutorService或线程大小为8的线程来运行它们.这次花了大约68秒来推送所有3M消息.

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

我想知道,为什么单线程生产者在这里表现更好?我每个场景运行了大约10次,但结果保持不变.

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.

推荐答案

从JMS应用程序的角度来看,您已将应用程序编写为多线程.多个会话,每个会话都有各自的JMS生产者对象,这些对象由单独的线程驱动.假设您的应用程序对资源(例如锁等)没有争用.

From a JMS application perspective you have written the application to multi-threaded; multiple sessions each with their own JMS producer objects being driven by separate threads. Assuming that your application has no contention on the resources such as locks etc that is good.

就发送消息的效率而言,取决于客户端和服务器端JMS提供程序的实现效率. JMS实现中是否存在任何锁定或争用?也许它试图通过同一个套接字发送所有内容-在这种情况下存在争用.也许还有其他锁.

In terms of how efficient it is then at sending messages is up to how efficiently implemented the JMS provider is, both in terms of client and server sides pieces. Are there any locks or contention within the JMS implementation? Maybe it's trying to send everything over the same socket - in which case there's contention. Or maybe there's some other lock.

底层队列数据结构服务器端可能有一个锁服务器端.

Maybe there's a lock server side on the underlying queue data structure server side.

要回答您的问题,确实需要特定JMS提供程序的详细知识.

To answer your question really needs detailed knowledge of the specific JMS provider.

这篇关于单线程与多线程JMS生产者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 16:47