Código de servidor
La aplicación de servidor tiene que crear una instancia del objeto remoto y publicarla en un servicio de nombres. JNDI (Java™ Naming and Directory Interface) define un conjunto de interfaces estándar. Las interfaces se utilizan para consultar un servicio de nombres o para enlazar un objeto a ese servicio.
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- java.naming.factory.initial
- Definida también como javax.naming.Context.INITIAL_CONTEXT_FACTORY, esta propiedad especifica el nombre de clase de la fábrica de contexto inicial para el proveedor de servicios de denominación. Para el registro RMI, el nombre de clase es com.sun.jndi.rmi.registry.RegistryContextFactory. Para el servicio CosNaming , el nombre de clase es com.sun.jndi.cosnaming.CNCtxFactory.
- java.naming.provider.url
- Esta propiedad configura el contexto raíz de nombres, el intermediario de solicitudes de objetos (ORB) o ambos. Se utiliza cuando el servicio de nombres se almacena en un host diferente y puede tener varios esquemas de URI:
- rmi
- corbaname
- corbaloc
- IOR
- iiop
- iiopname
rmi://[<host>[:<port>]][/<initial_context>] for RMI registry iiop://[<host>[:<port>]][/<cosnaming_name>] for COSNaming
Hashtable env = new Hashtable();
Env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.cosnaming.CNCtxFactory");y pasar la tabla hash como argumento al constructor de InitialContext.Por ejemplo, con RMI(JRMP), cree una instancia del sirviente y siga los pasos anteriores para vincular esta referencia en el servicio de nombres.
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) {}
}
}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) {}
}
}Para utilizar el código de servidor POA (Portable Object Adapter) anterior, debe utilizar las opciones -iiop -poa juntas para habilitar rmic para generar el empate. Si no utiliza el POA, el código del servidor de RMI(IIOP) se puede reducir para crear una instancia del sirviente (SampleImpl sample = new SampleImpl()). A continuación, enlace el sirviente a un servicio de denominación como normalmente se realiza en el entorno RMI (JRMP). En este caso, sólo tiene que utilizar la opción -iiop para habilitar rmic con el fin de generar el vínculo RMI-IIOP. Si omite -iiop, se genera el esqueleto de RMI(JRMP).
Cuando exporta un objeto RMI-IIOP, no tiene necesariamente que elegir entre JRMP e IIOP. Si necesita un único objeto de servidor para dar soporte a clientes JRMP e IIOP, puede exportar el objeto RMI-IIOP a JRMP y a IIOP simultáneamente. En la terminología RMI-IIOP, esta acción se denomina exportación dual.
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) {}
}
} 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 correct class
Sample sampleRef = SampleHelper.narrow(o);
// Method Invocation
System.out.println(sampleRef.message());
}
catch(Exception e) {}
}
}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) {}
}
}