EJB3 Stateless Session Bean

A stateless session bean is a bean that does not possess a  state, i.e. It does some actions and is thrown away afterwards.

Create local and remote interfaces

The local interface should be used by default, because it is much faster. The remote interface should only be used when the client is not running in the same virtual machine. Remote access even works over the network and has a lot of overhead. Create a interface named BookTestBeanLocal in the package com.home.library. We mark this interface as local interface by the annotation @Local.

<em>package com.home.library; import javax.ejb.Local; @Local public interface BookTestBeanLocal { public void test(); } Create a interface named BookTestBeanRemote in the package com.home.library; package com.home.library; import javax.ejb.Remote; @Remote public interface BookTestBeanRemote { public void test(); }</em>

Now we will create the actual Stateless Session Bean. Create a new class named in the same package as the interfaces and let it implement the local and the remote interface. You configure a class as stateless bean by adding the @Stateless annotation.

<em>package com.home.library; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; @Stateless public class BookTestBean implements BookTestBeanLocal, BookTestBeanRemote { @PersistenceContext EntityManager em; public static final String RemoteJNDIName = BookTestBean.class.getSimpleName() + "/remote"; public static final String LocalJNDIName = BookTestBean.class.getSimpleName() + "/local"; }</em>

We want to access the book bean, so we need a EntityManager. The EntityManager provides all methods needed to select, update,lock or delete entities, to create SQL and EJB-QL queries.

<em>@Persisten</em><em>ceContext EntityManager em; </em>

The annotation @PersistenceContext tells the application server to inject a entity manager during deployment. Injection means that the entity manager is assigned by the application server. This is very useful approach frequently used in the Spring Framework or other Aspect Oriented Framework. The idea is:
A data access class should not be responsible for the persistenceContext. Our configuration decides which context for which database it receives. Imagine you hard code a context in 25 classes and then want to change the context.

We can have the JNDI name of the class somewhere, so we do not have to type it. This is why we are adding the following lines.

<em>public static final String RemoteJNDIName = BookTestBean.class.getSimpleName() +"/remote";</em><em> public static final String LocalJNDIName = BookTestBean.class.getSimpleName() +"/local";</em>


Implementing the test method. The following test method creates an entry, selects some and deletes an entry as well. Everything is done using the entity manager. You may read in the API about the other methods of this manager.

<em><em>package come.home.library; import java.util.Iterator; import java.util.List; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext;</em></em>
<em><em>@Stateless public class BookTestBean implements BookTestBeanLocal, BookTestBeanRemote { @PersistenceContext EntityManager em; public static final String RemoteJNDIName = BookTestBean.class.getSimpleName() + "/remote"; public static final String LocalJNDIName = BookTestBean.class.getSimpleName() + "/local"; public void test() { Book book = new Book(null, "The first bean ", "Hello"); em.persist(book); Book book2 = new Book(null, "another bean", "World"); em.persist(book2); Book book3 = new Book(null, "EJB 3 study material, coming soon", “World"); em.persist(book3); System.out.println("list some books"); List someBooks = em.createQuery("from Book b where b.author=:name") .setParameter("name", "World").getResultList(); for (Iterator iter = someBooks.iterator(); iter.hasNext();) { Book element = (Book) iter.next(); System.out.println(element); } System.out.println("List all books"); List allBooks = em.createQuery("from Book").getResultList(); for (Iterator iter = allBooks.iterator(); iter.hasNext();) { Book element = (Book) iter.next(); System.out.println(element); } System.out.println("delete a book"); em.remove(book2); System.out.println("List all books"); allBooks = em.createQuery("from Book").getResultList(); for (Iterator iter = allBooks.iterator(); iter.hasNext();) { Book element = (Book) iter.next(); System.out.println(element); } } }</em></em><em> </em>

Deploy the application

Deploy the application now. It must be deployed as jar. Here is how it looks like in the explorer:

Now open the jmx-console to verify the deployment.
http://localhost:8080/jmx-console/
Select the JNDI View and list to show all deployed beans.

Once the deployment is successful you will see something like this