【问题标题】:activemq consumer does not return data even when queue not empty即使队列不为空,activemq消费者也不返回数据
【发布时间】:2016-03-01 11:13:21
【问题描述】:

我编写了一个示例代码来将元素添加到 activemq,然后检索它们。我成功地添加了大约 1000 个元素,但是在检索元素时,即使队列有很多元素,在检索到大约 50 到 200 个元素后,代码也会以某种方式卡住。

以下是我用于向队列添加元素的代码

@POST
@Path("/addelementtoqueue")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void addElementToQeueue(@FormParam("count") int count) throws Exception {
    IntStream.range(0, count)
        .forEach(e -> {
            try {
                addElement(e);
            }catch(Exception e1) {
                throw new RuntimeException(e1);
            }
        });
}

private void addElement(int i) throws Exception {
    Connection conn = GlobalConfiguration.getJMSConnectionFactory().createConnection();
    conn.start();
    Session session = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    MessageProducer prod = session.createProducer(queue);
    prod.send(queue, session.createTextMessage("message "+ i), DeliveryMode.PERSISTENT, 4, 0);
    prod.close();
    session.close();
    conn.close();
}

这是我用来从队列中检索元素的 sn-p

@POST
@Path("/removeelementfromqueue")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void removeElementToQeueue(@FormParam("count") int count) throws Exception {
    IntStream.range(0, count)
        .forEach(e -> {
            try {
                extractElement();
            }catch(Exception e1) {
                throw new RuntimeException(e1);
            }
        });
}

private void extractElement() throws Exception {
    Connection conn = GlobalConfiguration.getJMSConnectionFactory().createConnection();
    conn.start();
    Session session = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
    queue = session.createQueue("walkin.testing");
    MessageConsumer consumer = session.createConsumer(queue);
    TextMessage msg = (TextMessage)consumer.receive();
    System.out.println(msg.getText());
    msg.acknowledge();
    consumer.close();
    session.close();
    conn.close();
}

我通过resource.xml获取连接工厂,同样的sn-p是

<resources>    
<Resource id="MyJmsResourceAdapter" type="ActiveMQResourceAdapter">
    BrokerXmlConfig = jdbcBroker:(tcp://0.0.0.0:61616)
    ServerUrl       = tcp://0.0.0.0:61616?jms.prefetchPolicy.queuePrefetch=0
</Resource>

<Resource id="MyJmsConnectionFactory" type="javax.jms.ConnectionFactory">
    ResourceAdapter = MyJmsResourceAdapter
</Resource></resources>

我正在使用 activeMQ 5.13.1,带有 apache-tomee-plus-1.7.2 和 Java 8,jdbc 存储为 mysql。我已经配置了activemq-jdbc-performance.xml作为apache activemq的配置文件。

我已尝试对此进行大量研究,但我无法确定此问题的根本原因。如果有人可以建议我做错了什么,这将非常有帮助

【问题讨论】:

    标签: jms activemq java-ee-6 producer-consumer


    【解决方案1】:

    我建议不要为每个操作打开/关闭连接/会话/队列,而是可以使用池来最小化每个资源的需要数量。很确定连接是线程安全的,但会话不是,您需要为每个活动线程创建/使用/专用会话。通过池化,您可以最大限度地减少为当前正在运行的活动线程创建的会话数量,并在以后重用它们。

    所以我假设您遇到了资源问题,即使看起来一切都已正确关闭/释放,但有些东西不是(可能在我在这里看到的代码之外)。你检查过activemq日志吗?您是否对此进行了调试并确保在尝试创建第 n 个连接或会话时它没有挂起?

    【讨论】:

      猜你喜欢
      • 2014-06-27
      • 2016-05-27
      • 2019-03-21
      • 2012-08-11
      • 2021-12-07
      • 2018-12-22
      • 2013-08-01
      • 2019-02-26
      • 2014-02-05
      相关资源
      最近更新 更多