// EJB: RISort extends javax.ejb.EJBObject // HISort extends javax.ejb.EJBHome // SortSlsbBean implements javax.ejb.SessionBean // implement "business" interface // implement "technical" interface // SortSlsbClient: // Object obj = ic.lookup( "SortHome" ); // HISort factoryObj = (HISort) PortableRemoteObject.narrow( obj, HISort.class ); // RISort sorterObj = factoryObject.create(); // j2ee -verbose // deploytool interface RISort extends javax.ejb.EJBObject { int[] sort( int[] array ) throws java.rmi.RemoteException; } interface HISort extends javax.ejb.EJBHome { RISort create() throws javax.ejb.CreateException, java.rmi.RemoteException; } public class SortSlsbBean implements javax.ejb.SessionBean { private javax.ejb.SessionContext context; // corresponds to HISort.create() public void ejbCreate() { System.out.println( "SortBean.ejbCreate" ); } // "business" interface from RISort public int[] sort( int[] array ) { System.out.print( "SortBean.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; } // "technical" interface from javax.ejb.SessionBean public void ejbActivate() { System.out.println( "SortBean.ejbActivate" ); } public void ejbPassivate() { System.out.println( "SortBean.ejbPassivate" ); } public void ejbRemove() { System.out.println( "SortBean.ejbRemove" ); } public void setSessionContext( javax.ejb.SessionContext ctx ) { System.out.println( "SortBean.setSessionContext" ); context = ctx; } } import javax.rmi.*; // PortableRemoteObject.narrow(), NamingException import javax.naming.*; // InitialContext public class SortSlsbClient { 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( "SortHome" ); HISort factoryObject = (HISort) PortableRemoteObject.narrow( obj, HISort.class ); RISort sorterObject = factoryObject.create(); array = sorterObject.sort( array ); } catch (javax.ejb.CreateException ex) { ex.printStackTrace(); } catch (java.rmi.RemoteException ex) { ex.printStackTrace(); } catch (NamingException ex) { ex.printStackTrace(); } for (int i=0; i < array.length; i++) System.out.print( array[i] + " " ); System.out.println(); } } // d:\> j2ee -verbose // SortBean.setSessionContext // SortBean.ejbCreate // SortBean.sort() - 16 4 6 19 13 17 13 5 17 9 9 17 15 6 6 // SortBean.sort() - 12 3 9 9 19 17 2 20 2 5 18 16 7 15 20 // d:\j2ee> deploytool // Remote message: Contacted Server.... // Remote message: SortDemo has 1 ejbs, 0 web components to deploy. // Remote message: Deploying Ejbs.... // Remote message: Processing beans .... // Remote message: Generating wrapper code for SortSLSB. // Remote message: Compiling wrapper code .... // Remote message: Compiling RMI-IIOP code .... // Remote message: Deployment of SortDemo is complete. // D:\j2ee\demos> javac SortSlsbBean.java // D:\j2ee\demos> javac SortSlsbClient.java // D:\j2ee\demos> java SortSlsbClient // 16 4 6 19 13 17 13 5 17 9 9 17 15 6 6 // 4 5 6 6 6 9 9 13 13 15 16 17 17 17 19 // D:\j2ee\demos> java SortSlsbClient // 12 3 9 9 19 17 2 20 2 5 18 16 7 15 20 // 2 2 3 5 7 9 9 12 15 16 17 18 19 20 20