// RMI. Arrays are Serializable, and therefore passed by value in RMI. // This sort() method only sorts the copy that is passed to it, and has // no effect on the client's unsorted array. import java.rmi.*; import java.rmi.server.*; interface RISort extends Remote { void sort( int[] array ) throws RemoteException; } public class RmiSortServer extends UnicastRemoteObject implements RISort { public RmiSortServer() throws RemoteException { } public void sort( int[] array ) { System.out.print( "RmiSortServer.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; } } public static void main( String[] args ) { try { Naming.rebind( "SortServer", new RmiSortServer() ); } catch (java.net.MalformedURLException ex) { ex.printStackTrace(); } catch (RemoteException ex) { ex.printStackTrace(); } System.out.println( "SortServer bound in rmiregistry" ); } } import java.rmi.*; public class RmiSortClient { 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 { RISort sorter = (RISort) Naming.lookup( "SortServer" ); sorter.sort( array ); } catch (NotBoundException ex) { ex.printStackTrace(); } catch (java.net.MalformedURLException 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\demos> rmiregistry // // D:\j2ee\demos> javac RmiSortServer.java // D:\j2ee\demos> rmic -v1.2 RmiSortServer // D:\j2ee\demos> java RmiSortServer // SortServer bound in rmiregistry // RmiSortServer.sort() - 18 7 13 3 15 5 12 17 11 4 15 13 20 1 3 // // D:\j2ee\demos> javac RmiSortClient.java // D:\j2ee\demos> java RmiSortClient // 18 7 13 3 15 5 12 17 11 4 15 13 20 1 3 // 18 7 13 3 15 5 12 17 11 4 15 13 20 1 3