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.Record;

import com.ibm.as400.access.RecordFormat;

 

import common.IpDtaQKey;

import common.MDLIFMFormat;

import common.Me;

 

 

public class DtaQExample3d {

 

      /**

       * 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

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

 

                  // create instance of threat which reads data queue

                  ModelOutputDataQueue mdloDq = new ModelOutputDataQueue(as400, Me.getMDLO_DTAQ(),

                              IpDtaQKey.getDqKey() );

// If running in QSHELL or environment where data queue key can't be built

// From IP address use the static KEY defined in ME

//                ModelOutputDataQueue mdloDq = new ModelOutputDataQueue(as400, Me.getMDLO_DTAQ(),

//                            Me.getKEY, mdloFormat);

                  Thread mdloDqThread = new Thread(mdloDq);

                 

                  // register a separate class as the listener for

                  // property change events

                  mdloDq.addPropertyChangeListener(new DtaQExample3dListener() );

                 

                  // execute the run() method of the thread

                  mdloDqThread.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", mdloDq.getDqKey());

                        mdliData.setField("IINSTRTN", mdliInstruction);

                        mdliData.setField("IMESSAGE", mdliMessage);

 

                        Date start = new Date();

                        System.out

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

                        mdliDq.writeDataQueue(mdliData);

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

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

 

                  } // end of while(true)

                 

                  // stop reading the model output data queue, method sets

                  // flag to break loop then writes a dummy record to the model

                  // output data queue which the class will read then fall out

                  // of loop.

                  // wait for thread to end and disconnect.

                  try {

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

                        mdloDq.stopReadingDq();

                        mdloDqThread.join(10000);

                        as400.disconnectAllServices();

                  } catch(InterruptedException e) {

                        System.out.println("...Boo hiss, the thread didn't shut down!" +

                                    "finally clause will clean up");                     

                  }

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

            }

            finally {

            // in case the as400 connection is still alive

                  if (as400 != null) {

                        if( as400.isConnected() )

                              as400.disconnectAllServices();

                  }

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

                  System.exit(0);

            }

      }

}