Sample Java Program

import com.basis.bbj.bridge.*;

import java.util.*;

/**
* com.basis.bbj.bridge.TestBridge
* TestBridge is a demo program that demonstrates the usage of JavaBBjBridge
* There is no need to compile this class yourself, it is already included with BBj.
* Running this program will invoke TestBridge.bbj.
*/
public class TestBridge
{
 final static int testCount = 100;
 final static String testString = "now is the time\nfor all good men\n" +
     "to come to the aid\nof their country\n";

 final static int TIMEOUT = -1;

 public static void main(String argv[])
 {
  JavaBBjBridge bridge = null;
  try
  {
   String cmdLine = " -cconfig.BBx -tT0 -d - a b c ";
   bridge = JavaBBjBridgeFactory.createBridge(
        cmdLine,true,true,false,true);
   BBjBridgeRequest request =
      JavaBBjBridgeFactory.createBridgeRequest();

   BBjBridgeResponse response =
      JavaBBjBridgeFactory.createBridgeResponse();

   request.setProgramName("testbridge.bbj");

  /*
   * In each case, pass the testID to testbridge.bbj as the
   * first parameter so that testbridge.bbj will know what to do
   *
   * test 0 only passes command line params. testbridge.bbj
   *  is expected to use WRITE RECORD with key values
   *  to write argc and each argv[]
   * expected output:
   *  test 0
   *  argc: : 5
   *  argv[0] = : bbjthinclient
   *  argv[1] = : 0
   *  argv[2] = : AA
   *  argv[3] = : BB
   *  argv[4] = : CC
   *
   * test 1 sends testString which contains multiple lines.
   *  testbridge.bbj is expected to read individual lines and
   *  write them into the response.
   * expected output:
   *  test 1
   *  received: now is the time
   *  received: for all good men
   *  received: to come to the aid
   *  received: of their country
   *
   * test2 sends two key/value pairs. testbridge uses hardcoded
   *  keys to retrieve the values and writes key/value pairs
   *  where the key is the same hardcoded key and the value
   *  is a modified value
   * expected output:
   *  test 2
   *  name: : the name is someName
   *  month: : the month is someMonth
   *
   * test3 sends strings.
   *
   * test4 sends key/value pairs
   *  in both these tests, testbridge uses fin() information
   *  to determine whether the bridge channel is sending strings
   *  or is sending key/value pairs. it then reads data and
   *  echos that data to channel 1
   * expected output:
   *  test 3
   *  received: now is the time
   *  received: for all good men
   *  received: to come to the aid
   *  received: of their country
   *  test 4
   *  name: : the name is someName
   *  month: : the month is someMonth
   *
   * test5 sends key/value pairs. testbridge.bbj does not use
   *  hard-coded keys but instead uses the key() function to
   *  obtain keys and then reads using the obtained key. It
   *  then uses the value as a key and the key as a value
   *  to write all value/keys to channel 1
   * expected output:
   *  test 5
   *  someName : name
   *  someMonth : month
   *
   * test6 sends key/value pairs. testbridge.bbj intentionally
   *  generates an error and the error handler writes error
   *  information to channel 1
   * expected output:
   *  test 6
   *  error: Improper file or device usage on line 39
   *
   * timing tests execute tests #3,4,5 each 100 times then
   *  write out the average execution times
   * expected output:
   *  similar to:
   *  timing tests
   *  TestBridge timing test 1 avg time in mSec: 92
   *  TestBridge timing test 2 avg time in mSec: 77
   *  TestBridge timing test 3 avg time in mSec: 75
  */

   for(int q=0; q<7; ++q)
   {
    System.out.println("\n\n test " + q);
    prepareTest(request,q);
    bridge.runBBj(request,response,TIMEOUT);
    writeResponseToSystemOut(response);
    request.clearData();
   }

   // now do some timing tests
   System.out.println("\n\n\n timing tests");

   for (int test = 1; test <= 3; ++test)
   {
    long start = System.currentTimeMillis();
    for(int q=0; q<testCount; ++q)
    {
     prepareTest(request,test+2);
     bridge.runBBj(request,response,TIMEOUT);
     request.clearData();
    }
    long end = System.currentTimeMillis();
    long ellapsedTime = end - start;
    System.out.println("TestBridge timing test #"+test+
      " avg time in mSec: " +
       (ellapsedTime/testCount));
   }

   // close the bridge
   bridge.close();
  }
  catch(BBjBridgeException e)
  {
   e.printStackTrace();
   if(bridge != null)
    bridge.close();
  }
 }

 private static void
 prepareTest(BBjBridgeRequest p_request, int p_testNumber)
 {
  p_request.setProgramParameters(Integer.toString(p_testNumber));

  switch(p_testNumber)
  {
  case 0:
   // set some more program params.
   // testBridge.bbj should echo the program params
   p_request.setProgramParameters(""+p_testNumber+" AA BB CC");
   break;

  case 1:
  case 3:
   // set string param. program should return
   // modified string
   p_request.setBytes(testString.getBytes());
   break;

  case 2:
  case 4:
  case 5:
  case 6:
  case 7:
   // set some key/value pairs. program should echo
   // key value pairs
    try {
     p_request.put("name".getBytes(),"someName ".getBytes());
     p_request.put("month".getBytes(),"someMonth ".getBytes());        
    } catch (IllegalStateException | BBjBridgeException e) {
     e.printStackTrace();
    }
   break;
  }
 }

 private static void
 writeResponseToSystemOut(BBjBridgeResponse response)
 {
  if(response.isRecordOriented())
  {
   int count = response.getRecordCount();
   byte[] key;
   byte[] value;

 for(int q=0; q<count; ++q)
  {
  key = response.getKey(q);
  value = response.getValue(q);
  System.out.println(new String(key) + " : " + new String(value));
   }
  }
 else
  System.out.println(new String(response.getBytes()));
 }
}