Show TOC Start of Content Area

Procedure documentation Implementing a Client  Locate the document in its SAP Library structure

...

       1.      Obtain a reference to the remote object by getting an InitialContext from the Naming System.

       2.      Look up the implementation of the remote object.

       3.      Call remote methods of that object.

Example

The BankClient class below is an example of a client that looks up the Bank remote object from the Naming System and then calls methods on it remotely.

Syntax

package exapmles.rmi_p4;

 

import javax.naming.Context;

import javax.naming.InitialContext;

import java.util.Properties;

 

public class BankClient {

 

  public static void main(String[] args) {

    try {

      if (args.length < 2) {

        System.out.println(" Use: BankClient <hostName> <port>");

        return;

      }

      Properties p = new Properties();

      // Specify the type of the InitialContext factory.

      p.put(Context.INITIAL_CONTEXT_FACTORY, "com.sap.engine.services.jndi.InitialContextFactoryImpl");

      // Specify the provider URL.

      p.put(Context.PROVIDER_URL, args[0] + ":" + args[1]);

      // Specify the security principal (user).

      p.put(Context.SECURITY_PRINCIPAL, args[2]);

      // Specify the security credentials (password).

      p.put(Context.SECURITY_CREDENTIALS, args[3]);

 

      // Connect to the server by the InitialContext.

      Context initialContext = new InitialContext(p);

      Account account =  (Account) initialContext.lookup("Bank");

      // Invoke methods remotely.

      account.deposit(100);

      System.out.println("Balance:" + account.getBalance());

      System.out.println("Try to draw...");

      account.draw(50);

      System.out.println("Balance:" + account.getBalance());

    } catch (Exception ex) {

      ex.printStackTrace();

    }

  }

}

 

 

End of Content Area