简体   繁体   English

JMS生产者和消费者花费太多时间。 为什么?

[英]JMS Producer and Consumer taking too much time. Why?

I have used ActiveMQ as broker and JMS to receive async. 我已经使用ActiveMQ作为代理和JMS来接收异步。 messages in queue then start consuming these messages as soon as message comes in queue. 消息进入队列后,便开始使用这些消息。 For this i have Written producer and consumer code. 为此,我有书面的生产者和消费者代码。 Everything is working fine but the entire process is taking too much time around 2-3 minutes for 10000 records.(i have used a loop just for simulation) Below is the entire code: 一切工作正常,但整个过程花费了大约2-3分钟的时间来处理10000条记录。(我已使用一个循环进行仿真)以下是整个代码:

This is JMS Producer: 这是JMS生产者:

public class JmsMessageProducer
{
    public void jmsListener(String obj) throws Exception 
    {
        BrokerService broker = BrokerFactory.createBroker(new URI("broker:(tcp://localhost:61616)"));
        broker.start();
        Connection connection = null;
        Session session = null;
        MessageProducer producer = null;
        try
        {
            long millis = System.currentTimeMillis() % 1000;
            // Producer
            ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
            connection = connectionFactory.createConnection();
            session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

            Queue queue = session.createQueue("customerQueue3");

            MessageConsumer consumer = session.createConsumer(queue);
            consumer.setMessageListener(new ConsumerMessageListener("Consumer3"));
            connection.start();

            producer = session.createProducer(queue);

            for(int i=0; i<10000; i++)
            {
                String payload = "Important Task"+i;
                Message msg = session.createTextMessage(payload);



                System.out.println("Sending text '" + payload + "'");

                producer.send(msg);
            }

            long millis2 = System.currentTimeMillis() % 1000;
            long millis3    =   millis2- millis;
            System.out.println("time taken: "+ millis3 );

        } 
        finally 
        {
            if(producer != null)
            {
                producer.close();
            }
            if (session != null)
            {
                session.close();
            }
            if (connection != null)
            {
                connection.close();
            }
            broker.stop();
        }
    }
}

This is listener code: 这是侦听器代码:

public class ConsumerMessageListener implements MessageListener 
{ 
    private String consumerName;

    public ConsumerMessageListener(String consumerName) 
    {
        this.consumerName = consumerName;
    }

    DummyAdapter adapter    =   new DummyAdapter();
    public void onMessage(Message message) 
    {
        TextMessage textMessage = (TextMessage) message;
        try
        {           
            System.out.println(consumerName + " received "+ textMessage.getText());
        //  adapter.dummy(textMessage.getText());
        } 
        catch (JMSException e)
        {
            e.printStackTrace();
        }
    }
}

I am doing this first time. 我第一次这样做。 Can anyone tell me why is this process taking too much time? 谁能告诉我为什么这个过程要花太多时间? What am i doing wrong? 我究竟做错了什么?

I am not sure about the SLA that you are expecting, but your message Broker is working at the rate of 80 messages/sec (approx.), which is NOT bad. 我不确定您所期望的SLA,但是您的消息代理正在以80消息/秒(大约)的速度工作,这还不错。

And one issue with your code is session.createProducer(queue) is the problem for the time lag as it is a costly operation (takes time) and you can use a single producer object to produce multiple messages. 代码的一个问题是session.createProducer(queue)是时间滞后的问题,因为它是一项昂贵的操作(花费时间),并且您可以使用单个生产者对象来产生多个消息。

So create MessageProducer outside the for loop as shown below: 因此,在for循环外创建MessageProducer ,如下所示:

MessageProducer producer = session.createProducer(queue);
for(int i=0; i<10000; i++) {
    String payload = "Important Task"+i;
    Message msg = session.createTextMessage(payload);

   System.out.println("Sending text '" + payload + "'");

   producer.send(msg);
}

You have to close both producer and session objects in the finally block. 您必须在finally块中同时close producer对象和session对象。

PS: Also, as a side note, it would be good if you name your producer class name as JmsMessagePrducer instead of JmsMessageListener as in general we use the name as Listener only for JMS consumers. PS:另外,作为一个附带说明,如果将生产者class名称命名为JmsMessagePrducer而不是JmsMessageListener ,那将是很好的,因为通常我们仅将名称用作JMS使用者的Listener

UPDATE: 更新:

I just want to know that is it good to process around 80 messages/sec.? 我只想知道每秒处理约80条消息是否很好? that too without any operation after consuming. 食用后也没有任何操作。 What if add more tasks after message consumption from queue, like inserting in db or some business operation. 如果从队列使用消息后添加更多任务,例如在db中插入或进行某些业务操作,该怎么办?

It is NOT wise to simply tell that 80 messages/sec is better or 50 messages/seconds is better without knowing which Server/OS/etc...(many parameters need to be considered). 不知道哪个Server / OS / etc ...(需要考虑许多参数),简单地说80消息/秒或50消息/秒会更好是不明智的。 When it comes to performance requirements you need to specify/define the requirements first. 关于性能需求,您需要首先指定/定义需求。

If your current code processes around 80 messages/sec, then that is what your application (test program) benchmark is, for the given conditions. 如果您当前的代码每秒处理大约80条消息,那么在给定条件下,这就是您的应用程序(测试程序)基准测试。 So if you feel like it is NOT meeting your performance requirements, then you need to configure multiple JMS listeners to process messages parallelly and change your design. 因此,如果您认为它不满足您的性能要求,那么您需要配置多个JMS侦听器以并行处理消息并更改设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM