EJB无状态的bean
无状态会话bean是一种企业bean,它通常用来做独立操作。无状态会话Bean根据其名字没有任何关联的客户端的状态,但它可能会保留其实例的状态。 EJB容器通常会创建一些无状态Bean的对象池,并使用这些对象来处理客户端的请求。在池中,实例变量的值不能保证是相同的跨越查找/方法调用。
下面是创建一个无状态EJB所需的步骤。
创建远程/本地接口暴露商业方法。
该接口将由EJB客户端应用程序使用。
如果EJB客户端在同一个环境中,EJB会话Bean被部署使用本地注释。
如果EJB客户端是在不同的环境中,EJB会话Bean被部署使用远程注释。
创建一个无状态会话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
}
示例应用程序
让我们创建一个测试EJB应用程序来测试无状态EJB。
| 步骤 | 描述 |
|---|---|
| 1 | 用包com.tutorialspoint.stateless下一个名字EjbComponent在EJB作为解释的创建项目-创建应用程序一章。您也可以使用EJB创建的项目-创建应用程序章这样本章了解无状态的EJB概念。 |
| 2 | 创建LibrarySessionBean.java和LibrarySessionBeanRemote作为EJB解释-创建应用程序一章。保持不变的文件其余部分。 |
| 3 | 清理并生成应用程序,确保业务逻辑正在按要求。 |
| 4 | 最后,部署JBoss应用服务器上的jar文件的形式应用。如果尚未启动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 /远程。
我们将使用此查找字符串来获得类型的远程业务对象- 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()方法,JNDI查找与名行 - “LibrarySessionBean /远程”,以获得远程业务对象(无状态EJB)。
然后用户显示库存储用户界面和他(她)被要求输入选择/。
如果用户输入1,系统要求书名并保存使用无状态会话bean addBook()方法的书。会话bean存储本书的实例变量。
如果用户输入2,系统检索使用无状态会话bean getBooks()方法,并退出书籍。
然后另一个JNDI查找与名行 - “LibrarySessionBean /远程”,以获得远程业务对象(无状态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豆是保持实例变量的值,直到没有重新启动服务器。

免费 AI IDE


更多建议: