Web Applications / Web Systems
Juan Pavón Mestras
Dep. Software Engineering and Artificial Intelligence
Facultad de Informática
Universidad Complutense Madrid
Material under Creative Commons license, Juan Pavón 2013
Enterprise Beans
Java components that implement Enterprise technology
Java Beans (EJB)
They are executed in an EJB container (in an application server)
They implement the business logic of the application
They are reusable
They can be adapted and configured in the deployment
The EJB container provides various services
EJB lifecycle
Persistence management
Name service
Asynchronous messaging
Access security
Transaction management
Load balancing
Juan Pavón – UCM 2012-13
Enterprise Beans
2
EJB architecture
Client
Client [19459] 004] Client
Enterprise
Bean
Enterprise
Bean
Enterprise
Bean
EJB Container
EJB Server
Juan Pavón – UCM 2012-13
Enterprise Beans
EJB Server
Execution Environment
Process and Thread Management
System Resource Management
Database Connection
Management API
] Juan Pavón – UCM 2012-13
Enterprise Beans
3
4
EJB container
Execution environment of the EJBs
javax.ejb.EJBHome, javax.ejb. EJBObject
javax.ejb.SessionBean
d
to
d
i
r
u
g
e
s
s
e
n
or
i
c
c
to
s
n
to
r
T
SessionBean1
Descriptor of
deployment [19659] 002] MessageBean
Descriptor of
deployment
SessionBean2
Descriptor of
deployment
Access to remote services
Juan Pavón – UCM 2012-13
Enterprise Beans
i
C
i
c
l
or
d
e
v
d
to
r
e
c
u
r
s
or
s
g
e
s
t
i
or
n
d
e
l
e
s
p
to
c
i
or [19659004] d
e
n
or
m
b
r
e
s
J
N
D
I
p
e
r
s
i
s
t
e
n
c
i
to
EJB Clients
They can be servlets or applications
The container of EJBs can control the access of the clients
To locate the EJBs the clients use the Java Naming and
Directory Interface (JNDI)
Access to the EJBs can be done remotely with Java RMI
(Remote Method Invocation)
Juan Pavón – UCM 2012-13
Enterprise Beans
5
6
Enterprise Beans
Implement business logic
] Types (EJB 3.1)
Enterprise Bean Session
• Manage business logic for a client
• Types
• Stateless Session Beans: they do not maintain state between invocations
• Stateful Session Beans: keep state between invocations of the same
client
• Singletons: a single instance that is shared by many clients
Enterprise Bean directed by Messages
• Listener for a particular message (usually through [19659002] of the Java Message Service API)
• Does not maintain state between invocations
Entity Bean
• Representations of data stored in a DB
• Recently it is recommended to use Java Persistence API (JPA)
] Juan Pavón – UCM 2012-13
Enterprise Beans
Enterprise Beans
Much of the functionality is managed by the container
In execution, the container intercepts all calls to the EJB
Executes code related to the multi-threading policy,
transactions, and security
Will call the corresponding method
Performs the corresponding cleaning tasks after the call
Defined by declarations
In the development: annotations in the code
In deployment descriptors
Juan Pavón – UCM 2012-13
Enterprise Beans
7
8
Enterprise Beans
Persistenc ia
The storage of the beans data
• Can be managed by the server: Container-Managed Persistence
• Or the bean itself: Bean-Managed Persistence
Distributed transactions
The server will manage all transactions
It can also be managed directly (ideas must be clear
)
Security
Transmission of data via SSL / RMI
Client authentication and server with SSL
Access control to objects, methods and services with ACLs (lists from
access control)
Multithreading
The multithreading policies to be applied are declared and the server is [19659002] responsible for executing them
The programmer does not have to deal with this aspect
Juan Pavón – UCM 2012-13
Enterprise Beans
9
Interfaces
Initially the EJBs had to implement several interfaces
Since specification 3.0 may be POJO objects but with
annotations
Home interface
Extends the interface javax.ejb.EJBHome
Remote interface to create, find and delete beans
Remote Interface
Extends the interface javax.ejb.EJBObject
Remote interface that defines the concrete methods that the bean will implement
Class Bean
Extends the class javax.ejb.EnterpriseBean
Implements the business logic
Juan Pavón – UCM 2012-13
Enterprise Beans
10
Operation
] Naming Service
Client
RMI
RMI
Written by the programmer
Implements
Invokes
Create / Use
Server
Home Interface
(Factory )
Container
Remote
Interface
EJB Object
(W rapper)
Enterprise
Java Bean
(Biz Logic)
Adapted from jGuru (2000): Enterprise JavaBeans Fundamentals
Juan Pavón – UCM 2012-13
Enterprise Beans
11
EJB Programming
Remote Interface
Extends the interface javax.ejb.EJBObject
Remote interface that defines the concrete methods to be implemented by the
bean
import javax.ejb. EJBObject;
import java.rmi.RemoteException;
public interface Client extends EJBObject {
public Name getName () throws RemoteException;
public void setName (String name) throws RemoteException;
public Address getEmail () throws RemoteException;
public void setEmail (String email) throws RemoteException;
}
Juan Pavón – UCM 2012-13
Enterprise Beans
12
EJB Programming
] Home Interface
Extends the javax.ejb interface. EJBHome
Remote interface for creating, finding and deleting beans
The class that implements it is automatically generated
import javax.ejb.EJBHome;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import java.rmi.RemoteException;
public interface ClientHome extends EJBHome {
public Client create (Integer idClient)
throws RemoteException, CreateException;
public Client findByPrimaryKey ( Integer idCliente)
throws RemoteException, FinderException;
public Enumeration findByName (String name)
throws RemoteException, FinderException;
}
Juan Pavón – UCM 2012-13
Enterprise Beans
] 13
EJB Programming
Bean Class
Extend interface javax.ejb.EJBHome
Remote interface to create, find and delete beans
import javax.ejb.EntityBean; [19659002] public class ClientBean implements EntityBean {
String name;
String email;
public String getName () {
return name;
}
public void setName (String name) {
this.name = name;
}
public Address getEmail () {
return email;
}
public void setEmail (String email) {
this.email = email; [19659002]}
Juan Pavón – UCM 2012-13
}
Enterprise Beans
14
Work environment
To develop, Eclipse or Netbeans
As an application server that has container of EJBs
JBoss: http://www.jboss.org
• There is a version for developers: JBoss Developer Studio (Eclipse
+ JBoss Tools)
Glassfish: https: //glassfish.java.net/en/
An interesting comparison:
http://bit.ly/2AcertM -thou / [19659002] Databases
Apache Derby: http://bit.ly/xylwiI
H2: http://bit.ly/y7HfJr
Juan Pavón – UCM 2012-13
Enterprise Beans
15
First EJB application with Eclipse and JBoss
One possibility is to install JBoss Developer Studio
Another is to configure Eclipse to work with JBoss Tools
Help Eclipse Marketplace
Search: JBoss
Select JBoss Tools for the current eclipse version (Juno)
• Only Java Development Tools and JBoss
AS Tools
can be selected • Other plugins can be selected install later if necessary
When finished, reinitialize eclipse
As BD you can install Derby, very efficient and written in Java
Install the plugins Derby 10 Core Plug-in and Derby 1.0 UI [19659002] • Download them from http://bit.ly/zs0tO3
• Y desco write them in the eclipse installation folder
Alternative: Eclipse Data Tools Platform
http://bit.ly/2A8Xgt2
Juan Pavón – UCM 2012-13
] Enterprise Beans
16
First EJB application with Eclipse and JBoss
Install JBoss
http://bit.ly/2SbQmKy /
jboss-as-distribution-6.0.0.Final.zip/download
Add the JBoss server in eclipse
In Servers or File New Other Server Server
Add JBoss AS 6.x
• Indicate the directory where the JBoss was unpacked
• To start the server, select it and with the context menu
choose Start
Create a new EJB project
] New EJB Project
• Indicate that the JBoss server will be used in Target Runtime
Create a Java class that will be the first EJB
Create a package (en.ucm.jpavon.ejb)
In ejbModule create the first EJB with New Session Bean (EJB 3.x)
Juan Pavón – UCM 2012-13 [19659002] Enterprise Beans
17
First EJB application with Eclipse and JBoss
package es.ucm.jpavon.ejb;
import javax.ejb.Remote;
import javax.ejb.Stateless;
Indicates that the POJO is a
Bean stateless so that the
container treats it as
an EJB
@Remote
@Stateless
public class PrimerBean implements PrimerBeanRemote {[19659002] public String greets (String name) {
return "Hello" + name;
Having declared @Remote
eclipse adds the interface
}
}
is generated at
] have declared
the class as
Remote
package es.ucm.jpavon.ejb;
import javax.ejb.Remote;
@Remote
public interface PrimerBeanRemote {[19659002] public String salutes (String name);
}
We must declare the
methods that go to
offer the Bean
Juan Pavón – UCM 2012-13
Enterprise Beans [19659002] 18
First EJB application – Client
The client can be another application
Create a new Java project in eclipse
So you can use the settings of the previous project (and
access the corresponding libraries of EJB)
In the project, select Build Path Configure Build Path
Projects tab, Add button, select the previous one
Create the client program class
Execute as an ap
from Nettech Post http://bit.ly/2A8Xh04
No hay comentarios:
Publicar un comentario