Ejb-callbacks
提供:Dev Guides
EJB-コールバック
コールバックは、エンタープライズBeanのライフサイクルを傍受できるメカニズムです。 EJB 3.0仕様では、コールバックハンドラメソッドが作成されるコールバックが指定されています。 EJBコンテナはこれらのコールバックを呼び出します。 EJBクラス自体または別のクラスでコールバックメソッドを定義できます。 EJB 3.0は、コールバック用の多くの注釈を提供しています。
以下は、ステートレスBeanのコールバックアノテーションのリストです-
Annotation | Description |
---|---|
@PostConstruct | Invoked when a bean is created for the first time. |
@PreDestroy | Invoked when a bean is removed from the bean pool or is destroyed. |
以下は、ステートフルBeanのコールバックアノテーションのリストです-
Annotation | Description |
---|---|
@PostConstruct | Invoked when a bean is created for the first time. |
@PreDestroy | Invoked when a bean is removed from the bean pool or is destroyed. |
@PostActivate | Invoked when a bean is loaded to be used. |
@PrePassivate | Invoked when a bean is put back to bean pool. |
以下は、メッセージ駆動型Beanのコールバックアノテーションのリストです-
Annotation | Description |
---|---|
@PostConstruct | Invoked when a bean is created for the first time. |
@PreDestroy | Invoked when a bean is removed from the bean pool or is destroyed. |
以下は、エンティティBeanのコールバックアノテーションのリストです-
Annotation | Description |
---|---|
@PrePersist | Invoked when an entity is created in database. |
@PostPersist | Invoked after an entity is created in database. |
@PreRemove | Invoked when an entity is deleted from the database. |
@PostRemove | Invoked after an entity is deleted from the database. |
@PreUpdate | Invoked before an entity is to be updated in the database. |
@PostLoad | Invoked when a record is fetched from database and loaded into the entity. |
応用例
テスト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 - Persistence chapter as such for this chapter to add various callbacks to EJB. |
2 | Create LibrarySessionBean.java and LibrarySessionBeanRemote as explained in the EJB - Create Application chapter. Keep rest of the files unchanged. |
3 | Use Beans created in the EJB - Persistence chapter. Add callback methods as shown below. Keep rest of the files unchanged. |
4 | Create a java class BookCallbackListener under package com.finddevguides.callback. This class will demonstrates the separation of callback methods. |
5 | Clean and Build the application to make sure business logic is working as per the requirements. |
6 | 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. |
7 | 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モジュール)
BookCallbackListener.java
Book.java
LibraryStatefulSessionBean.java
LibraryStatefulSessionBeanRemote.java
LibraryPersistentBean.java
LibraryPersistentBeanRemote.java
- EjbComponentプロジェクトをJBOSSにデプロイするとすぐに、jbossログに注目してください。
- JBossは、セッションBeanのJNDIエントリ- LibraryPersistentBean/remote を自動的に作成しました。
- この検索文字列を使用して、タイプのリモートビジネスオブジェクトを取得します- com.finddevguides.stateless.LibraryPersistentBeanRemote
JBoss Application Serverのログ出力
EJBTester(EJBクライアント)
jndi.properties
- これらのプロパティは、JavaネームサービスのInitialContextオブジェクトを初期化するために使用されます。
- InitialContextオブジェクトは、ステートレスセッションBeanのルックアップに使用されます。
EJBTester.java
EJBTesterは次のタスクを実行します-
- jndi.propertiesからプロパティをロードし、InitialContextオブジェクトを初期化します。
- testStatelessEjb()メソッドでは、jndiルックアップが「LibrarySessionBean/remote」という名前で実行され、リモートビジネスオブジェクト(ステートレスEJB)が取得されます。
- 次に、ユーザーにライブラリストアのユーザーインターフェイスが表示され、ユーザーは選択肢を入力するよう求められます。
- ユーザーが1を入力すると、システムはブック名を要求し、ステートレスセッションBeanのaddBook()メソッドを使用してブックを保存します。 セッションBeanはデータベースに本を保存しています。
- ユーザーが2を入力すると、システムはステートレスセッションBeanのgetBooks()メソッドを使用して本を取得し、終了します。
クライアントを実行してEJBにアクセスする
プロジェクトエクスプローラーでEJBTester.javaを見つけます。 EJBTesterクラスを右クリックして、 run file を選択します。
Netbeansコンソールで次の出力を確認します。
JBoss Application Serverのログ出力
JBossログで次のコールバックエントリを見つけることができます