package dataqueue;

 

import java.beans.PropertyChangeEvent;

import java.beans.PropertyChangeListener;

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 DtaQExample3b {

 

      /**

       * 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 an anymous inner class class as the listener for

                  // property change events

                  dtaqThread.addPropertyChangeListener(new PropertyChangeListener() {

 

                        public void propertyChange(PropertyChangeEvent evt) {

                              // TODO Auto-generated method stub

                              // Print to console that a property change event was fired and

                              // the name of the property.

                              System.out.println("     ************* propertyChange() name is: " +

                                    evt.getPropertyName() + " ********");

                              // The thread listening to the data queue could fire different

                              // events based on what was read from the data queue.

                              if ( evt.getPropertyName().equals("dqRead") ) {

                                    // dump the data read from the data queue to the console.

                                    try {

                                          // Record object with the data read from the data queue

                                          // is passed as the new value in the event

                                          Record mdloData = (Record) evt.getNewValue();

                                         

                                          // 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.trim()

                                                      + "\n     *                and returned data: "

                                                      + mdloMessage.trim());

                                          System.out

                                                      .println("     ********************************************************");

                                    } catch (Exception e) {

                                          e.printStackTrace();

                                    }

                              }

                             

                        }

 

                  } );

                 

                  // 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);

      }

 

}