Ejb 简明教程

EJB - Stateful Bean

有状态会话 bean 是企业 bean 的一种类型,它与客户端保持会话状态。有状态会话 bean 根据其名称将关联客户端状态保留在其实例变量中。EJB 容器创建独立的有状态会话 bean 来处理客户端的每个请求。请求范围结束时,有状态会话 bean 将被销毁。

A stateful session bean is a type of enterprise bean, which preserve the conversational state with client. A stateful session bean as per its name keeps associated client state in its instance variables. EJB Container creates a separate stateful session bean to process client’s each request. As soon as request scope is over, statelful session bean is destroyed.

Steps to Create Stateful EJB

以下是有状态 EJB 的创建步骤:

Following are the steps required to create a stateful EJB −

  1. Create a remote/local interface exposing the business methods.

  2. This interface will be used by the EJB client application.

  3. Use @Local annotation if EJB client is in same environment where EJB session bean need to be deployed.

  4. Use @Remote annotation if EJB client is in different environment where EJB session bean need to be deployed.

  5. Create a stateful session bean, implementing the above interface.

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

Remote Interface

import javax.ejb.Remote;

@Remote
public interface LibraryStatefulSessionBeanRemote {
   //add business method declarations
}

Stateful EJB

@Stateful
public class LibraryStatefulSessionBean implements LibraryStatefulSessionBeanRemote {
   //implement business method
}

Example Application

让我们创建一个测试 EJB 应用程序来测试有状态 EJB。

Let us create a test EJB application to test stateful EJB.

Step

Description

1

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

2

Create LibraryStatefulSessionBean.java and LibraryStatefulSessionBeanRemote as explained in the EJB − Create Application chapter. Keep rest of the files unchanged.

3

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

4

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

5

Now create the EJB client, a console based application in the same way as explained in the EJB - Create Application chapter under topic Create Client to access EJB.

EJBComponent (EJB Module)

LibraryStatefulSessionBeanRemote.java

package com.tutorialspoint.stateful;

import java.util.List;
import javax.ejb.Remote;

@Remote
public interface LibraryStatefulSessionBeanRemote {
   void addBook(String bookName);
   List getBooks();
}

LibraryStatefulSessionBean.java

package com.tutorialspoint.stateful;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateful;

@Stateful
public class LibraryStatefulSessionBean implements LibraryStatefulSessionBeanRemote {

   List<String> bookShelf;

   public LibraryStatefulSessionBean() {
      bookShelf = new ArrayList<String>();
   }

   public void addBook(String bookName) {
      bookShelf.add(bookName);
   }

   public List<String> getBooks() {
      return bookShelf;
   }
}
  1. As soon as you deploy the EjbComponent project on JBOSS, notice the jboss log.

  2. JBoss has automatically created a JNDI entry for our session bean − LibraryStatefulSessionBean/remote.

  3. We will be using this lookup string to get remote business object of type −* com.tutorialspoint.stateful.LibraryStatefulSessionBeanRemote *

JBoss Application Server Log Output

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

   LibraryStatefulSessionBean/remote - EJB3.x Default Remote Business Interface
   LibraryStatefulSessionBean/remote-com.tutorialspoint.stateful.LibraryStatefulSessionBeanRemote - EJB3.x Remote Business Interface
...

EJBTester (EJB Client)

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
  1. These properties are used to initialize the InitialContext object of java naming service.

  2. InitialContext object will be used to lookup stateful session bean.

EJBTester.java

package com.tutorialspoint.test;

import com.tutorialspoint.stateful.LibraryStatefulSessionBeanRemote;
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;

public 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));
   }

   public 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 \n1. Add Book\n2. Exit \nEnter Choice: ");
   }

   private void testStatelessEjb() {

      try {
         int choice = 1;

         LibraryStatefulSessionBeanRemote libraryBean =
         LibraryStatefulSessionBeanRemote)ctx.lookup("LibraryStatefulSessionBean/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);
               libraryBean.addBook(book);
            } else if (choice == 2) {
               break;
            }
         }

         List<Book> booksList = libraryBean.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++;
         }
         LibraryStatefulSessionBeanRemote libraryBean1 =
            (LibraryStatefulSessionBeanRemote)ctx.lookup("LibraryStatefulSessionBean/remote");
         List<String> booksList1 = libraryBean1.getBooks();
         System.out.println(
            "***Using second lookup to get library stateful 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执行以下任务-

EJBTester performs the following tasks −

  1. Load properties from jndi.properties and initialize the InitialContext object.

  2. In testStatefulEjb() method, jndi lookup is done with name - "LibraryStatefulSessionBean/remote" to obtain the remote business object (stateful ejb).

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

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

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

  6. Then another jndi lookup is done with the name - "LibraryStatefulSessionBean/remote" to obtain the remote business object (stateful EJB) again and listing of books is done.

Run Client to Access EJB

在项目浏览器中找到EJBTester.java。右键单击EJBTester类并选择 run file

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

在 Netbeans 控制台中验证以下输出 −

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 library stateful object***
Book(s) entered so far: 0
BUILD SUCCESSFUL (total time: 13 seconds)

Run Client Again to Access EJB

在项目浏览器中找到EJBTester.java。右键单击EJBTester类并选择 run file

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

在Netbeans控制台中验证以下输出。

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 library stateful object***
Book(s) entered so far: 0
BUILD SUCCESSFUL (total time: 12 seconds)
  1. Output shown above states that for each lookup, a different stateful EJB instance is returned.

  2. Stateful EJB object is keeping value for single session only. As in second run, we are not getting any value of books.