【问题标题】:Cannot receive embedded ActiveMQ statistics message无法接收嵌入的 ActiveMQ 统计信息
【发布时间】:2016-09-01 16:19:28
【问题描述】:

我正在尝试通过我的 JUnit 测试从嵌入式 ActiveMQ 获取我的主题的订阅统计信息。我能够订阅该主题,向该主题发送消息,并能够在我的侦听器/订阅者中接收该消息。

但是,当我尝试从该 ActiveMQ 获取统计信息时,消费者接收超时。如果我不添加“receiveTimeout”,那么消费者会无限期地等待消息。这是我的统计代码:

    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
    connectionFactory.setStatsEnabled(true);

    Connection connection = connectionFactory.createConnection();
    connection.setClientID(format("ActiveMqStatistics-%s", System.nanoTime()));
    connection.start();

    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue replyTo = session.createTemporaryQueue();
    MessageConsumer consumer = session.createConsumer(replyTo);

    String queueName = "ActiveMQ.Statistics.Subscription";
    Queue testQueue = session.createQueue(queueName);
    MessageProducer producer = session.createProducer(testQueue);
    Message msg = session.createMessage();
    msg.setJMSReplyTo(replyTo);
    producer.send(msg);

    System.out.println("Statistics request sent. Waiting to receive reply...");

    long receiveTimeout = 5000L;
    MapMessage reply = (MapMessage) consumer.receive(receiveTimeout);
    assertNotNull(reply);

    for (Enumeration e = reply.getMapNames();e.hasMoreElements();) {
        String name = e.nextElement().toString();
        System.out.println(name + "=" + reply.getObject(name));
    }

    connection.close();

我得到断言失败,因为“回复”为空。

有什么想法吗?

【问题讨论】:

  • 您的代理中是否安装了 StatisticBrokerPlugin?
  • Tim:不,我使用的是虚拟嵌入式 activemq,即代理 URL 是“vm://localhost?broker.persistent=false”。根据文档 (activemq.apache.org/statisticsplugin.html),这应该是支持的。

标签: java junit activemq


【解决方案1】:

我为了让这个工作你需要创建一个安装了统计代理插件的代理实例,它不是开箱即用的。

在 XML 配置中,您可以按如下方式启用它:

<broker ...>
  <plugins>
    <statisticsBrokerPlugin/>
  </plugins>
</broker>

或者在单元测试中,您可以使用类似于以下代码的内容创建虚拟机代理。

protected BrokerService createBroker() throws Exception {
    BrokerService answer = new BrokerService();
    BrokerPlugin[] plugins = new BrokerPlugin[1];
    plugins[0] = new StatisticsBrokerPlugin();
    answer.setPlugins(plugins);
    answer.setDeleteAllMessagesOnStartup(true);
    answer.addConnector("tcp://localhost:0");
    answer.start();
    return answer;
}

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,我找到了解决方案。我正在使用 XML 配置 activeMQ 嵌入式代理,我可以添加 statisticsBrokerPlugin,如下所示。

    <!-- lets create an embedded ActiveMQ Broker -->
        <amq:broker useJmx="false" persistent="false" enableStatistics="true" brokerName="xxx-test-broker" brokerId="xxx-test">
            <amq:transportConnectors>
                <amq:transportConnector uri="tcp://localhost:0" />
            </amq:transportConnectors>
            **<amq:plugins>
                <amq:statisticsBrokerPlugin/>
            </amq:plugins>**
        </amq:broker>
    

    所以我的 activeMQ 连接 URL 将是 vm://localhost。

    我认为这对某人有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-07
      • 2016-03-27
      • 1970-01-01
      • 1970-01-01
      • 2012-11-02
      • 2015-06-02
      • 1970-01-01
      相关资源
      最近更新 更多