目录

EJB - 无状态Bean( Stateless Beans)

无状态会话bean是一种企业bean,通常用于执行独立操作。 无状态会话bean根据其名称没有任何关联的客户端状态,但它可以保留其实例状态。 EJB容器通常会创建一个包含少量无状态bean对象的池,并使用这些对象来处理客户端的请求。 由于池,在查找/方法调用中不保证实例变量值相同。

创建无状态EJB的步骤

以下是创建无状态EJB所需的步骤 -

  • 创建公开业务方法的远程/本地接口。

  • EJB客户端应用程序将使用此接口。

  • 如果EJB客户端位于要部署EJB会话bean的相同环境中,请使用@Local批注。

  • 如果EJB客户端位于要部署EJB会话bean的不同环境中,请使用@Remote注释。

  • 创建无状态会话bean,实现上述接口。

  • 使用@Stateless注释将其表示为无状态bean。 EJB容器通过在部署期间读取此批注来自动创建所需的相关配置或接口。

远程接口

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

无状态EJB

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

例子 Example Application

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

描述
1

EJB - Create Application章节中解释,在com.iowiki.stateless包下创建一个名为EjbComponent的项目。 您还可以使用在EJB - Create Application的项目EJB - Create Application章节,以便本章了解无状态EJB概念。

2

按照EJB - Create Application章节中的说明创建LibrarySessionBean.javaLibrarySessionBeanRemote 。 保持其余文件不变。

3

清理并构建应用程序以确保业务逻辑按照要求运行。

4

最后,在JBoss Application Server上以jar文件的形式部署应用程序。 如果JBoss Application服务器尚未启动,它将自动启动。

5

现在创建EJB客户端,一个基于控制台的应用程序,其方式与EJB - Create Application章节中主题Create Client to access EJB

EJBComponent (EJB Module)

LibrarySessionBeanRemote.java

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

LibrarySessionBean.java

 
package com.iowiki.stateless;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
@Stateless
public class LibrarySessionBean implements LibrarySessionBeanRemote {
   List<String> bookShelf;    
   public LibrarySessionBean() {
      bookShelf = new ArrayList<String>();
   }
   public void addBook(String bookName) {
      bookShelf.add(bookName);
   }    
   public List<String> getBooks() {
      return bookShelf;
   }
}
  • 只要在JBOSS上部署EjbComponent项目,请注意jboss日志。

  • JBoss已自动为会话bean创建了一个JNDI条目 - LibrarySessionBean/remote

  • 我们将使用此查找字符串来获取类型的远程业务对象 - com.iowiki.stateless.LibrarySessionBeanRemote

JBoss应用服务器日志输出

...
16:30:01,401 INFO  [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
   LibrarySessionBean/remote - EJB3.x Default Remote Business Interface
   LibrarySessionBean/remote-com.iowiki.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.iowiki.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.iowiki.stateless.LibrarySessionBeanRemote - 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
  • 这些属性用于初始化java命名服务的InitialContext对象。

  • InitialContext对象将用于查找无状态会话bean。

EJBTester.java

package com.iowiki.test;
import com.iowiki.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;
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; 
         LibrarySessionBeanRemote libraryBean =
         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);
               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++;
         }       
         LibrarySessionBeanRemote libraryBean1 = 
            (LibrarySessionBeanRemote)ctx.lookup("LibrarySessionBean/remote");
         List<String> booksList1 = libraryBean1.getBooks();
         System.out.println(
            "***Using second lookup to get library 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执行以下任务 -

  • 从jndi.properties加载属性并初始化InitialContext对象。

  • 在testStatelessEjb()方法中,使用名称 - “LibrarySessionBean/remote”完成jndi查找以获取远程业务对象(无状态ejb)。

  • 然后向用户显示库存储用户界面,并要求他/她输入选择。

  • 如果用户输入1,系统将询问书名并使用无状态会话bean addBook()方法保存书籍。 会话Bean将书存储在其实例变量中。

  • 如果用户输入2,系统将使用无状态会话bean getBooks()方法检索书籍并退出。

  • 然后使用名称“LibrarySessionBean/remote”完成另一个jndi查找,以再次获取远程业务对象(无状态EJB)并完成书籍列表。

运行客户端以访问EJB

在项目资源管理器中找到EJBTester.java。 右键单击EJBTester类并选择run file

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

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

再次运行客户端以访问EJB

在项目资源管理器中找到EJBTester.java。 右键单击EJBTester类并选择run file

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

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 stateless object***
Book(s) entered so far: 1
1. Learn Java
BUILD SUCCESSFUL (total time: 12 seconds)
  • 上面显示的输出可能会有所不同,具体取决于JBoss维护的无状态EJB对象的数量。

  • 如果维护单个无状态EJB对象,则每次查找后可能会看到相同的书籍列表。

  • EJB容器可以为每个查找返回相同的无状态EJB对象。

  • 无状态EJB bean保留实例变量的值,直到服务器未重新启动。

↑回到顶部↑
WIKI教程 @2018