< Prev - Examples - Next >

RMI Remoting

This example is based on the simple object example.

Example Description

Server Main Program

Java Class : MainServerFactoryApi.java
package application;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Properties;

import javax.sql.DataSource;

import factory.RmiServerRegistry;
import oracle.ucp.jdbc.PoolDataSource;
import oracle.ucp.jdbc.PoolDataSourceFactory;

public class MainServerFactoryApi {
  public static void main(String[] args) {
    try {
      Registry registry = LocateRegistry.createRegistry(1099);

      // call bindServices to register services and set data source
      RmiServerRegistry.bindServices(registry, 1098, getDataSource());
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static DataSource getDataSource() throws Exception {
    // TODO : set your individual connection parameter settings !
    PoolDataSource poolDataSource = PoolDataSourceFactory.getPoolDataSource();
    poolDataSource.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
    Properties properties = new Properties();
    properties.load(MainServerFactoryApi.class.getClassLoader().getResourceAsStream("examples.properties"));
    poolDataSource.setURL(properties.getProperty("dbw_examples.url"));
    poolDataSource.setUser(properties.getProperty("dbw_examples.username"));
    poolDataSource.setPassword(properties.getProperty("dbw_examples.password"));
    poolDataSource.setMinPoolSize(Integer.parseInt(properties.getProperty("dbw_examples.poolsize.min")));
    poolDataSource.setMaxPoolSize(Integer.parseInt(properties.getProperty("dbw_examples.poolsize.max")));
    return poolDataSource;
  }
}

Client Setup

Set up the registry. This registry configuration is used by the factory.
Java Class : RmiClientRegistryFactoryApi.java
package factory;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class RmiClientRegistryFactoryApi {
  private static Registry registry;

  public static synchronized Registry getRegistry() throws RemoteException {
    if (registry == null) {
      registry = LocateRegistry.getRegistry();
    }
    return registry;
  }
}

Calling the Remote Services

Java Class : RmiServerInfoFactoryApi.java
package plsql_workbench_examples;

import factory.ExamplesRPCFactory;
import service.ServerInfoSimpleObjectService;
import transferobject.SimpleObject;

public class RmiServerInfoFactoryApi {
  public static void main(String[] args) {
    try {
      int diff = 10;

      // get the service
      ServerInfoSimpleObjectService service = ExamplesRPCFactory.getServerInfoSimpleObjectService();

      // calling the stored procedure, receiving a transfer object
      SimpleObject info = service.call(diff);

      // print server information
      System.out.println("database date(+" + diff + "):" + info.d);
      System.out.println("database timestamp(-" + diff + "):" + info.ts);
      System.out.println("database instance name:" + info.instance);
      System.out.println("database version:" + info.dbVersion + "." + info.dbRelease);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}

< Prev - Examples - Next >