简体   繁体   English

使用Java监视队列

[英]Monitoring Queue using Java

I am trying to monitor a queue without using any API such as Hermes or GEMS ie I want to use purely JAVA. 我试图在不使用任何API(如Hermes或GEMS)的情况下监视队列,即我想使用纯粹的JAVA。 SO in order to browse the queue ie check if message has reached the queue or not without actually consuming the message I have written below piece of code: 因此,为了浏览队列,即检查消息是否已到达队列而不实际消耗我在下面写的代码消息:

javax.jms.QueueBrowser browser = session.createBrowser(queue);

        Enumeration msgs = browser.getEnumeration();

        int Count=0;

        while(msgs.hasMoreElements())
        {
            message = (javax.jms.Message)msgs.nextElement();
            System.out.println("Message"+message);
            Count++;
        }

However when I am publishing the messages on queue it is not displaying the result. 但是,当我在队列中发布消息时,它不显示结果。 I have verified that messages are reaching the queue and same are being consumed by the receiver. 我已经验证了消息到达队列并且接收器正在消耗相同的消息。 So as this approach was not working I thought to use a different approach which is by counting the numberOfMessages recevied by queue before and after. 因为这种方法不起作用,我想使用一种不同的方法,即通过计算队列之前和之后接收的numberOfMessages。 So I used the below piece of code 所以我使用下面的代码

QueueInfo q= new QueueInfo(queueName);
    long l=q.getInTransitMessageCount();
    System.out.println("In transit Mesasge Count="+l+"\n");

But this is also not working. 但这也行不通。 Any suggestion or explanation to resolve this problem would be highly appreciated. 任何有关解决此问题的建议或解释都将受到高度赞赏。 Please note there is no compilation error in the code and all the necessary classes are imported. 请注意,代码中没有编译错误,导入了所有必需的类。

The code you are using will count the number of messages in the queue at a single moment in time. 您使用的代码将在一个时刻计算队列中的消息数。 Messages posted and then consumed outside this timeslice will not show up and messages are consumed very quickly in most JMS implementations. 在大多数JMS实现中,在此时间片之外发布然后消息的消息将不会显示并且消息会非常快速地消耗。

When we monitor an active message queue we are only concerned if there are messages in the queue as this means the consumer has stopped or is not consuming fast enough. 当我们监视活动消息队列时,我们只关心队列中是否有消息,因为这意味着消费者已经停止或消耗得不够快。

If you are trying to count the messages going past then you will need to intercept the messages (ie consume them yourself and post them to another queue). 如果您正在尝试计算过去的消息,那么您将需要拦截消息(即自己使用它们并将它们发布到另一个队列)。

You might want to give more information about what you really want. 您可能想要提供有关您真正想要的更多信息。

你的浏览器代码似乎是正确的..检查你是否正在传递正确的队列参数..或只是调试,看看为什么不执行该循环?

QueueBrowser does not need to give you a snapshot. QueueBrowser不需要为您提供快照。 IF you have other consumers on the queue, then they will be draining it, so it's possible that they are being pulled off. 如果队列中有其他消费者,那么他们将会消耗它,因此它们可能会被取消。 Please try turning off your consumer first to see what happens. 请先尝试关闭您的消费者,看看会发生什么。 See [1] 见[1]

[1] http://docs.oracle.com/javaee/1.3/api/javax/jms/QueueBrowser.html [1] http://docs.oracle.com/javaee/1.3/api/javax/jms/QueueBrowser.html

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

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