Contact us | Links | |||||||||||||||||||||
|
Accessing an EJB session bean with .NET remotingIn this tutorial a .NET client accesses an adder object hosted in a java EJB. This EJB adder provides a method to add two values.
Prerequisites
This tutorial consists of the following three steps:
Implementing the EJB
//Adder.java package ch.elca.iiop.demo.ejbAdder; import javax.ejb.*; import java.rmi.RemoteException; /** * The methods in this interface are the public face of the Adder. */ public interface Adder extends EJBObject { public double add(double sum1, double sum2) throws RemoteException; } The home interface defines only a simple parameterless create method: //AdderHome.java package ch.elca.iiop.demo.ejbAdder; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBHome; /** * This interface is the home interface of the AdderEJB. */ public interface AdderHome extends EJBHome { /** * This method corresponds to the ejbCreate method in the bean * "AdderBean.java". * @return Adder */ Adder create() throws CreateException, RemoteException; } The adder session bean implements the adder funcionality: // AdderBean.java package ch.elca.iiop.demo.ejbAdder; import javax.ejb.CreateException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; /** * AdderBean is a stateless Session Bean. It implements the adder functionality. */ public class AdderBean implements SessionBean { private SessionContext m_ctx; public void ejbActivate() { } public void ejbRemove() { } public void ejbPassivate() { } /** * Sets the session context. * * @param ctx SessionContext Context for session */ public void setSessionContext(SessionContext ctx) { m_ctx = ctx; } /** * This method corresponds to the create method in the home interface * "AdderHome.java". * */ public void ejbCreate () throws CreateException { // nothing special to do here } public double add(double sum1, double sum2) { return sum1 + sum2; } } These files can be found in the IIOP.Net source distribution in the directory Examples\EJBExample\java\ch\elca\iiop\demo\ejbAdder. Compiling / Deploying java weblogic.ejbc -iiop -compiler javac bin\adder_ejb.jar bin\adder_ejb_app.jar
java weblogic.ejbc -idl bin\adder_ejb.jar IDL
The makefile produces as a result the deployable jar
Implementing the .NET Remoting Client
IDLToCLSCompiler.exe -o ..\bin adderClient ch\elca\iiop\demo\ejbAdder\Adder.idl ch\elca\iiop\demo\ejbAdder\AdderHome.idl
This command creates a .NET multi module assembly adderClient.dll in the bin directory containing the CLS for all relevant types used in the adder ejb.
The java exception types are mapped to IDL using CORBA value types. For this value types you need to provide an implementation class.
Because these valuetypes contains no methods, the implementation is trivial. As an example such an implementation is shown for the javax.ejb.CreateException
valuetype:
using System; namespace javax.ejb { [Serializable] public class CreateExceptionImpl : CreateException { public CreateExceptionImpl() : base() { } } }
The implementation of all exception value types can be found in the IIOP.Net source distribution in the file Now we are ready to implement the client application. The following listing shows the code: using System; using System.Runtime.Remoting.Channels; using Ch.Elca.Iiop; using Ch.Elca.Iiop.Services; using omg.org.CosNaming; using ch.elca.iiop.demo.ejbAdder; namespace Ch.Elca.Iiop.Demo.EjbAdder { public class Client { [STAThread] public static void Main(string[] args) { try { string nameServiceHost = "localhost"; int nameServicePort = 1050; parseArgs(ref nameServiceHost, ref nameServicePort, args); Console.WriteLine("input the two summands"); Console.WriteLine("sum1:"); double sum1 = Double.Parse(Console.ReadLine()); Console.WriteLine("sum2:"); double sum2 = Double.Parse(Console.ReadLine()); // register the channel IiopClientChannel channel = new IiopClientChannel(); ChannelServices.RegisterChannel(channel); // access COS nameing service RmiIiopInit init = new RmiIiopInit(nameServiceHost, nameServicePort); NamingContext nameService = init.GetNameService(); NameComponent[] name = new NameComponent[] { new NameComponent("ch.elca.iiop.demo.ejbAdder.AdderHome", "") }; // get the reference to the adder-home AdderHome adderHome = (AdderHome)nameService.resolve(name); // create Adder Adder adder = adderHome.create(); // call add double result = adder.add(sum1, sum2); Console.WriteLine("result: " + result); // dispose the ejb adder.remove(); } catch (Exception e) { Console.WriteLine("exception: " + e); } } private static void parseArgs(ref string host, ref int port, string[] args) { if (args.Length> 0) { host = args[0]; } if (args.Length> 1) { port = Int32.Parse(args[1]); } } } }
An
The client implementation can be found in the IIOP.Net source distribution in the file Compiling
Running the Demo
|
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 |