EJB - 无状态 Bean
无状态会话 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
}
Stateless EJB
@Stateless
public class LibrarySessionBean implements LibrarySessionBeanRemote {
//implement business method
}
示例应用程序
让我们创建一个测试 EJB 应用程序来测试无状态 EJB。
| 步骤 | 描述 |
|---|---|
| 1 | 按照EJB - 创建应用程序章节中的说明,在com.tutorialspoint.stateless包下创建一个名为EjbComponent的项目。 您还可以使用本章中的EJB - 创建应用程序章节中创建的项目来了解无状态 EJB 概念。 |
| 2 | 按照EJB - 创建应用程序章节中的说明创建LibrarySessionBean.java和LibrarySessionBeanRemote。 保持其余文件不变。 |
| 3 | 清理并构建应用程序,以确保业务逻辑按照要求运行。 |
| 4 | 最后,将应用程序以jar文件的形式部署在JBoss应用服务器上。 如果 JBoss 应用服务器尚未启动,它将自动启动。 |
| 5 | 现在创建 EJB 客户端,这是一个基于控制台的应用程序,其方式与创建客户端以访问 EJB 主题下的EJB - 创建应用程序一章中所述的方式相同。 |
EJBComponent(EJB 模块)
LibrarySessionBeanRemote.java
package com.tutorialspoint.stateless;
import java.util.List;
import javax.ejb.Remote;
@Remote
public 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
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.tutorialspoint.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.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 客户端)
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.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;
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
1. Add Book
2. Exit
Enter 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 类并选择运行文件。
在 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 类并选择运行文件。
在 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 会保留实例变量的值,直到服务器未重新启动为止。

