The objects that the server hosted so far are called transient objects, as they cannot live beyond the server process. If an object’s server process halts, the transient object disappears with it and any object references held by the client becomes invalid. In other words, a transient object has the same lifetime as the execution of the server process that creates it.
However, CORBA specified another type of objects, called persistent objects that can live beyond the process that created it. It allows a server process to create an object, register it with the persistent service and exits. The persistence service deals with the object thereafter. If a client has reference to a persistent object and sends an invocation request to the target object, the persistence service creates and activates (if not already done) a server process for the object and then activates (if not already activated) the object itself. The object persists until it is explicitly removed from the persistence service. The entire activation process is transparent to the client.
Note that transient or persistent has nothing to do with the state of the object. An application may create transient objects to access some persistent information maintained in a database. Similarly, some persistent object references may have no state or no persistent state. The persistence merely means duration of existence.
Also note that transiency or persistency is implemented by a specific policy, called LifeSpanPolicy, of the underlying POA. This means that any application that wants to provide persistent objects must use POAs with the persistent life span policy. The following section describes how to work with persistent objects.
We first create and initialize an ORB instance:
ORB orb = ORB.init(args, null);
Then get a reference to the root POA:
POA rootpoa = POAHelper.narrow(orb.resolve_initial_references(”RootPOA”));
The standard life span policy for the RootPOA is transient. So, we create a child POA of rootPOA with life span policy set to persistent as follows:
Policy[] pPol = new Policy[1];
pPol[0] = rootpoa.create_lifespan_policy(LifespanPolicyValue.PERSISTENT);
POA pPOA = rootpoa.create_POA(”childPOA”, null, pPol );
Activate persistent POA’s POAManager, since by default POAManager remains in the ‘HOLD’ state.
pPOA.the_POAManager().activate();
Now create a servant and activate the servant with persistent POA:
SimpleCalculator cal = new SimpleCalculator(); pPOA.activate_object(cal );
Now get a reference and bind it with the naming service as described before. The complete source code of this server (PersistentCalculatorServer.java) is shown below:
// PersistentCalculatorServer.java import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
public class PersistentCalculatorServer {
public static void main(String args[]) {
try{
ORB orb = ORB.init(args, null);
POA rootpoa =
POAHelper.narrow(orb.resolve_initial_references(”RootPOA”));
// Create the persistent Policy Policy[] pPol = new Policy[1];
pPol[0] = rootpoa.create_lifespan_policy(LifespanPolicyValue.PERSISTENT);
// Create a POA specifying this persistent Policy
POA pPOA = rootpoa.create_POA(”childPOA”, null, pPol );
// Activate the PersistentPOA’s POAManager pPOA.the_POAManager().activate();
SimpleCalculator cal = new SimpleCalculator();
// Activate the servant with persistent POA pPOA.activate_object(cal );
org.omg.CORBA.Object ref = pPOA.servant_to_reference(cal);
org.omg.CORBA.Object objRef = orb.resolve_initial_references(”NameService”);
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
NameComponent path[] = ncRef.to_name( “Calculator” );
ncRef.rebind(path, ref);
orE.run();
} catch (Exception e) { e.printStackTrace();
}
}
}
1. Running Persistent Application
Compile the program as before. Go in the directory containing the class PersistentCaicuiatorServer. class and use the following to start the persistence service, the orbd daemon on port 6789:
orbd -ORBInitialPort 6789
We assume that the daemon is started in a host having IP address 172.16.4.248. Since, a persistent server usually creates a persistent object, registers it and then terminates, the server has to be started in a different way. Fortunately, Java provides a tool servertool that allows developer to register, unregister, startup, and shutdown a persistent server. It may be started on any host specifying the port and the host (if different) on which orbd is executing as follows:
servertool -ORBInitialHost 172.16.4.248 -ORBInitialPort 6789
A message and a command prompt appear as follows:
Welcome to the Java IDL Server Tool
please enter commands at the prompt
servertool >
Use the following command to register an instance of persistence server:
servertool > register -server PersistentCaicuiatorServer -applicationName ps – classpath .
If everything goes fine, the following message appears:
server registered (serverid = 260).
To see the list of registered server, use list command, a sample result of which is shown below:
For more commands, type help. Now, start the client in any other computer using the following command:
java CalculatorClient -ORBInitialHost 172.16.4.248 -ORBInitialPort 6789
Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.