1. for notification service, refer to document http://www.prismtech.com/download-documents/635
2. 简单的push event的例子(来自openfusion文档):
The Supplier-Consumer example contains the worked example code from the OpenFusion Notification Service manual. The example illustrates how to create a simple supplier and consumer that use the push model to send and receive events. The supplier sends events to the Notification Service and the consumer receives them.
This example consists of two components:
MyPushSupplier.java: The Supplier uses the simple push model to send events to the event channel. MyPushSupplier will perform the following tasks:
/************************************************************************* * - COPYRIGHT NOTICE - * * * * The information contained herein and which follows is proprietary * * information and is the property of PrismTech Corporation. This * * information is not to be divulged, released, copied, reproduced, or * * conveyed to unauthorized personnel, companies, or other institutions * * without the direct and expressed approval in writing of PrismTech * * Corporation. * * * * * * - DISCLAIMER - * * Neither PrismTech Limited nor PrismTech Corporation ("PrismTech") * * are under any obligation to support the use of this code which is * * provided without warranty of any kind. PrismTech shall (except as * * otherwise prohibited by law) have no liability whatsoever in relation * * to its use. * * * ************************************************************************/ /** * @author PrismTech * @version Version 2.5 */ package com.prismt.cos.CosNotification.examples.Push; import org.omg.CosNotification.EventHeader; import org.omg.CosNotification.EventType; import org.omg.CosNotification.FixedEventHeader; import org.omg.CosNotification.Property; import org.omg.CosNotification.StructuredEvent; import org.omg.CosNotification.UnsupportedAdmin; import org.omg.CosNotification.UnsupportedQoS; import org.omg.CosNotifyChannelAdmin.AdminLimitExceeded; import org.omg.CosNotifyChannelAdmin.ChannelNotFound; import org.omg.CosNotifyChannelAdmin.ClientType; import org.omg.CosNotifyChannelAdmin.EventChannel; import org.omg.CosNotifyChannelAdmin.EventChannelFactory; import org.omg.CosNotifyChannelAdmin.EventChannelFactoryHelper; import org.omg.CosNotifyChannelAdmin.ProxyConsumer; import org.omg.CosNotifyChannelAdmin.StructuredProxyPushConsumer; import org.omg.CosNotifyChannelAdmin.StructuredProxyPushConsumerHelper; import org.omg.CosNotifyChannelAdmin.SupplierAdmin; import org.omg.CosNotifyComm.StructuredPushSupplier; import org.omg.CosNotifyComm.StructuredPushSupplierHelper; import org.omg.CosNotifyComm.StructuredPushSupplierOperations; import org.omg.CosNotifyComm.StructuredPushSupplierPOATie; import org.omg.PortableServer.POAHelper; import org.omg.PortableServer.POAPackage.ServantNotActive; import org.omg.PortableServer.POAPackage.WrongPolicy; /* * Example Push Supplier. This shows how to create a simple push * supplier that creates structured events and sends them to the event * channel. It will connect to channel 0, if it exists. If it does not exist * it will create the channel and push the structured events to it. */ public class MyPushSupplier implements StructuredPushSupplierOperations { /* The ORB*/ private static org.omg.CORBA.ORB orb; /* The root POA*/ private static org.omg.PortableServer.POA rootPoa; /* Proxy push consumer variable */ private StructuredProxyPushConsumer proxy = null ; /* The event channel */ private EventChannel channel = null; /* A counter to enable the events to be numbered */ private int counter = 0; /** * The resolve name for the notification service. If the user does not * specify this via the "-r" parameter the default is used. */ private static String resolveName = null; private static String defaultResolveName = "NotificationService"; /* The number of events to send */ private int eventCount = 100; /** * Extracts a value for a specific switch from an array of arguments. * @param args The argument array (e.g. command-line). * @param arg The argument to search for. * @param def The default value to use, if the argument is not found or * if there is no argument following it. * @return The found argument or the default. */ private static String extractArg (String[] args, String arg, String def) { String ret = def; for (int i = 0 ; i < args.length ; i++) { if (args[i].equals(arg)) { if (i <= (args.length-2)) { ret = args[i+1]; } break; } } return ret; } /** * The <code>main</code> method. This initialises the ORB and constructs * a Push Supplier object. The push supplier is then connected to the proxy * and sends events. * * @param args a <code>String[]</code> Arguments passed in. */ public static void main (String[] args) { /* Initialise the ORB */ try { orb = org.omg.CORBA.ORB.init (args, null); } catch( Exception e ) { e.printStackTrace(); System.exit(1); } /* In order to avoid problems with VisiBroker when registering the name NotificationService, rename it */ if (com.prismt.openfusion.Version.isVisibroker ()) { defaultResolveName = "OFNotificationService"; } /* End of specific code to deal with VisiBroker and registering NotificationService name problem*/ /* Extract any command-line arguments we need for configuration. */ resolveName = extractArg (args, "-r", defaultResolveName); try { org.omg.CORBA.Object poa = orb.resolve_initial_references ("RootPOA"); rootPoa = POAHelper.narrow (poa); } catch (org.omg.CORBA.ORBPackage.InvalidName e) { System.err.println ("Can't get RootPOA"); System.exit (1); } runServer (); /* Create a new instance of a push supplier */ System.out.println ("Creating a new push supplier instance"); MyPushSupplier ps = new MyPushSupplier (); /* Connect to the proxy push consumer using the connect method */ System.out.println ("Connecting to proxy"); ps.connect (); /* Push structured events */ ps.pushEvents (); /* Disconnect */ ps.disconnect (); orb.shutdown (true); System.exit (0); } /** * The constuctor for the class which creates a new * <code>PushSupplier</code> instance. */ public MyPushSupplier () { org.omg.CORBA.Object obj = null; EventChannelFactory factory = null; try { /* resolve the Notification Service */ System.out.println ("Resolving initial references to "+ resolveName); obj = orb.resolve_initial_references (resolveName); System.out.println ("Success"); } catch (org.omg.CORBA.ORBPackage.InvalidName ex) { System.err.println ("Failed to resolve Notification Service"); System.exit (1); } /* obtain an object reference to the EventChannelFactory */ factory = EventChannelFactoryHelper.narrow (obj); try { System.out.println ("Looking for channel 0"); /* try and obtain the event channel with ID 0 */ channel = factory.get_event_channel (0); System.out.println ("Channel found"); } catch (ChannelNotFound ex) { /* Channel 0 was not found so create a channel with no QoS * or admin properties. */ System.out.println ("Unable to find channel so creating one"); /* Admin property, id holder and QoS property variables.*/ Property[] qosProp = new Property[0]; Property[] admProp = new Property[0]; org.omg.CORBA.IntHolder id = new org.omg.CORBA.IntHolder (); try { /* Channel 0 was not found so create a channel with no QoS * or admin properties * */ channel = factory.create_channel (qosProp, admProp, id); System.out.println ("Created channel"); } catch (UnsupportedQoS exc) { /* Thrown if unknown QoS properties were specified */ System.err.println ("Unsupported QoS properties specified!"); } catch (UnsupportedAdmin exc) { /* Thrown if we specified unknown admin properties */ System.err.println ("Unsupported admin properties specified"); } } } /** * Connects to the proxy structured push consumer * @return a <code>StructuredProxyPushConsumer</code> instance */ public void connect () { /* Defines the type of proxy required */ ClientType ctype = ClientType.STRUCTURED_EVENT; /* Holder to hold the proxy id */ org.omg.CORBA.IntHolder pid = new org.omg.CORBA.IntHolder (); ProxyConsumer proxyConsumer = null; /* Obtain the supplier admin object reference */ SupplierAdmin admin = channel.default_supplier_admin (); try { /* obtain a structured push consumer object reference */ proxyConsumer = admin.obtain_notification_push_consumer (ctype, pid); } catch (AdminLimitExceeded ex) { /* Thrown if the admin object is unable to have any more proxy * consumers associated with it. */ System.err.println ("Maximum number of proxies exceeded!"); System.exit (1); } /* Narrow the proxy consumer to a Structured Proxy Push consumer */ proxy = StructuredProxyPushConsumerHelper.narrow (proxyConsumer); try { /* connect to the proxy */ proxy.connect_structured_push_supplier(getObject()); } catch (org.omg.CosEventChannelAdmin.AlreadyConnected ex) { /*Thrown if the proxy already has a connected supplier*/ System.err.println ("Already connected!"); System.exit (1); } } /** * Disconnects this client from the proxy structured push consumer */ public void disconnect () { if (proxy != null) { proxy.disconnect_structured_push_consumer (); } } /** * The <code>disconnect_sequence_push_supplier</code> method is used * by the event channel to disconnect the supplier. */ public void disconnect_structured_push_supplier () { System.out.println ("Disconnected!"); } /** * The <code>subscription_change</code> method allows the event channel to * notify the supplier of the types of events required by the consumers. * @param added[] the <code>EventType</code> now required * @param removed[] the <code>EventType</code> that has been removed */ public void subscription_change (EventType added[], EventType removed[]) { } /** Push structured events * @param StructuredProxyPushConusmer the proxy object */ void pushEvents () { // Loop for pushing events onto the proxy for (int i = 0 ; i < eventCount ; i++) { try { /* create structured events which contain a long */ StructuredEvent event = new StructuredEvent (); EventType type = new EventType ("example", "test"); FixedEventHeader fixed = new FixedEventHeader (type, "event"); Property variable[] = new Property[0]; org.omg.CORBA.Any data = orb.create_any (); data.insert_long (counter); event.header = new EventHeader (fixed, variable); event.filterable_data = new Property[0]; event.remainder_of_body = data; proxy.push_structured_event (event); System.out.println ("Pushed event: " + (counter++)); /* Wait for a second before sending the next event */ Thread.sleep (1000); } catch (InterruptedException ex) {} catch (org.omg.CosEventComm.Disconnected ex) { System.err.println ("Proxy was disconnected"); System.exit (0); } } } static private void runServer () { try { rootPoa.the_POAManager ().activate (); } catch (org.omg.PortableServer.POAManagerPackage.AdapterInactive ex) { System.err.println ("Can't activate POAManager"); System.exit (1); } Thread thread = new Thread () { public void run () { orb.run (); } }; thread.setDaemon (true); thread.setName ("OpenFusion ORB thread"); thread.start (); } private StructuredPushSupplier getObject() { StructuredPushSupplier serverObj = null; org.omg.PortableServer.Servant servant = new StructuredPushSupplierPOATie (this, rootPoa); try { org.omg.CORBA.Object ref = rootPoa.servant_to_reference (servant); serverObj = StructuredPushSupplierHelper.narrow (ref); } catch (ServantNotActive e) { System.err.println ("Unexpected Exception: " + e); System.exit (1); } catch (WrongPolicy e) { System.err.println ("Unexpected Exception: " + e); System.exit (1); } return serverObj; } }
MyPushConsumer.java: MyPushConsumer uses the simple push model to receive messages from the Notification Service. The MyPushConsumer will perform the following tasks:
/************************************************************************* * - COPYRIGHT NOTICE - * * * * The information contained herein and which follows is proprietary * * information and is the property of PrismTech Corporation. This * * information is not to be divulged, released, copied, reproduced, or * * conveyed to unauthorized personnel, companies, or other institutions * * without the direct and expressed approval in writing of PrismTech * * Corporation. * * * * * * - DISCLAIMER - * * Neither PrismTech Limited nor PrismTech Corporation ("PrismTech") * * are under any obligation to support the use of this code which is * * provided without warranty of any kind. PrismTech shall (except as * * otherwise prohibited by law) have no liability whatsoever in relation * * to its use. * * * ************************************************************************/ /** * @author PrismTech * @version Version 2.5 */ package com.prismt.cos.CosNotification.examples.Push; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import org.omg.CosNotification.EventType; import org.omg.CosNotification.Property; import org.omg.CosNotification.StructuredEvent; import org.omg.CosNotification.UnsupportedAdmin; import org.omg.CosNotification.UnsupportedQoS; import org.omg.CosNotifyChannelAdmin.AdminLimitExceeded; import org.omg.CosNotifyChannelAdmin.ChannelNotFound; import org.omg.CosNotifyChannelAdmin.ClientType; import org.omg.CosNotifyChannelAdmin.ConsumerAdmin; import org.omg.CosNotifyChannelAdmin.EventChannel; import org.omg.CosNotifyChannelAdmin.EventChannelFactory; import org.omg.CosNotifyChannelAdmin.EventChannelFactoryHelper; import org.omg.CosNotifyChannelAdmin.ProxySupplier; import org.omg.CosNotifyChannelAdmin.StructuredProxyPushSupplier; import org.omg.CosNotifyChannelAdmin.StructuredProxyPushSupplierHelper; import org.omg.CosNotifyComm.StructuredPushConsumer; import org.omg.CosNotifyComm.StructuredPushConsumerHelper; import org.omg.CosNotifyComm.StructuredPushConsumerOperations; import org.omg.CosNotifyComm.StructuredPushConsumerPOATie; import org.omg.PortableServer.POAHelper; import org.omg.PortableServer.POAPackage.ServantNotActive; import org.omg.PortableServer.POAPackage.WrongPolicy; /** * Example Push Consumer. This shows how to create a basic push consumer * that connects to channel 0, if it exists. If the channel does not exist it * is created and the consumer will receive structured events from this event * channel. */ public class MyPushConsumer implements StructuredPushConsumerOperations { /* The ORB */ private static org.omg.CORBA.ORB orb; /* The root POA */ private static org.omg.PortableServer.POA rootPoa; /* Proxy push supplier variable */ private StructuredProxyPushSupplier proxy = null ; /* The event channel */ private EventChannel channel = null; /** * The resolve name for the notification service. If the user does not * specify this via the "-r" parameter the default is used. */ private static String resolveName = null; private static String defaultResolveName = "NotificationService"; /** * Extracts a value for a specific switch from an array of arguments. * @param args The argument array (e.g. command-line). * @param arg The argument to search for. * @param def The default value to use, if the argument is not found or * if there is no argument following it. * @return The found argument or the default. */ private static String extractArg (String[] args, String arg, String def) { String ret = def; for (int i = 0 ; i < args.length ; i++) { if (args[i].equals(arg)) { if (i <= (args.length-2)) { ret = args[i+1]; } break; } } return ret; } /** * The <code>main</code> method. This initialises the ORB and constructs * a Push Consumer object. The push consumer is then connected to the proxy * and waits for events. * @param args a <code>String[]</code> Arguments passed in */ public static void main (String[] args) { /* Initialise the ORB */ try { orb = org.omg.CORBA.ORB.init (args, null); } catch( Exception e ) { e.printStackTrace(); System.exit(1); } /* In order to avoid problems with VisiBroker when registering the name NotificationService, rename it */ if (com.prismt.openfusion.Version.isVisibroker ()) { defaultResolveName = "OFNotificationService"; } /* End of specific code to deal with VisiBroker and registering NotificationService name problem*/ /* Extract any command-line arguments we need for configuration. */ resolveName = extractArg (args, "-r", defaultResolveName); try { org.omg.CORBA.Object poa = orb.resolve_initial_references ("RootPOA"); rootPoa = POAHelper.narrow (poa); } catch (org.omg.CORBA.ORBPackage.InvalidName e) { System.err.println ("Can't get RootPOA"); System.exit (1); } runServer (); /* Create a new instance of a Push Consumer */ System.out.println ("Creating a push consumer"); MyPushConsumer pc = new MyPushConsumer (); /* Connect to the proxy push supplier using the connect method.*/ System.out.println ("Connecting to the proxy"); pc.connect (); pc.waitForReturn ("Ready for events... press return to exit"); /* Disconnect */ pc.disconnect (); orb.shutdown (true); System.exit (0); } /** * The constuctor for the class which creates a new * <code>PushConsumer</code> instance. */ public MyPushConsumer () { /* CORBA Object reference to the Notification Service.*/ org.omg.CORBA.Object obj = null; /* Event channel factory */ EventChannelFactory factory = null; try { /* resolve the Notification Service */ System.out.println ("Resolving initial references to "+ resolveName); obj = orb.resolve_initial_references (resolveName); System.out.println ("Success"); } catch (org.omg.CORBA.ORBPackage.InvalidName ex) { /* Thrown if there is no Notification Service registered with the * ORB or if the call to resolve_initial_references returns null. */ System.err.println ("Failed to resolve Notification Service"); System.exit (1); } /* obtain an object reference to the EventChannelFactory */ factory = EventChannelFactoryHelper.narrow (obj); try { /* try and obtain the event channel with ID 0 from the factory*/ System.out.println ("Trying to get channel 0 ..."); channel = factory.get_event_channel (0); System.out.println ("Success"); } catch (ChannelNotFound ex) { /* * thrown if the channel with ID 0 does not exist. A new channel * is created which has no Admin or QoS properties set. */ System.out.println ("Channel not found. Creating new channel ..."); /* Admin property, id holder and QoS property variables.*/ Property[] qosProp = new Property[0]; Property[] admProp = new Property[0]; org.omg.CORBA.IntHolder id = new org.omg.CORBA.IntHolder (); try { /* Create a new channel */ channel = factory.create_channel (qosProp, admProp, id); } catch (UnsupportedQoS exc) { /* Thrown if unknown QoS properties were specified */ System.err.println ("Unsupported QoS properties specified!"); } catch (UnsupportedAdmin exc) { /* Thrown if we specified unknown admin properties */ System.err.println ("Unsupported admin properties specified"); } } } /** * The <code>connect</code> method is used to obtain a proxy push supplier * from the admin object and connect the push consumer to it. * @return a <code>StructuredProxyPushSupplier</code> instance */ public void connect () { /* Defines the type of proxy required */ ClientType ctype = ClientType.STRUCTURED_EVENT; /* Holder to hold the proxy id */ org.omg.CORBA.IntHolder pid = new org.omg.CORBA.IntHolder (); /* Proxy supplier variable */ ProxySupplier proxySupplier = null; /* Obtain the consumer admin object reference*/ ConsumerAdmin admin = channel.default_consumer_admin (); try { /* obtain a structured push supplier object reference.*/ proxySupplier = admin.obtain_notification_push_supplier (ctype, pid); } catch (AdminLimitExceeded ex) { /* Thrown if the admin object is unable to have any more proxy * suppliers associated with it. */ System.err.println ("Maximum number of proxies exceeded!"); System.exit (1); } /* Narrow the proxy supplier to a Structured Proxy Push Supplier */ proxy = StructuredProxyPushSupplierHelper.narrow (proxySupplier); try { /* connect the consumer to the proxy */ proxy.connect_structured_push_consumer (getObject()); } catch (org.omg.CosEventChannelAdmin.TypeError ex) { /* * This exception is thrown if you attempt to connect * a sequenced consumer to a structured proxy or vice versa. */ System.err.println ("Type error!"); System.exit (1); } catch (org.omg.CosEventChannelAdmin.AlreadyConnected ex) { /* * This exception is thrown if a consumer is already connected * to this proxy. This should not be thrown because the proxy * has just been created. */ System.err.println ("Already connected!"); System.exit (1); } } /** * Disconnects this client from the proxy structured push consumer */ public void disconnect () { if (proxy != null) { proxy.disconnect_structured_push_supplier (); } } /** * The <code>disconnect_structured_push_consumer</code> method is * defined in the StructuredPushConsumerOperations interface. It allows * the event channel to disconnect the push consumer from the channel. */ public void disconnect_structured_push_consumer () { System.out.println ("Disconnected!"); } /** * The <code>offer_change</code> method is defined in the * StructuredPushConsumerOperations interface. It is called by the * channel to inform the consumer of new event types available or those no * longer offered * @param added[] the new <code>EventType</code> offered * @param removed[] the <code>EventType</code> that was removed */ public void offer_change (EventType added[], EventType removed[]) { } /** * This method is defined in the StructuredPushConsumerOperations * interface. It is a callback method that is called by the * NotificationService when an event is received by the channel. * The method attempts to extract a long from the event received * and prints this out to the screen. * @param event the <code>StructuredEvent</code> to be sent */ public void push_structured_event (StructuredEvent event) { org.omg.CORBA.Any data = event.remainder_of_body; int value = data.extract_long (); System.out.println ("Received event: " + value); } static private void runServer () { try { rootPoa.the_POAManager ().activate (); } catch (org.omg.PortableServer.POAManagerPackage.AdapterInactive ex) { System.err.println ("Can't activate POAManager"); System.exit (1); } Thread thread = new Thread () { public void run () { orb.run (); } }; thread.setDaemon (true); thread.setName ("OpenFusion ORB thread"); thread.start (); } private StructuredPushConsumer getObject() { StructuredPushConsumer serverObj = null; org.omg.PortableServer.Servant servant = new StructuredPushConsumerPOATie (this, rootPoa); try { org.omg.CORBA.Object ref = rootPoa.servant_to_reference (servant); serverObj = StructuredPushConsumerHelper.narrow (ref); } catch (ServantNotActive e) { System.err.println ("Unexpected Exception: " + e); System.exit (1); } catch (WrongPolicy e) { System.err.println ("Unexpected Exception: " + e); System.exit (1); } return serverObj; } /** * Displays an (optional) message and waits for the user to hit return. * @param msg The message (if any) to display. */ protected void waitForReturn (String msg) { if (msg != null) { System.out.println (msg); } BufferedReader m_br = new BufferedReader (new InputStreamReader (System.in)); try { m_br.readLine (); } catch (IOException ioe) { } } }
bin
directory of the JDK and the bin
directory of the OpenFusion distribution. From the command line:
server -start NotificationService
From the Administration Manager:
Start the Admin Manager, right click on the NotificationService node and select Start from the pop-up menu.
run com.prismt.cos.CosNotification.examples.Push.MyPushConsumer
run com.prismt.cos.CosNotification.examples.Push.MyPushSupplier
Entering Ctrl-C in each window will stop the processes.
说明:可以启动多个consumer,体会event的broadcast机制。同一个消息,由supplier push到channel,所有的consumer都可以收到。