Show TOC Start of Content Area

This graphic is explained in the accompanying text RMI-IIOP Client That Invokes an Enterprise JavaBean  Locate the document in its SAP Library structure

This is an example of an RMI-IIOP client that looks up the Calculator bean (distributed as an example with the default application on the J2EE Engine) and calls methods of it.

Implementation consists of two major tasks:

·        Creating an initial context in order to obtain a reference to the Calculator bean using it.

·        Looking up the home interface of the bean and creating an instance of it, which allows the client to call methods on it.

The following code performs these tasks:

Syntax

// Define the initial context properties

Properties p = new Properties();

// Specify the type of the initial context factory

p.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");

// Specify the provider url. Use prefix “iiop://” for IIOP connections

p.put(Context.PROVIDER_URL, "iiop://<Server_Host>:<iiop_port>");

// Specify the security principal (user)

p.put(Context.SECURITY_PRINCIPAL, "Administrator");

// Specify the security credentials (password)

p.put(Context.SECURITY_CREDENTIALS, "");

 

// Connect to the server by obtaining the initial context

Context initialContext = new InitialContext(p);

  

// Lookup the Calculator example

//“sap.com” is the provider name for all examples on the J2EE Engine;

// it is default provider name.

//“Calc” is the display name of the Calculator example application

//“Calculator” is the display name of the bean from the Calculator example

Object lookuped = initialContext.lookup("sap.com/Calc/Calculator");

// Narrow the looked up object to the bean's home class using the PortableRemoteObject

Object narrowed = PortableRemoteObject.narrow(lookuped, CalcHome.class);

// Cast the narrowed object to the bean’s home interface

CalcHome home = (CalcHome) narrowed;

// Create an instance of the bean from the home interface

CalcBean bean = home.create();

// Call a remote method of the bean

bean.add(1,1);

 

 

 

End of Content Area