Broker API document traversal

Hi all,

I’m trying to use the Broker API to publish a large number of documents to the Broker for load testing.

As an exercise to familiarise myself with the API I wanted to write something that traversed the field names of a document, much like that you would see in Developer.

Here is an excerpt of my code:

public static void main(String[] args) throws BrokerException {
	BrokerClient broker = getBrokerClient("host:port", "brokername", null, "clientgroup", "appname");
	BrokerEvent event = getBrokerEvent(broker, "wm::is::x::y::x");	
	traverseBrokerEvent(event, 0);
	broker.disconnect();
}

private static void traverseBrokerEvent(BrokerEvent event, int level) throws BrokerException {
		
	String[] fields = event.getFieldNames(null);
		
	for (String field : fields) {
		printField(event, field, level);
			
		short fieldType = event.getFieldType(field);
			
		// recursively traverse another struct
		if (fieldType == BrokerTypeDef.FIELD_TYPE_STRUCT) {
			BrokerEvent structEvent = event.getStructFieldAsEvent(field);
			traverseBrokerEvent(structEvent, level+1);
				
		} else if (fieldType == BrokerTypeDef.FIELD_TYPE_SEQUENCE) {
			BrokerField seqField = event.getSequenceField(field, 0, 0);
//			a mess goes here
//			BrokerEvent tmpEvent = new BrokerEvent(null);
//			BrokerEvent[] seqEvent = event.getStructSeqFieldAsEvents(field, 0, 0);
//			System.out.println(seqField.toString());
		}
	}
}


private static void printField(BrokerEvent event, String fieldName, int level) throws BrokerException {
		
	printIndent(level);
	System.out.print(fieldName);
		
	short fieldType = event.getFieldType(fieldName);
		
	if (fieldType == BrokerTypeDef.FIELD_TYPE_BOOLEAN) {
		System.out.print(" (bool)");
	} else if (fieldType == BrokerTypeDef.FIELD_TYPE_STRING || fieldType == BrokerTypeDef.FIELD_TYPE_UNICODE_STRING) {
		System.out.print(" (string)");
	} else if (fieldType == BrokerTypeDef.FIELD_TYPE_STRUCT) {
		System.out.print(" (struct)");
	} else if (fieldType == BrokerTypeDef.FIELD_TYPE_SEQUENCE) {
		System.out.print("[] (seq)");
	}
	System.out.println();
}

private static void printIndent(int level) {
	if (level == 0) return;
	for (int x=0; x<level; x++) {
		System.out.print("   ");
	}
	System.out.print("+- ");
}

This works pretty good, I get results like this:

Doc1 (struct)
   +- Field1 (string)
   +- Field2 (string)
   +- Field3 (string)
   +- Field4 (string)
Doc2 (struct)
   +- Doc3 (struct)
      +- Doc4 (struct)
         +- Field7 (string)
         +- Doc5 (struct)
            +- Field8 (string)
            +- Doc6[] (seq)
      +- Field9 (string)
      +- Field10 (string)
      +- Field11 (string)
      +- Field12 (string)
      +- Doc7 (struct)
         +- Doc8[] (seq)

You would notice that I have not traversed into the sequences / document lists (ie, Doc6 and Doc8). As far as I can tell from the API this is not something I can do as there are no instances of Doc6 or Doc8 present (being a sequence). Calling getEventSeqField returns an empty array of BrokerEvents.

Yet at the same time, I cannot create instances of them and set them into the document as neither Doc6 nor Doc8 are publishable.

The Broker Client API documentation recommends creating a BrokerEvent without a BrokerClient set then manually setting the fields to disable type checking and then setting that BrokerEvent into the document, however then the traversal is no longer generic.

What I would be hoping to get is something like:

      +- Doc7 (struct)
         +- Doc8[] (seq)
            +- Field13 (string)
            +- Field14 (string)

Surely I’m overlooking something simple that should be able to do this?