如何加快JMS MDB队列消耗?

时间:2016-01-21 20:51:41

标签: java ejb jms wildfly message-driven-bean

我在使用WildFly 8,java 1.8时遇到了MDB队列消耗速度问题。为了说明问题,我编写了一个简单的测试应用程序,它执行以下操作:servlet向MDB发送100条消息,MDB模拟一些处理并转到下一条消息。问题是MDB开始处理下一条消息需要40毫秒以上(在具有相同WildFly的其他类似PC上需要不到5毫秒)。

有没有人有类似的问题?我猜它是一些平庸的java配置问题,我无法弄清楚。对于更复杂的消息,消费应该花费大约10-15ms,对我来说可能需要150-200ms。

Servlet代码:

String destinationName = "java:/queue/test";
    PrintWriter out = response.getWriter();
    Context ic = null;
    ConnectionFactory cf = null;
    Connection connection = null;

    try {
        ic = new InitialContext();

        cf = (ConnectionFactory) ic.lookup("/ConnectionFactory");
        Queue queue = (Queue) ic.lookup(destinationName);

        connection = cf.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer publisher = session.createProducer(queue);

        connection.start();

        for (int i = 0; i < 100; i++) {
            TextMessage message = session.createTextMessage("Hello " + i);
            publisher.send(message);
            LOGGER.info("Sent msg " + i);
        }
        out.println("Message sento to the JMS Provider");

    } catch (Exception exc) {
        exc.printStackTrace();
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }

MDB代码:

@MessageDriven(
    activationConfig = { 
            @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/test"), 
            @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
            @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
            @ActivationConfigProperty(propertyName = "maxSession", propertyValue = "1")
    }, 
    name = "ConsumerMdb")
public class ConsumerMdb implements MessageListener {
    private static final Logger LOGGER = Logger.getLogger(ConsumerMdb.class.getName());
    private static int globalId = 1;
    private int id;
    public ConsumerMdb() { 
        id = ++globalId;
        LOGGER.warning("created mdb " + id);
    }
    public void onMessage(Message message) {
        LOGGER.info("IN ON MSG " + id);
        try {
            Thread.sleep(50);
            TextMessage msg = (TextMessage) message;
            LOGGER.info("Proccessing: " + msg.getText());
        } catch (Exception e) {
            e.printStackTrace();
        }
        LOGGER.info("OUT ON MSG");
    }

}

记录的问题是(&#39; OUT ON MSG&#39;&#39; IN ON MSG&#39;之间的时差):

21:36:24,414 INFO  [mdb.ConsumerMDB] (Thread-0 (HornetQ-client-global-threads-929329197)) IN ON MSG 4
21:36:24,465 INFO  [mdb.ConsumerMDB] (Thread-0 (HornetQ-client-global-threads-929329197)) Proccessing: Hello 91
21:36:24,465 INFO  [mdb.ConsumerMDB] (Thread-0 (HornetQ-client-global-threads-929329197)) OUT ON MSG
21:36:24,502 INFO  [mdb.ConsumerMDB] (Thread-0 (HornetQ-client-global-threads-929329197)) IN ON MSG 4
21:36:24,553 INFO  [mdb.ConsumerMDB] (Thread-0 (HornetQ-client-global-threads-929329197)) Proccessing: Hello 92
21:36:24,553 INFO  [mdb.ConsumerMDB] (Thread-0 (HornetQ-client-global-threads-929329197)) OUT ON MSG
21:36:24,587 INFO  [mdb.ConsumerMDB] (Thread-0 (HornetQ-client-global-threads-929329197)) IN ON MSG 4
21:36:24,637 INFO  [mdb.ConsumerMDB] (Thread-0 (HornetQ-client-global-threads-929329197)) Proccessing: Hello 93
21:36:24,637 INFO  [mdb.ConsumerMDB] (Thread-0 (HornetQ-client-global-threads-929329197)) OUT ON MSG

0 个答案:

没有答案