Different types of EJB's in java
=======================
1) Session bean – suitable for situation where we have business method call and client application data. Different Types of session Beans
a) Stateless Session Bean (@Stateless)
No client specific state
It uses pooling (ejb container)
Highly Scalable
An instance can serve any request
State is not maintained between calls
b) Stateful Session Bean (@Stateful) – suitable for business methods and client applications maintain client specific state, One instance per client session.
State persists across method calls. Can be passivated and activated.
Example shopping cart of retail app user.
c) Singleton session bean (@singleton) – suitable for situation where once instance per application shared by all users. It requires thread safety. Example – an ejb cleaning up and modifying cache of an application or making configuration changes in application.
2) Message Driven Bean(@MessageDriven)
Triggered by JMS Queue/topic during message processing
No direct Client calls.
3) Entity bean – Old EJB 2.x Approach, Used with JPA entity classes.Entity Beans were the persistence mechanism in EJB 2.x. They are not supported in Liberty and have been replaced by JPA entities in modern Jakarta EE applications.
Note - For EJB 2.x implementation, annotations (using @stateful ,@Statless etc .) are not applicable .It will be discussed later in bog .
Lifecycle Methods Invoked by Container (During EJB lifecycle)
==============================================
There are container managed call backs during EJB life cycle. These are not initiated from application code side, liberty container calls them. Life cycle methods are not same for all EJB type.
Common to most EJBs
-----------------------------
@PostConstruct – After bean creation /dependency Injection before first use.
@PreDestroy -- Before bean is destroyed.
Stateful bean -specific
---------------------------
@PrePassivate -- Before bean is passivated (moved out of memory)
@PostActivate -- After bean is activated (restored to memory)
@Remove. -- Called by client → signals end of lifecycle
Definition of passivation & activation
Passivation = container saving bean to disk/memory optimization
Activation = restoring it back
Transaction-related (Stateful only)
--------------------------------------------------
These are methods in case EJB 2.X and annotation in case of EJB 3.X
AfterBegin. -- After transaction starts
BeforeCompletion -- Before transaction commit
AfterCompletion -- After commit/rollback
Interceptor lifecycle (all types)- provide hooks through api request that modify request, marshall etc
-----------------------------------------------------------------------------------------------------------------
@AroundInvoke -- Wraps method invocation (before & after business method)
@PostConstruct (Interceptor) -- Intercept lifecycle
@PreDestroy (Interceptor) -- Cleanup hook
Major difference between EJB2.X and EJB 3.X implementation.
===============================================
EJB2X implementation uses below interfaces.
EJBHome Interface is factory interface used for creating bean, removing beans and proving metadata .
EJBObject is remote business interface. Once EJB is created all business methods are invoked using this interface.
EJBLocalObject = Local business interface (same JVM, no RMI overhead).
EJBLocalHome equivalent to EJBHome.
EJB 3.X implementations are annotation based and light weight.
Feature EJB2.X EJB3.X
ejb-jar.xml mandatory optional
Home Interface required removed
Annotations not supported supported
Configuration XML only Annotation +Optional XML
Dependency Injection Not supported @EJB, @Inject supported
Customer must write
JNDI look up code to locate
and access beans.
Call interfaces and mandatory optional
Life cycle methods
EJB 2.x applications primarily rely on ejb-jar.xml deployment descriptors, whereas EJB 3.x applications typically use annotations and only use ejb-jar.xml when deployment-specific overrides are required.
Feature EJBHome EJBObject EJBLocalObject
Business method No Yes Yes
Use create () Yes No No
Remote capable Yes Yes No
Requires RemoteException Yes Yes No
(declare java.rmi.RemoteException)
Uses RMI/IIOP Yes Yes No
Same JVM only No No Yes
Implementing EJB 3.X -->EJB 2.X bridge call on Websphere Liberty
=======================================================================
Project Structure

