EJB - 拦截器

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(); 
}


拦截器方法可以在三个级别应用或绑定.

  • 默认 : 部署中的每个bean都会调用默认拦截器.默认拦截器只能通过xml(ejb-jar.xml)应用.

  • : 为bean的每个方法调用类级别拦截器.类级别拦截器可以通过via xml(ejb-jar.xml)的注释来应用.

  • 方法 : 为bean的特定方法调用方法级拦截器.方法级拦截器可以通过via xml(ejb-jar.xml)的注释来应用.

我们在这里讨论类级别拦截器.

拦截器类

 
 package com.it1352.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
}


示例应用

让我们创建一个测试用于测试截获的无状态EJB的EJB应用程序.

Step描述
1

在包 com.it1352.interceptor 下创建一个名为 EjbComponent 的项目,如 EJB  - 创建应用程序中所述章节.您还可以使用在本章中 EJB  - 创建应用程序章节中创建的项目来理解截获的EJB概念.

2

创建 LibraryBean.java LibraryBeanRemote EJB  - 创建应用程序章节中所述,在 com.it1352.interceptor 下.保持其余文件不变.

3

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

4

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

5

现在创建ejb客户端,一个基于控制台的应用程序,其方式与主题创建中的 EJB  - 创建应用程序章节中所述的相同客户端访问EJB .

EJBComponent(EJB模块)

LibraryBeanRemote.java

package com.it1352.interceptor;

import java.util.List;
import javax.ejb.Remote;

@Remote
public interface LibraryBeanRemote {
   void addBook(String bookName);
   List getBooks();
}


LibraryBean.java

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;
   } 
}


  • 一旦部署EjbComponent关于JBOSS的项目,请注意jboss日志.

  • JBoss已自动为会话bean创建了一个JNDI条目 :   LibraryBean/remote .

  • 我们将使用此查找字符串来获取类型为&minus的远程业务对象; com.it1352.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-com.it1352.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: com.it1352.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-com.it1352.interceptor.LibraryBeanRemote - 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.it1352.test; 
 import com.it1352.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类并选择运行文件.

在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