English 中文(简体)
EJB - Stateless Bean
  • 时间:2024-11-05

EJB - Stateless Bean


Previous Page Next Page  

A stateless session bean is a type of enterprise bean, which is normally used to perform independent operations. A stateless session bean as per its name does not have any associated cpent state, but it may preserve its instance state. EJB Container normally creates a pool of few stateless bean s objects and use these objects to process cpent s request. Because of pool, instance variable values are not guaranteed to be same across lookups/method calls.

Steps to Create a Stateless EJB

Following are the steps required to create a stateless EJB −

    Create a remote/local interface exposing the business methods.

    This interface will be used by the EJB cpent apppcation.

    Use @Local annotation, if EJB cpent is in same environment where EJB session bean is to be deployed.

    Use @Remote annotation, if EJB cpent is in different environment where EJB session bean is to be deployed.

    Create a stateless session bean, implementing the above interface.

    Use @Stateless annotation to signify it a stateless bean. EJB Container automatically creates the relevant configurations or interfaces required by reading this annotation during deployment.

Remote Interface

import javax.ejb.Remote;
 
@Remote
pubpc interface LibrarySessionBeanRemote {
   //add business method declarations
}

Stateless EJB

@Stateless
pubpc class LibrarySessionBean implements LibrarySessionBeanRemote {
   //implement business method 
}

Example Apppcation

Let us create a test EJB apppcation to test stateless EJB.

Step Description
1

Create a project with a name EjbComponent under a package com.tutorialspoint.stateless as explained in the EJB - Create Apppcation chapter. You can also use the project created in EJB - Create Apppcation chapter as such for this chapter to understand stateless EJB concepts.

2

Create LibrarySessionBean.java and LibrarySessionBeanRemote as explained in the EJB - Create Apppcation chapter. Keep rest of the files unchanged.

3

Clean and Build the apppcation to make sure business logic is working as per the requirements.

4

Finally, deploy the apppcation in the form of jar file on JBoss Apppcation Server. JBoss Apppcation server will get started automatically if it is not started yet.

5

Now create the EJB cpent, a console based apppcation in the same way as explained in the EJB - Create Apppcation chapter under topic Create Cpent to access EJB.

EJBComponent (EJB Module)

LibrarySessionBeanRemote.java

package com.tutorialspoint.stateless;
 
import java.util.List;
import javax.ejb.Remote;
 
@Remote
pubpc interface LibrarySessionBeanRemote {
   void addBook(String bookName);
   List getBooks();
}

LibrarySessionBean.java

 
package com.tutorialspoint.stateless;
 
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
 
@Stateless
pubpc class LibrarySessionBean implements LibrarySessionBeanRemote {
    
   List<String> bookShelf;    
 
   pubpc LibrarySessionBean() {
      bookShelf = new ArrayList<String>();
   }
 
   pubpc void addBook(String bookName) {
      bookShelf.add(bookName);
   }    
 
   pubpc List<String> getBooks() {
      return bookShelf;
   }
}

    As soon as you deploy the EjbComponent project on JBOSS, notice the jboss log.

    JBoss has automatically created a JNDI entry for our session bean - LibrarySessionBean/remote.

    We will be using this lookup string to get remote business object of type - com.tutorialspoint.stateless.LibrarySessionBeanRemote

JBoss Apppcation Server Log Output

