(See horb/examples/daemon for example code.)
You might just want to run a daemon object. A daemon object is created once and serves many client objects. While the server objects described previously are created for each request of remote object creation, the daemon object is created once and then stays alive in the background.
You can use those three methods to start a daemon object.
The horb command can take an option -start. The -start option takes two arguments, a classname and an object name.
horb -start horb.examples.daemon.Server daemonServer
You can start only one object from a command line.
You can start arbitrary number of objects from a configuration file. The below is an example of a configuration file.
object[0].className=horb.examples.daemon.Server object[0].objectID=daemonServer object[0].port=0
The -conf option takes the filename of a configuration file.
horb -conf daemon.conf
This method is the recommened one.
You can write a program that starts some objects as daemon objects. The rest of this section describes this method. To invoke a daemon object, follow these steps:
If HORBServer is instantiated in your program, your program becomes a HORB server that can accept connect requests from outside.
HORBServer hs = new HORBServer(port) Server server = new Server(); HORBServer.registerObject("horb.examples.daemon.Server", server, "daemonServer");
The class HORBServer has a static method named registerObject(). The first argument of the method is a fully qualified class name (packagename.classname) to be invoked as a daemon object. The second argument is an object that becomes a daemon object. The third argument is the name of the daemon object. If the object name is "daemonServer" and the machine name is "serverA", the object can be accessed by a HorbURL as "horb://serverA/daemonServer". The registerObject() will return this URL.
Now, here is the complete source of a daemon object. This class has a method named HowManyClients() that returns a sucessive number for each call. Note that if you use the above method 1 or method 2, Server need not to have main().
// Server.java package horb.examples.daemon; import horb.orb.*; import java.io.*; public class Server { int numClients = 0; public static void main(String argv[]) { HORBServer hs = new HORBServer(8887); // register an object as a HORB object Server server = new Server(); HORBServer.registerObject("horb.examples.daemon.Server", server, "daemonServer"); } synchronized int HowManyClients() { return ++numClients; } }
Since a new thread of the server object is created for each client, access to shared data must be synchronized among the threads. In this case, HowManyClients() is a synchronized method so that numClients is updated in a transaction.
Class Client connects to the object and calls the method. Any proxy class has a hidden method named _getObjectURL() that returns the URL of the remote object.
// Client.java package horb.examples.daemon; import horb.orb.*; import java.io.*; class Client { public static void main(String argv[]) throws HORBException, IOException { String host = (argv.length == 1) ? argv[0] : "-"; // "horb://hostname/myServer" HorbURL url = new HorbURL(host, "daemonServer"); Server_Proxy s = new Server_Proxy(url); int result = s.HowManyClients(); System.out.println(result+" clients have visited to "+s._getObjectURL()); } }
Here is an example of compilation and execution:
c:> horbc Server.java c:> horbc -c Client.java S:> java horb.examples.daemon.Server (on serverA) c:> java horb.examples.daemon.Client serverA 1 clients have visited to horb://serverA:8887/daemonServer c:> java horb.examples.daemon.Client serverA 2 clients have visited to horb://serverA:8887/daemonServer c:> java horb.examples.daemon.Client serverA 3 clients have visited to horb://serverA:8887/daemonServer