1 / 27

EJB Session Beans

EJB Session Beans. “ Controlling your application ” (the business logic). Model 2 with J2EE EJB ’ s. Model Two Architecture. EJB Container. Web Container. View. Control. Model. Web Server. Servlet (request handler). Entity EJB. Entity EJB. HTTP Request. Session EJB. Session Bean.

rico
Download Presentation

EJB Session Beans

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. EJB Session Beans “Controlling your application” (the business logic)

  2. Model 2 with J2EE EJB’s Model Two Architecture EJB Container Web Container View Control Model WebServer Servlet (request handler) Entity EJB Entity EJB HTTP Request Session EJB Session Bean Session EJB JavaBean JavaBean <<forward>> <<creates>> JavaBean HTTP Response JSP (view builder)

  3. Advantages • Automatic Transaction management • Resource management • Security • Modularity • Simple programming interface

  4. ClientProgram Remote Interface LocalInterface Clients View of Session EJB <<EJB>> Bean(Implementation) Client Interface Implementation

  5. <<Realization>>CustomerOrderEJB <<Remote Interface>>CustomerOrderRemote +startOrder() +addItem()+deleteItem()+checkout()+cancelOrder()+checkOrder() +startOrder() +addItem()+deleteItem()+checkout()+cancelOrder()+checkOrder() Session EJB Developers View <<EJB Session Bean>>OrderControl Client Interface Implementation

  6. Please Use Whenever Possible!!! Remote vs. Local Entity Beans • Remote Session EJB • Anywhere on the network • Pass by value • Local EJB are faster but not as flexible, scalable and adaptable • On same server as Session Beans • No network traffic • Pass by reference

  7. Please Use Whenever Possible!!! Stateful vs. Stateless Transactions • A stateless transactions is • One call to server • Does not the save transaction state data • No memory is allocated in the pool for each session with a client • EJB is never passivated or activated • Stateful transaction • Used for multi-step transactions • Saves state data for each client session • Memory is allocated in pool for each client session • EJB is passivated and activated

  8. timeout, @PreDestroy, Stateless Session Bean LifeCycle Does not exist Class.newInstance(), @PostConstruct Ready in pool Business method

  9. Timeout, @PreDestroy Class.newInstance(),@PostConstruct() business methods Stateful EJB Session Bean Lifecycle System exception Does not exist Timeout, @PreDestroy @PrePassivate() Ready in pool Passive @PostActivate ()

  10. Guidelines for use • Coarse-Grained better than Finely-Grained • Use stateless whenever possible • Use the remote interface for flexibility

  11. Locating an EJB • Must use a directory service and DNS to locate network objects • JNDI (Java Naming Directory Interface) • Programming interface to the directory services to locate any object in a network (files, EJBs, web services, etc.)

  12. Directory Service names • Most use X.500 naming standard c (country name) o (organizationName) ou (organizationUnitName) l (localityName) cn (commonName) dc (domainComponent) uid (userid) • Each type of directory service has it’s own naming syntax • LDAP example cn=Martin Bond, ou=Authors, o=SAMS, c=us • Microsoft Active Directory Service cn=Martin Bond/ou=Authors/o=SAMS/c=us

  13. JNDI names • JNDI names are not specific to directory services • JNDI maps universal name to specific directory service syntax • Typical JNDI name “SAMS/authors/Martin Bond”

  14. Outline for using remote EJBs • Get initial JNDI naming context • Use JNDI to lookup and find EJB • Call business function

  15. Defining the JNDI name

  16. Calling business methods • try • { • InitialContext initialContext = new InitialContext(); // get jndi context • String jndiName = BusinessRulesBean.RemoteJNDIName; ; // get jndi name • // lookup and get remote interface for session bean • BusinessRulesRemote businessRulesRemote = • (BusinessRulesRemote) initialContext.lookup(jndiName ); • // call any business methods defined in the interface • Person person = businessRulesRemote.login(username, password); … • } • catch (NamingException ne) { • throw new MyAppException( “Session EJB not found, jndiname=” + jndiName)); • } • catch (Exception e) { • throw new MyAppException(ee.getMessage()); • }

  17. Calling business methods • try • { • InitialContext initialContext = new InitialContext(); // get jndi context • String jndiName = BusinessRulesBean.RemoteJNDIName; ; // get jndi name • // lookup and get remote interface for session bean • BusinessRulesRemote businessRulesRemote = • (BusinessRulesRemote) initialContext.lookup(jndiName ); • // call any business methods defined in the interface • Person person = businessRulesRemote.login(username, password); … • } • catch (NamingException ne) { • throw new MyAppException( “Session EJB not found, jndiname=” + jndiName)); • } • catch (Exception e) { • throw new MyAppException(ee.getMessage()); • }

  18. Calling business methods • try • { • InitialContext initialContext = new InitialContext(); // get jndi context • String jndiName = BusinessRulesBean.RemoteJNDIName; ; // get jndi name • // lookup and get remote interface for session bean • BusinessRulesRemote businessRulesRemote = • (BusinessRulesRemote) initialContext.lookup(jndiName ); • // call any business methods defined in the interface • Person person = businessRulesRemote.login(username, password); … • } • catch (NamingException ne) { • throw new MyAppException( “Session EJB not found, jndiname=” + jndiName)); • } • catch (Exception e) { • throw new MyAppException(ee.getMessage()); • }

  19. Calling business methods • try • { • InitialContext initialContext = new InitialContext(); // get jndi context • String jndiName = BusinessRulesBean.RemoteJNDIName; ; // get jndi name • // lookup and get remote interface for session bean • BusinessRulesRemote businessRulesRemote = • (BusinessRulesRemote) initialContext.lookup(jndiName ); • // call any business methods defined in the interface • Person person = businessRulesRemote.login(username, password); … • } • catch (NamingException ne) { • throw new MyAppException( “Session EJB not found, jndiname=” + jndiName)); • } • catch (Exception e) { • throw new MyAppException(ee.getMessage()); • }

  20. Calling business methods • try • { • InitialContext initialContext = new InitialContext(); // get jndi context • String jndiName = BusinessRulesBean.RemoteJNDIName; ; // get jndi name • // lookup and get remote interface for session bean • BusinessRulesRemote businessRulesRemote = • (BusinessRulesRemote) initialContext.lookup(jndiName ); • // call any business methods defined in the interface • Person person = businessRulesRemote.login(username, password); … • } • catch (NamingException ne) { • throw new MyAppException( “Session EJB not found, jndiname=” + jndiName)); • } • catch (Exception e) { • throw new MyAppException(e.getMessage()); • }

  21. Insert object into Datastore public void addBranch() { // create a branch java bean Branch branch = new Branch(); branch.setName(“Bank of Nauvoo”); branch.setPhone(“203-356-1426”); // inserts a branch into the database entityManager.persist(branch); }

  22. Update Object in Datastore public void renameBranch(String branchid, String newName) { // get branch by its Primary Key from datastore Branch branch = (Branch) entityManager.find(Branch.class, branchid); // update the branch branch.setBranchname(newName); entityManager.merge(branch); }

  23. Delete Object from Datastore public void deleteBranch(String branchid) { // get branch by its Primary Key from datastore Branch branch = (Branch) entityManager.find(Branch.class, branchid); // Delete the branch entityManager.remove(branch); }

  24. Query Object/s from Datastore public Collection<Branch> getBranches(String name){ // Define query prepared statement String ejbql = "SELECT b FROM Branch b WHERE b.branchname LIKE :branchname”; // Create query object Query query = entityManager.createQuery(ejbql); // Substitute value to search for in prepared statement query.setParameter("branchname", searchValue); // Execute query to get list of branches List<Branch>branches = query.getResultList(); return branches; }

  25. From Examples: SELECT jFROM Job AS j WHERE j.jobType = ‘web development’ SQL equivalent: SELECT j.*FROM Job AS j WHERE j.jobType = ‘web development’; Entity Bean query language (ejb-ql) select_clause from_clause[where_clause]

  26. From Examples: SELECT sFROM Job AS j, INNER JOIN j.Skills AS s SQL equivalent: SELECT s.*FROM Job AS j INNER JOIN JobSkill AS sON j.FK_skillID = s.skillID Entity Bean query language (ejb-ql) select_clause from_clause[where_clause]

  27. SQL equivalent: SELECT C.*, O.*, I.*   FROM Customer C INNER JOIN Orders O INNER JOIN Items I ON C.customerId = O.FK_CustomerId ON O.FK_ItemId = I.ItemId WHERE C.lastName = ‘FLINSTONE’; Entity Bean query language (ejb-ql) Inner Joins of Entities Where Examples: SELECT OBJECT o FROM Customer AS c, INNER JOIN c.orders AS o, INNER JOIN o.items AS I WHERE c.lastName = ‘FLINTSTONE”

More Related