/*

 *  JXPoller

 *  JXPollerMXBean implementation

 * 

 */

 

package jPoller;

 

import java.io.File;

import javax.management.*;

 

public class JXPoller

      extends NotificationBroadcasterSupport

      implements JXPollerMXBean {

     

      public JXPoller() {          

            _Status = _INITIALIZED;

      }

 

      // keep the MXBean running on the local JVM

      public void Daemon() {

        //run until interrupted checking MXBean state

      while( !Thread.interrupted() ) {

            try {

               Thread.sleep(3000L);                

                  if( getStatus() == _START) {

                        System.out.println("Polling: " + getBase());

                        runPoller();

                  }              

               

                   // enable NO NEW DATA-NEW DATA notifications

                  //checkNewData();

               

            } catch ( InterruptedException  e ) {

                  System.out.println("jPoller is exiting.");

            }

 

            } // while( !Thread.interrupted()

      }

     

      public void Run() {

              if( poller == null) {

                    poller = new FcgPoller(_Base);

                    System.out.println("Starting Poller: " + poller.getName());

                    _LastCount = 0;

                    _LastTime = System.currentTimeMillis();

              }

              _Status = _START;

      }

     

      // allow JXPoller.Run() to return immediately (dahlia.Poller.run() is blocking)

      public void runPoller() {

              if(poller != null) {

                   _Status = _RUNNING;

                   poller.run(); 

              }

      }          

     

      // stop the poller and destroy its reference

      public void Stop() {

            if( poller != null){

                  System.out.println("Stopping Poller: " + poller.getName());

            poller.stopWhenEmpty();

         

            _MsgCountAccum += poller._MsgCount;

            poller = null;

             }

        _Status = _STOP;

      }   

              

      public int getMsgCount() {         

        if( poller != null) {

              return poller._MsgCount + _MsgCountAccum;

        }

      return _MsgCountAccum;

      }

     

      public int getStatus() {

            return _Status;

      }

     

      public String getBase() {

            return _Base.toString();

      }

     

      public void setBase(String base) {

            _Base.equals(base);

      }    

 

      //this code isn't finished yet...

      private void checkNewData() {

        // NO NEW DATA notification

            if( getMsgCount() <= _LastCount) {

                     if( (System.currentTimeMillis() - _LastTime) > _TIMEOUT ) {

DATASTATE = 0;

//SendNotification(_DATASTATE);                   

System.out.println("No new data within " + (_TIMEOUT / 60) + " seconds in\n" +

getBase() + "\nLast count: " + _LastCount + " Last time: " + _LastTime);                          

                     }                   

            }              

 

        // NEW DATA notification

            if( getMsgCount() > _LastCount) {

                     _LastCount = getMsgCount();

                     _LastTime = System.currentTimeMillis();

                    

                     // send NEW DATA notification if NEW DATA after NO NEW DATA

                     if( _DATASTATE == 0) {

                        _DATASTATE = 1;

                        //SendNotification(_DATASTATE);

                        System.out.println("New data in\n" + getBase());

                     }

            }          

      }

     

// dahlia.Poller

private FcgPoller poller = null;

//private File _Base = new File("C:/Documents and Settings/martin_j/workspace/JeffSimplePoller/hl7messages/JVHospitals");

private File _Base = new File("C:/Documents and Settings/Jeffrey Martin/workspace/JeffSimplePoller/hl7messages/JVHospitals"); 

 

//NO NEW DATA notifications and timers

private int _LastCount = 0;

private long _LastTime = System.currentTimeMillis();

private long _ThisTime = 0;

private final long  _TIMEOUT = 60000;                 // 60 seconds

private int _DATASTATE = 0;                           // NO NEW DATA-NEW DATA toggle

 

// MXBean state flags

private int _Status = -1;

private final int _INITIALIZED = 0;                   // JXPoller MXBean created and registered

private final int _START = 1;                         // poller START command processed (new poller reference created)

private final int _STOP = 2;                          // poller STOP command processed (poller reference destroyed)

private final int _RUNNING = 3;                       // poller run() method called

 

//misc

private int _MsgCountAccum = 0;                       // count processed messages between poller invocations

}