Package: java.rmi.* |
Product: JDK |
Release: 1.1 |
Related Links: |
General
LocateRegistry
|
Comment: |
import java.rmi.*; import java.rmi.server.*; import java.rmi.registry.*; import java.applet.Applet; interface xWatch extends java.rmi.Remote { public int getClientId() throws java.rmi.RemoteException; public boolean removeClientId(int id) throws java.rmi.RemoteException; // all additional remote methods here!!! } interface xNotify extends java.rmi.Remote { } class xClient extends Applet implements xNotify { private int connectionId = -1; private xWatch servlet = null; private String registryName = null; public void init() { try { UnicastRemoteObject.exportObject( this ); // get the host name where the applet was loaded String host = ""; if (! (host = getCodeBase().getHost()).equals("") ) { host = "localhost"; } registryName = "rmi://" + getCodeBase().getHost() + ":1099/"; // append the name of the remote object within the registry registryName += "xRMI"; // obtain a reference to the remote object System.out.println("obtain remote object reference for " + registryName ); servlet = (xWatch)Naming.lookup(registryName); if (servlet == null ) { System.err.println("Failed to bind to remote object."); } } catch (Exception e) { dbg("Cannot connect to RMI registry for jdbcrmi"); return; } try { if (servlet == null ) { System.err.println("Failed to bind to remote object."); } connectionId = servlet.getClientId(); if (connectionId == -1) { dbg("Cannot open database connection."); } else { dbg("connected as id " + connectionId ); } if (servlet == null ) { System.err.println("connectionId:" + connectionId); } } catch (Exception ex) { dbg("Cannot open connection."); } } public void finalize() { try { servlet.removeClientId(connectionId); } catch (Exception ex) {} super.destroy(); } private void dbg(String str) { System.out.println( str ); } } class xImpl extends UnicastRemoteObject implements java.rmi.Remote, xWatch, xNotify { private xService [] service = new xService[5]; public int getClientId() throws java.rmi.RemoteException { int connectionId; // Loop through connection table until an empty slot is found for (connectionId = 0; connectionId < service.length; connectionId++) { if (service [connectionId] == null) break; } // If no empty slots found, generate an error if (connectionId >= service.length) { System.out.println("Out of connection objects."); return -1; } // Create a connection for the new process and run it service[connectionId] = new xService(connectionId,this); // return the connection identifier return connectionId; } public boolean removeClientId(int id) throws java.rmi.RemoteException { try { service[id] = null; } catch (Exception e) { e.printStackTrace(); return false; } return true; } } class xService { private int id = -1; private xNotify xn = null; public xService(int id, xNotify xn) { this.id = id; this.xn = xn; } } class xStart { public static void main(String [] args) { System.out.println("Starting xRMI..."); // Create and install the security manager System.setSecurityManager(new RMISecurityManager()); try { xImpl servlet = new xImpl (); System.out.println("Binding to the registry"); Naming.rebind("xrmi", servlet); //Naming.bind("xrmi", servlet); System.out.println("xRMI is started."); } catch (Exception e) { System.out.println("xImpl.main: an exception occurred: " + e.getMessage()); e.printStackTrace(); } } } |