Enterprise JavaBeans
description
Transcript of Enterprise JavaBeans
![Page 1: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/1.jpg)
Enterprise JavaBeans
Introduction and architecture
![Page 2: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/2.jpg)
The beginning - Applets
• One reason the Java initially gained
popularity was its support for
downloadable Java programs known
as applets
• Java applets were first introduced in 1995
• Applet executes only on the "client" platform environment of a system
![Page 3: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/3.jpg)
Java on the server
• Recognizing the potential for Java as a server language in Web environments, Sun Microsystems wrote the Java Servlet specification
• Java servlet specification was finalized in 1997
• Servlets are Java programs designed to run on Web server machines
![Page 4: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/4.jpg)
Servlets
• Java servlets are best suited as a middle tier component connecting front-end Web requests with back-end data resources
• However, servlets alone do not provide a sufficient model for true enterprise computing
![Page 5: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/5.jpg)
What is Enterprise Computing?
• What is an enterprise?• Large organization such as multinational corporation,
university, hospital, research laboratory, or government organization
• Requires special computing solutions because of its size
• Enterprise computing - use of computers in networks that encompass variety of operating systems, protocols, and network architectures
![Page 6: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/6.jpg)
Three-tier architecture
• Emerged in the 1990s to overcome the limitations of the two-tier architecture
• The middle tier
supports application
server software, a
functional extension
of the Web server
Presentationtier
Business logic tier
Data tier
![Page 7: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/7.jpg)
Advantages of multi-tier model
• Scalability (mērogojamība)
• Better re-use
• Improved data integrity
• Improved security
• Reduced distribution
• Improved availability
• Hidden database structure
![Page 8: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/8.jpg)
Java Platform Enterprise Edition
• Java EE technology aims to extend the reach of the Java platform to large-scale server environments
• Industry standard for developing portable, robust, scalable, distributed and secure multi-tier server-side Java applications
• The Enterprise JavaBeans specification is one of the several Java APIs in the Java EE
![Page 9: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/9.jpg)
Enterprise JavaBeans
• EJB is a server-side component that encapsulates the business logic of an application
• Standard way to implement the "business" code typically found in enterprise applications
![Page 10: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/10.jpg)
Motivation
• Solutions to common problems are often repeatedly re-implemented by programmers
• EJB were intended to handle such common concerns as:• persistence • transactional integrity• security
in a standard way, leaving programmers free to concentrate on the particular problem
![Page 11: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/11.jpg)
EJB goals
• To be the standard component architecture for building distributed object-oriented business applications
• To make it easy to write applications: developers will not have to understand • low-level transaction and state management details
• multi-threading
• resource pooling
• and other complex low-level APIs
![Page 12: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/12.jpg)
EJB goals
• To follow the "Write Once, Run Anywhere" philosophy of the Java programming language• EJB can be developed once and then deployed on
multiple platforms without recompilation or source code modification
• To address the development, deployment, and run-time aspects of an enterprise application's life cycle
![Page 13: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/13.jpg)
EJB goals
• To define the contracts that enable tools from multiple vendors to develop and deploy components that can interoperate at run time
• To provide interoperability between EJBs and non-Java programming language applications
• To be compatible with• existing server platforms• other Java programming language APIs• CORBA (Common Object Request Broker
Architecture)
![Page 14: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/14.jpg)
Benefits of using EJBs
• EJBs make it simpler to write applications • EJB container is charged with the task of making system
services available to EJB components
• Component portability • A simple, elegant component container model• Java server components can be developed once and
deployed in any EJB-compliant server
• Architecture independence• Independent of any specific platform, proprietary
protocol, or middleware infrastructure
![Page 15: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/15.jpg)
Benefits of using EJBs
• Built-in support for typical enterprise-level system services:• distributed objects• transactions• database• security• global naming
• Developer productivity • Standardization and automation of complex infrastructure
services • Developers can create complex applications by focusing on
business logic rather than environmental issues
![Page 16: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/16.jpg)
EJB history
• The EJB specification was originally developed in 1997 by IBM and later adopted by Sun Microsystems
• EJB 1.1 ( J2EE 1.2 ), 1999• Session Beans (stateless & stateful), Entity Beans
• Remote interface
• EJB 2.0 ( J2EE 1.3 ), 2001• Message-Driven Beans
• Entity Beans 2.x and EJB QL
• Local and Remote interfaces
![Page 17: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/17.jpg)
Adoption and criticism
• Enterprise JavaBeans were quickly adopted by large companies
• Problems were quick to appear and the reputation of EJBs began to suffer
• APIs of the standard were too complex and counter-intuitive• required interfaces• checked exceptions• deployment descriptors
![Page 18: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/18.jpg)
Adoption and criticism
• Businesses found that using EJBs to encapsulate business logic brought a performance penalty• Original specification only allowed for remote
method invocation
• Long development cycle
• Tools made it easy to create and use EJBs by automating most of the repetitive tasks...
• But tools did not make it easier to learn how to use the technology!
![Page 19: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/19.jpg)
EJB history
• EJB 2.1 ( J2EE 1.4 ) 2003• EJB Timer Service
• EJB Web Service Endpoints
• Minor EJB QL enhancements
• Reinventing EJBs• The functionality delivered by simpler frameworks
like Spring and Hibernate was more useful to enterprise applications
• EJB 3.0 specification was a radical departure from its predecessors
![Page 20: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/20.jpg)
EJB history• EJB 3.0 ( Java EE 5.0 ) 2006
• Many improvements to its predecessor!
• Metadata annotations configuration by exception
• A higher degree of control over bean persistence
• Much more simplified programming model for developing EJBs
EJB 2 vs. EJB 3
![Page 21: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/21.jpg)
EJB 3.1
• Status: In Progress
• Now in “Final Approval Ballot” stage:• Start: 17 Nov, 2009
• Finish: 30 Nov, 2009
• The purpose of the EJB 3.1 specification is to further simplify the EJB architecture by reducing its complexity from the developer's point of view, while also adding new functionality in response to the needs of the community
![Page 22: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/22.jpg)
EJB specification
JSR-000220 Enterprise JavaBeans 3.0 Final Release:http://jcp.org/aboutJava/communityprocess/final/jsr220/index.html
Consists of three documents:
• EJB 3.0 Simplified API (59 pages)
• Java Persistence API (256 pages)
• EJB Core Contracts and Requirements (562 pages)
![Page 23: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/23.jpg)
Overview – EJB in Java EE
http://java.sun.com/javaee/5/docs/tutorial/doc/bnabo.html
![Page 24: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/24.jpg)
Types of EJBs
• There are actually three kinds of EJBs: • session beans
• entity beans
• message-driven beans
• Session beans• Implement the business logic of an application
• Can be Stateless or Stateful
• Entity beans• Are persisted in some data store
![Page 25: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/25.jpg)
Session and Entity beans
In a typical scenario, the UI calls the methods of the session beans. Session beans can call other session beans and entity beans.
![Page 26: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/26.jpg)
EJB 2.x: classes and interfaces
To implement an EJB, one needs to define:
• Remote interface• Defines the business methods a bean presents to the
outside world
• Home interface• Defines the bean's life cycle methods: create,
remove, find
• Bean class• Actually implements the bean's business methods
![Page 27: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/27.jpg)
Example: EJB 2.x implementation
public interface TestSessionBean extends javax.ejb.EJBObject{
public String sayHello()
throws java.rmi.RemoteException;
}
public interface TestSessionBeanHome extends
javax.ejb.EJBHome{
public TestSessionBean create()
throws javax.ejb.CreateException, java.rmi.RemoteException;
}
![Page 28: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/28.jpg)
Example: EJB 2.x implementation
public class MyTestSessionBean implements SessionBean{
public void ejbCreate() throws CreateException { } public void setSessionContext(SessionContext
aContext) throws EJBException { }
public void ejbActivate() throws EJBException { } public void ejbPassivate() throws EJBException { } public void ejbRemove() throws EJBException { }
public String sayHello(){ String msg="Hello! I am Session Bean"; System.out.println(msg); return msg; }}
![Page 29: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/29.jpg)
Example: EJB 3 implementation
@Remote
public interface TestSessionBean {
public String sayHello();
}
@Stateless
public class MyTestSessionBean implements TestSessionBean {
public String sayHello(){
String msg="Hello! I am Session Bean";
System.out.println(msg);
return msg;
}
}
![Page 30: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/30.jpg)
EJB server and container
• The EJB server provides an environment that supports the execution of applications developed using EJBs
• An EJB server manages and coordinates the allocation of resources to the applications
• The EJB server must provide one or more EJB containers
• An EJB container manages the enterprise beans contained within it
![Page 31: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/31.jpg)
EJB container
For each enterprise bean, the container is responsible for • registering the object
• providing a remote interface for the object
• creating and destroying object instances
• checking security for the object
• managing the active state for the object
• coordinating distributed transactions
• (optionally) manage all persistent data within the object
![Page 32: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/32.jpg)
Packaging Java EE applications
• A Java EE application is delivered in an Enterprise Archive (EAR) file
• An EAR file contains Java EE modules and deployment descriptors
![Page 33: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/33.jpg)
EJB module
• An EJB module is used to assemble one or more EJBs into a single deployable unit
• An EJB module is stored in a standard Java archive (JAR) file
• Directory structure:
![Page 34: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/34.jpg)
Creating directory structure with Maven
• Execute a command:
• Develop EJBs in /ejbs/ folder
mvn archetype:create -DgroupId=com.web.ejb
-DartifactId=ejb-jboss-demo
-DarchetypeArtifactId=maven-archetype-j2ee-simple
![Page 35: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/35.jpg)
Bug related to “site” module
• There is a known bug regarding Maven’s J2EE archetype:
http://jira.codehaus.org/browse/ARCHETYPE-67
• If you inspect such an error when trying to build your application, then comment out “site” module from root pom.xml <modules>
<module>projects</module> <module>primary-source</module> <module>servlets</module> <module>ejbs</module> <module>ear</module> <!--module>site</module--> </modules>
![Page 36: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/36.jpg)
Developing and packaging EJBs
• To work with \ejbs\ module in Eclipse separately do the following:• From project root execute “mvn install”
• From \ejbs\ folder execute “mvn eclipse:eclipse”
• Import “ejbs” project into Eclipse
• To package EJBs execute “mvn package” from \ejbs\ folder
• Result: EJB module is created:
\ejbs\target\ejbs-1.0.jar
![Page 37: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/37.jpg)
Deploying EJBs
• You can deploy JAR file with EJBs as a standalone module
OR
• Deploy the full EAR
\my_project\ear\target\ear-1.0.ear
![Page 38: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/38.jpg)
References
• Presentation about Enterprise Computing
http://joung.im.ntu.edu.tw/teaching/bcc/2004/Chapter14EnterpriseComputing_s.pdf
• Enterprise JavaBeans Technology http://java.sun.com/products/ejb/
• EJB 3.0 in a nutshell http://www.javaworld.com/javaworld/jw-08-2004/jw-0809-ejb.html
![Page 39: Enterprise JavaBeans](https://reader036.fdocuments.us/reader036/viewer/2022062410/568152eb550346895dc10a2a/html5/thumbnails/39.jpg)
References
• Spring Vs. EJB 3.0 http://www.onjava.com/pub/a/onjava/2005/06/29/spring-ejb3.html
• Creating a simple J2EE Project with Maven
http://maven.apache.org/plugins/maven-archetype-plugin/examples/j2ee-simple.html
• Getting Started with EJB 3.0http://www.devx.com/Java/Article/30045/0/page/1