package dataqueue;

 

import java.beans.PropertyChangeListener;

import java.beans.PropertyChangeSupport;

 

import com.ibm.as400.access.AS400;

import com.ibm.as400.access.ConnectionDroppedException;

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

import common.Me;

 

 

/**

 * Read the model output data queue. The AS400 object and the key to read was

 * passed to the constructor of the class when instantiated. The IFS path of the

 * model output data queue is read from Me class. Disconnecting the DATAQUEUE

 * service of the AS400 object causes and exception and ends the exection of the

 * thread. Any unexpected AS400 exceptions also end the execution of the thread

 * after printing a stack trace.

 *

 * @author Bill Blalock

 *

 */

public class DtaQExample3Thread extends Thread {

     

      /**

       * Key of model data queue for read, passed to the class when instantiated.

       */

      private String KEY;

 

      /**

       * AS400 object to be passed to the class when instantiated.

       */

      private AS400 as400 = null;

 

      final PropertyChangeSupport pcs = new PropertyChangeSupport(this);

 

      // don't allow default constructor

      private DtaQExample3Thread() {

      };

 

      /**

       * Constructor of thread to listen to model output data queue.

       *

       * @param as400

       *            Instance of AS400 provided by creator of thread.

       * @param KEY

       *            Value of key to listen for.

       */

      DtaQExample3Thread(AS400 as400, String KEY) {

            this.as400 = as400;

            this.KEY = KEY;

      }

 

      /**

       * Read the model output data queue.  The AS400 object and the key to

       * read was passed to the constructor of the class when instantiated. 

       * The IFS path of the model output data queue is read from Me class.

       * Disconnecting the DATAQUEUE service of the AS400 object causes and

       * exception and ends the exection of the thread.  Any unexpected AS400

       * exceptions also end the execution of the thread after printing a

       * stack trace.

       */

      public void run() {

            RecordFormat mdloFormat = new MDLOFMFormat();

 

            try {

 

                  // connect to data queue

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

                 

                  Record mdloData = null;

                  Record oldMdloData = null;

 

                  // this loop won't end unless an Exception occurs or the JVM is ended

                  while (true) {

                        // Wait for the model response.

                        KeyedDataQueueEntry mdloDataQueueEntry = mdloDq.read(KEY, 3600, "EQ");

 

                        // Using a wait of -1 is easier programming, you don't need to

                        // handle time out.  This is okay for short lived program but

                        // not for long life programs.  If the data queue service

                        // failed on the System i, say the system was IPL'd, the program

                        // waiting forever would never see the interruption.

                        if (mdloDataQueueEntry == null) {

                              System.out

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

                              continue;

                        }

                        // 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.

                        mdloData = mdloFormat

                                    .getNewRecord(mdloDataQueueEntry.getData());

                        // Pass the record object to the property change listener.

                        this.pcs.firePropertyChange("dqRead", oldMdloData, mdloData);

                        // save the record object just read as the old record object

                        oldMdloData = mdloData;

 

                  } // end of while(true)

            } catch (ConnectionDroppedException e) {

                  // drop connection okay -- main() drops the connection before

                  // exiting ... that will cause the KeyedDataQueue.read() method to

                  // fail

                  System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

                  System.out

                              .println("The rug has been pulled out from under the data queue reading thread!!!");

                  System.out.println("The thread has been forced to end.");

                  System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

            } catch (InterruptedException e) {

                  System.out.println("InterruptedException: " + e.getMessage());

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

                  if (as400 != null)

                        if (as400.isConnected())

                              as400.disconnectAllServices();

                  System.exit(0);

            }

      }

 

      /**

       * Add a PropertyChangeListener for bound property using the services of the

       * PropertyChangeSupport class.

       * @param listener

       */

      public void addPropertyChangeListener(PropertyChangeListener listener) {

            this.pcs.addPropertyChangeListener(listener);

      }

     

       /**

       * Remove a PropertyChangeListener using the services of the

       * PropertyChangeSupport class.

       * @param listener

       */

      public void removePropertyChangeListener(PropertyChangeListener listener) {

            this.pcs.removePropertyChangeListener(listener);

      }

 

}