PreCache NetInjector Documentation Center : PreCache NetInjector Application Development

Basic Case, Subscription— Java

   







Basic Case, Subscription code— Java

— the complete subscription code


The previous code samples presented the basics of subscription. A subscription application retrieves content in much the same way as it was published, albeit after first defining a subscription class.

Here is the basic sequence of the subscription process:

  1. Create a proxy connection for the channel. ( Proxy Initialization, Basic Case, Publication — Java)
  2. Define a subscriber class. ( Subscriber)
  3. Create the subscription. ( Subscription)
  4. Associate a closure with the subscription. ( Subscription closure)
  5. Retrieve the content of the notification. ( Notification)

import com.precache.event.*;

import java.io.*;

//Define a subscriber class.

class MySubscriber implements Subscriber

{

      public MySubscriber()

      {

      }

//Define the callback method.

      public void notify(Notification n,
            Object closure)

      {

            String message;

            try

            {

//Retrieve the value of the predefined attribute.

                   message = n.getPredefinedAttrString(
"Message");

                  System.out.println(
                  "Incoming notification for (
                  " + closure + "):" + message);

            }

            catch(Exception e)

            {

                  System.out.println(e);      

            }

      }

};

public class sub1

{

      static

      {

            System.loadLibrary("event-j");

      }

      public static void main(String argv[])

      {

//Create a proxy for the channel and client.

      Proxy p;

      try

      {

            p = new Proxy(7,3002);

//Submit the subscription.

            p.subscribe("Sample.Intro1",
                  new MySubscriber(),
                  "My First Subscription");

            System.out.println(
                  "Subscription was submitted...
                  waiting 30 seconds for incoming
                  notifications");

                  Thread.currentThread().sleep(30000);

            }

            catch (Exception e)

            {

                  e.printStackTrace();

            }

      }

}

In the next chapter, Common Cases, Publication — Java, we will learn more about attributes.