Show TOC

Procedure documentationImplementing a Client Locate this document in the navigation structure

 

To be able to call the methods of your object remotely, you need to implement a client application that first looks it up from the server via the Naming system, and then call the remote methods.

Procedure

  1. Obtain a reference to the remote object by getting an Initial Context 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 Syntax

  1. package examples.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, "p4://" + 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 the code.