Ejb-interceptors
提供:Dev Guides
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();
}
インターセプターメソッドは、3つのレベルで適用またはバインドできます。
- デフォルト-デプロイメント内のすべてのBeanに対してデフォルトのインターセプターが呼び出されます。デフォルトのインターセプターは、xml(ejb-jar.xml)を介してのみ適用できます。
- Class -Beanのすべてのメソッドに対してクラスレベルのインターセプターが呼び出されます。 クラスレベルのインターセプターは、xml(ejb-jar.xml)を介した注釈によって両方に適用できます。
- メソッド-Beanの特定のメソッドに対してメソッドレベルのインターセプターが呼び出されます。 メソッドレベルのインターセプターは、xml(ejb-jar.xml)を介したアノテーションによって適用できます。
ここでは、クラスレベルのインターセプターについて説明します。
インターセプタークラス
package com.finddevguides.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 | Description |
|---|---|
| 1 | Create a project with a name EjbComponent under a package com.finddevguides.interceptor 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 intercepted EJB concepts. |
| 2 | Create LibraryBean.java and LibraryBeanRemote under package com.finddevguides.interceptor 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*. |
EJBComponent(EJBモジュール)
LibraryBeanRemote.java
package com.finddevguides.interceptor;
import java.util.List;
import javax.ejb.Remote;
@Remote
public interface LibraryBeanRemote {
void addBook(String bookName);
List getBooks();
}
LibraryBean.java
package com.finddevguides.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;
}
}
- EjbComponentプロジェクトをJBOSSにデプロイするとすぐに、jbossログに注目してください。
- JBossは、セッションBeanのJNDIエントリを自動的に作成しました- LibraryBean/remote 。
- このルックアップ文字列を使用して、タイプのリモートビジネスオブジェクトを取得します- com.finddevguides.interceptor.LibraryBeanRemote
JBoss Application Serverのログ出力
...
16:30:01,401 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
LibraryBean/remote - EJB3.x Default Remote Business Interface
LibraryBean/remote-com.finddevguides.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.finddevguides.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.finddevguides.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.finddevguides.test;
import com.finddevguides.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()メソッドでは、jndiルックアップが「LibraryBean/remote」という名前で実行され、リモートビジネスオブジェクト(ステートレス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 Application Serverのログ出力
JBoss Application Serverのログ出力で次の出力を確認します。
....
09:55:40,741情報[STDOUT] ** *LibraryBeanメソッドの呼び出しをインターセプト:addBook 09:55:43,661 INFO [STDOUT]* ** LibraryBeanメソッドの呼び出しをインターセプト:getBooks