Main page / J2EE Connectivity page

Getting started - A simple "Hello" example

(Sun Application Server)


Description: 
	Build a small 'hello world' mobile application with C# as client and a J2EE Application server. 
	The client calls the function 'hello' on the server with a name as parameter and displays the result.

Goal: 
	Write an EJB for your J2EE server, create IDL files, generate .Net C# code and write the .Net client.  

Source:
	MinCor\DemoJ2EE\Hello



Example:


Step 1. Write an J2EE Server 

        Write an EJB with the following interfaces and implementation.
        Create the jar/ear files and deploy it to your J2EE Server.

    a.) Remote interface (Hello\Greetings_Remote.java):
    	  
    	package Hello;
    	import javax.ejb.EJBObject;

        public interface Greetings_Remote extends EJBObject { 
    	    public String hello ( String name) throws java.rmi.RemoteException;
    	} 
    	


    b.) The Bean (Hello\Greetings_Bean.java):
	      
        package Hello;
        import java.rmi.RemoteException;
        import javax.ejb.SessionBean;
        import javax.ejb.SessionContext;
        import javax.ejb.CreateException;

        public class Greetings_Bean implements SessionBean { 
	    
            public Greetings_Bean () {
                super ();
            } 

            private SessionContext        m_ctx = null; 
	        
            public void setSessionContext (SessionContext ctx) {m_ctx = ctx;} 
            public void ejbCreate    () throws RemoteException, CreateException {System.out.println ("ejbCreate   () on " + this);} 
            public void ejbRemove    ()                                         {System.out.println ("ejbRemove   () on " + this);} 
            public void ejbActivate  ()                                         {System.out.println ("ejbActivate () on " + this);} 
            public void ejbPassivate ()                                         {System.out.println ("ejbPassivate() on " + this);} 

            public String hello ( String name) throws java.rmi.RemoteException {
                return "Greeting from J2EE Server to " + name;
            };
        }
        

    
    c.) Home interface (Hello\Greetings_Home.java):
	      
        package Hello;
        import java.rmi.RemoteException;
        import javax.ejb.CreateException;
        import javax.ejb.EJBHome;

        public interface Greetings_Home extends EJBHome { 

            public Greetings_Remote create ()
                throws  RemoteException, CreateException; 
        }
        

Step 2. Generate the IDL files  

        IDL-File: Hello.idl
        
        > set classpath=%classpath%;C:\Sun\AppServer\lib\j2ee.jar
        > MCjava2idl -cp %Classpath%;.\ejb\assemble\jar\helloEjb.jar -v -d .\IDL Hello.Greetings_Home
        
    

Step 3. Generate the .Net code from the IDL files
            
        > MCidl2cs -d .\IDL .\IDL\Hello.idl
        

Step 4. Write the .Net client

        Implementation: ClientImpl.cs
        Generated     : Hello.cs

    a.) Create a mobile application
    
    b.) Add a reference to './MinCor/bin/MinCorCF.DLL' and
                        to './MinCor/bin/JavaBuildInTypesCF.DLL'.
    
    c.) Add the generated file: ./Generated/Hello.cs
    
    d.) Write the client:
         
        using System;
        using System.Drawing;
        using System.Collections;
        using System.Windows.Forms;
        using System.Data;
        
        namespace DotNetClient
        {
            public class ClientImpl
            {
                private static string[]     m_strORBInit;
                
                public static void runDemo( FrmClt a_oFrmClt, string a_strIpAddr)
                {
                    Middsol.CORBA.ORB oOrb = null;
                    
                    m_strORBInit = new string[] {
                                     "-ORBInitRef NameService=corbaloc:iiop:1.2@" + a_strIpAddr + ":3700/NameService", 
                                     "-ORBDebug"};
                    
                    try
                    {
                        Hello.Greetings_Remote oGreetings;
                        
                        // initialize the ORB:
                        oOrb = Middsol.CORBA._ORB.init( m_strORBInit, null);
                        
                        // retrieve the Naming Servoce from the ORB:
                        Middsol.CosNaming.NamingContextExt oNC = Middsol.CosNaming.NamingContextExtHelper.narrow( oOrb.resolve_initial_references("NameService"));
                        
                        try
                        {
                            // retrieve the server objects home interface from the Naming Service:
                            Hello.Greetings_Home oGreetingsHome = Hello.Greetings_HomeHelper.narrow( oNC.resolve_str("ejb/Hello"));
                            
                            // create a new server object using its home interface:
                            oGreetings = oGreetingsHome.create();
                        }
                        catch(Middsol.CosNaming.NamingContextPackage.NotFound ex)
                        {
                            a_oFrmClt.writeLog("NS Manager Error: " + ex.why);
                            throw new System.Exception();
                        }
                        
                        // call the server object:
                        string strRet = oGreetings.hello("Middsol Client");
                        a_oFrmClt.writeLog(strRet);
                    }
                    catch( Middsol.CORBA.SystemException exSys)
                    {
                        a_oFrmClt.writeLog( "Catch Exception from Server (Middsol.CORBA.SystemException).");
                        a_oFrmClt.writeLog( "ID:" + exSys.ID);
                    }
                    finally
                    {
                        if( oOrb != null)
                            oOrb.destroy();
                    }
                }
            }
        }
         
        The client first connects to the naming service of the J2EE server, retrieves the remote object registered 
        under the name 'ejb/Hello' (which is a home interface), generates a new server object from it, and eventually
        excecutes the method 'hello'.
  
Step 5. Run the example

    a.) Start the Sun ONE AppServer:
        
        > asadmin start-domain domain1
        
    
    b.) Deploy your bean
        (copy Hello.ear into the AppServers autodeploy directory):
        
        > copy ..\assemble\ear\Hello.ear c:\Sun\AppServer\domains\domain1\autodeploy
        
        
    c.) Copy the .Net mobile client to the target plattform and start the client.

    d.) Provide the correct server address to the client.