PreCache NetInjector Documentation Center : PreCache NetInjector Application Development

Common Cases, Subscription — Java

   







Common Cases, subscription code — Java

— the complete subscription code


The previous code samples presented a simple publish/subscribe application. In this tutorial, we have learned how to enhance subscriptions by

import com.precache.event.*;

import java.io.*;

//Define a subscriber.

class Subscriber1 implements Subscriber

{

      public Subscriber1()

      {

      }

      public void notify(Notification n,
            Object closure)

      {

            String message;

            try

            {

                  String username;

                  char gender;

                  long age;

//Retrieve the value of the predefined attributes.

                  message = n.getPredefinedAttrString(
                        "Message");

                  username = n.getPredefinedAttrString(
                        "UserName");

                  age = n.getPredefinedAttrLong("Age");

                  gender = n.getPredefinedAttrChar(
                        "Gender");

//Build the notification.

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

                  System.out.println("From:");

                  System.out.println("Name = " + username);

                  System.out.println("Age = " + age);

                  if ((gender == 'M')||(gender=='m'))

                        System.out.println("Male");

                  else

                  if ((gender == 'F')||(gender=='f'))

                        System.out.println("Female");

                        System.out.println();

            }

            catch(Exception e)

            {

                  System.out.println(e);      

            }

      }

};

//Define a subscriber which can retrieve a
//discretionary attribute.

      public Subscriber2()

      {

      }

      public void notify(Notification n,
            Object closure)

      {

            String message;

            try

            {

                  String username;

                  char gender;

                  long age;

                  String interest;

//Retrieve the value of the predefined attributes.

                  message = n.getPredefinedAttrString(
                        "Message");

                  username = n.getPredefinedAttrString(
                        "UserName");

                  age = n.getPredefinedAttrLong("Age");

                  gender = n.getPredefinedAttrChar(
                        "Gender");

//Retrieve the value of the discretionary attribute.

                  n.getDiscretionaryAttr("Interest");

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

                  System.out.println("From:");

                  System.out.println("Name = " + username);

                  System.out.println("Age = " + age);

                  if ((gender == 'M')||(gender=='m'))

                        System.out.println("Male");

                  else

                  if ((gender == 'F')||(gender=='f'))

                        System.out.println("Female");

                        System.out.println("Interest =
                              " + interest);

                        System.out.println();

            }

            catch(Exception e)

            {

                  System.out.println(e);      

            }

      }

};

public class sub2

{

      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(40,3002);

//Submit the subscription.

                  p.subscribe("Sample.Chat",
                        "UserName==\"Jane\"UserName==\"Jane\"",

                  new Subscriber1(),

                        "Listen to Jane");

                  p.subscribe("Sample.Chat",
                        "((Gender == 'F' or Gender == 'f')
                        and Age < 14)",

                        new Subscriber1(),

                        "Girl Scouts");

                  p.subscribe("Sample.Chat",
                        "Interest == \"Country Music\"Music\"",

                        new Subscriber2(),

                        "Country music fans");

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

                  Thread.currentThread().sleep(30000);

            }

            catch (Exception e)

            {

                  e.printStackTrace();

            }

      }

}