Contact us | Links

Accessing an EJB session bean with .NET remoting

In 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

For this tutorial you need

  • .NET
  • IIOP.NET
  • BEA Weblogic 6.1 with jdk 1.3 (jdk 1.4 is not supported by WebLogic 6.1)

This tutorial consists of the following three steps:

  1. Implementing the bean
  2. Implementing the client
  3. Running the Demo

Implementing the EJB

In this section the EJB adder is implemented. The following source files are created:

  • Adder.java: contains the adder interface
  • AdderHome.java: contains the home interface
  • AdderBean.java: contains the bean implementation
The adder interface specifies the public face of the bean:
    //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

First you need to execute the setEnv.cmd script, to prepare your environment for compiling ejbs for weblogic.

A Makefile is provided for compiling the files and to create the deployable jar.

Important is to use the iiop option of your ejb stub compiler to generate stubs suitable for interoperation with IIOP.NET. Furthermore you need to generate the IDL files for your EJB's using the idl option of your ejb stub compiler.
Use the following commands to create the iiop stubs and the idl:

    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 adder_ejb_app.jar in the java\bin directory. You need to deploy this jar with your weblogic server.

Implementing the .NET Remoting Client

As a first step, you need to generate the CLS types for the IDL. For this task the IDLtoCLSCompiler is used.
After copying the orb.idl file from the directory IIOP.NET\IDLToCLSCompiler\IDL\ to your ejb idl directoy, you can use the following command in the IDL directory to generate the CLS:

    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.

Remark: Important is here, that the idl of the public bean interface and the home interface are specified together in the compiler command line to generate only one CLS assembly for the bean.

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 Examples\EJBExample\net\ExceptionImpl.cs.

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 IiopClientChannel instance is registered with the ChannelServices to enable communication using the IIOP.NET channel. After this a reference to the CORBA name service provided by the ejb container is obtained, using the RmiIiopInit class.
From this nameservice the client obtains a reference to the ejb home interface. Now using this home interface an adder ejb can be created. Finally we can call the add method to add the two summands.

The client implementation can be found in the IIOP.Net source distribution in the file Examples\EJBExample\net\Client.cs.

Compiling

The client is compiled with the following steps:

  • copy the IIOPChannel.dll into the bin directory
  • run the following command in the .NET sources directory
    csc /r:bin\adderClient.dll /r:bin\IIOPChannel.dll /out:bin\Client.exe Client.cs ExceptionImpl.cs

Running the Demo

Before starting the client, make sure that the ejb is deployed and that the weblogic server is started.

The demo is started with the following command line:

Client.exe localhost 7001

The two arguments specify the host and port of the corba name service of the EJB container.

About this project

This 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 Cases

Read the IIOP.NET success stories.

News

 
© 2003-2004 ELCA All Rights Reserved