Getting a Pub-Sub Server Instance and Creating a Local Client Publishing Messages to a Channel

Using the HTTP Publish-Subscribe Server 12-9 on the distribution kit: WL_ HOME samplesserverexamplessrcexampleswebapppubsubstocksr cstockWar, where WL_HOME refers to the main WebLogic Server installation directory, such as oraclehomewlserver_10.3.

12.3.2.2 Getting a Pub-Sub Server Instance and Creating a Local Client

Before you can perform any server-side tasks on the pub-sub server and its channels, you must first instantiate a PubSubServer object which represents the pub-sub server and then create a local client which you use to manipulate the channels on behalf of the pub-sub server. The following code snippet shows an example: import com.bea.httppubsub.FactoryFinder; import com.bea.httppubsub.LocalClient; import com.bea.httppubsub.PubSubSecurityException; import com.bea.httppubsub.PubSubServer; import com.bea.httppubsub.PubSubServerException; import com.bea.httppubsub.PubSubServerFactory; import org.json.JSONObject; public class ApiBasedClient implements Client { private PubSubServer pubSubServer; private LocalClient localClient; public ApiBasedClientString serverName throws PubSubServerException { PubSubServerFactory pubSubServerFactory = PubSubServerFactoryFactoryFinder.getFactoryFactoryFinder.PUBSUBSERVER_ FACTORY; pubSubServer = pubSubServerFactory.lookupPubSubServerserverName; localClient = pubSubServer.getClientManager.createLocalClient; } ... } The FactoryFinder class searches for an implementation of the PubSubServerFactory which in turn is used to create PubSubServer instances. The lookupPubSubServer method of PubSubServerFactory returns a PubSubServer instance based the context path of the servlet from which the method is run. Finally, the createLocalClient method of the ClientManager of the PubSubServer instance returns a LocalClient object; this is the object that the pub-sub server uses to subscribe and publish to a channel.

12.3.2.3 Publishing Messages to a Channel

To publish a message to a channel, use the PubSubServer.publishToChannel method, passing it the LocalClient object, the name of the channel, and the text of the message, as shown in the following code snippet: public void publishString channel, JSONObject data throws IOException { try { pubSubServer.publishToChannellocalClient, channel, data.toString; } catch PubSubSecurityException e { throw new IOExceptione; } } In the example, the channel variable would contain the name of a channel, such as mychannel. 12-10 Developing Web Applications, Servlets, and JSPs for Oracle WebLogic Server The publishToChannel method is asynchronous and returns immediately, or in other words, the method does not wait for the subscribed clients to receive the message.

12.3.2.4 Subscribing to a Channel