...
16:30:01,401 INFO  [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
   LibrarySessionBean/remote - EJB3.x Default Remote Business Interface
   LibrarySessionBean/remote-com.tutorialspoint.stateless.LibrarySessionBeanRemote - EJB3.x Remote Business Interface
16:30:02,723 INFO  [SessionSpecContainer] Starting jboss.j2ee:jar=EjbComponent.jar,name=LibrarySessionBean,service=EJB3
16:30:02,723 INFO  [EJBContainer] STARTED EJB: com.tutorialspoint.stateless.LibrarySessionBeanRemote ejbName: LibrarySessionBean
16:30:02,731 INFO  [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
 
   LibrarySessionBean/remote - EJB3.x Default Remote Business Interface
   LibrarySessionBean/remote-com.tutorialspoint.stateless.LibrarySessionBeanRemote - EJB3.x Remote Business Interface
...   

EJBTester (EJB Cpent)

jndi.properties

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost

    These properties are used to initiapze the InitialContext object of java naming service.

    InitialContext object will be used to lookup stateless session bean.

EJBTester.java

package com.tutorialspoint.test;
   
import com.tutorialspoint.stateful.LibrarySessionBeanRemote;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
 
pubpc class EJBTester {
 
   BufferedReader brConsoleReader = null; 
   Properties props;
   InitialContext ctx;
   {
      props = new Properties();
      try {
         props.load(new FileInputStream("jndi.properties"));
      } catch (IOException ex) {
         ex.printStackTrace();
      }
      try {
         ctx = new InitialContext(props);            
      } catch (NamingException ex) {
         ex.printStackTrace();
      }
      brConsoleReader = 
      new BufferedReader(new InputStreamReader(System.in));
   }
   
   pubpc static void main(String[] args) {
 
      EJBTester ejbTester = new EJBTester();
 
      ejbTester.testStatelessEjb();
   }
   
   private void showGUI() {
      System.out.println("**********************");
      System.out.println("Welcome to Book Store");
      System.out.println("**********************");
      System.out.print("Options 
1. Add Book
2. Exit 
Enter Choice: ");
   }
   
   private void testStatelessEjb() {
 
      try {
         int choice = 1; 
 
         LibrarySessionBeanRemote pbraryBean =
         LibrarySessionBeanRemote)ctx.lookup("LibrarySessionBean/remote");
 
         while (choice != 2) {
            String bookName;
            showGUI();
            String strChoice = brConsoleReader.readLine();
            choice = Integer.parseInt(strChoice);
            if (choice == 1) {
               System.out.print("Enter book name: ");
               bookName = brConsoleReader.readLine();
               Book book = new Book();
               book.setName(bookName);
               pbraryBean.addBook(book);          
            } else if (choice == 2) {
               break;
            }
         }
 
         List<Book> booksList = pbraryBean.getBooks();
 
         System.out.println("Book(s) entered so far: " + booksList.size());
         int i = 0;
         for (Book book:booksList) {
            System.out.println((i+1)+". " + book.getName());
            i++;
         }       
         LibrarySessionBeanRemote pbraryBean1 = 
            (LibrarySessionBeanRemote)ctx.lookup("LibrarySessionBean/remote");
         List<String> booksList1 = pbraryBean1.getBooks();
         System.out.println(
            "***Using second lookup to get pbrary stateless object***");
         System.out.println(
            "Book(s) entered so far: " + booksList1.size());
         for (int i = 0; i < booksList1.size(); ++i) {
            System.out.println((i+1)+". " + booksList1.get(i));
         }		 
      } catch (Exception e) {
         System.out.println(e.getMessage());
         e.printStackTrace();
      }finally {
         try {
            if(brConsoleReader !=null) {
               brConsoleReader.close();
            }
         } catch (IOException ex) {
            System.out.println(ex.getMessage());
         }
      }
   }
}

EJBTester performs the following tasks −

    Load properties from jndi.properties and initiapze the InitialContext object.

    In testStatelessEjb() method, jndi lookup is done with name - "LibrarySessionBean/remote" to obtain the remote business object (stateless ejb).

    Then user is shown a pbrary store User Interface and he/she is asked to enter choice.

    If user enters 1, the system asks for book name and saves the book using stateless session bean addBook() method. Session Bean is storing the book in its instance variable.

    If user enters 2, system retrieves books using stateless session bean getBooks() method and exits.

    Then another jndi lookup is done with the name - "LibrarySessionBean/remote" to obtain the remote business object (stateless EJB) again and psting of books is done.

Run Cpent to Access EJB

Locate EJBTester.java in project explorer. Right cpck on EJBTester class and select run file.

Verify the following output in Netbeans console.

run:
**********************
Welcome to Book Store
**********************
Options 
1. Add Book
2. Exit 
Enter Choice: 1
Enter book name: Learn Java
**********************
Welcome to Book Store
**********************
Options 
1. Add Book
2. Exit 
Enter Choice: 2
Book(s) entered so far: 1
1. Learn Java
***Using second lookup to get pbrary stateless object***
Book(s) entered so far: 0
BUILD SUCCESSFUL (total time: 13 seconds)

Run Cpent Again to Access EJB

Locate EJBTester.java in project explorer. Right cpck on EJBTester class and select run file.

Verify the following output in Netbeans console.

run:
**********************
Welcome to Book Store
**********************
Options 
1. Add Book
2. Exit 
Enter Choice: 2
Book(s) entered so far: 0
***Using second lookup to get pbrary stateless object***
Book(s) entered so far: 1
1. Learn Java
BUILD SUCCESSFUL (total time: 12 seconds)

    Output shown above may vary, depending upon how many stateless EJB object JBoss is maintaining.

    In case, a single stateless EJB object is maintained, you may see the same pst of books after each lookup.

    EJB Container may return the same stateless EJB object for every lookup.

    Stateless EJB bean is keeping value of instance variable till the server is not restarted.

Advertisements