Named Fields


Not only can you add and access fields to a message sequentially as demonstrated in the previous example, you can also add, access, update, and delete fields in messages by name. A name is associated with a field when the field is added to the message, using the TipcMsg.addNamedType methods. Once you have added a named field to a message, you can:

Named fields and those without names can co-exist in the same message without conflict. In addition, named fields can be accessed either using their name or sequentially, like any other field.

This example shows how to add a named field and how to access it both by name and sequentially:

import com.smartsockets.*; 
 
/** 
 * Named fields example program. 
 */ 
 
public class NamedFieldsExample { 
 
  public static void main(String[] argv) { 
 
    try { 
      /* Create a message */ 
      TipcMsg msg = TipcSvc.createMsg(TipcMt.INFO); 
       
      /* Add a non-named int4 field, and a named string field */ 
      msg.appendInt4(5); 
      msg.addNamedStr("string one", "hello"); 
 
      /* Now get the string field */ 
      String str = msg.getNamedStr("string one"); 
      System.out.println("named string field is " + str); 
 
      /* Rewind the index back to the first field, and get the int4 field */ 
      msg.setCurrent(0); 
      int i = msg.nextInt4(); 
      System.out.println("first field is " + i); 
 
      /*  
       * Get the string field again.  Note that we don't have to use the 
       * name to get it, it's still just an indexed field, like any other. 
       */ 
      str = msg.nextStr(); 
      System.out.println("second field is " + str); 
 
      /* 
       * Rewind the index pointer again, and we can "name" the int4 field. 
       */ 
      msg.setCurrent(0); 
      msg.setNameCurrent("int4 zero");  
 
      /* 
       * We can also get the name of the current field. 
       */ 
      str = msg.getNameCurrent(); 
      System.out.println("name of first field is " + str); 
    } 
    catch (Exception e) { 
      Tut.fatal(e); 
    } 
  } 
} 

TIBCO SmartSockets™ Java Library User’s Guide and Tutorial
Software Release 6.8, July 2006
Copyright © TIBCO Software Inc. All rights reserved
www.tibco.com