IBM User Guide for Java V7 on Windows > IBM SDK for Java > The ORB > Examples of client–server applications



Server code

The server application has to create an instance of the remote object and publish it in a naming service. The Java™ Naming and Directory Interface (JNDI) defines a set of standard interfaces. The interfaces are used to query a naming service, or to bind an object to that service.

The implementation of the naming service can be a CosNaming service in the Common Object Request Broker Architecture (CORBA) environment. A CosNaming service is a collection of naming services, and implemented as a set of interfaces defined by CORBA. Alternatively, the naming service can be implemented using a Remote Method Invocation (RMI) registry for an RMI(JRMP) application. You can use JNDI in CORBA and in RMI cases. The effect is to make the server implementation independent of the naming service that is used. For example, you could use the following code to obtain a naming service and bind an object reference in it:

Context ctx = new InitialContext(...); 	// get hold of the initial context 
ctx.bind("sample", sampleReference);		// bind the reference to the name "sample"	 	
Object obj = ctx.lookup("sample");		// obtain the reference

To tell the application which naming implementation is in use, you must set one of the following Java properties:

java.naming.factory.initial

Defined also as javax.naming.Context.INITIAL_CONTEXT_FACTORY, this property specifies the class name of the initial context factory for the naming service provider. For RMI registry, the class name is com.sun.jndi.rmi.registry.RegistryContextFactory. For the CosNaming Service, the class name is com.sun.jndi.cosnaming.CNCtxFactory.

java.naming.provider.url

This property configures the root naming context, the Object Request Broker (ORB), or both. It is used when the naming service is stored in a different host, and it can take several URI schemes:
  • rmi
  • corbaname
  • corbaloc
  • IOR
  • iiop
  • iiopname

For example:

rmi://[<host>[:<port>]][/<initial_context>] for RMI registry    
iiop://[<host>[:<port>]][/<cosnaming_name>] for COSNaming

To get the previous properties in the environment, you could code:

Hashtable env = new Hashtable(); 	
Env.put(Context.INITIAL_CONTEXT_FACTORY, 		
       "com.sun.jndi.cosnaming.CNCtxFactory");
and pass the hash table as an argument to the constructor of InitialContext.

For example, with RMI(JRMP), you create an instance of the servant then follow the previous steps to bind this reference in the naming service.

With CORBA (Java IDL), however, you must do some extra work because you have to create an ORB. The ORB has to make the servant reference available for remote calls. This mechanism is typically controlled by the object adapter of the ORB.

public class Server {
	public static void main (String args []) {
		try {
			ORB orb = ORB.init(args, null);
			
			// Get reference to the root poa & activate the POAManager 
			POA poa = (POA)orb.resolve_initial_references("RootPOA"); 
			poa.the_POAManager().activate(); 
			
			// Create a servant and register with the ORB
			SampleImpl sample = new SampleImpl();			
			sample.setORB(orb); 

			// TIE model ONLY
			// create a tie, with servant being the delegate and
			// obtain the reference ref for the tie
			SamplePOATie tie = new SamplePOATie(sample, poa); 
			Sample ref = tie._this(orb);

			// Inheritance model ONLY
			// get object reference from the servant 
			org.omg.CORBA.Object ref = poa.servant_to_reference(sample);
      Sample ref = SampleHelper.narrow(ref);

			// bind the object reference ref to the naming service using JNDI
	   		..........(see previous code) .....
			orb.run();
		}
		catch(Exception e) {}
	}}

For RMI-IIOP:

public class Server {
	public static void main (String args []) {
		try {
			ORB orb = ORB.init(args, null);
			
			// Get reference to the root poa & activate the POAManager 
			POA poa = (POA)orb.resolve_initial_references("RootPOA"); 
			poa.the_POAManager().activate();

			// Create servant and its tie
			SampleImpl sample = new SampleImpl();
			_SampleImpl_Tie tie = (_SampleImpl_Tie)Util.getTie(sample); 
			
			// get an usable object reference 
			org.omg.CORBA.Object ref = poa.servant_to_reference((Servant)tie);

			// bind the object reference ref to the naming service using JNDI
	   		..........(see previous code) .....
		}
		catch(Exception e) {}
	}}

To use the previous Portable Object Adapter (POA) server code, you must use the -iiop -poa options together to enable rmic to generate the tie. If you do not use the POA, the RMI(IIOP) server code can be reduced to instantiating the servant (SampleImpl sample = new SampleImpl()). You then bind the servant to a naming service as is typically done in the RMI(JRMP) environment. In this case, you need use only the -iiop option to enable rmic to generate the RMI-IIOP tie. If you omit -iiop, the RMI(JRMP) skeleton is generated.

When you export an RMI-IIOP object on your server, you do not necessarily have to choose between JRMP and IIOP. If you need a single server object to support JRMP and IIOP clients, you can export your RMI-IIOP object to JRMP and to IIOP simultaneously. In RMI-IIOP terminology, this action is called dual export.

RMI Client example:

public class SampleClient { 	 
  public static void main(String [] args) {  	    
    try{ 			 		
      Sample sampleref
      //Look-up the naming service using JNDI and get the reference 			
            ......... 		
      // Invoke method   		
      System.out.println(sampleRef.message()); 	    
    } 	    
    catch(Exception e) {} 	  
  } 	}  

CORBA Client example:

public class SampleClient {
	public static void main (String [] args) {
		try {
			ORB orb = ORB.init(args, null);
			// Look up the naming service using JNDI
			......
			// Narrowing the reference to the right class 			Sample sampleRef = SampleHelper.narrow(o);
			// Method Invocation
			System.out.println(sampleRef.message());
		}
		catch(Exception e) {}
	}}

RMI-IIOP Client example:

public class SampleClient {
	public static void main (String [] args) {
		try{
			ORB orb = ORB.init(args, null);
			// Retrieving reference from naming service
			........
			// Narrowing the reference to the correct class 			Sample sampleRef = (Sample)PortableRemoteObject.narrow(o, Sample.class);
			// Method Invocation
			System.out.println(sampleRef.message());
		}
		catch(Exception e) {}
	}}


Parent: Examples of client–server applications








Error 404 - Not Found

Error 404 - Not Found

The document you are looking for may have been removed or re-named. Please contact the web site owner for further assistance.