Middleware Technology (Part VIII, Enterprise Java …Middleware Technology (Part VIII, Enterprise...
Transcript of Middleware Technology (Part VIII, Enterprise Java …Middleware Technology (Part VIII, Enterprise...
Middleware Technology(Part VIII, Enterprise Java
Beans)Andrei Popovici
Information and Communication SystemsResearch Group
Department of Computer ScienceETH Zürich
[email protected]://www.inf.ethz.ch/department/IS/iks/
2A. Popovici. Information and Communication Systems Research Group. ETH Zürich. Part VIII - EJB
Outline
ο EJB fundamentals: beans and containersο Containersο Beans
• session beans• entity beans
ο Exampleο EJB Internals:
⇒ Persistence⇒ Pooling⇒ Transactions
ο Conclusions and Outlook
3A. Popovici. Information and Communication Systems Research Group. ETH Zürich. Part VIII - EJB
The EJB Component ModelAhead towards the ideal middleware system:
ο portability⇒ Java⇒ components: pure application logic
ο Interoperability⇒ components have hooks to cooperate with the
environment (container)• EJB API and coding conventions
ο Integration⇒ Naming services⇒ Data Persistence⇒ Transactions⇒ Authentication & Authorization
4A. Popovici. Information and Communication Systems Research Group. ETH Zürich. Part VIII - EJB
EJB: Beans and ContainersTwo main concepts dominate the EJB paradigm:ο Beans: fit together in a standard way
⇒ contain pure application logic and hooks
ο Containers = middlewareο know how to communicate with the
beans and with the clientsο know how to create and destroy
beansο implement services developers don’t
want to worry aboutο all messages go through the
container
CONTAINER
clients
Java Virtual Machine
CONTAINER
5A. Popovici. Information and Communication Systems Research Group. ETH Zürich. Part VIII - EJB
Containersο A bean container is the server
application on which the beancode runs.⇒ container vendors: IBM,
SUN, Oracleο The container interposes itself
between the client and the bean.Server Host
ο Before a bean is able to live in a container, ithas to be adapted to a specific container.This process is called deployment.
ο Deployment is vendor specific... The client triggers the creation of beans inside the container,
but does not directly access the methods of the beans it hascreated. Instead, the container exports a remote interface tothe client.
6A. Popovici. Information and Communication Systems Research Group. ETH Zürich. Part VIII - EJB
EJB: The Big Picture
client
= the XML-formatted file created bythe deployer in which the type ofthe bean, its name, etc.. arespecified Deployment
descriptor
CONTAINER
deploy bean
JNDI: ejb/beans/myBeanH = bean code (app.logic)
Deploymentdescriptor
= bean home, generated by the deployer,published as jndi:ejb/beans/myBeanH
Step 1: install bean
(create Bean)ask home forbean instance
use bean
Step 2: use bean
7A. Popovici. Information and Communication Systems Research Group. ETH Zürich. Part VIII - EJB
Beans and Clientsο Beans are (independent) business objects. Some examples:
⇒ The small software piece behind a web site counter⇒ A currency converter⇒ A shopping cart in a web store may be implemented by a
bean. The items inside may be other beans.ο Scenarios:
⇒ The bean is not influenced by what the client did before(e.g., currency converter)
• Stateless session bean⇒ The bean is influenced by what the client did (e.g.,
shopping cart)• Stateful session bean
⇒ The bean has to survive a server crash (e.g., web sitecounter)
• Stateful, entity bean. All entity beans are stateful.
8A. Popovici. Information and Communication Systems Research Group. ETH Zürich. Part VIII - EJB
Bean Typesο Session Beans
⇒ are used by one client at a time⇒ are lost in a server crash⇒ Stateless: look the same to the client before and after the
operation⇒ Stateful: persistence
ο Entity Beans⇒ are the core business objects⇒ represent shared data in a database⇒ allow shared access from multiple clients⇒ survive a server crash
9A. Popovici. Information and Communication Systems Research Group. ETH Zürich. Part VIII - EJB
Example: Web Counter
Web server = client role
Pagerequest
container
ο Developer:
EJBObject
Counter
EntityBean
CounterBeanCounterHome
EJBHome
int click() create() ejbCreate()int click()
ο Deployer: <name>ejb/beans/myBeanH</name><home>CounterHome</home>...
(1) Counter.xml:
(2) bash$ deployejb counter.xml container
ο Java client (code running in the web server):
counterHome=ctx.lookup(“jndi:ejb/beans/myBeanH”);Counter c = counterHome.create();
app. logic
utility
ctx: JNDI Context
10A. Popovici. Information and Communication Systems Research Group. ETH Zürich. Part VIII - EJB
EJB and Persistence (1)ο Session beans have to use JDBC. Fortunately, beans do not
have to bother with the JDBC driver setup, which is the mainportability problem in Java applications which access adatabase. Instead, the database (data source) is published inthe JNDI naming service. The beans just specify the SQLcommand for retrieving the data from a database:⇒ Example stateless session bean:
⇒ Example stateful session bean:
ejbCreate() { /* ask container for data source*/}ejbRemove() { /* tell container we’re finished */ }
ejbCreate() {..}ejbRemove() {..}ejbActivate() { /* similar to ejbCreate */ }ejbPassivate() { /* similar to ejbRemove */}
(code from CounterBean.java)
(code from CounterBean.java)
11A. Popovici. Information and Communication Systems Research Group. ETH Zürich. Part VIII - EJB
EJB and Persistence (2)ο Entity beans usually correspond to a record in a relational database.
Therefore, it has a unique identifier which survives over time and overclient accesses: the primary key (PK). The bean developer must create aspecial PK Java class.
ο Clients get references to entity beans by specifying the PKο The persistence of an entity bean may be:
⇒ bean managed (like in session beans, using JDBC). In this case, thebean developer has to provide the proper code for storing andretrieving the state of the bean (ejbLoad, ejbStore)
⇒ container managed: the container automatically stores in thedatabase all attributes of an entity bean instance. Container managedpersistence makes life easier (no SQL statements) for the developer.The developer still has to implement some special methods.
ο What kind of persistence mechanisms the bean uses is specified in thedeployment descriptor:
<persistence-type>container</persistence-type>
12A. Popovici. Information and Communication Systems Research Group. ETH Zürich. Part VIII - EJB
EJB and Poolingο If a container has many clients, it cannot afford to keep all
beans associated to the clients in the main memory.Therefore⇒ it reuses the stateless session beans living in the
container⇒ it temporarily stores the data of a stateful session bean in
persistent storage (activation and passivation)ο As a consequence, the developer of a bean has to implement
methods like ejbActivate, ejbPassivate, ejbCreate,ejbRemove to react to the container’s decision about a bean.
ο On the other hand, the container has to consistently callthese methods every time it creates, destroys, or stores abean.
13A. Popovici. Information and Communication Systems Research Group. ETH Zürich. Part VIII - EJB
EJB and Transactionsο A business method implemented by a bean (e.g.
‘doReservation(hotel,flight)’ has to be executed atomically,like a transaction
ο The deployer can specify this in the deploymentdescriptor,like this:
ο The transaction management in EJB is based the JTA (Java Transaction API)ο The transaction management (like persistence) may be
⇒ implicit - container uses JTA, developer writes pureapplication logic
⇒ explicit - developer writes JTA and JDBC code
<container-transaction> <method>doReservation</method> <trans-attribute>required</trans-attribute></container-transaction>
14A. Popovici. Information and Communication Systems Research Group. ETH Zürich. Part VIII - EJB
Practical Considerationsο Shall I choose Corba or EJB?
⇒ Corba provides at least the same services as EJB.⇒ EJB application is easier to write and deploy.⇒ The EJB framework is complex.
ο Which container vendor shall I choose?⇒ Sun’s specifications are ahead of the vendor’s
implementation.ο Current trend: everybody is reshaping their applications
towards EJBο World Wide Web: Java Server Pages (JSP) or Servlets work
well together with EJBο Performance: Java is still slower its counterparts for WWW
⇒ however, EJB provides good scalability.
15A. Popovici. Information and Communication Systems Research Group. ETH Zürich. Part VIII - EJB
Conclusions and Outlookο Enterprise Java Beans are easy to writeο But the EJB architecture is complex and is closely tied to
other Java packages and specifications, like JNDI, JDBC,JTA or RMI. A bean developer will have to understand themas well.
ο By separating deployment and development, reusing ofbeans becomes a reality. Most of the services needed bysuch components can be transparently managed by thecontainer. The almost complete separation between servicesand business logic is one of the main advantages of EJB.
ο SUN’s specifications are ahead of the products supplied bycontainer vendors.
ο EJB can be used in conjunction with other server-side javatechnologies like servlets or Java Server Pages (JSP). That’swhy many E-commerce companies are shifting their productstowards EJB and Java-based www technologies.
16A. Popovici. Information and Communication Systems Research Group. ETH Zürich. Part VIII - EJB
Appendix: ContainerBean.java
Class CounterBean implements EntityBean { public String counterId; public int counterValue; public CoutnerPK ejbCreate(String id, int cval) {..} public ejbPostCreate(String id,int cval) {..} public setEntityContext(EntityContext ctx) {..} public unsetEntityContext() {..} public void ejbActivate() {..} public void ejbPassivate() {..} public void ejbLoad() {..} public void ejbStore() {..} public void click() { } public int getValue();}
This Java class is an example ofabout half the methods whichhave to be implemented in aBean. Sometimes, the methodsmay be left empty, e.g., if thepersistence is container-managed.