拦截器(Interceptors)
EJB 3.0提供了使用@AroundInvoke批注注释的方法来拦截业务方法调用的规范。 在业务方法调用它被拦截之前,ejbContainer会调用一个拦截器方法。 以下是拦截器方法的示例签名
@AroundInvoke
public Object methodInterceptor(InvocationContext ctx) throws Exception {
System.out.println("*** Intercepting call to LibraryBean method: "
+ ctx.getMethod().getName());
return ctx.proceed();
}
拦截器方法可以在三个级别应用或绑定。
Default - 为部署中的每个bean调用默认拦截器。默认拦截器只能通过xml(ejb-jar.xml)应用。
Class - 为bean的每个方法调用类级别拦截器。 可以通过via xml(ejb-jar.xml)的注释来应用类级拦截器。
Method - 为bean的特定方法调用方法级别拦截器。 方法级拦截器可以通过via xml(ejb-jar.xml)的注释来应用。
我们在这里讨论类级拦截器。
拦截器类
package cn.xnip.interceptor;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
public class BusinessInterceptor {
@AroundInvoke
public Object methodInterceptor(InvocationContext ctx) throws Exception {
System.out.println("*** Intercepting call to LibraryBean method: "
+ ctx.getMethod().getName());
return ctx.proceed();
}
}
远程接口
import javax.ejb.Remote;
@Remote
public interface LibraryBeanRemote {
//add business method declarations
}
拦截无状态EJB
@Interceptors ({BusinessInterceptor.class})
@Stateless
public class LibraryBean implements LibraryBeanRemote {
//implement business method
}
例子 Example Application
让我们创建一个测试EJB应用程序来测试截获的无状态EJB。
步 | 描述 |
---|---|
1 | 在EJB - Create Application章节中解释,在cn.xnip.interceptor包下创建一个名为EjbComponent的项目。 您还可以使用在EJB - Create Application的项目EJB - Create Application本章的EJB - Create Application章节来理解截获的EJB概念。 |
2 | 在EJB - Create Application章节中解释,在cn.xnip.interceptor包下创建LibraryBean.java和LibraryBeanRemote 。 保持其余文件不变。 |
3 | 清理并构建应用程序以确保业务逻辑按照要求运行。 |
4 | 最后,在JBoss Application Server上以jar文件的形式部署应用程序。 如果JBoss Application服务器尚未启动,它将自动启动。 |
5 | 现在创建ejb客户端,一个基于控制台的应用程序,其方式与在Create Client to access EJB主题Create Client to access EJB下的EJB - Create Application章节中所述的相同。 |
EJBComponent (EJB Module)
LibraryBeanRemote.java
package cn.xnip.interceptor;
import java.util.List;
import javax.ejb.Remote;
@Remote
public interface LibraryBeanRemote {
void addBook(String bookName);
List getBooks();
}
LibraryBean.java
package cn.xnip.interceptor;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
@Interceptors ({BusinessInterceptor.class})
@Stateless
public class LibraryBean implements LibraryBeanRemote {
List<String> bookShelf;
public LibraryBean() {
bookShelf = new ArrayList<String>();
}
public void addBook(String bookName) {
bookShelf.add(bookName);
}
public List<String> getBooks() {
return bookShelf;
}
}
只要在JBOSS上部署EjbComponent项目,请注意jboss日志。
JBoss已自动为会话bean创建了一个JNDI条目 - LibraryBean/remote 。
我们将使用此查找字符串来获取类型的远程业务对象 - cn.xnip.interceptor.LibraryBeanRemote
JBoss应用服务器日志输出
...
16:30:01,401 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibraryBean/remote - EJB3.x Default Remote Business Interface
LibraryBean/remote-cn.xnip.interceptor.LibraryBeanRemote - EJB3.x Remote Business Interface
16:30:02,723 INFO [SessionSpecContainer] Starting jboss.j2ee:jar=EjbComponent.jar,name=LibraryBean,service=EJB3
16:30:02,723 INFO [EJBContainer] STARTED EJB: cn.xnip.interceptor.LibraryBeanRemote ejbName: LibraryBean
16:30:02,731 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibraryBean/remote - EJB3.x Default Remote Business Interface
LibraryBean/remote-cn.xnip.interceptor.LibraryBeanRemote - 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 cn.xnip.test;
import cn.xnip.stateful.LibraryBeanRemote;
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.testInterceptedEjb();
}
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 testInterceptedEjb() {
try {
int choice = 1;
LibraryBeanRemote libraryBean =
LibraryBeanRemote)ctx.lookup("LibraryBean/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++;
}
} 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对象。
在testInterceptedEjb()方法中,使用名称“LibraryBean/remote”完成jndi查找以获取远程业务对象(无状态EJB)。
然后向用户显示库存储用户界面,并要求他/她输入选项。
如果用户输入1,系统将询问书名并使用无状态会话bean addBook()方法保存书籍。 会话Bean将书存储在其实例变量中。
如果用户输入2,系统将使用无状态会话bean getBooks()方法检索书籍并退出。
运行客户端以访问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
BUILD SUCCESSFUL (total time: 13 seconds)
JBoss应用服务器日志输出
验证JBoss应用程序服务器日志输出中的以下输出。
....
09:55:40,741 INFO [STDOUT] *** Intercepting call to LibraryBean method: addBook
09:55:43,661 INFO [STDOUT] *** Intercepting call to LibraryBean method: getBooks