// JNDI: UnicastRemoteObject ==> PortableRemoteObject // Naming.rebind() ==> ic.rebind() // Naming.lookup() ==> ic.lookup() // Java cast ==> PortableRemoteObject.narrow() // rmic -v1.2 ==> rmic -iiop // rmiregistry ==> j2ee -verbose import java.rmi.*; // Remote, RemoteException import javax.rmi.*; // PortableRemoteObject import javax.naming.*; // InitialContext interface RISort extends Remote { int[] sort( int[] array ) throws RemoteException; } public class JndiSortServer extends PortableRemoteObject implements RISort { public JndiSortServer() throws RemoteException { } public int[] sort( int[] array ) { System.out.print( "JndiSortServer.sort() - " ); for (int i=0; i < array.length; i++) System.out.print( array[i] + " " ); System.out.println(); for (int g = array.length/2; g > 0; g /= 2) for (int i = g; i < array.length; i++) for (int j = i-g; j >= 0; j -= g) if (array[j] > array[j+g]) { int temp = array[j]; array[j] = array[j+g]; array[j+g] = temp; } return array; } public static void main( String[] args ) { try { InitialContext ic = new InitialContext(); ic.rebind( "SortServer", new JndiSortServer() ); } catch (NamingException ex ) { ex.printStackTrace(); } catch (RemoteException ex ) { ex.printStackTrace(); } System.out.println( "JndiSortServer bound in JNDI" ); } } import java.rmi.*; // RemoteException import javax.rmi.*; // PortableRemoteObject import javax.naming.*; // InitialContext public class JndiSortClient { public static void main( String[] args ) { int[] array = new int[15]; java.util.Random rn = new java.util.Random(); for (int i=0; i < array.length; i++) array[i] = rn.nextInt(20) + 1; for (int i=0; i < array.length; i++) System.out.print( array[i] + " " ); System.out.println(); try { InitialContext ic = new InitialContext(); Object obj = ic.lookup( "SortServer" ); RISort sorter = (RISort) PortableRemoteObject.narrow( obj, RISort.class ); array = sorter.sort( array ); } catch (NamingException ex) { ex.printStackTrace(); } catch (RemoteException ex) { ex.printStackTrace(); } for (int i=0; i < array.length; i++) System.out.print( array[i] + " " ); System.out.println(); } } // D:\> j2ee -verbose // // D:\j2ee\demos> javac JndiSortServer.java // D:\j2ee\demos> rmic -iiop JndiSortServer // D:\j2ee\demos> java JndiSortServer // JndiSortServer bound in JNDI // JndiSortServer.sort() - 16 20 1 4 3 7 20 11 11 2 15 12 8 3 17 // // D:\j2ee\demos> javac JndiSortClient.java // D:\j2ee\demos> java JndiSortClient // 16 20 1 4 3 7 20 11 11 2 15 12 8 3 17 // 1 2 3 3 4 7 8 11 11 12 15 16 17 20 20