nAdminAPI code not returning current number of events in topic/queue

Hi All,

I am trying the below code to get the total number of events currently present in the topic/queue

String RNAME={realmUrl};
nSessionAttributes nsa=new nSessionAttributes(RNAME);
nRealmNode realm = new nRealmNode(nsa);

nNode nc=realm.findNode(destinationName);
nLeafNode myChannel=(nLeafNode) realm.findNode(destinationName);
Long evt = myChannel.getCurrentNumberOfEvents ();

evt is always coming as 0. Kindly suggest.

Regards
Abir Banerjee

Here you go:


                nSessionAttributes nsa = new nSessionAttributes(RNAME);
                nSession mySession=nSessionFactory.create(nsa);
		mySession.init();
                nChannelAttributes cattrib = new nChannelAttributes();
                cattrib.setName(destinationName);
                int numberOfEvents;
		int numberOfReaders;
		try{
			nQueue myQueue=mySession.findQueue(cattrib);
			nQueueDetails nqd = myQueue.getDetails();
			numberOfEvents=nqd.getNoOfEvents();
			numberOfReaders=nqd.getNoOfReaders();
		}
		catch(nChannelNotFoundException e){
			throw new ServiceException("Queue with name '"+queueName+"' does not exist");
		}	
2 Likes

Thanks a lot Prasad for the details. I have another query. I am trying to use the code snippet provided in the UM document to list the topics and queues inside realm.

public void searchNodes(nContainer container)
Enumeration children = container.getNodes();
while (children.hasMoreElements()) {
nNode child = (nNode)children.nextElement();
if (child instanceof nContainer) {
searchNodes((nContainer)child);
} else if (child instanceof nLeafNode) {
nLeafNode leaf = (nLeafNode)child;
if (leaf.isChannel) {
System.out.println(“Leaf Node “+leaf.getName()+” is a channel”);
} else if (leaf.isQueue()) {
System.out.println(“Leaf Node “+leaf.getName()+” is a queue”);
}
}
}
}

The code is not working as it is showing class cast exception from String to nNode for the below line,

nNode child = (nNode)children.nextElement();

Is there any tweak for this. Kindly suggest.

Regards
Abir

Above code worked fine for me. Below is the outcome:

Coming to TWEAKS… as you are facing cast exception try to change your line as below:
CHANGE THIS LINE:

 nNode child = (nNode)children.nextElement(); 

TO:

 Object child = children.nextElement();

Thanks the tweak is working , but the issue is only the topics under JNDI topics are showing up , if a topic is like say “ext/temp/topics/testTopic” , these types of topics and queues are not showing up , that is nested ones.

Below is the code snippet that is used in the shared section of the java service

public static String searchQueues(nContainer container)
{
Enumeration children = container.getNodes();
String arr={“”};
StringBuilder sb=new StringBuilder(“”);
while (children.hasMoreElements()) {
nNode child = (nNode)children.nextElement();
if (child instanceof nContainer) {
searchQueues((nContainer)child);
} else if (child instanceof nLeafNode) {
nLeafNode leaf = (nLeafNode)child;
if (leaf.isQueue()) {
sb.append(leaf.getName());
sb.append(“\n”);
}
}
}
arr=sb.toString().split(“\n”);
return arr;
}

public static String[] searchTopics(nContainer container) 
{
	Enumeration children = container.getNodes(); 
	String arr[]={""};
	StringBuilder sb=new StringBuilder("");
	while (children.hasMoreElements()) { 
		nNode child = (nNode)children.nextElement(); 
		if (child instanceof nContainer) { 
				searchTopics((nContainer)child); 
		} else if (child instanceof nLeafNode) { 
				nLeafNode leaf = (nLeafNode)child; 
				if (leaf.isChannel()) { 
						sb.append(leaf.getName());
						sb.append("\n");
				} 
				} 
		} 
	arr=sb.toString().split("\n");
	return arr;
} 

Now these methods are getting called as below

try
{
String RNAME={realmUrl};
nSessionAttributes nsa=new nSessionAttributes(RNAME);
nRealmNode realm = new nRealmNode(nsa);

		nContainer nc=(nContainer)realm;
		
		String topics[]=searchTopics(nc);
		String queues[]=searchQueues(nc);
		 
		
			
	}
	catch(Exception e)
	{
		errorMsg=e.toString();
	}

In the attached image , the topic , naming/defaultContext is not showing up in the result , for I believe when the Enum is getting listed , “naming” is not an instance of nContainer. Hence it is ignoring it. Kindly share your thoughts on this.

Regards
Abir Banerjee
Capture.JPG

Nope. “naming” is indeed an instance of nContainer. The issue you are facing might be because of a small code issue.

The below lines are local to “searchQueues/Topics” methods. The variables arr and sb will get created every time with a local scope during recursion. Because of this, all the queue names/topic names that are collected inside recursive call will not be propagated up to the call stack.

String arr[]={""}; 
StringBuilder sb=new StringBuilder(""); 

To fix this either make arr and sb as static fields in shared area or pass on these two into the searchX methods during recursion.

1 Like

Thanks Prasad , it is working now. Thanks for your suggestions.

Regards
Abir

Hi Prasad,

I am having issues with the static variables now. They are holding the reference from earlier calls . After compiling the code , when I am calling for first time , it is returning the correct number of topics and queues. Now if I am calling again , the values are getting duplicated , and if called again values are getting triplicated and so on…

Is there any way to remove the static variables after each call.

Regards
Abir Banerjee

Try below code:

try 
{ 
String[] RNAME={realmUrl}; 
nSessionAttributes nsa=new nSessionAttributes(RNAME); 
nRealmNode realm = new nRealmNode(nsa); 

nContainer nc=(nContainer)realm; 

StringBuilder queue_sb=new StringBuilder(); 
String queues[]=searchQueues(nc,queue_sb); 
 
StringBuilder topic_sb=new StringBuilder(); 
String topics[]=searchTopics(nc,topic_sb); 

} 
catch(Exception e) 
{ 
errorMsg=e.toString(); 
} 

public static String[] searchQueues(nContainer container, StringBuilder sb) 
{ 
String arr[]={""};
Enumeration children = container.getNodes(); 
while (children.hasMoreElements()) { 
nNode child = (nNode)children.nextElement(); 
if (child instanceof nContainer) { 
searchQueues((nContainer)child, sb); 
} else if (child instanceof nLeafNode) { 
nLeafNode leaf = (nLeafNode)child; 
if (leaf.isQueue()) { 
sb.append(leaf.getName()); 
sb.append("\n"); 
} 
} 
} 
arr=sb.toString().split("\n"); 
return arr; 
} 

public static String[] searchTopics(nContainer container, StringBuilder sb) 
{ 
Enumeration children = container.getNodes(); 
String arr[]={""}; 
while (children.hasMoreElements()) { 
nNode child = (nNode)children.nextElement(); 
if (child instanceof nContainer) { 
searchTopics((nContainer)child, sb); 
} else if (child instanceof nLeafNode) { 
nLeafNode leaf = (nLeafNode)child; 
if (leaf.isChannel()) { 
sb.append(leaf.getName()); 
sb.append("\n"); 
} 
} 
} 
arr=sb.toString().split("\n"); 
return arr; 
}

Hi Prasad,

Thanks for the details . I have tried another method which is sort of a band aid solution. :slight_smile: . I will try your method. What I did was,

//MAIN CODE SECTION//

StringBuilder qq=new StringBuilder(“”);
String queues={“”};

qq.append(searchQueues(nc));

queues=qq.toString().split(“\n”);
int qCap = sbQ.capacity(); ----> calculating the current capacity of the StringBuilder object
sbQ.delete(0, qCap); ----> deleting the contents of the StrinBuilder object after each call

//MAIN CODE SECTION//

//START SHARED CODE SECTION//
private static StringBuilder sbQ=new StringBuilder(“”);

public static StringBuilder searchQueues(nContainer container) 
{
	Enumeration children = container.getNodes(); 
	
	//String arr[]={""};
	//StringBuilder sb=new StringBuilder("");
	while (children.hasMoreElements()) { 
		nNode child = (nNode)children.nextElement(); 
		if (child instanceof nContainer) { 
				searchQueues((nContainer)child); 
		} else if (child instanceof nLeafNode) { 
				nLeafNode leaf = (nLeafNode)child; 
				if (leaf.isQueue()) { 
						//sbQ.append(leaf.getName());
						sbQ.append(leaf.getAbsolutePath ());
						sbQ.append("\n");
				} 
				} 
		} 
	
	return sbQ;
} 

// END SHARED CODE SECTION //

Hi Prasad,

I am seeing that , when I am calling the nLeafNode methods to get the channel/topic properties I am not able to get the actual values like total publishes,total consumed etc. I am doing the code like this,

String RNAME={realmUrl};
nSessionAttributes nsa=new nSessionAttributes(RNAME);
nRealmNode realm = new nRealmNode(nsa);
nNode nn=realm.findNode(topicName);

nLeafNode nlf = (nLeafNode)nn;

		float connectionRate = nlf.getConnectionRate();
		float consumedRate = nlf.getConsumedRate();
		long currentConnections = nlf.getCurrentNoOfConnections();
		long totalConsumed = nlf.getTotalConsumed();
		long totalPublished = nlf.getTotalPublished();
		long usedSpace = nlf.getUsedSpace();
		nChannelAttributes nch = nlf.getAttributes();
		long ttl=nch.getTTL();
		int type=nch.getType ();

Here only getTTL and type are giving the proper values , the total consumed and total published are not providing correct values only returning 0.

Regards
Abir Banerjee

I never tried reading those kind of fields using nLeafNode. Like I posted early in this topic, I prefer going with nQueue or nChannel kind of classes where I got results desired. Once again, below is a sample code to read “noOfEvents” and “noOfReader (connections)”.

You may explore other methods of nQueueDetails and see if it has got the details you are looking for.

nSessionAttributes nsa = new nSessionAttributes(RNAME);  
              nSession mySession=nSessionFactory.create(nsa);  
mySession.init();  
              nChannelAttributes cattrib = new nChannelAttributes();  
              cattrib.setName(destinationName);  
              int numberOfEvents;  
int numberOfReaders;  
try{  
    nQueue myQueue=mySession.findQueue(cattrib);  
    nQueueDetails nqd = myQueue.getDetails();  
    numberOfEvents=nqd.getNoOfEvents();  
    numberOfReaders=nqd.getNoOfReaders();  
}  
catch(nChannelNotFoundException e){  
    throw new ServiceException("Queue with name '"+queueName+"' does not exist");  
}

Hi all,

I have a slightly different issue, when I run this to connect:


nSessionAttributes nsa=new nSessionAttributes(RNAME);
nRealmNode realm = new nRealmNode(nsa);

I get the following exception:

Exception in thread "main" java.lang.NoClassDefFoundError: com/pcbsys/nirvana/client/p2p/nServiceFactory
	at com.pcbsys.nirvana.nAdminAPI.nRealmNode.loadChannels(nRealmNode.java:3787)
	at com.pcbsys.nirvana.nAdminAPI.nRealmNode.createNameSpace(nRealmNode.java:3669)
	at com.pcbsys.nirvana.nAdminAPI.nRealmNode.connectToRealm(nRealmNode.java:3447)
	at com.pcbsys.nirvana.nAdminAPI.nRealmNode.create(nRealmNode.java:1389)
	at com.pcbsys.nirvana.nAdminAPI.nRealmNode.<init>(nRealmNode.java:1385)
	at com.pcbsys.nirvana.nAdminAPI.nRealmNode.<init>(nRealmNode.java:1371)
	at com.pcbsys.nirvana.nAdminAPI.nRealmNode.<init>(nRealmNode.java:1359)

I suspect that it is a version issue, it will help if I get the nClient.jar and nAdminAPI.jar that you guys are using.

Can you please assist me?

Regards,
Patrick