Search code examples
javajmswebspherejms-topic

How to get the current number of messages in a jms topic on IBM WAS


I need to create a REST service in the Java programming language that receives JMS connection factory's JNDI name and JMS topic's JNDI name as input and should return the number of messages in the resource at the moment. The problem is getting the length of the topic from IBM WAS.

I know about the existence of TopicBrowser from Oracle, with which you can get all the messages in the topic and just count their number. But for some reason, we do not use it.

My idea is to get the SIB Destination queue length property, which is located in: Buses> Bus> Recipients> MyTopic.Space> Publication Points, in the web console.

I use:
IBM WAS 9.0. ND.
Default Message Provider.

I will be glad to any advice.


Solution

  • You can get the state of a subscriber using the admin client for example:

                              // Start by querying the objectName of the Publication Point (Topic Space on a specific node).
             AdminClient adminClient = AdminClientFactory.createAdminClient(connectProps);
             StringBuffer oNameQuery= new StringBuffer();        
             oNameQuery.append(adminClient.getDomainName()).append(":*");
             oNameQuery.append(",type=").append("SIBPublicationPoint");
             oNameQuery.append(",name=").append("Default.Topic.Space");
             oNameQuery.append(",node=").append(nodeName); 
             oNameQuery.append(",process=").append("server1"); 
             oSet= adminClient.queryNames(new ObjectName(oNameQuery.toString()), null); 
             ObjectName defaultTopicSpaceOn = (ObjectName) oSet.iterator().next();
             System.out.println("Default.Topic.Space ObjectName:"+defaultTopicSpaceOn);
    
            // Then look at each subscription storing messages in the Publication Point.
            Long depth = (Long) adminClient.invoke (defaultTopicSpaceOn, "getDepth", null, null);
            System.out.println("DefaultTopicSpace Depth:"+depth+"\n");
            SIBSubscription[] subscriptions = (SIBSubscription[]) adminClient.invoke (defaultTopicSpaceOn, "getSubscriptions", null, null);
            for (SIBSubscription subscription : subscriptions) {
              System.out.print("DefaultTopicSpace Subscription:"+subscription.getName() 
                                 +" Id:"+subscription.getId()
                                 +" SubscriberId:"+subscription.getSubscriberId()
                                 +" Selector:"+subscription.getSelector()
                                 +" Depth:"+subscription.getDepth());
              for (String topicName: subscription.getTopics())
                System.out.print(" Topic:"+topicName);
              System.out.println();
            }
    

    This produces something like:

    DefaultTopicSpace Depth:2

    DefaultTopicSpace Subscription:Default.Topic.Space Id:21974964F5B726A6C21C7E59 SubscriberId:jmsThinClient.JMSSendReceiveclientID##jmsThinClient.JMSSendReceiveSubscription Selector:null Depth:2 Topic:Topic1/*

    Api doc:https://www.ibm.com/support/knowledgecenter/SSEQTP_8.5.5/com.ibm.websphere.javadoc.doc/web/apidocs/com/ibm/websphere/sib/admin/package-summary.html