A Distributed Computing Lecture by Steven Choy
What is Java EE?
- Component-based approach for design, development, assembly & deployment of enterprise applications
- Specification, not a product!
Java EE Components and Services
- Primary technologies
- Servlets
- JavaServer Pages (JSP)
- Enterprise JavaBeans (EJB)
- Standard services & supporting technologies
- Java database connectivity(JDBC) data access API
- Remote Method Invocations (RMI)
- Extensible Markup Languages(XML)
- JavaIDL
- JavaMail
Multi-tier applications in Java EE
(Source: java.sun.com)
- Client Presentation
- HTML or Java applets deployed in Browser
- XML documentations transmitted through HTTP
- Java clients running in Client Java Virtual Machine (JVM)
- Presentation Logic
- Servlets or JavaServer Pages running in web server
- Application Logic
- Enterprise JavaBeans running in Server
- Working Model
- Browser is able to process HTML and applets pages.
- It forwards requests to the web server, which has JSPs and Servlets
- Servlets and JSPs may access EJB server.
- Java Standalone runs on java client, which access EJB server using RMI.
Business and EIS (Enterprise Information System) Tiers
(Source: java.sun.com)
Java EE containers
- Container = the runtime environment of application components
- Web container – for hosting servlets and JavaServer Page (JSP)
- EJB container – for hosting EJB components
- Application container – for hosting client Java applications including console and GUI applications
- Applet container – for hosting Java applets
Containers in Java EE
(Source: java.sun.com)
EJB – Enterprise Java Beans
- Enterprise JavaBeans is the server-side component architecture for the Java EE platform.
- EJB enables rapid and simplified development of distributed, transactional, secure and portable Java applications.
- http://java.sun.com/products/ejb/index.jsp
- Enterprise Java Beans are components that are deployed into containers
- The container provides services
- Loading / Initialization
- Transactions
- Persistence
- Communication with EJB clients
- Enterprise Naming Context (JNDI name space)
EJB container
- Tasks of the EJB container
- Create the EJBs and manage their life cycle
- Manage the resources used by the EJBs
- Manage the access from client applications to EJBs
- The EJB container interacts with EJBs by
- Callback methods (Implemented by EJB and invoked during its life cycle)
- EJBContext (A reference to the container used by EJB)
- Java Naming and Directory Interface (JNDI) - Naming services to EJB for locating resources
Using an EJB
- Step 1: The client looking for a service
- Step 2: An object that implements the EJB home interface is returned to the client. Then the create method of the object is invoked.
- Step 3: The business method of the EJB remote object interface is invoked, which would then result in the business method of the EJB object being invoked.
Anatomy of an EJB
- Remote Interface
- Methods that can be accessed by the outside world.
- Extends javax.ejb.EJBObject
- Remote Home Interface
- Life-cycle methods (create, findByPrimaryKey)
- Extends javax.ejb.EJBHome which extends java.rmi.Remote
- Bean class
- The class performing the actual business process
- Implements an interface based on type of bean
Creating a session/entity EJB
- Programmer needs to create
- EJB home interface
- EJB remote interface
- Implementation of EJB object
- Deployment descriptors
- Edited manually or created by deployment tool
- Automatically generated during deployment
- Implementation of EJB home interface
- Implementation of EJB remote interface
- Stub and skeleton classes
HelloWorld EJB
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface HelloWorld
extends EJBObject
{
public String sayHelloWorld
() throws RemoteException;
}
- EJB home interface
- Define EJB creation methods.
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface HelloWorldHome
extends EJBHome
{
HelloWorld create
() throws RemoteException, CreateException;
}
- EJB object implementation
import javax.naming.*;
import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class HelloWorldBean
implements SessionBean
{
public String sayHelloWorld
() {
return "Hello World from EJB";
}
public HelloWorldBean
() {}
public void ejbCreate
() {}
public void ejbRemove
() {}
public void ejbActivate
() {}
public void ejbPassivate
() {}
public void setSessionContext
(SessionContext sc
) {}
}
- Lookup home object.
- Create remote bean.
- Invoke bean's methods.
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
public class HelloWorldClient
{
public static void main
(String[] args
) {
try {
Context initial =
new InitialContext();
Object objref = initial.
lookup("HelloWorld");
HelloWorldHome home =
(HelloWorldHome
)
PortableRemoteObject.
narrow(objref, HelloWorldHome.
class);
HelloWorld helloBean = home.
create();
String msg = helloBean.
sayHelloWorld();
System.
out.
println();
System.
out.
println(msg
);
} catch (Exception e
) {
System.
err.
println("unexpected exception!"); e.
printStackTrace();
}
}
}
Illustration
Local interfaces
- Local EJB is used by another EJB in the same container
- No need to use RMI remote interface on RMI-IIOP
- Less overhead – better performance!
Bean 1 has only remote interface.
Bean 2 has only local interface.
Bean 3 has both remote & local interfaces.
HelloWorld EJB with local interface
- EJB local interface
- Note: No need to throw RemoteException!
import javax.ejb.EJBLocalObject;
import java.rmi.RemoteException;
public interface HelloWorldLocal
extends EJBLocalObject
{
public String sayIt
();
}
- EJB local home interface
- Note: No need to throw RemoteException!
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBLocalHome;
public interface HelloWorldLocalHome extends EJBLocalHome {
HelloWorldLocal create() throws CreateException;
}
- EJB bean implementation
- Use a bean via local interface. Specify EJB referenced in code on deployment.
import javax.naming.*;
import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class HelloWorldBean
implements SessionBean
{
public String sayHelloWorld
() {
String s =
"";
try {
Context initial =
new InitialContext();
HelloWorldLocalHome localhome =
(HelloWorldLocalHome
)
initial.
lookup("java:comp/env/ejb/LocalHello");
HelloWorldLocal helloBean = localhome.
create();
s = helloBean.
sayIt();
} catch (Exception e
) {
System.
out.
println("unexpected exception!"); e.
printStackTrace();
}
return s;
}
public String sayIt
() {
return "Hello Local World from EJB";
}
public HelloWorldBean
() {}
public void ejbCreate
() {}
public void ejbRemove
() {}
public void ejbActivate
() {}
public void ejbPassivate
() {}
public void setSessionContext
(SessionContext sc
) {}
}
Types of EJB
- Session bean – perform tasks for clients
- Entity bean – represent persistence storage by RDBMS or other data source
- Message-driven bean (MDB) – provide asynchronous processing of requests from clients
- Work with Java Message Service (JMS)
- Less work load on server
Session Bean
- Perform tasks for clients
- Stateless session bean
- No state maintained between method invocations
- Stateful session bean
- Maintain some status between consecutive method invocations for the same client
- Life cycle of a stateless session bean
- Life cycle of a stateful session bean
Entity Bean
- Represent persistence storage by RDBMS or other data source
- Can survive a server shutdown!
- Object-relational mapping
- A class implements a table in RDBMS
- A instance stores data for a row/record – an entity bean
BMP vs. CMP entity beans
- Bean-managed persistence entity bean
- Developer writes JDBC code to read/write data
- Complete control of data access
- May have performance advantage
- Container-managed persistence entity bean
- No need to write JDBC code
- Better portability with no RDBMS specific code
- CMP 1.0 supports remote interface only
- CMP 2.0 supports both remote and local interfaces
- EJB Query Language (CMP 2.0) defines queries
Reference: The Life Cycles of Enterprise Beans
MDB
- Work with Java Message Service (JMS)
- Provide asynchronous processing of requests from clients
- Methods in entity and session beans must return before clients can continue with its job
- MDB client is just a JMS sender program, not knowing the fate of the message after sending – like UDP
- Less work load on server
- New in EJB 2.0 spec
Java EE and EJB Resources
Thanks for Reading
If you would rather like to have this lecture note in printed format, please click the print action link in the top right corner.
If you find any problem in this lecture note, please feel free to reach Steven by steven@findaway.hk