Note - EJB2X-EJB3X-EJB4X ear contains 3x and 2X EJB implementation. no EJB 4X implementation in it. EJB 4X will be described later in blog.
Sample codes
============
EJB 2.X implementation (LegacyStatefulBean.java )

String execute() is the business method and there is a sleep time of 2000 milliseconds.It is stateful session bean in 2.X style implementation.
LegacyStatefulHome.java

LegacyStatefulHome interface extends EJBHome and create method is declared there .
LegacyStatefulRemote.java

LegacyStatefulRemote interface extends EJBObject .String execute() is business method declared there .
ejb-jar.xml
=========

pom.xml (EJB2X-EJB3X-EJB4X.ear)

EJB 3.X bridge bean
=================
Ejb3BridgeBean.java

No ejb-jar.xml used with Ejb3BridgeBean.invokeEJB2() is the business method exposed by the EJB 3.x bean (Ejb3BridgeBean).This is an EJB local look up using string "java:global/EJB2X-EJB3X-EJB4XEAR/EJB2X-EJB3X-EJB4X/LegacyStatefulEJB".
LegacyStatefulEJB is defined on ejb-jar.xml of ejb2x. Here a general rule is Home Interface of ejb2x contains create() method and Remote Interface of ejb2x contains business method.
pom.xml for Ejb3BridgeBean.jar

web.war
=======
start.jsp

Start.jsp is doing a local ejb lookup using string "java:global/EJB2X-EJB3X-EJB4XEAR/EJB3X/Ejb3BridgeBean". There are no interfaces defined on Ejb3BridgeBean only bean name mentioned in look up .
pom.xml for Web.war

Here EJB3X added EJB2X module as a maven dependency and Web.war added EJB3X module as a maven dependency. This method will work out only for local look ups . For remote look ups ,let us say ejb 3.x is EJB client application and EJB2X-EJB3X-EJB4X is EJB target application.
For LegacyStatefulHome and LegacyStatefulRemote interfaces to resolve on EJB3x Client application
Method - 1
While creating the EJB2X-EJB3X-EJB4X EJB project, enable the option to create EJB client JARs
and add these client JAR files to the class path of the EJB3X client application.

Method -2
Generate stubs using twas stub creation scripts and attach to EJB3X client application.
Final out put
==========

Liberty Features used -ejbRemote-3.2, ejb-3.2
EJB 4.X(Jakarta)
=============
What is EJB 4.X - Enterprise JavaBeans (EJB) 4.x is a Jakarta EE server-side component model used to implement business logic in enterprise applications.
With EJB 4.X, transaction management, security, Dependency Injection, remote invocation, concurrency management, life cycle management etc. are automatically managed by EJB Container. This gives developer opportunity to focus on business logic in detail.
Like other EJB’s EJB 4.x also supports stateful, stateless and singleton beans.
EJB4X.war
=========
GreetingBean.java


GreetingServlet.java

index.jsp

Result.jsp

Calling ejb4x

EJB response

Liberty Features used - jakartaee-10.0
Question 1 - what is difference between EJB 2.x implementation in traditional WAS and In Liberty
The EJB 2.x programming model is largely supported in Liberty, so many EJB 2.x session bean applications can migrate with minimal code changes. However, there are two major exceptions:EJB 2.x Entity Beans (CMP/BMP) are not supported in Liberty and must be migrated, typically to JPA.JAX-RPC web service endpoints are not supported in Liberty and must be migrated to JAX-WS (Java EE) or Jakarta XML Web Services (Jakarta EE).
Question 2 – EJB 3.x to EJB 2.x bridge call will work for remote looks ups using RMI/IIOP?
Yes, an EJB 3.x → EJB 2.x bridge can work for remote lookups, provided the EJB 2.x bean exposes a remote home interface (EJBHome) and remote business interface (EJBObject).
Useful references .
https://www.ibm.com/docs/en/was-liberty/zos?topic=environment-developing-ejb-applications-liberty
https://www.ibm.com/docs/en/was/8.5.5?topic=beans-create-stubs-command