NetAcquire Corporation Logo Image Image Image Image Image Image Image Image
NetAcquire Corporation Logo Home Search Products News Services Careers Support Contact

NetAcquire Publish/Subscribe Java Notification Example

Remote temperature data acquisition 

Publisher and Subscriber Hardware Diagram

The example below uses the event-driven variant of the NetAcquire Java Publish/Subscribe API. Instead of explicitly calling the subscription to obtain its latest value as was done in the simpler examples, here the flow of control is reversed and the subscription calls a user-defined callback method whenever the subscription's value changes.


   import com.netacquire.naps.*;

   public class SubscribeExample2
   {
      public static void main(String[] args)
      {
         try {
            Connection netacquire1 = new Connection("na1");
            Subscription reactor_temperature = 
               new Subscription(netacquire1, "reactor_temperature");
            reactor_temperature.addSubscriptionListener(new SubscriptionListener()
            {
               public void subscriptionReceived(SubscriptionEvent event)
               {
                  System.out.println(event.getTimestamp() + ": " + event.asReal32());
               }
               public void listenerRemoved(Subscription sub)
               {
                  System.out.println("Removed from " + sub.getPublicationName());
               }
               public void connectionLost(Subscription sub)
               {
                  System.out.println(sub.getPublicationName() + " lost its connection");
               }
            });
            System.in.read();    // wait until users presses <Enter>
         } catch(Exception e) {
            e.printStackTrace(System.out);
         }
         System.exit(0);
      }
   }

The user declares a customized callback class. In Java this is most easily done with an anonymous class registered as a subscription listener. This class overrides the three SubscriptionListener methods: subscriptionReceived, listenerRemoved, and connectionLost.

Note, how the subscriptionReceived callback method accesses the event's timestamp. Note, also that an explicit accessor is used to retrieve the subscription's value in the form of a 32 bit float. Instead, one can also insert the event directly into an output stream, in which case the implementation would display all the information associated with the event (name, type, raw value, etc.).

The first and second lines of the main method represent a data publisher and a specific data subscription. They are created exactly as in the simpler examples. Then the anonymous callback object is registered with the subscription. From this point on the subscriptionReceived method of this object will be called whenever the physical quantity that corresponds to the subscription changes. The application's main thread can now continue with other chores, in this example it simply waits for the user to press the <Enter> key.


Home | Search | Products | News | Services | Careers | Support | Contact | Telemetry | Range/Radar | Avionics
Aerospace Test | Communication | Remote Monitoring | Industrial Automation | Extreme | Protocol Conversion
Simulation | Hardware Overview | Analog I/O | Digital I/O | GPIB/IEEE 488 | SpaceWire | FPGA
Source Selection
| High Availability | Software Overview | WWW | Storage | Publish/Subscribe
Java | Windows | Data Flow | Time | Extensions | Decom | CCSDS | Mux/Demux | Demos



Last modified 12/2/2016
Copyright 1996 - 2016
NetAcquire Corporation
All rights reserved.
webmaster@netacquire.com