Enterprise Java Beans 3.0
by Sandeep Desai (http://www.thedesai.net)
EJB 3 uses Java Persistence API
Java Persistence API can be used in Java Application does
not need J2EE Server like EJB 2
Oracle JDeveloper 10.1.3.1 includes support for creating
Java SE EJB3 applications
All annotations have an XML equivalent
Message Bean
- Handle
message concurrently one bean per message
- Supports
asynchronous messaging using JMS and Message Oriented Middleware
- JMS
API can send messages using products like Progress SonicMQ, IBM MQSeries,
Oracle Advance Queuing
- Messaging
can use JCA 1.5 to connect to any messaging source e.g email
- @javax.ejb.MessageDrivenBean
- implements
javax.jms.MessageListener
Interface
- implement
onMessage() method
- can
interact with Local SessionBean,
cannot be tagged remote
Entity Bean
- Entity
Bean uses @javax.Persistence.Entity,
- Primary
Key uses @javax.Persistence.Id
(can be class or primitive type)
- Should
only have validation business logic
- Table
Mapping can be in annotation or XML (XML overrides annotation)
- Managed
(attached) or Unmanaged (detached)
- EntityManager flushes changes to
Entity Bean after
Session Bean
- Support
one client at a time
- @javax.ejb.Stateful or @javax.ejb.Stateless
- @javax.ejb.Remote or @javax.ejb.Local
- Contains
Business logic (e.g. transfer funds from once account to another)
- Can be
Web Service endpoint interface @javax.jws.WebService
- default
transaction is required
- default
security is unchecked
- Stateful
lifecycle
- Passivation
(Serialize state) @javax.ejb.PrePassivate
call method before passivation (e.g close files if any)
- Activation @javax.ejb.PostActivate
(e.g open files if any)
- Stateless
lifecycle
- No
State (note created)
- Pooled
State
- Ready
State (@javax.ejb.EJBContext
interface is injected)
Persistence Unit (Persistence.xml)
- located
in META-INF directory
- can
be in plain jar, EJB-JAR or JAR in
EJB-JAR or JAR in WAR file or JAR in EAR file
- defines
identities and properties of each named persistence unit
persistence.xml
<?xml version="1.0"
encoding="windows-1252" ?>
<persistence
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="ejb3test">
<class>net.thedesai.ejb3.Dept</class>
<class>net.thedesai.ejb3.Emp</class>
<properties>
<property name="toplink.jdbc.driver"
value="oracle.jdbc.OracleDriver"/>
<property
name="toplink.jdbc.url"
value="jdbc:oracle:thin:@localhost:1521:ORCL"/>
<property
name="toplink.jdbc.user" value="scott"/>
<property
name="toplink.jdbc.password"
value="3E20F8982C53F4ABA825E30206EC8ADE"/>
<property
name="toplink.target-database" value="Oracle"/>
<property
name="toplink.logging.level" value="FINER"/>
</properties>
</persistence-unit>
</persistence>
Persistence Context
- set
of managed entity object instances. Peristence Contexts are managed by
Entity Manager
- When
persistence context closed, entity beans are detached
- two
types of persistence contexts
- transaction
scoped (Persistence Context
destroyed at end of transaction and Entity Beans become unmanaged)
- extended
persistence context used for long running transactions (detached Entity
Bean can be serialized and sent over network)
transaction scoped example requires PersistenceContext to be
injected
@PersistenceContext(unitName=”foo”)
EntityManager entityManager;
@TransactionAttribute(REQUIRED)
public void updateEmp() {
Employee emp =
entityManager.find(Employee.class, 1);
emp.setSalary(1234567);
}
Extend Persistence Context example
Employee emp = null;
transaction.begin();
emp = extendedEntityManager.find(Employee.class, 1);
transaction.commit();
transaction.begin();
emp.setSalary(7654321);
extendedEntityManager.flush();
transaction.commit();
Client Applications
- Never
access Beans directly only through Proxy
EJB Servers (Primary Services)
- Concurrency
(don’t create threads or use synchronized)
- Transaction
management,
- Persistence:
1.
EntityManager
provides method to create, find, query, remove and update beans
2.
can attach and detach instance, detached instance can be sent
across network
3.
manage m:n relationship
4.
manage collections
5. @Entity @Table(name=”EMP”) public class Emp {
6. private int id, private String name;
7. @Column(name=”NAME”) ..get() set() .. @Id @Column(name=”ID”) }
- Object
distribution, support RMI-IIOP and SOAP 1.2 using JAX-RPC
1.
Any SOAP Client e.g. .NET can access stateless session bean
- Asynchronous
Messaging. Server will attempt to redeliver message on failure. Session,
Message and Entity Bean can send message
- EJB
Timer Service (e.g. mortgage payment past due) can be set on all Beans,
can be used for batch processing also
- Naming
(JNDI)
1.
javax.naming.Context c = new javax.naming.InitialContext();
2.
Object ref = c.lookup(“FooRemote);
3.
FooRemote f = PortableRemoteObject.narrow(ref,
FooRemote.class);
4.
f.doSomething(...);
- Security:
Authentication, Authorization (access control), Secure Communication
(support SSL 3.0 and Transport Layer Security 1.0)
-
- supports
instance pooling
Limitations
- Supports
collection of entities only does not support primitive collections such as
Set<Integer>
Books:
Links