J2EE Concepts :

advertisement
J2EE Concepts
Why J2EE ?
Client
Client
Server
Client
Server
Database
J2EE Concepts
2
Why J2EE ?
Things to consider when building large business systems :
 Load Balancing
 Transparent fail-over
 Back end integration
 Transactions
 Threading
Middleware
 Dynamic Deployment
 Object Life cycle
 Resource pooling
 Caching
 Security
J2EE Concepts
3
J2EE Concepts :
Definition :
The Java 2 Enterprise Edition (J2EE) is a multi-tiered
architecture for implementing enterprise-class
applications and web based applications
Model :
It is based on component based application model. In
this model such components use services provided by
the container which would otherwise typically need to
be incorporated in the application code.
J2EE Concepts
4
J2EE Components
Application clients and applets are components that
run on the client.
Java Servlet and JavaServer Pages technology
components are Web components that run on the
web server. (Web Container)
Enterprise JavaBeans components (enterprise beans)
are business components that run on the application
server. (EJB Container)
J2EE Concepts
5
J2EE Components -- continued
J2EE Concepts
6
J2EE Containers
Definition :
Containers are the interface between a component
and the low-level platform-specific functionality that
supports the component.
Types :
 Enterprise JavaBeans (EJB) container
 Web container
 Application client container
 Applet container
J2EE Concepts
7
J2EE Container -- continued
J2EE Concepts
8
J2EE APIs













Enterprise JavaBeans Technology
JDBC API
Java Servlet Technology
Java Server Pages Technology
Java Message Service (JMS)
Java Naming and Directory Interface (JNDI)
Java Transaction API (JTA)
Java Mail API
Java API for XML Processing (JAXP)
Java API for XML Registries (JAXR)
Java API for XML-Based RPC (JAX-RPC)
SOAP with Attachment API for Java (SAAJ)
Java Authentication and Authorization Service
J2EE Concepts
9
EJB Container
Isolates the enterprise bean from direct access by
client applications.
A container may provide the following services:
 Lifecycle management
 Transactions
 Security
 Connection pooling
 Instance pooling
J2EE Concepts
10
J2EE Application Packaging
A J2EE application is delivered in an Enterprise Archive
(EAR) file with an extension “.ear”
EAR file is a standard Java Archive file (JAR) with an
extension “.ear”
It consists of multiple J2EE modules.
J2EE Module => one or more J2EE components for the
same container type + component deployment descriptor.
Deployment Descriptor => an XML document with an
.xml extension that describes a component's deployment
settings.
J2EE Concepts
11
Enterprise JavaBeans
Definition :
Enterprise Javabeans (EJBs) standard is the
component architecture for deployable server side
components in Java.
Values :
 It is Agreed Upon by the industry
 Portability is easier
 Rapid application development
J2EE Concepts
12
Roles In EJB
Enterprise Bean provider
creates and sells EJBs
Application assembler
uses EJBs to build an application
EJB Server provider
creates and sells EJB server
EJB Container provider
creates and sells EJB containers
server provider will likely provide containers
J2EE Concepts
13
EJB Architecture
EJB SERVER
EJB Container
Enterprise
Bean
EJB CLIENT
J2EE Concepts
14
Types Of Beans
Session Beans
Entity Beans
1. Interact with Client
1. Represent persistent data
2. Model Business logic
3. Do not survive system 2. Survive system shutdown
shutdown
J2EE Concepts
15
Session Beans
Stateless
Stateful
1. No Internal state
2. Do not need to be
1. Possess internal state
2. Need to be
passivated
3. Lifetime is controlled
by the container
passivated/activated
3. Assigned to one client
for a lifetime
J2EE Concepts
16
Entity Beans
Container Managed
Bean Managed
1. Container is responsible
1. Bean is responsible for
for saving state.
2. In dd, specify container
managed fields
3. Persistence is data
store independent.
saving state
2. Container doesn’t
need to generate DB
calls
3. Less Adaptable :
Persistence is hard
coded in the code.
J2EE Concepts
17
What constitutes an Enterprise Bean ?
1. Enterprise Bean Class –
Implements the methods defined in the
following interfaces.
 Must implement either javax.ejb.SessionBean
