EJB 3.2 : Local View – Client and EJBs in the Same WAR (Tutorial 5)

How to invoke a Local View of a Session Bean using Dependency Injection and JNDI Lookup?

How to invoke a Session Bean from a JSF Managed Bean?

How to automatically destroy a Stateful Session Bean obtained within a JSF Managed Bean?


Tutorial 1 : Eclipse + Maven 3 + Wildfly 9  

Tutorial 2 : Session Beans

Tutorial 3 : Solution Design

Tutorial 4 : Session Beans and Business Interfaces Projects

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

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


In this tutorial we will package the Client and the Session Beans in the same WAR. This way we will be able to invoke the Local View of the Session Beans. We will inject/lookup our EJBs in a JSF Session Scoped Managed Bean.

Client and Session Beans in the Same WAR

1. Maven Artifacts

  • To create :
ejb3-server-client-war jndi-lookup-util
  • Group Id : com.letsprog.learning
  • Artifact Id : ejb3-server-client-war
  • Version : 0.0.1-SNAPSHOT
  • Packaging : war
  • Group Id : com.letsprog.learning
  • Artifact Id : jndi-lookup-util
  • Version : 0.0.1-SNAPSHOT
  • Packaging : jar
  • To reuse (the server side) :
ejb3-server-impl
  • Group Id : com.letsprog.learning
  • Artifact Id : ejb3-server-impl
  • Version : 0.0.1-SNAPSHOT
  • Packaging : war

We have created the server side in the previous tutorial.

2. JNDI Lookup Util

2.1. pom.xml

2.2. Programmatic JNDI Lookup for Local View

The pattern of the JNDI Name of the EJB to lookup if Local View exposed :

So let’s create LookerUp.java :

3. ejb3-server-client-war

The Session Beans and the Client invoking them will be packaged in the same WAR.

3.1. pom.xml :

3.2. JSF Managed Bean

We are going to inject the Singleton Session Bean (ILocalPlayedQuizzesCounter) and JNDI Lookup the Stateful Session Bean (ILocalQuiz).

I prefer to programmatically invoke the SFSB via JNDI to have full control on :

  • When to create the SFSB
  • When to remove the SFSB

3.3. What about if the Session Scoped Managed Bean expires? How to remove the SFBS bound to it?

You got to add this method in the JSF Managed Bean that will be called by the Container just before the Managed Bean QuizzManagedBean destruction, using @PreDestroy annotation :

3.4. The GUI

Under src > main > webapp, add the pages described next :

ejb3-server-client-war

  • index.xhtml :

  • quiz.xhtml :

  • end.xhtml

4. Testing our Local View Invokation :

Start your WildFly server then deploy to it ejb3-server-client-war. You must be able to access the quiz : http://localhost:8080/ejb3-server-client-war/index.xhtml

quiz-test

Retro-Flower-Fire-2-iconNow that you have learnt how to implement a local client, move to the next tutorial to see the first example of remote clients.