package dataqueue;

 

 

/*

 * Send instruction to demonstration model running on System i

 * and display response.  The console is used for input and

 * output.

 *

 * Enter *STOPNOW through the console as the instruction for the

 * demo model to stop this program.

 *

 */

 

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.util.Date;

 

import com.ibm.as400.access.AS400;

import com.ibm.as400.access.AS400FileRecordDescription;

import com.ibm.as400.access.DataQueue;

import com.ibm.as400.access.KeyedDataQueue;

import com.ibm.as400.access.KeyedDataQueueEntry;

import com.ibm.as400.access.Record;

import com.ibm.as400.access.RecordFormat;

import common.Me;

 

 

public class DtaQExample1 extends Object {

 

      public static void main(String[] parameters) {

            AS400 as400 = null;

            System.out.println(" ");

 

            try {

 

                  // Create an AS400 object for the server that has the data queue.

                  System.out.println("Instantiating AS400 object.");

                  as400 = new AS400(Me.getSYSTEM(), Me.getUSER(), Me.getPASSWORD());

 

                  // This isn't necessary, the AS400 object will make the

                  // connections as they are needed.  Forcing the connections at this

                  // point in the the program put all the starting connection overhead

                  // in once place where you can see it.

                  System.out.println("Starting services.");

                  as400.connectService(AS400.COMMAND);

                  as400.connectService(AS400.DATAQUEUE);

                 

                  // Create data queue objects

                  System.out.println("Instantiating data queue objects");

                  DataQueue mdliDq = new DataQueue(as400, Me.getMDLI_DTAQ());

                  KeyedDataQueue mdloDq = new KeyedDataQueue(as400, Me.getMDLO_DTAQ());

 

                  // Dynamically create RecordFormat objects to format

                  // data sent to and received from data queue

                  System.out.print("Retrieving record format descriptions ... ");

            Date startMilliseconds = new Date();

                  AS400FileRecordDescription mdliFormatDescription = new AS400FileRecordDescription(

                              as400, Me.getMDLI_DS());

                  AS400FileRecordDescription mdloFormatDescription = new AS400FileRecordDescription(

                              as400, Me.getMDLO_DS());

 

                  RecordFormat mdliFormat = mdliFormatDescription

                              .retrieveRecordFormat()[0];

                  RecordFormat mdloFormat = mdloFormatDescription

                              .retrieveRecordFormat()[0];

                 

            long milliseconds = new Date().getTime() - startMilliseconds.getTime();

            System.out.println("finished.  Time in milliseconds: " + milliseconds + "\n\n");

           

            /*

             * Debugging dump field names in RecordFormats

             *

                  String[] names = mdliFormat.getFieldNames();

                  for (int i = 0; i < names.length; i++)

                        System.out.println(names[i]);

                  names = mdloFormat.getFieldNames();

                  for (int i = 0; i < names.length; i++)

                        System.out.println(names[i]);

                  */

                 

                  // input from console

                  BufferedReader stdin = new BufferedReader(new InputStreamReader(

                              System.in), 1);

                 

                  while (true) {

                        // get input from the keyboard

                  System.out.println("Instruction *STOPNOW ends this application");

                        System.out

                                    .print("Please enter the instruction to send to the model: ");

                        String mdliInstruction = stdin.readLine();

                        if (mdliInstruction.equalsIgnoreCase("*STOPNOW")) {

                              System.out.println(".... ok! stopping now .... goodbye");

                              break;

                        }

                       

 

                        System.out

                                    .print("Please enter the data to send to the model: ");

                       

                        // At this point program is waiting for stdin

                        String mdliMessage = stdin.readLine();

 

                        // load the model input record and send it to the model

                        // either way to get Record works

                        // Record mdliData = mdliFormat.getNewRecord();

                        Record mdliData = new Record(mdliFormat);

                        mdliData.setField("VCID", Me.getKEY());

                        mdliData.setField("IINSTRTN", mdliInstruction);

                        mdliData.setField("IMESSAGE", mdliMessage);

 

                        // Send instruction and data to model on

                // model input data queue

                        startMilliseconds = new Date();

                        System.out.print("Writing to MDLI data queue ... ");

                        mdliDq.write(mdliData.getContents());

                        milliseconds = new Date().getTime() - startMilliseconds.getTime();

                        System.out.println("finished.  Time in milliseconds: " + milliseconds);

 

                        // Wait for the model response on model output data queue.

                        // Wait maximum of 15 seconds, if longer there is a problem

                        startMilliseconds = new Date();

                        System.out.print("Reading to MDLO data queue ... ");

                        KeyedDataQueueEntry mdloDataQueueEntry = mdloDq.read(Me.getKEY(),

                                    15, "EQ");

                        if (mdloDataQueueEntry == null) {

                              System.out

                                          .println("... the model took too long to respond "

                                                      + "... program terminated.");

                              break;

                        }

                        milliseconds = new Date().getTime() - startMilliseconds.getTime();

                        System.out.println("finished. Time in milliseconds: " + milliseconds

                                    + "\n\n");

 

                        // We just read an entry off the queue. Put the data into

                        // a record object so the program can access the fields of

                        // the data by name. The Record object will also convert

                        // the data from server format to Java format.

                        Record mdloData = mdloFormat.getNewRecord(mdloDataQueueEntry

                                    .getData());

 

                        // Get two values out of the record and display them.

                        String mdloInstruction = (String) mdloData.getField("OINSTRTN");

                        String mdloMessage = (String) mdloData.getField("OMESSAGE");

 

                        System.out.println("The model received instruction: "

                                    + mdloInstruction + " and returned data: "

                                    + mdloMessage);

                        System.out.println("\n");

 

                  }  // end of while(true)

            } catch (Exception e) {

                  // If any of the above operations failed say the data queue

                  // operation failed and output the exception.

                  if (as400 != null)

                        as400.disconnectAllServices();

                  System.out.println("Data Queue operation failed");

                  System.out.println(e);

                  e.printStackTrace();

            }

 

            // clean up

            if (as400 != null)

                  as400.disconnectAllServices();

            System.exit(0);

      }

}