Advanced java code dealing with real world problems.

Friday, September 24, 2010

Java COBOL data exchange - Quick Start Guide

Java has rapidly grown into enterprise landscape and the needs for integrating with mainframe world through JMS/MQ or CICS Transaction Gateway has become a critical piece to many enterprise IT stacks.

JavaCobolExchanger - an open source google code project is developed to address the needs.

Here is the link to the project: http://code.google.com/a/eclipselabs.org/p/java-cobol-exchanger/

To get started first we need to understand COBOL copy books. A copy book is a data description of data layout in fixed format. For example:

01 MY-EXCHANGE-RECORD.
  05 USER-ID          PIC X(20).
  05 USER-AGE         PIC 9(3).
  05 USER_NAME.
    10 FIRST-NAME     PIC X(20).
    10 LAST-NAME      PIC X(20).
    10 MIDDLE_INITIAL PIC X(1).
  05 AMOUNT-RECEIVED  PIC 9(5).99.
  05 DATE-REGISTERED.
    10 DATE-CCYY      PIC 9(4).
    10 DATE-SEP1      PIC X.
    10 DATE-MM        PIC 9(2).
    10 DATE-SEP2      PIC X.
    10 DATE-DD        PIC 9(2).

This copy book tells us that the first 20 characters are reserved for user Id, the next 3 characters are reserved for user age and it must be numeric. The next 41 characters are reserved for a group called USER-NAME and the group contains three elements. Followed after the group is a decimal element with equivalent decimal format of  "00000.00". The last 10 characters are reserved for a date element, which again is a group consisting of five elements, with equivalent date format of "yyyy-MM-dd".

To construct a java exchange instance of above COBOL copy book:

public class CobolCopybook extends ExchangeRecord {

    public CobolCopybook() {
        // define user name group
        BaseElement userNameGroup[] = {
                new StringElement("firstName", 20),
                new StringElement("lastName", 20),
                new StringElement("middleInitial",1),
        };
    
        // define the exchange record
        list.add(new StringElement("userId", 20));
        list.add(new IntegerElement("userAge", 3));
        list.add(new StructElement("userName", userNameGroup));
        list.add(new DecimalElement("amount", "00000.00"));
        list.add(new DateTimeElement("date", "yyyy-MM-dd"));
    }
}

To load the instance with data, simply add the following code:


ExchangeRecord bean = new CobolCopybook();
bean.getElement("userId").setValue("test user");
bean.getElement("userAge").setValue("35");
StructElement userNameGroup = (StructElement) bean.getElement("userName");
userNameGroup.getElement("firstName").setValue("John");
userNameGroup.getElement("lastName").setValue("Smith");
bean.getElement("amount").setValue("199.99");
bean.getElement("date").setValue("2010-01-01");






Now you can export the instance to a fixed format string which can then be consumed by a COBOL program:
 
String cobolString = bean.exportToString();
// XXX: add your code here to send it to mainframe

The data exchange can be performed in either direction, we just demonstrated a data exchange from java to COBOL. Now let's see how we handle exchange from a COBOL output:

First we need create an java exchange instance:

ExchangeRecord bean = new ComplexCopybook();

Then we import the COBOL output to the instance:

bean.importFromString();

And now we can access the COBOL data using getValue method:

String userId = bean.getElement("userId").getValue();
Integer userAge = (Integer) bean.getElement("userAge").getValue();

1 comment:

  1. Hi Jack,
    Your tutorial in wss4j with axis 1.4 is fantastic.However I am currently getting the error
    org.xml.sax.SAXParseException: The prefix "ds" for element "ds:KeyInfo" is not bound.

    I am kind of stuck for last few days.

    Please help how to proceed.My email id is amarsharma222@gmail.com

    ReplyDelete

Followers

About Me

An IT professional with more than 20 years of experience in enterprise computing. An Audio enthusiast designed and built DIY audio gears and speakers.