簡體   English   中英

JMS生產者和消費者花費太多時間。 為什么?

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

我已經使用ActiveMQ作為代理和JMS來接收異步。 消息進入隊列后,便開始使用這些消息。 為此,我有書面的生產者和消費者代碼。 一切工作正常,但整個過程花費了大約2-3分鍾的時間來處理10000條記錄。(我已使用一個循環進行仿真)以下是整個代碼:

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

這是偵聽器代碼:

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

我第一次這樣做。 誰能告訴我為什么這個過程要花太多時間? 我究竟做錯了什么?

我不確定您所期望的SLA,但是您的消息代理正在以80消息/秒(大約)的速度工作,這還不錯。

代碼的一個問題是session.createProducer(queue)是時間滯后的問題,因為它是一項昂貴的操作(花費時間),並且您可以使用單個生產者對象來產生多個消息。

因此,在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);
}

您必須在finally塊中同時close producer對象和session對象。

PS:另外,作為一個附帶說明,如果將生產者class名稱命名為JmsMessagePrducer而不是JmsMessageListener ,那將是很好的,因為通常我們僅將名稱用作JMS使用者的Listener

更新:

我只想知道每秒處理約80條消息是否很好? 食用后也沒有任何操作。 如果從隊列使用消息后添加更多任務,例如在db中插入或進行某些業務操作,該怎么辦?

不知道哪個Server / OS / etc ...(需要考慮許多參數),簡單地說80消息/秒或50消息/秒會更好是不明智的。 關於性能需求,您需要首先指定/定義需求。

如果您當前的代碼每秒處理大約80條消息,那么在給定條件下,這就是您的應用程序(測試程序)基准測試。 因此,如果您認為它不滿足您的性能要求,那么您需要配置多個JMS偵聽器以並行處理消息並更改設計。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM