Inconsistent behaviour of getEvents method in Java Client API.

Hello,
I am trying to get document names from a particular client queue using Java BrokerClient API.
I’m using the getEvents method to achieve the same.
My reason for choosing this approach has been discussed in this thread http://tech.forums.softwareag.com/techjforum/posts/list/55345.page

I have a client named gclient32-2 which has 1000 docs in its queue
Now, here’s my code:
public static final void getEventFrequencies(IData pipeline)
throws ServiceException {
//Initializing parameters
String broker_host = “localhost:6849”;
String broker_name = null;
String client_group = “eventLog”;
BrokerClient c;
String client_id="#gclient32-2";


try {
//Connecting to the client
c = BrokerClient.reconnect(broker_host, broker_name, client_id,null);


BrokerEvent[] s;
//Loop over 8 times
// getEvents retrieves maximum of 160 events at a time

while(count < 8 ) {

	   [b] s = c.getEvents(160, -1);[/b]
                count+=1;
                 ......
        [b]//To get the document names[/b]
              for (BrokerEvent str : s) {
                    doc_names = str.getTypeName();
                     .......
                       }

The getEvents method is retrieving only 70+ or 80+ (sometimes only 30+) in one iteration. I was able to get about 450 docs (getEvents in a loop for 8 times). Again, this number varies every time I run. I have also tried various combinations of parameters for this method. What am I missing ?
What is the reason for this inconsistent behaviour ? How do I proceed to get all document names ?

Any leads are appreciated.

Hi Abhilash,

can you please provide an enhanced version of your code snippet and place it inside a code tag?

The current snippet is at least missing some closing } brackets making it difficult to check the correctness of the approach.

How many documents are retrieved per run depends on how much documents are still availbale in the client queue.
If there are less than the requested amount, it will only return those.
This should be handled in some sort of exception handling.

Regards,
Holger

Thanks for replying.
Here’s my full code.
Here’s what it’s doing:

  1. read client ID from a file.
  2. Connect to that client
  3. Get the document names
  4. Store them in a Hash Map
  5. Write the Map in a file with each document names and their counts (No. of times they were published)

I have a client with 1000 docs (10 document types each published 100 times). When I ran this code, I got a total of 432 docs (I received 74 of one doc types, 73 of the other, only 37 of another etc…)

Here’s my code:



public static final void getEventFrequencies(IData pipeline)
			throws ServiceException {
		String broker_host = "localhost:6849";
		 String broker_name = null;
		 String client_group = "eventLog";
		  BrokerClient c;
		  String client_id;
		  String everything;
		  
		  try {
		
			  BufferedReader br = new BufferedReader(new FileReader(
					  "clientID.txt"));
		
			  StringBuilder sb = new StringBuilder();
			  String line = br.readLine();
		
			  while (line != null) {
				  sb.append(line);
				  sb.append(System.lineSeparator());
				  line = br.readLine();
		
			  }
			  br.close();
			  everything = sb.toString();
		
		  } catch (IOException ex) {
			  everything = "";
			  com.wm.util.JournalLogger.log(3, 90, 3, "" + ex);
		  }
		
		  client_id =everything;
		  client_id= client_id.trim();
		  
		  
		  try {
			  c = BrokerClient.reconnect(broker_host, broker_name, client_id,
					  null);
			  com.wm.util.JournalLogger.log(3, 90, 3, "Client working"
					  + client_id);
		
			  File file = new File("EventNamesWithCounts.txt");
			  try {
		
				  if (!file.exists()) {
					  file.createNewFile();
				  }
				  FileWriter fw = new FileWriter(file.getAbsoluteFile());
				  BufferedWriter bw = new BufferedWriter(fw);
				  try {
					  Map<String, String> map = new HashMap<String, String>();
					  String value;
					  String instring;
					  int num;
					  int length;
					  BrokerEvent[] s;
					   int count =1;
					  length = c.getQueueLength();
					  if (length == 0) {
						  bw.append("Total Queue Length: " + length);
		
					  } else {
						  bw.append("Total Queue Length: " + length);
						  bw.newLine();
						  bw.append("Event Names \t Count");
						  while(count < 8) {
						  s = c.getEvents(160, -1);
						  
						  com.wm.util.JournalLogger.log(3,90,3,"Browse Events: "+ s.length);
						  bw.newLine();
						  for (BrokerEvent str : s) {
							  instring = str.getTypeName();
							 
		
							  if (map.containsKey(instring)) {
								  value = map.get(instring);
								  num = Integer.parseInt(value);
								  num = num + 1;
								  map.put(instring, Integer.toString(num));
							  } else {
								  map.put(instring, Integer.toString(1));
							  }
							 
		
						  }
						  count +=1;
						  }
		
						  for (Map.Entry<String, String> entry : map.entrySet()) {
							 
							  bw.append(entry.getKey() + "\t\t"
									  + entry.getValue());
							  bw.newLine();
						  }
					  }
		
					  // com.wm.util.JournalLogger.log(3,90,3,"Length of the Queue: "
					  // + c.getQueueLength());
		
				  } catch (BrokerException ex) {
					  com.wm.util.JournalLogger.log(3, 90, 3,
							  "Catch Block: Couldn't Print Queue: " + ex);
					  // com.wm.util.JournalLogger.log(3,90,3,"EventBrowse Try Block"
					  // + s.length);
				  }
				  bw.close();
			  } catch (IOException ex) {
				  com.wm.util.JournalLogger.log(3, 90, 3, "" + ex);
			  }
		
			  try {
				  c.disconnect();
			  } catch (BrokerException ex) {
				  com.wm.util.JournalLogger.log(3, 90, 3,
						  "Catch block disconnect" + ex);
			  }
		
		  } catch (BrokerException ex) {
		
			  com.wm.util.JournalLogger.log(3, 90, 3, "Client ID not working"
					  + ex);
		
		  }
		  
	}