Sample Service

Here is code for the sample service followed by an explanation of the significant lines of code.

1      try {
2       // Get a Connection Factory instance
3       mcf = new CobolNoTxManagedConnectionFactory();
4       // set the appropriate fields.
5       mcf.setServerHost("localhost");
6       mcf.setServerPort("9003");

7       // Get a connection Factory (without using JNDI)
8       cxf = (javax.resource.cci.ConnectionFactory) 

9       // Get a Cobol Connection Handle
10      connection = cxf.getConnection();
11      initialize(connection, cxf, true);

12      // Set up an interaction
13      interaction = connection.createInteraction();

14      // create a new interaction spec
15      CobolInteractionSpec iSpec = 
             new CobolInteractionSpec();
16      iSpec.setFunctionName("myservice.add");

17      javax.resource.cci.RecordFactory rf = 
18      Calculator calc = new Calculator();
19      calc.setArg1(new java.math.BigDecimal(10));
20      calc.setArg2(new java.math.BigDecimal(20));
21      iSpec.setArgument(0, com.microfocus.cobol.

22      interaction.execute(iSpec, calc, calc);
23      System.out.println(
             "Input - Arg 1 was" + calc.getArg1());
24      System.out.println(
             "Input - Arg 2 was" + calc.getArg2());
25      System.out.println(
             "Result was" + calc.getResult());
26      System.out.println(
             "Memory was" + calc.getStorage());

28      interaction.close();
29      connection.close();
30      } catch( javax.resource.ResourceException re) 
31        re.printStackTrace();
32        Exception le = re.getLinkedException();
33      }

Lines 3-6:

mcf = new CobolNoTxManagedConnectionFactory();

Instantiate the above ConnectionFactory class. The class then configures the following attributes of the unmanaged connection between the Java code and the resource adapter:

Line 8:

cxf = (javax.resource.cci.ConnectionFactory) 

Instantiate the javax.resource.cci.ConnectionFactory object, by using the CobolNoTxManagedConnectionFactory that you created in line 1.

Line 10:

connection = cxf.getConnection();

Instantiate the javax.resource.cci.Connection class. Invoke the getConnection method on your Connection Factory object to obtain an instance of Connection, configured as stated by the configuration of your CobolNoTxManagedConnectionFactory. This connection handle maps to a ManagedConnection at any time.

Line 11:

initialize(connection, cxf, true);

Initialize the connection. This is a necessary step. The true parameter denotes the state of the COBOL program on the enterprise server. If the program requires the same initial state each time, then set the boolean (parameter three) to true. Otherwise if you want to preserve the state between service calls then set the value to false.

Line 13:

interaction = connection.createInteraction();

Instantiate the javax.resource.cci.Interaction object, by invoking createInteraction() on your Connection object. The Connection object is responsible for the creation of the Interaction object, which will execute interactions with the enterprise server.

Line 15:

CobolInteractionSpec iSpec = 
    new CobolInteractionSpec();

Instantiate and configure the specific object for the kind of interaction that is required. In this case, use the com.microfocus.cobol.connector.cci.CobolInteractionSpec class. This class builds the request and method of passing the parameters. The class is used to hold the function name and the direction (In, Out or I/O) of each of the arguments in an indexed record or the direction argument if it is a custom record.

Line 16:


Set the name of the program to be run on the enterprise server, through the setFunctionName method.

Lines 18-21:

Calculator calc = new Calculator();
calc.setArg1(new java.math.BigDecimal(10));
calc.setArg2(new java.math.BigDecimal(20));

Create a new object of custom record class. If only a custom record is being sent as an argument, the steps above are sufficient. The custom record is generated automatically when you generate the EJB. The source files generated for the custom record are available in myproject\repos\myService.deploy\packageName.

If more than one custom record or a mixture of custom records and basic Java types need to be passed as an argument, you need to use an indexed record. For example, if an integer and a custom record need to be passed as an argument, use the following code:

    javax.resource.cci.RecordFactory rf = 
    javax.resource.cci.IndexedRecord iRec = 
    javax.resource.cci.IndexedRecord oRec = 
    iRec.add(new Integer(5));

This sets the direction of the argument. The arguments are numbered from 0 and this is the first value passed to setArgument. The valid values for direction are:


Line 22:

interaction.execute(iSpec, calc, calc);

Execute the Interaction. The execute() method invoked on the Interaction object causes the request to be flowed to the enterprise server.

Lines 28-29:


Finally, close the Interaction and the Connection. The close() method on the Connection object is the final flow, and closes the underlying connection to the resource adapter. It must be executed after the close() method on the Interaction.