package dataqueue;

 

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.util.Date;

 

import com.ibm.as400.access.AS400;

import com.ibm.as400.access.DataQueue;

import com.ibm.as400.access.Record;

import com.ibm.as400.access.RecordFormat;

import common.MDLIFMFormat;

import common.Me;

 

 

public class DtaQExample3a {

 

      /**

       * Start thread to listen to model output data queue.  Read input

       * from console and send to model input data queue.

       * @param parameters

       */

      public static void main(String[] parameters) {

 

            AS400 as400 = null;

            System.out.println(" ");

 

            // if a system name was not specified, display help text and exit.

            try {

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

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

 

                  // Model input data queue

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

 

                  // create instance of threat which reads data queue

                  DtaQExample3Thread dtaqThread = new DtaQExample3Thread(as400, Me.getKEY());

                 

                  // register a separate class as the listener for

                  // property change events

                  dtaqThread.addPropertyChangeListener(new DtaQExample3aListener() );

                 

                  // execute the run() method of the thread

                  dtaqThread.start();

 

                  // create RecordFormat objects from pre-compiled classes

                  RecordFormat mdliFormat = new MDLIFMFormat();

 

                  // create a reader for the console

                  BufferedReader stdin = new BufferedReader(new InputStreamReader(

                              System.in), 1);

 

                  // loop forever until break statement of JVM ends

                  while (true) {

                        // get input from the keyboard

                        System.out

                                    .println("main()- Instruction *STOPNOW ends this application");

                        System.out

                                    .println("main()- Please enter the instruction to send to the model: ");

                        String mdliInstruction = stdin.readLine();

 

                        // break loop if *STOPNOW

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

                              System.out

                                          .println("main()- .... ok! stopping now .... goodbye\n\n");

                              break;

                        }

                       

                        System.out

                                    .println("main()- Please enter the data to send to the model: ");

                        String mdliMessage = stdin.readLine();

 

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

                        Record mdliData = mdliFormat.getNewRecord();

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

                        mdliData.setField("IINSTRTN", mdliInstruction);

                        mdliData.setField("IMESSAGE", mdliMessage);

 

                        Date start = new Date();

                        System.out

                                    .print("main()-          Writing to MDLI data queue ... ");

                        mdliDq.write(mdliData.getContents());

                        long mills = new Date().getTime() - start.getTime();

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

 

                  } // end of while(true)

                 

                  // pull the rug out from under the thread which is reading

                  // the model output data queue.  Give it time to detect the

                  // error (disconnected) and shut down

                  as400.disconnectAllServices();

                  try {

                        System.out.println("....terminating program.");

                        Thread.sleep(500);

                  } catch(InterruptedException e) {}

            } 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();

            }

 

            System.exit(0);

      }

 

}