Hints and tips
In this Appendix, we provide some tips through the evolution of what a typical Java program in CICS might look like.
Priority of public static void main() methods
CICS allows you to use two alternative main() methods for executing Java programs:
public static void main(CommAreaHolder commAreaHolder)
public static void main(String[] args)
The first passes COMMAREA data in a CommAreaHolder, which holds the data in a byte array. If this version of the main() method does not exist, CICS attempts to call the “typical” second version (note that with this main() method, no data is passed with the arguments and so the supplied String array is empty).
Getting transaction arguments using Java
When a Java program in CICS is executed by typing a transaction definition in the terminal, it is possible to pass values after the transaction name for use as parameters in the program, as shown in Example B-1. Using the JCICS implementation of EXEC CICS RECEIVE, the terminal data is got as a byte array and wrapped in a String object before returning.
Example: B-1 Return transaction arguments as a String
public static String getTerminalString() {
final DataHolder dataHolder = new DataHolder();
final Object principalFacility = Task.getTask().getPrincipalFacility();
 
String result = null;
 
if (principalFacility instanceof TerminalPrincipalFacility) {
try { ((TerminalPrincipalFacility)principalFacility).receive(dataHolder); }
catch (EndOfChainIndicatorException e) {}
catch (Exception e) { System.err.println(e.toString()); e.printStackTrace(); }
}
 
return dataHolder.value != null ? new String(dataHolder.value) : null;
}
Using this facility, you can go a step further and chop up the parameters and return them in a String array. The default separating character between the parameters is the “ “ (space) character. Because the transaction name is also part of the String data, this becomes the first element in the array, as shown in Example B-2.
Example: B-2 Separate transaction arguments into a String array
public static String[] getTerminalStringAsArray() {
String[] result = null;
int count = 0;
 
final String args = getTerminalString();
 
if (args != null) {
final java.util.StringTokenizer tokenizer =
new java.util.StringTokenizer(args, " "); // (1)
result = new String[tokenizer.countTokens()];
 
while (tokenizer.hasMoreTokens()) result[count++] = tokenizer.nextToken();
}
 
return result;
}
To include spaces in the resulting string array elements, change ‘ ‘ to something like ’,’.
Never use System.exit()
When Java applications are run in CICS, the public static void main() method is called through the use of another Java program called the Java wrapper (that is how functionality like that described in “Priority of public static void main() methods” on page 294 can happen). The use of the wrapper allows CICS to initialize the environment for Java applications, but more importantly, it performs cleanup for any processes that are used during the life of the application. Killing the JVM, even with a “clean” return code of 0, does not allow this cleanup process to run, and can lead to data inconsistency.
From an additional perspective, by using a System.exit() call, the continuous and resettable JVM modes become unusable because it terminates the JVM instance.
The recommended and only approach is to allow the program to run to the end of the public static void main() method and the JVM to terminate cleanly.
 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset