Java EE 7 tutorial
EJB 3.2 specification
Note, according to some Internet articles and my understanding, CDI is in favour of local session bean. Use annotation stereotypes for transaction / security.
implement business logic
enjoy services provided by container
types:
session
stateful
stateless
singleton
message-driven
can be invoked over
local
remote
web service
@stateful
contains state represents conversational state
not shared, have only one client
client terminate, its beans appears to terminate and no longer associates with client
client removes bean, session ends and state disappears
can NOT implement web service
@stateless
state only for the duration of invocation
can be pooled, all instances are equivalent
better scalability
can implement a web service
choose if
no data for specific client
in a single method invocation performs a generic task for all clients
@singleton
instanced per application
can implement web service endpoint
state not across server crashes or shutdowns
can be created upon server @Startup
manage concurrency with @ConcurrencyManagement
see more on container managed concurrency
choose if
state needs to be shared across application
needs to be concurrently accessed by multiple threads (thread safety)
to perform tasks on application startup / shutdown
Accessing Session Bean
no-interface view:
annotate class that DO NOT IMPLEMENT ANY INTERFACE (otherwise confuses server)
exposes public method to clients
business interface
one bean can have more than one
bean should (not required) to implement business interface(s)
client access
injection
within container
@EJB annotation, more see "EJB Reference"
JNDI lookup
can be used outside container
three namespaces
java:global - finding remote EJB "java:global[/application name]/module name /enterprise bean name[/interface name ]
java:module - find local EJB within same module "java:module/enterprise bean name/[interface name]"
java:app - find EJB within same application (within EAR) "java:app[/module name]/enterprise bean name [/interface name]"
define local access
no-interface view is local
@Session
@Local interface, or no decoration (default), is local
Examples:
@Stateful public class MyBean {...} - local through no-interface
@Local public interface InterfaceName {...} - annotate interface
@Local(InterfaceName.class) public class BeanName implements InterfaceName - annotate implementation
client local access
@EJB ExampleBean exampleBean
ExampleBean exampleBean = (ExampleBean)InitialContext.lookup("java:module/ExampleBean")
define remote access
@Remote public interface InterfaceName{...}
@Remote(InterfaceName.class) public BeanName implements InterfaceName{...}
client remote access
@EJB Example example;
ExampleRemote example = (ExampleRemote)InitialContext.lookup("java:global/myApp/ExampleRemote");
web service clients (skipped for now)
Bean Provider:
@EJB annotation contains following elements:
name - name by which resource is to be looked up in environment (logical name, e.g. "ejb/EmplRecord")
beanInterface - referenced interface type; can be business interface, session bean's no-interface view, or local home / remote home interface
beanName - references the value of the name element of the Stateful or Stateless annotation (or ejb-name element); allows disambiguation if several session beans implement the same interface
mappedName - product specific; using may not be portable
lookup - JNDI name for the target component (beanName or lookup cannot be both used)
If no explicit linking information provided and there is only one session bean within the same application that exposes the matching client view type, by default the EJB dependency resolves to that session bean.
process messages asynchronously
normally a JMS message listener
resembles a stateless session bean
retain no client data
all instances are equivalent
can contain some state such as
JMS API connection
open database connection
reference to EJB
invoked asynchronously
relatively short-lived
can be transaction-aware
stateless
message can be delivered in a transaction context: if operation in onMessage rolled back, message will be redelivered
Isolation
remote calls work on different copies of parameter object (by copy, not by reference)
local call work by reference
Granularity
remote calls are likely slower, should use relatively coarse-grained parameters
local calls are ok for fine-grained
Bean class: implement business methods and callback methods (naming convention: nameBean, also for bean name)
Business interfaces (naming convention: name)
Helper classes
Stateful bean:
None-existence <-> Ready <-> Passive
@PostConstruct
@PrePassive
@PostActive
@PreDestroy
@Remove - client remove bean instance
Stateless bean / Singleton:
None-existence <-> Ready
@PostConstruct
@PreDestroy
Message Driven Bean:
None-existence <-> Ready (onMessage)
@PostConstruct
@PreDestroy
Bean Dependency
@DependsOn("AnotherBean","YetAnotherBean")
system exception
bean throw a javax.ejb.EJBException (need not declare in "throws")
container might destroy bean instance
cannot be handled by client
container rolls back transaction
application exeption
client should be able to handle
container does NOT roll back transaction
Skipped
The bean is a proxy and is not "this". Using "this" will not have added features such as transaction, because "this" is the not decorated bare class. Obtain reference to a business object with SessionContext