EJB 3.2 : Session Beans and Business Interfaces Projects (Tutorial 4)

How to define a Stateful Bean, a Stateless Session Bean, and a Singleton Session Bean?

How to expose Multiple EJB 3 Views (Local and Remote Interfaces) in the same time for a Session Bean?


Tutorial 1 : Eclipse + Maven 3 + Wildfly 9 

Tutorial 2 : Session Beans

Tutorial 3 : Solution Design

Tutorial 4 : Session Beans and Business Interfaces Projects current-tutorial-icon-50 

Tutorial 5 : Local View – Client and EJBs in the Same WAR

Tutorial 6 : Remote View – Client and EJBs in the Same EAR

Tutorial 7 : Remote View – Client and EJBs in Different WARs but Same Container

Tutorial 8 : Remote View – Client and EJBs in different Containers

Tutorial 9 : Standalone Client

Server Side Artifacts

1. Maven Artifacts to Create

ejb3-server-api ejb3-server-impl ejb3-server-war
Group Id : com.letsprog.learning

Artifact Id : ejb3-server-api

Version : 0.0.1-SNAPSHOT

Packaging : jar

Group Id : com.letsprog.learning

Artifact Id : ejb3-server-impl

Version : 0.0.1-SNAPSHOT

Packaging : jar

Group Id : com.letsprog.learning

Artifact Id : ejb3-server-war

Version : 0.0.1-SNAPSHOT

Packaging : war

I have chosen to uncouple Session Beans (Implementation) and Business Interfaces (API) into 2 separate projects to be able to provide the client by the API only. This has advantages such as :

  • The client won’t be obliged to have “server side” dependencies, all it needs is “ordinary Java interfaces”.
  • The client might be somehow “foreigner” to you, so why would you provide him by the details of your Session Beans business?
  • Uncoupling is often good for maintenance.

For reusability reasons, I have uncoupled the implementation into 2 projects too: Session Beans Implementation and WAR wrapper.

And this has given 3 projects! Haha! Sowy 

2. ejb3-server-api

This project will contain the Business Interfaces. We will create 7 interfaces to show how we can design a solution where we need to expose local and remote beans for the same interface, so that we will have multiple EJB 3 views for the same Session Bean.

2.1. pom.xml :

2.2. Defining The EJB 3  Views : The interfaces hierarchy

Important : For the fact that we cannot use the same interface to be a Local View and A Remote View as well, we will cure this by defining generic interfaces (IQuiz and IPlayedQuizzesCounter) and extending them as follows :

EJB 3 Views - local and remote interfaces

Here we go with our interfaces free from any EJB dependencies :

  • IQuiz :

  • ILocalQuiz :

  • IRemoteQuiz :

  • IPlayedQuizzesCounter :

  • ILocalPlayedQuizzesCounter :

  • IRemotePlayedQuizzesCounter :

  • ILocalQuizQuestionGenerator :

  • LevelQuestion :

 

3. ejb3-server-impl

This project will contain the Session Beans implementation.

3.1. pom.xml :

3.2. Stateless Session Bean exposing a Local View only

Since we are not aiming to use QuizQuestionGeneratorBean outside of the project ejb3-server-impl we could very well declare it as no-interface view using annotation @LocalBean and we would inject directly the bean instead of the local interface. This feature has been added since EJB 3.1.

3.3. Singleton Session Bean exposing Local and Remote Views

Important : Instead of declaring a local interface to expose a local view we could simply annotate by @LocalBean :

And then to get the local view you got to inject the bean directly :

3.4. Stateful Session Bean exposing Local and Remote Views

Again, we could expose a local view without interface and a remote interface :

4. ejb3-server-war

This project will look as a wrapper for the Session Beans.

4.1. pom.xml :

As you can see we are again adding javaee-api dependency. You got to keep in mind that provided dependencies are not transitive and so even if we have it ejb3-server-impl as a dependency we must add it in ejb3-server-war too.

Retro-Flower-Fire-2-iconAt this stage, you have created the server side. So let’s move next tutorial as the first example of the client side.