ejb session beans
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/1.jpg)
EJB Session Beans
“Controlling your application”
(the business logic)
![Page 2: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/2.jpg)
EJB ContainerWeb Container
View
Model 2 with J2EE EJB’s
Control Model
Session BeanWeb
Server
Servlet (request handler)
JSP(view builder)
Session EJB
Model Two Architecture
JavaBean
JavaBean
Entity EJB
<<forward>>
HTTP Request
HTTP Response
Entity EJB
JavaBean
<<creates>>
Session EJB
![Page 3: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/3.jpg)
Advantages
Automatic Transaction management Resource management Security Modularity Simple programming interface
![Page 4: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/4.jpg)
Clients View of Session EJB
Implementation
<<EJB>>
Client Interface
Remote Interface
Bean(Implementation)
ClientProgram
LocalInterface
![Page 5: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/5.jpg)
Session EJB Developers View
<<EJB Session Bean>>OrderControl
Client Interface Implementation
<<Realization>>CustomerOrderEJB
+startOrder() +addItem()+deleteItem()+checkout()+cancelOrder()+checkOrder()
<<Remote Interface>>CustomerOrderRemote
+startOrder() +addItem()+deleteItem()+checkout()+cancelOrder()+checkOrder()
![Page 6: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/6.jpg)
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
Please Use
Whenever Possible!!!
![Page 7: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/7.jpg)
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
Please Use
Whenever Possible!!!
![Page 8: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/8.jpg)
Stateless Session Bean LifeCycle
Ready in pool
timeout, @PreDestroy,
Does not exist
Class.newInstance(),@PostConstruct
Business method
![Page 9: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/9.jpg)
Stateful EJB Session Bean Lifecycle
Does not exist
Ready in pool Passive
Timeout, @PreDestroy
Class.newInstance(),@PostConstruct()
business methods
@PrePassivate()
@PostActivate ()
System exception
Timeout, @PreDestroy
![Page 10: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/10.jpg)
Guidelines for use
Coarse-Grained better than Finely-Grained
Use stateless whenever possible
Use the remote interface for flexibility
![Page 11: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/11.jpg)
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.)
![Page 12: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/12.jpg)
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 Servicecn=Martin Bond/ou=Authors/o=SAMS/c=us
![Page 13: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/13.jpg)
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”
![Page 14: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/14.jpg)
Outline for using remote EJBs
1. Get initial JNDI naming context
2. Use JNDI to lookup and find EJB
3. Call business function
![Page 15: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/15.jpg)
Defining the JNDI name
![Page 16: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/16.jpg)
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());}
![Page 17: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/17.jpg)
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());}
![Page 18: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/18.jpg)
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());}
![Page 19: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/19.jpg)
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());}
![Page 20: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/20.jpg)
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());}
![Page 21: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/21.jpg)
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); }
![Page 22: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/22.jpg)
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);}
![Page 23: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/23.jpg)
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);}
![Page 24: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/24.jpg)
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;}
![Page 25: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/25.jpg)
Entity Bean query language (ejb-ql)
select_clause from_clause [where_clause]
SELECT jFROM Job AS jWHERE j.jobType = ‘web development’
From Examples:
SELECT j.*FROM Job AS jWHERE j.jobType = ‘web development’;
SQL equivalent:
![Page 26: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/26.jpg)
Entity Bean query language (ejb-ql)
select_clause from_clause [where_clause]
SELECT sFROM Job AS j, INNER JOIN j.Skills AS s
From Examples:
SELECT s.*FROM Job AS j INNER JOIN JobSkill AS sON j.FK_skillID = s.skillID
SQL equivalent:
![Page 27: EJB Session Beans](https://reader033.vdocument.in/reader033/viewer/2022051018/56814036550346895dab9da1/html5/thumbnails/27.jpg)
Entity Bean query language (ejb-ql)
SELECT OBJECT o FROM Customer AS c, INNER JOIN c.orders AS o, INNER JOIN o.items AS IWHERE c.lastName = ‘FLINTSTONE”
Where Examples:
SELECT C.*, O.*, I.* FROM Customer C INNER JOIN Orders O INNER JOIN Items ION C.customerId = O.FK_CustomerIdON O.FK_ItemId = I.ItemIdWHERE C.lastName = ‘FLINSTONE’;
SQL equivalent:
Inner Joins of Entities