Lesson 9
EJB Client Program Conclusion
This module examined in some detail the EJB client program and how it interacts with the container and the bean.
I initially showed you a typical sequence of operations that the client goes through, and I explained the responsibilities of each of the entities in our session bean architecture. Moving from the general to the specific, we looked at the concept of a
remote reference[1] , and then identified and explained the
Home interface[2] and the
Home object.
We wrote the code to look up and download the remote reference to the home object from the Name Service, and then, using that reference, we created the bean instance and its
EJBObject. The
create()
call returned the remote reference to the EJBObject, which we used in the client to invoke its business methods. We caught the system exceptions thrown by the infrastructure and application exceptions thrown by the bean.
I explained how a client can acquire a
handle to a bean instance. This handle can be used to re-acquire the bean instance at a later time in, perhaps, a different program, in a different
JVM.
In Java EE 8, the traditional concept of a **bean factory** or a **home interface** to listen on the network for client `create()` and `remove()` requests is no longer used. This was a practice in earlier versions of Java EE (specifically in J2EE) where Enterprise JavaBeans (EJBs) were accessed via Home Interfaces, which exposed methods like `create()`, `remove()`, and `find()`. These Home Interfaces would effectively "listen" for requests from clients to create or remove EJB instances.
However, starting from **Java EE 5** and continuing into **Java EE 8**, the EJB specification was significantly simplified. The introduction of annotations and dependency injection eliminated the need for Home Interfaces and Remote Interfaces in many scenarios. Instead, EJBs are now directly instantiated and managed by the container, and clients can access EJBs directly via dependency injection without explicitly invoking `create()` or `remove()` methods on a Home Interface.
Key Changes in Java EE 8:
- Annotations:** EJBs are declared using annotations like `@Stateless`, `@Stateful`, and `@Singleton`. These annotations indicate the type of bean and how it should be managed by the container, removing the need for explicit factory methods like `create()`.
- Dependency Injection:** Clients can inject EJBs using the `@EJB` annotation, allowing the container to automatically manage the lifecycle of the EJB without needing the client to explicitly call `create()` or `remove()`.
- Lifecycle Callbacks:** While the lifecycle of EJBs is still important, it is managed by the container rather than by the client. Annotations like `@PostConstruct` and `@PreDestroy` allow developers to define methods that the container will call at specific points in the EJB's lifecycle, such as after creation or before removal.
Example:
@Stateless
public class MyServiceBean {
public void businessMethod() {
// Business logic here
}
}
In the above example, the `MyServiceBean` is a stateless session bean. It does not require a Home Interface, and clients do not need to call `create()` or `remove()`; they can simply inject and use the bean as needed.
Conclusion:
In summary, Java EE 8 does not use a bean factory to listen on the network for `create()` and `remove()` requests. Instead, it relies on container-managed EJB lifecycles and simplifies client interaction with EJBs through annotations and dependency injection. This reflects a broader trend in Java EE toward reducing complexity and improving developer productivity.
Container and Server Provider
The container provider supplies an EJB container (the application server). This is the runtime environment in which beans live.
The container supplies middleware services to the beans and manages them. Examples of EJB containers are Oracle WebLogic Server 12c, IBM's WebSphere, and JBoss open source code application server.
The server provider is the same as the container provider. Sun has not yet differentiated these (and they may never do so).
We will use the terms EJB container and EJB server interchangeably in this course.
- Tool Vendors:
To facilitate the component development process, there should be a standardized way to build, manage, and maintain components. In the EJB Ecosystem, there are several Integrated Development Environments (IDEs) assist you in rapidly building and debugging components.
Examples are Eclipse IDE as well as NetBeans IDE. Other tools enable you to model components in the Unified Modeling Language (UML), which is the diagram style used in this book. You can then autogenerate EJB code from that UML.
Examples of products in this space are LucidChart, GenMyModel and Gliffy.
There are other tools as well, such as tools to organize components, testing tools such as JUnit, and build tools like Ant.
- Home object: The EJB bean factory (see above). It listens on the network for client create() and remove() requests.
- EJBObject: That skeleton of the bean that implements the bean's interface.
- Handle: An object which contains all the information required to access an existing remote session bean.
- JVM: The Java Virtual Machine, a software implementation of a portable environment, that executes Java programs.
In the next module, we will actually deploy the Hello bean and then you can test it with the Hello client that I will provide.
A client for a simple BankAccount bean will be written from scratch.
[1]Remote reference: The reference to an object, usually the stub, that knows where, and how to communicate with, a remote object.
[2]Home interface: The Java interface that is implemented by the deployer to build the home object.