or javax.ejb.EntityBean
Home Interface –
 Define methods for creating, destroying and
finding EJB objects.
 Must extend javax.ejb.EJBHome interface

2.
J2EE Concepts
18
What constitutes an Enterprise Bean ?
3. Remote Interface –
Defines business logic functions that the
corresponding bean class implement.
 Must extend javax.ejb.EJBObject interface.
Deployment Descriptor –
An XML file that specifies information about the
bean such as type of the bean, persistence etc.
Utility/Helper Classes –
Vendor Specific files. (jboss.xml & jaws.xml)

4.
5.
6.
J2EE Concepts
19
Writing a Session Bean
Home Interface :
import
import
import
import
java.io.Serializable;
java.rmi.RemoteException;
javax.ejb.CreateException;
javax.ejb.EJBHome;
public interface CartHome extends EJBHome {
Cart create(String person) throws RemoteException, CreateException;
Cart create(String person, String id) throws RemoteException,
CreateException;
}
J2EE Concepts
20
Writing a Session Bean – continued
Remote Interface :
The remote interface, which extends javax.ejb.EJBObject,
defines the business methods that a remote client may invoke.
import java.util.*;
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface Cart extends EJBObject {
public void addBook(String title) throws RemoteException;
public void removeBook(String title) throws BookException,
RemoteException;
public Vector getContents() throws RemoteException;
}
J2EE Concepts
21
Writing a Session Bean – continued
Bean Class :
public class CartBean implements SessionBean {
String customerName;
String customerId;
Vector contents;
public void ejbCreate(String person) throws CreateException {
if (person == null) {
throw new CreateException("Null person not allowed.");
} else {
customerName = person;
}
customerId = "0";
contents = new Vector();
}
J2EE Concepts
22
Writing a Session Bean – continued
public void ejbCreate(String person, String id) throws CreateException {
if (person == null) {
throw new CreateException("Null person not allowed.");
} else {
customerName = person;
}
IdVerifier idChecker = new IdVerifier();
if (idChecker.validate(id)) {
customerId = id;
} else {
throw new CreateException("Invalid id: "+ id);
}
contents = new Vector();
}
J2EE Concepts
23
Writing a Session Bean – continued
public void removeBook(String title) throws BookException {
boolean result = contents.removeElement(title);
if (result == false) {
throw new BookException(title + "not in cart.");
}
}
public void addBook(String title) {
contents.addElement(title);
}
public Vector getContents() {
return contents;
}
J2EE Concepts
24
Writing a Session Bean – continued
public CartBean() {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext sc) {}
}
J2EE Concepts
25
Writing a Session Bean – continued
Helper Classes
The CartEJB session bean has two helper classes:
BookException and IdVerifier. The BookException is thrown by
the removeBook method and the IdVerifier validates the
customerId in one of the ejbCreate methods.
J2EE Concepts
26
Writing a Session Bean – continued
Deployment Descriptor (ejb-jar.xml)
<?xml version="1.0" encoding="Cp1252"?>
<ejb-jar>
<description>jBoss test application</description>
<display-name>Test</display-name>
<enterprise-beans>
<session>
<ejb-name>CartBean</ejb-name>
<home>cartHome</home>
<remote>Cart</remote>
<ejb-class>CartBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Bean</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
J2EE Concepts
27
Session Bean Client
public static void main(String[] args) {
try {
InitialContext jndiContext = new InitialContext();
Object ref = jndiContext.lookup(“ejb/CartBean");
// Get a reference from this to the Bean's Home interface
CartHome home = (CartHome)
PortableRemoteObject.narrow (ref, CartHome .class);
Cart cart = home.create();
} catch(Exception e) {
System.out.println(e.toString());
}
}
J2EE Concepts
28
Writing an Entity Bean (CMP)
Let’s write a entity bean CD that’s represents a music CD. It
contains attributes like title code id and other properties of a
music CD.
Remote Interface :
public interface CD extends EJBObject {
public Integer getId() throws RemoteException;
public void setId(Integer id) throws RemoteException;
public String getTitle() throws RemoteException;
public void setTitle(String title) throws RemoteException;
public String getArtist() throws RemoteException;
public void setArtist(String artist) throws RemoteException;
public String getType() throws RemoteException;
public void setType(String type) throws RemoteException;
public String getNotes() throws RemoteException;
public void setNotes(String type) throws RemoteException;
}
J2EE Concepts
29
Writing an Entity Bean -- continued
Home Interface :
/** * This interface defines the home interface for the CD Bean */
public interface CDHome extends EJBHome {
public CD create(Integer id) throws RemoteException,
CreateException;
public CD findByPrimaryKey (Integer id) throws RemoteException,
FinderException;
public Collection findByType (String type) throws RemoteException,
FinderException;
public Collection findAll() throws RemoteException, FinderException;
}
J2EE Concepts
30
Writing an Entity Bean -- continued
Bean Class :
public abstract class CDBean implements EntityBean {
public CDBean() {}
public void setEntityContext( EntityContext entityContext ) throws EJBException {}
public void unsetEntityContext() throws EJBException {}
public void ejbRemove() throws RemoveException, EJBException {}
public void ejbActivate() throws EJBException {}
public void ejbPassivate() throws EJBException{}
public void ejbLoad() throws EJBException{}
public void ejbStore() throws EJBException{}
public abstract Integer getId();
public abstract void setId( Integer id );
public abstract String getTitle();
public abstract void setTitle( String title );
public abstract String getType();
public abstract void setType( String type );
}
J2EE Concepts
31
Writing an Entity Bean -- continued
Deployment Descriptor (ejb-jar.xml)
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar>
<display-name></display-name>
<enterprise-beans>
<entity>
<ejb-name>CDEJB</ejb-name>
<home>CDHome</home>
<remote>CD</remote>
<ejb-class>CDBean</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Integer</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
J2EE Concepts
32
Writing an Entity Bean -- continued
<cmp-field>
<field-name>id</field-name>
</cmp-field>
<cmp-field>
<field-name>title</field-name>
</cmp-field>
<cmp-field>
<field-name>type</field-name>
</cmp-field>
<cmp-field>
<field-name>artist</field-name>
</cmp-field>
<primkey-field>id</primkey-field>
</entity>
</enterprise-beans>
</ejb-jar>
J2EE Concepts
33
Writing an Entity Bean -- continued
Jboss.xml
<?xml version="1.0" encoding="UTF-8"?>
<jboss>
<enterprise-beans>
<entity>
<ejb-name>CDBean</ejb-name>
<jndi-name>cd/CD</jndi-name>
</entity>
</enterprise-beans>
</jboss>
J2EE Concepts
34
Writing an Entity Bean -- continued
JAWS – Just Another Web Store
JAWS is the O/R mapper used by JBoss to manage CMP entity
beans.
JAWS is configured in a file named standardjaws.xml, located in
the conf/config-name directory in the JBoss distribution. This file
configures JAWS for all JBoss.
What can you do with JAWS.xml ?
Specify a datasource and the type-mappings to use with it
Set a bunch of options concerning jaws behavior
Specify how JAWS should build/use your tables
Define finders to access you entity beans
Define a type mapping
J2EE Concepts
35
JAWS
<jaws>
<datasource>java:/MySqlDS</datasource>
<type-mapping>mySQL</type-mapping>
<enterprise-beans>
<entity>
<ejb-name>CDBean</ejb-name>
<table-name>CLASS</table-name>
<create-table>false</create-table>
<cmp-field>
<field-name>id</field-name>
<column-name>ID</column-name>
</cmp-field>
</entity>
</enterprise-beans>
<jaws>
J2EE Concepts
36
JAWS
<jaws>
<enterprise-beans>
<entity>
<ejb-name>ClassBean</ejb-name>
...
<cmp-field>
<field-name>title</field-name>
<column-name>Title</column-name>
<jdbc-type>VARCHAR</jdbc-type>
<sql-type>VARCHAR(100)</sql-type>
</cmp-field>
...
</entity>
</enterprise-beans>
</jaws>
J2EE Concepts
37
Download