|
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, { String message; try { String username; char gender; long age;
//Retrieve the value of the predefined attributes. message = n.getPredefinedAttrString( username = n.getPredefinedAttrString( age = n.getPredefinedAttrLong("Age"); gender = n.getPredefinedAttrChar( //Build the notification. System.out.println("Incoming notification 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 public Subscriber2() { } public void notify(Notification n, { String message; try { String username; char gender; long age; String interest; //Retrieve the value of the predefined attributes. message = n.getPredefinedAttrString( username = n.getPredefinedAttrString( age = n.getPredefinedAttrLong("Age"); gender = n.getPredefinedAttrChar( //Retrieve the value of the discretionary attribute. n.getDiscretionaryAttr("Interest"); System.out.println("Incoming notification 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 = 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", new Subscriber1(), "Listen to Jane"); p.subscribe("Sample.Chat", new Subscriber1(), "Girl Scouts");
p.subscribe("Sample.Chat", new Subscriber2(), "Country music fans"); System.out.println("Subscription was Thread.currentThread().sleep(30000); } catch (Exception e) { e.printStackTrace(); } } } |
|