Contact us | Links | |||||||||||||||||||||
|
Accessing a .NET MarshalByRefObject with RMI/IIOPIn this tutorial a RMI/IIOP client calls a method on a .NET MarshalByRefObject: an adder. This .NET adder provides a method to add two double summands. This tutorial consists of the following three steps:
Implementing the .NET Server
The AdderImpl class implements the adder functionality: using System; using System.Runtime.Remoting; using Ch.Elca.Iiop.Idl; namespace Tutorial.GettingStarted { public class AdderImpl : MarshalByRefObject { public override object InitializeLifetimeService() { return null; } public double add(double arg1, double arg2) { return arg1 + arg2; } } }
Every .NET remoting object implementation has to extend MarshalByRefObject or a subclass of it. Therefore
AdderImpl inherits from MarshalbyRefObject. The NAdderServer class creates an adder and publishes it with .NET Remoting. using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using Ch.Elca.Iiop; namespace Tutorial.GettingStarted { public class NAdderServer { [STAThread] public static void Main(string[] args) { // register the channel int port = 8087; IiopChannel chan = new IiopChannel(port); ChannelServices.RegisterChannel(chan); // publish the adder AdderImpl adder = new AdderImpl(); string objectURI = "adder"; RemotingServices.Marshal(adder, objectURI); Console.WriteLine("server running"); Console.ReadLine(); } } } In a first step the IiopChannel must be registered with .NET remoting to enable access to .NET Remoting objects using the IIOP.NET channel. Afterwards an instance of AdderImpl is created and published. The IiopChannel includes a CORBA naming service, which encapsulates the .NET remoting nameing service. Therefore registering the adder object with a separate CORBA name service is not needed. The files can be found here: Compiling
Implementing the RMI/IIOP Client
Creating IDL for CLS types module Tutorial { module GettingStarted { interface AdderImpl { double add(in double arg1, in double arg2) raises (::Ch::Elca::Iiop::GenericUserException); }; }; };
Launch the following command in the directory, containing the .NET AdderServer binary, to generate the idl
in the destination directory JAVA_SOURCES_DIRECTORY: Mapping IDL to Java
Client code import javax.naming.NamingException; import javax.naming.InitialContext; import javax.naming.Context; import javax.rmi.PortableRemoteObject; import java.io.BufferedReader; import java.io.InputStreamReader; import Tutorial.GettingStarted.AdderImpl; public class AddClient { public static void main(String[] args) { try { BufferedReader reader = new BufferedReader( new InputStreamReader(System.in)); System.out.println("input the two summands"); System.out.println("summand 1: "); double sum1 = Double.parseDouble(reader.readLine()); System.out.println("summand 2: "); double sum2 = Double.parseDouble(reader.readLine()); System.out.println("get inital naming context"); Context ic = new InitialContext(); System.out.println("ic received, retrieve add"); Object objRef = ic.lookup("adder"); AdderImpl adder = (AdderImpl) PortableRemoteObject.narrow(objRef, AdderImpl.class); System.out.println("call add method"); double result = adder.add(sum1, sum2); System.out.println("result: " + result); } catch (Exception e) { System.out.println("exception encountered: " + e); } } }
The client first obtains a reference to the CORBA name service. Afterwards it looks up the adder at the name service. Finally the client
uses the adder to add sum1 and sum2.
Compiling
Running the Demo
java -Djava.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory -Djava.naming.provider.url=iiop://localhost:8087 -cp . AddClient Remark: A CORBA naming service is automatically available at the same port the channel is listening on. The RMI/IIOP client contacts this namingservice to obtain object references from. |
About this projectThis project is maintained by Elca Informatique SA and was developed in collaboration with the Programming Languages and Runtime Systems Research Group of the ETH-Zurich as part of Dominic Ullmann's diploma thesis. IIOP.NET Use CasesRead the IIOP.NET success stories. News
|
|||||||||||||||||||
© 2003-2004 ELCA All Rights Reserved |