Ejb-stateless-beans

提供:Dev Guides
移動先:案内検索

EJB-ステートレスBean

ステートレスセッションBeanは、通常、独立した操作を実行するために使用されるエンタープライズBeanの一種です。 その名前のとおりのステートレスセッションBeanには、関連付けられたクライアント状態はありませんが、インスタンス状態は保持されます。 EJBコンテナは通常、少数のステートレスBeanのオブジェクトのプールを作成し、これらのオブジェクトを使用してクライアントの要求を処理します。 プールのため、インスタンス変数値はルックアップ/メソッド呼び出し全体で同じであるとは限りません。

ステートレスEJBを作成する手順

ステートレスEJBを作成するために必要な手順は次のとおりです-

  • ビジネスメソッドを公開するリモート/ローカルインターフェイスを作成します。
  • このインターフェイスは、EJBクライアントアプリケーションによって使用されます。
  • EJBクライアントがEJBセッションBeanがデプロイされるのと同じ環境にある場合は、@ Localアノテーションを使用します。
  • EJBクライアントがEJBセッションBeanがデプロイされる異なる環境にある場合は、@ Remoteアノテーションを使用します。
  • 上記のインターフェースを実装して、ステートレスセッションBeanを作成します。 *@Statelessアノテーションを使用して、ステートレスBeanであることを示します。 EJBコンテナは、デプロイメント中にこのアノテーションを読み取ることで必要な関連する構成またはインターフェースを自動的に作成します。

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

import javax.ejb.Remote;

@Remote
public interface LibrarySessionBeanRemote {
  //add business method declarations
}

ステートレスEJB

@Stateless
public class LibrarySessionBean implements LibrarySessionBeanRemote {
  //implement business method
}

応用例

テストEJBアプリケーションを作成して、ステートレスEJBをテストしましょう。

Step Description
1 Create a project with a name EjbComponent under a package com.finddevguides.stateless 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 stateless EJB concepts.
2 Create LibrarySessionBean.java and LibrarySessionBeanRemote 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モジュール)

LibrarySessionBeanRemote.java

package com.finddevguides.stateless;

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

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

LibrarySessionBean.java

package com.finddevguides.stateless;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;

@Stateless
public class LibrarySessionBean implements LibrarySessionBeanRemote {

   List<String> bookShelf;

   public LibrarySessionBean() {
      bookShelf = new ArrayList<String>();
   }

   public void addBook(String bookName) {
      bookShelf.add(bookName);
   }

   public List<String> getBooks() {
      return bookShelf;
   }
}
  • EjbComponentプロジェクトをJBOSSにデプロイするとすぐに、jbossログに注目してください。
  • JBossは、セッションBeanのJNDIエントリ- LibrarySessionBean/remote を自動的に作成しました。
  • このルックアップ文字列を使用して、タイプのリモートビジネスオブジェクトを取得します- com.finddevguides.stateless.LibrarySessionBeanRemote

JBoss Application Serverのログ出力

...
16:30:01,401 INFO  [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:
   LibrarySessionBean/remote - EJB3.x Default Remote Business Interface
   LibrarySessionBean/remote-com.finddevguides.stateless.LibrarySessionBeanRemote - EJB3.x Remote Business Interface
16:30:02,723 INFO  [SessionSpecContainer] Starting jboss.j2ee:jar=EjbComponent.jar,name=LibrarySessionBean,service=EJB3
16:30:02,723 INFO  [EJBContainer] STARTED EJB: com.finddevguides.stateless.LibrarySessionBeanRemote ejbName: LibrarySessionBean
16:30:02,731 INFO  [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:

   LibrarySessionBean/remote - EJB3.x Default Remote Business Interface
   LibrarySessionBean/remote-com.finddevguides.stateless.LibrarySessionBeanRemote - 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.LibrarySessionBeanRemote;
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.testStatelessEjb();
   }

   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 testStatelessEjb() {

      try {
         int choice = 1;

         LibrarySessionBeanRemote libraryBean =
         LibrarySessionBeanRemote)ctx.lookup("LibrarySessionBean/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++;
         }
         LibrarySessionBeanRemote libraryBean1 =
            (LibrarySessionBeanRemote)ctx.lookup("LibrarySessionBean/remote");
         List<String> booksList1 = libraryBean1.getBooks();
         System.out.println(
            "***Using second lookup to get library stateless object***");
         System.out.println(
            "Book(s) entered so far: " + booksList1.size());
         for (int i = 0; i < booksList1.size(); ++i) {
            System.out.println((i+1)+". " + booksList1.get(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オブジェクトを初期化します。
  • testStatelessEjb()メソッドでは、jndiルックアップが名前-"LibrarySessionBean/remote"で実行され、リモートビジネスオブジェクト(ステートレスejb)が取得されます。
  • 次に、ユーザーにライブラリストアのユーザーインターフェイスが表示され、選択を入力するように求められます。
  • ユーザーが1を入力すると、システムはブック名を要求し、ステートレスセッションBeanのaddBook()メソッドを使用してブックを保存します。 セッションBeanは、ブックをそのインスタンス変数に保存しています。
  • ユーザーが2を入力すると、システムはステートレスセッションBeanのgetBooks()メソッドを使用して本を取得し、終了します。
  • 次に、「LibrarySessionBean/remote」という名前で別のjndiルックアップが実行されて、リモートビジネスオブジェクト(ステートレスEJB)が再度取得され、ブックのリストが作成されます。

クライアントを実行して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
***Using second lookup to get library stateless object***
Book(s) entered so far: 0
BUILD SUCCESSFUL (total time: 13 seconds)

EJBにアクセスするためのクライアントの再実行

プロジェクトエクスプローラーでEJBTester.javaを見つけます。 EJBTesterクラスを右クリックして、 run file を選択します。

Netbeansコンソールで次の出力を確認します。

run:
**********************
Welcome to Book Store
**********************
Options
1. Add Book
2. Exit
Enter Choice: 2
Book(s) entered so far: 0
***Using second lookup to get library stateless object***
Book(s) entered so far: 1
1. Learn Java
BUILD SUCCESSFUL (total time: 12 seconds)
  • 上記の出力は、JBossが保持しているステートレスEJBオブジェクトの数によって異なる場合があります。
  • 単一のステートレスEJBオブジェクトが保持されている場合、各ルックアップ後に同じブックのリストが表示される場合があります。
  • EJBコンテナは、ルックアップごとに同じステートレスEJBオブジェクトを返す場合があります。
  • ステートレスEJB Beanは、サーバーが再起動されない限り、インスタンス変数の値を保持しています。