Application Techniques



Calling EJB Session Beans from External Java Clients

How to code an external Java client (outside of the SilverStream development environment) that accesses the SilverStream Server to call methods of an EJB session bean and return the results.

About this technique

Details

Category

Java Client Techniques> External Java clients

Description

You'll learn about:

Source code

You can get the source code for this technique from:

Related reading

See the chapter on writing external Java clients in the Programmer's Guide

Setting up   Top of page

For this technique, you need:

Coding the client   Top of page

Here's a class that provides a basic implementation of this technique.

  package extjtech; 
   
  import java.io.*; 
  import java.math.*; 
  import javax.naming.*; 
  import javax.rmi.*; 
   
  import com.sssw.rt.util.*; 
   
  import com.snuckerby.sesbean.*; 
   
  // The ExtSesBean class shows how you can access a SilverStream 
  // server from an external Java client. It includes an example 
  // of calling an EJB session bean to execute business logic on 
  // the server and return the result to the client.  
  public class ExtSesBean { 
   
       // Instance variable for the SilverStream server session  
       // object used by the ExtSesBean class. 
       AgrServerSession session; 
   
    // Constructor for the ExtSesBean class. It does the following: 
    // * Connects to a SilverStream server 
    // * Finds a specific EJB session bean on that server 
    // * Calls a method of that session bean 
    // * Displays the result returned by that method (if any)  
    // 
    // It takes 1 argument: 
    // * SilverStream server host 
    //     myserver 
    public ExtSesBean(String s3serverhost) { 
   
      try  { 
        // Initialize the SilverStream runtime environment. Do 
        // this if you need to specify a login handler for the  
        // SilverStream server to call back when it requires user 
        // authentication. (The login handler is a class that you 
        // code and instantiate.) 
        AgRuntime.init(new LoginHandler()); 
   
        // Connect to the appropriate SilverStream server. This 
        // establishes a server session (represented by an instance 
        // of AgrServerSession). 
        // 
        // The following example connects via RMI-IIOP, but it 
        // could use HTTP instead (if you code the connect method 
        // rather than connectRMI).  
        session = AgRuntime.connectRMI(s3serverhost); 
   
   
        // Find the appropriate EJB session bean on that server. 
   
          // Get the host name and port for the server. 
          String servername = session.getServerHostName(); 
          int portnum = session.getServerPort(); 
          if (portnum != -1) { 
            servername += ":" + Integer.toString(portnum); 
          }  
   
          // Get the JNDI name under which the bean is registered. 
          String sbjndiname = "sbOrderSummary"; 
   
          // Do a JNDI lookup to return the bean's home interface 
          // as an Object. Note that a couple parts of the address 
          // (sssw://.../RMI/...) are the same for any EJB. 
          InitialContext context = new InitialContext(); 
          Object sbobj = context.lookup("sssw://" +  
                                        servername +  
                                        "/RMI/" +  
                                        sbjndiname); 
   
          // Narrow the Object returned by the lookup to make sure 
          // it can be cast to the appropriate type (the class that 
          // corresponds to your bean's home interface). Then, cast 
          // it. 
          sbobj = PortableRemoteObject.narrow(sbobj, 
                           sbOrderSummaryHome.class); 
          sbOrderSummaryHome sbhome = (sbOrderSummaryHome)sbobj; 
   
          // Call the home object's create() method to get an  
          // instance of the bean's remote interface.  
          sbOrderSummaryRemote sbremote = sbhome.create(); 
   
   
        // Once you have the remote object, you're ready to call 
        // business methods of the EJB session bean. (These are 
        // the methods exposed by the bean's remote interface.) 
        BigDecimal result = sbremote.getTotal(); 
   
        // Display the string representation of the result returned 
        // from the business method. 
        System.out.println("Result from getTotal() method of " + 
                           "sbOrderSummary EJB session bean is:"); 
        System.out.println(String.valueOf(result)); 
      } 
      catch (Exception e) { 
        System.out.println("Application error in ExtSesBean"); 
        e.printStackTrace(); 
        System.exit(0); 
      } 
      finally { 
        // When all done, close the SilverStream server session. 
        session.close(); 
      } 
    } 
   
   
    // Main method (used for application startup). 
    public static void main(String[] args) { 
   
      if (args.length < 1) { 
        // Make sure all of the required command-line args 
        // have been provided to the application. If not, 
        // display an error message and terminate. 
        System.out.println( 
          "Missing Command-Line Arguments\n\n" +   
          "Required arguments:\n" + 
            "*  SilverStream server host\n\n" + 
          "Example:\n" + "java extjtech.ExtSesBean " + 
            "myserver"); 
        System.exit(0); 
      } 
      else { 
        // Get the command-line args so the application can 
        // pass them to the ExtSesBean constructor. 
        String s3serverhost = args[0]; 
   
        // Create an instance of ExtSesBean. This executes the  
        // constructor for the class, which then accesses the 
        // SilverStream server and calls a particular EJB 
        // session bean. 
        ExtSesBean extsesbean = new ExtSesBean(s3serverhost); 
      } 
    } 
  } 

Running the client   Top of page

When this class runs, it calls the appropriate session bean method, then displays the result.






Copyright © 2000, SilverStream Software, Inc. All rights reserved.