EJB3.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 - 默认的拦截器被调用内deployment.Default拦截每个bean只能应用于通过XML(ejb-jar.xml)。
Class - 类级别拦截所有的bean的方法被调用。类级别拦截器可以应用通过XML注释(ejb-jar.xml)。
Method - 方法级别的拦截器是一个特定的bean的方法调用。方法级的拦截器可以应用通过XML注释(ejb-jar.xml)。
我们在这里讨论类级别拦截。
Interceptor class
package com.tutorialspoint.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();
}
}
Remote Interface
import javax.ejb.Remote;
@Remote
public interface LibraryBeanRemote {
//add business method declarations
}
Intercepted Stateless EJB
@Interceptors ({BusinessInterceptor.class})
@Stateless
public class LibraryBean implements LibraryBeanRemote {
//implement business method
}
示例应用程序
让我们创建一个测试测试截获的无状态EJB的EJB应用程序。
Step描述
1Create a project with a name EjbComponent under a package com.tutorialspoint.interceptoras explained in the EJB - Create Application chapter. You can also use the project created inEJB - Create Application chapter as such for this chapter to understand intercepted ejb concepts.
2Create LibraryBean.java and LibraryBeanRemote under packagecom.tutorialspoint.interceptor as explained in the EJB - Create Application chapter. Keep rest of the files unchanged.
3Clean and Build the application to make sure business logic is working as per the requirements.
4Finally, 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.
5Now create the ejb client, a console based application in the same way as explained in theEJB - Create Application chapter under topic Create Client to access EJB.
EJBComponent (EJB Module)
LibraryBeanRemote.java
package com.tutorialspoint.interceptor;
import java.util.List;
import javax.ejb.Remote;
@Remote
public interface LibraryBeanRemote {
void addBook(String bookName);
List getBooks();
}
LibraryBean.java
package com.tutorialspoint.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 bookShelf;
public LibraryBean(){
bookShelf = new ArrayList();
}
public void addBook(String bookName) {
bookShelf.add(bookName);
}
public List getBooks() {
return bookShelf;
}
}
只要你部署在JBoss EjbComponent项目,会注意到jboss的日志。
JBoss已经自动为我们的会话bean创建一个JNDI条目 - LibraryBean/remote.
我们将使用这个查询字符串来获得远程类型的业务对象 -com.tutorialspoint.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.tutorialspoint.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.tutorialspoint.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.tutorialspoint.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
这些属性是用来初始化InitialContext对象的Java命名服务
InitialContext的对象将被用于查找无状态会话bean
EJBTester.java
package com.tutorialspoint.test;
import com.tutorialspoint.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 1. Add Book 2. Exit Enter 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 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对象。
JNDI查找testInterceptedEjb()方法,完成了名称 - “LibraryBean/remote”获得远程业务对象(无状态EJB)。
如果用户输入1,系统要求输入书籍名称和节约使用无状态的会话bean addBook()方法的书。会话Bean存储在它的实例变量的书。
如果用户输入2,系统检索书使用状态会话Bean getBooks()方法和退出。
Run Client to access 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
(编辑:雷林鹏 来源:网络|侵删)