import javax.naming.*; import javax.ejb.*; import java.rmi.*; import javax.rmi.*; // Note the extension to RMI for PortableRemoteObject() public class HelloClient { public static void main2(String[] args) { HelloHome home = null; String response = null; Hello bean = null; System.out.println("Looking up the home object"); try { Context initialContext = new InitialContext(); Object objRef = initialContext.lookup("MyHello"); home = (HelloHome) PortableRemoteObject.narrow(objRef, HelloHome.class); } catch (NamingException e) { System.err.println("JNDI Naming Exception"); System.err.println(e.getMessage()); return; } System.out.println("Creating the bean instance"); try { bean = home.create(); } catch (CreateException e) { System.err.println("CreateException on HelloHome"); System.err.println(e.getMessage()); return; } catch (RemoteException e) { System.err.println("RemoteException: create() on HelloHome"); System.err.println(e.getMessage()); return; }
System.out.println("Calling the business methods"); try { System.out.println("calling String sayHello()"); response = bean.sayHello(); System.out.println("got " + response); } catch (RemoteException e) { System.err.println("RemoteException while invoking sayHello()"); System.err.println(e.getMessage()); return; } catch (HelloException e) { System.err.println("HelloException while invoking sayHello()"); System.err.println(e.getMessage()); } try { System.out.println("calling void sayHello(\"Mickey\")"); response = bean.sayHello("Mickey"); System.out.println("got " + response); } catch (RemoteException e) { System.err.println("RemoteException while invoking sayHello()"); System.err.println(e.getMessage()); return; } System.out.println("Removing the bean"); try { bean.remove(); } catch (RemoteException e) { System.err.println("RemoteException while removing bean"); System.err.println(e.getMessage()); return; } catch (RemoveException e) { System.err.println("RemoveException while removing bean"); System.err.println(e.getMessage()); return; } } }
try{ // All the code goes here } catch (Exception e){ System.out.println("EJB failure!"); }