1 / 24

Service Computing

Service Computing. Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009. EJB. EJB CartBean Example (1). import java.util .*; import javax.ejb.*; public class CartBean implements SessionBean { String customerName ; String customerId ; Vector contents;

violet
Download Presentation

Service Computing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Service Computing Prof. Dr. Ramin YahyapourIT & Medien Centrum12. November 2009

  2. EJB

  3. EJB CartBean Example (1) import java.util.*; import javax.ejb.*; public class CartBean implements SessionBean { String customerName; String customerId; Vector contents; public void ejbCreate(String person) throws CreateException { if (person == null) { throw new CreateException("Null person not allowed."); } else { customerName = person; } customerId = "0"; contents = new Vector(); } public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext sc) {} …

  4. EJB CartBean Example (2) … public void addBook(String title) { contents.addElement(title); } public void removeBook(String title) throws BookException { boolean result = contents.removeElement(title); if (result == false) { throw new BookException(title + "not in cart."); } } public Vector getContents() { return contents; } public CartBean() {} }

  5. CartBean –Home Interface import java.io.Serializable; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBHome; public interface CartHome extends EJBHome { Cart create(String person) throws RemoteException, CreateException; }

  6. CartBean –Remote Interface import java.util.*; import javax.ejb.EJBObject; import java.rmi.RemoteException; public interface Cart extends EJBObject { public void addBook(String title) throws RemoteException; public void removeBook(String title) throws BookException, RemoteException; public Vector getContents() throws RemoteException; }

  7. Savings Account -Database Definition CREATE TABLE savingsaccount (id VARCHAR(3) CONSTRAINT pk_savingsaccount PRIMARY KEY, firstname VARCHAR(24), lastname VARCHAR(24), balance NUMERIC(10,2));

  8. Savings Account –ejbCreate public String ejbCreate(String id, String firstName, String lastName, BigDecimal balance) throws CreateException { if (balance.signum() == -1) { throw new CreateException ("A negative initial balance is not allowed."); } try { insertRow(id, firstName, lastName, balance); } catch (Exception ex) { throw new EJBException("ejbCreate: " + ex.getMessage()); } this.id = id; this.firstName = firstName; this.lastName = lastName; this.balance = balance; return id; }

  9. Savings Account –ejbRemove public void ejbRemove() { try { deleteRow(id); catch (Exception ex) { throw new EJBException("ejbRemove: " + ex.getMessage()); } } }

  10. Savings Account –ejbLoad/ejbStore public void ejbLoad() { try { loadRow(); } catch (Exception ex) { throw new EJBException("ejbLoad: " + ex.getMessage()); } } public void ejbStore() { try { storeRow(); } catch (Exception ex) { throw new EJBException("ejbStore: " + ex.getMessage()); } }

  11. Savings Account –Finder Methods SavingsAccountjones = home.findByPrimaryKey("836"); ... Collection c = home.findByLastName("Smith"); ... Collection c = home.findInRange(20.00, 99.00); • For every finder method available to a client, the entity bean class must implement a corresponding method that begins with the prefix ejbFind. The SavingsAccountBean class, for example, implements the ejbFindByLastName method as follows: public Collection ejbFindByLastName(String lastName) throws FinderException { Collection result; try { result = selectByLastName(lastName); } catch (Exception ex) { throw new EJBException("ejbFindByLastName " + ex.getMessage()); } return result; }

  12. Savings Account –Finder Methods (2) • the ejbFindByPrimaryKey method is required • other Finder methods are optional public String ejbFindByPrimaryKey(String primaryKey) throws FinderException { boolean result; try { result = selectByPrimaryKey(primaryKey); } catch (Exception ex) { throw new EJBException("ejbFindByPrimaryKey: " + ex.getMessage()); } if (result) { return primaryKey; } else { throw new ObjectNotFoundException ("Row for id " + primaryKey + " not found."); } }

  13. Lifecycle of a Stateful Session Bean

  14. Lifecycle of a Stateless Session Bean

  15. Lifecycle of a Message-Driven Bean

  16. Lifecycle of an Entity Bean

  17. Java Application Server

  18. Deployment Descriptor <?xml version="1.0" encoding="UTF-8"?> <ejb-jar> <description>JBoss Hello World Application</description> <display-name>Hello World EJB</display-name> <enterprise-beans> <session> <ejb-name>HelloWorld</ejb-name> <home>com.mastertech.sample.HelloWorldHome</home> <remote>com.mastertech.sample.HelloWorld</remote> <ejb-class>com.mastertech.sample.HelloWorldBean</ejb-class> <session-type>Stateless</session-type> <transaction-type>Bean</transaction-type> </session> </enterprise-beans> </ejb-jar>

  19. Deployment Descriptorwith Query Definition <query> <query-method> <method-name>findByName</method-name> <method-params> <method-param>java.lang.String</method-param> </method-params> </query-method> <ejb-ql> select object(p) From CEntityBean2 as p where p.prof = ?1 </ejb-ql> </query>

  20. Message Driven-Beans

  21. JNDI Architecture

  22. JNDI Name Service Example try { // Create the initial context Context ctx = new InitialContext(env); // Look up an object Object obj = ctx.lookup(name); // Print it System.out.println(name + " is bound to: " + obj); } catch (NamingException e) { System.err.println("Problem looking up " + name + ": " + e); }

  23. JNDI Directory Service Example try { // Create the initial directory context DirContextctx = new InitialDirContext(env); // Ask for all attributes of the object Attributes attrs = ctx.getAttributes("cn=Ted Geisel, ou=People"); // Find the surname attribute ("sn") and print it System.out.println("sn: " + attrs.get("sn").get()); } catch (NamingException e) { System.err.println("Problem getting attribute:" + e); }

  24. JNDI Create Context { Hashtableenv = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); Context ctx = new InitialContext(env); … } { Hashtableenv = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial"); DirContextctx = new InitialDirContext(env); … }

More Related