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つのレベルで適用またはバインドできます。

  • Default −デフォルトのインターセプターは、デプロイメント内のすべてのBeanに対して呼び出されます。デフォルトのインターセプターは、xml(ejb-jar.xml)を介してのみ適用できます。

  • Class−クラスレベルのインターセプターは、Beanのすべてのメソッドに対して呼び出されます。クラスレベルのインターセプターは、via xml(ejb-jar.xml)のアノテーションによって両方に適用できます。

  • Method−メソッドレベルインターセプターは、Beanの特定のメソッドに対して呼び出されます。メソッドレベルのインターセプターは、via xml(ejb-jar.xml)のアノテーションによって両方に適用できます。

ここでは、クラスレベルのインターセプターについて説明しています。

インターセプタークラス

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

リモートインターフェース

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アプリケーションを作成しましょう。

ステップ 説明
1

名前を持つプロジェクト作成はEJBComponentパッケージの下com.tutorialspoint.interceptorをで説明したようにEJB -アプリケーション作成の章を。この章では、EJB-アプリケーションの作成の章で作成されたプロジェクトを使用して、インターセプトされたEJBの概念を理解することもできます

2

作成LibraryBean.javaLibraryBeanRemoteをパッケージの下com.tutorialspoint.interceptorで説明したようにEJB -アプリケーション作成の章を。残りのファイルは変更しないでください。

3

アプリケーションをクリーンアップしてビルドし、ビジネスロジックが要件に従って機能していることを確認します。

4

最後に、アプリケーションをjarファイルの形式でJBoss ApplicationServerにデプロイします。JBoss Application Serverは、まだ起動されていない場合、自動的に起動されます。

5

次に、トピックの「EJB-アプリケーションの作成」の章で説明したのと同じ方法で、コンソールベースのアプリケーションであるejbクライアントを作成します。Create Client to access EJB

EJBComponent(EJBモジュール)

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<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.tutorialspoint.interceptor.LibraryBeanRemote

JBoss ApplicationServerのログ出力

...
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クライアント)

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.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()メソッドを使用して本を保存します。Session 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 ApplicationServerのログ出力

JBossApplicationサーバーのログ出力で次の出力を確認します。

....
09:55:40,741 INFO  [STDOUT] *** Intercepting call to LibraryBean method: addBook
09:55:43,661 INFO  [STDOUT] *** Intercepting call to LibraryBean method: getBooks