Getting started - A simple "Hello" example (Sun Application Server)

Description:

Build a small 'hello world' console 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:

\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

Please make sure you modify the path to J2EE.jar.

> set J2EE_PATH=%CLASSPATH%;C:\Sun\AppServer\lib\j2ee.jar
> BrewerCmd -cp .\ejb\assemble\jar\helloEjb.jar;%J2EE_PATH% -v -d .\IDL -g idl Hello.Greetings_Home

Step 3. Generate the .NET code from the IDL files

> BrewerCmd -g cs -d .\DotnetClient\Generated .IDL\Hello.idl

Alternative Step 2 & 3. Generate the .NET code from the Java jar

Please make sure you modify the path to J2EE.jar.

    
> set J2EE_PATH=%CLASSPATH%;C:\Sun\AppServer\lib\j2ee.jar
> BrewerCmd -cp .\ejb\assemble\jar\helloEjb.jar;%J2EE_PATH% -v -d .\DotnetClient\Generated -g cs Hello.Greetings_Home

Step 4. Write the .NET client

Implementation: main.cs
Generated : Hello.cs

a.) Create a console application

b.) Add a reference to '$ESPRESSO_INSTALL_DIR$/bin/Jintegra.Espresso.dll'

c.) Add the generated file: ./IDL/Hello.cs

d.) Write the client:

 
using System;
using Ics.J2EE;
using Ics.Runtime;

namespace DotNetClient
{
  class HelloClient
  {
    static void Main(string[] args)
    {
      Console.WriteLine(  "Hello-Client\n"
        +"\t(c) 2007 Intrinsyc Software International, Inc.  All rights reserved.\n"        
        +"\tCORBA Demo");

      HelloClient oHelloClient = new HelloClient();

      oHelloClient.run( args);

      Console.WriteLine( "bye");
    }

    private void run(string[] args)
    {
      Hello.Greetings_Remote oGreetings;

      J2eeQuickConfig qc = new J2eeQuickConfig(ApplicationServer.SUN_JDK_RMI_IIOP);
      // For Sun application Server
      qc.SetNamingServicePort(3700);

      // Set the host
      //qc.SetApplicationServerHost("some_machine");     

      // Default Java version 1.4.2. Options are JDK_131, JDK_141, JDK_142 and JDK_150
      qc.SetTargetJavaVersion(JavaVersion.JDK_150);

      // Turn on log with name Log.txt and without appending exist log file
      // qc.SetApplicationLog(OrbErrorLevel.ERRORS, "Log.txt", false);
           
      J2eeEasyContext.Init(qc);

      Hello.Greetings_Home oGreetingsHome = (Hello.Greetings_Home) J2eeEasyContext.Lookup("ejb/Hello", "Hello.Greetings_Home");   
      oGreetings = oGreetingsHome.create();

      string strRet = oGreetings.hello("Ics Client");

      System.Console.WriteLine("{0}", strRet);
      System.Console.WriteLine("Press Enter to exit.");
      System.Console.ReadLine();
      J2eeEasyContext.ShutdownRuntime();
    }
  }
}

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.) Start the client.