ejb clients ejb clients
play

EJB Clients EJB Clients Transactions in Clients Transactions in - PowerPoint PPT Presentation

Topics Topics EJB Bean as a Client to Another Bean EJB Bean as a Client to Another Bean Serializing a Handle EJB Clients EJB Clients Transactions in Clients Transactions in Clients Unit III Authentication in Clients


  1. Topics Topics � EJB Bean as a Client to Another Bean � EJB Bean as a Client to Another Bean � Serializing a Handle EJB Clients EJB Clients � Transactions in Clients � Transactions in Clients Unit – III � Authentication in Clients Middleware Technologies g � Getting Meta Data � G tti M t D t � Servlet Client � Applet Client Roy Antony Arnold G � CORBA Client Lecturer P Panimalar Engineering College i l E i i C ll Chennai, Tamilnadu, India An EJB Bean as a Client to Another Bean Environment Properties Environment Properties � EJB Client Bean import ….; public class ClientBean implements SessionBean � To define an environment property two � To define an environment property, two { SessionContext ctx; { SessionContext ctx; public void ejbCreate() { } options available public void run() { try { Properties p = new Properties(); � Deployment Descriptor can be used � Deployment Descriptor can be used p.put( Context.INITIAL_CONTEXT_FACTORY, weblogic.jndi.TengahInitialContextFactory ); p put( Context INITIAL CONTEXT FACTORY “weblogic jndi TengahInitialContextFactory”); InitialContext ic = new InitialContext(p); � New Properties Object in the Client can be OrderHome oh = (OrderHome) ic.lookup(“OrderHome”); Order o1 = oh.create(1, “1”, 1); created and can be passed it to the created and can be passed it to the Order o2 oh.create(1, 2 , 1); Order o2 = oh create(1 “2” 1); o1.remove(); InitialContext Constructor o2.remove(); catch(NamingException e) { } catch (CreateExeception e) {} catch(RemoveException e) {} catch (RemoteException e) {} catch(RemoveException e) {} catch (RemoteException e) {} } public void setSessionContext (SessionContext ctx) { this.ctx = ctx; } public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate{} }

  2. The Client The Client Topics Topics � EJB Bean as a Client to Another Bean � EJB Bean as a Client to Another Bean import javax.naming.InitialContext; public class ClientClient { bli l Cli tCli t { � Serializing a Handle public static void main(String[] args) throws Exception { InitialContext ic = new InitialContext(); � Transactions in Clients � Transactions in Clients ClientHome ch = (ClientHome) ic.lookup(“ClientHome”); ( ) p( ); Client c = ch.create(); � Authentication in Clients c.run(); c.remove(); � G tti � Getting Meta Data System.out.println( Client is done ); System out println(“Client is done”); M t D t } � Servlet Client } Command-line parameter to define the initial naming factory can be used. � Applet Client For Example: java –Djava.naming.factory.initial=weblogic.jndi. � CORBA Client T3InitialContextFactory ClientClient Serializing a Handle Serializing a Handle The Client The Client � A handle is a serializable reference to a bean; i.e. you � If the client is called with the command-line � If the client is called with the command line can obtain a handle from a bean’s remote interface, write bt i h dl f b ’ t i t f it argument of “write”, it obtains a reference to an it out to persistent storage, and then close the program down. Order bean, obtains a handle to the reference � At � At some later time, you can read in and use it to obtain a l t ti d i d it t bt i and writes the handle to a file. new reference to a remote bean. � If it is called with the command-line argument of � This mechanism allows to serialize a remote reference to the bean the bean – but not to recreate the bean itself. but not to recreate the bean itself “read”, it reads the handle back in from the file, � The server-side object must still exist when attempting to reobtains the remote reference, and uses this reobtain the reference otherwise “NoSuchObjectException” will be thrown by the handle NoSuchObjectException will be thrown by the handle. reference to finish its work reference to finish its work. � The bean’s create() or find() method can be invoked to know about the existence of the object in the container.

  3. beginWork() beginWork() writeHandle writeHandle 1. Creating an initial naming context static void beginWork() { static void writeHandle() { 2. Using the naming try { try { context to look up t t t l k try { { InitialContext ic = new InitialContext(); the home interface FileOutputStream fos = new FileOutputStream System.out.println(“looked up initial context”); 3. Using the home (“beanhandle.ser”); OrderHome oh = (OrderHome) ic.lookup(“OrderHome”); interface to create a ObjectOutputStream oos = new ObjectOutputStream(fos); ObjectOutputStream oos = new ObjectOutputStream(fos); Order o1 = oh.create(1, 1 ,1); Order o1 = oh create(1 “1” 1); b bean. myHandle = o1.getHandle(); oos.writeObject(myHandle); } oos.flush(); catch (CreateException e) { Obtains a handle and oos.close(); (); System.out.println( CreateException occurred: + e); System out println(“CreateException occurred:” + e); stores it in the t it i th } } myHandle static class catch (RemoteException e) { catch (IOException e) { variable. System.out.println(“RemoteException occurred:” + e); The writeHandle System.out.println(“Exception occurred in writeHandle: “ + e); } } method later uses this th d l t thi } catch (NamingException e) { value to write the object } System.out.println(“NamingException occurred:” + e); out to persistent } storage. } } readHandle() readHandle() finishWork() finishWork() static void finishWork() { static void readHandle() try { try { { System.out.println(“Using handle to obtain reference”); try { Order o1 = (Order) myHandle.getEJBObject(); FileInputStream fis = new FileInputStream (“beanhandle.ser”); System.out.println(“removing”); ObjectInputStream ois = new ObjectInputStream (fis); ObjectInputStream ois = new ObjectInputStream (fis); o1.remove(); myHandle = (Handle) ois.readObject(); } ois.close(); catch(RemoteException e) { } System out println(“RemoteException Occurred:” + e); System.out.println( RemoteException Occurred: + e); catch (Exception e) { e.printStackTrace(); } System.out.println(“Exception occurred in readHandle:” + e); catch (RemoveException e) { } System.out.println(“RemoveException occurred” + e); e.printStackTrace(); } }

  4. Contd… Contd… Topics Topics � myHandle is a static variable of Handle type. � EJB Bean as a Client to Another Bean � EJB Bean as a Client to Another Bean � Classes in the java.io package is used to store and � Serializing a Handle retrieve the handle. � If “read” or “write” is found on the command line, the � Transactions in Clients � Transactions in Clients routine invokes the appropriate procedures. � Authentication in Clients � Invoking the clients to write out a serialized handle to write out a serialized handle � G tti � Getting Meta Data M t D t java –Djava.naming.factory.initial=weblogic.jndi. � Servlet Client T3InitialContextFactory HandleClient write � Applet Client to read the handle back in and finish working with it � CORBA Client java –Djava.naming.factory.initial=weblogic.jndi. T3InitialContextFactory HandleClient read T3InitialContextFactory HandleClient read import …. public class ClientTx { Transactions in Clients Transactions in Clients public static void main(String[] argv) { bli t ti id i (St i [] ) { UserTransaction utx = null; try { � A client can create its own transaction context and use it InitialContext ic = new InitialContext(); InitialContext ic = new InitialContext(); to manage transactions across multiple invocations. OrderHome oh = (OrderHome) ic.lookup(“OrderHome”); utx = (UserTransaction) ic.lookup (“javax.jts.UserTransaction”); utx.begin(); utx.begin(); � javax.jts.UserTransaction interface is imported. This is Order o1 = oh.create(1, “1”, 1); part of the Java Transaction Services (JTS) Order o2 = oh.create(1, “2”, 1); Specification. Specification. utx.commit(); // or utx.rollback(); () () } catch (CreateException e) { if(utx!=null) { utx.rollback();} } catch (RemoteException e) { if(utx!=null) { utx.rollback();} } catch (NamingException e) { if( t ! n ll) { if(utx!=null) { utx.rollback(); } } t rollback() } } } }

Recommend


More recommend