📅  最后修改于: 2020-11-16 06:12:22             🧑  作者: Mango
无状态会话Bean是企业Bean的一种,通常用于执行独立的操作。根据其名称的无状态会话Bean没有任何关联的客户端状态,但是它可以保留其实例状态。 EJB容器通常创建一个由几个无状态bean对象组成的池,并使用这些对象来处理客户机的请求。由于存在池,因此不能保证实例变量值在查找/方法调用之间是相同的。
以下是创建无状态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
public class LibrarySessionBean implements LibrarySessionBeanRemote {
//implement business method
}
让我们创建一个测试EJB应用程序以测试无状态EJB。
Step | Description |
---|---|
1 |
Create a project with a name EjbComponent under a package com.tutorialspoint.stateless 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 stateless EJB concepts. |
2 |
Create LibrarySessionBean.java and LibrarySessionBeanRemote 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. |
package com.tutorialspoint.stateless;
import java.util.List;
import javax.ejb.Remote;
@Remote
public interface LibrarySessionBeanRemote {
void addBook(String bookName);
List getBooks();
}
package com.tutorialspoint.stateless;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
@Stateless
public class LibrarySessionBean implements LibrarySessionBeanRemote {
List bookShelf;
public LibrarySessionBean() {
bookShelf = new ArrayList();
}
public void addBook(String bookName) {
bookShelf.add(bookName);
}
public List getBooks() {
return bookShelf;
}
}
在JBOSS上部署EjbComponent项目后,请注意jboss日志。
JBoss已经为我们的会话bean- LibrarySessionBean / remote自动创建了一个JNDI条目。
我们将使用该查找字符串来获取类型为com.tutorialspoint.stateless.LibrarySessionBeanRemote的远程业务对象。
...
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
...
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。
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 \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 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 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),并完成了书的清单。
在项目资源管理器中找到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)
在项目资源管理器中找到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将保持实例变量的值,直到不重新启动服务器。