1. コンテナ管理 Entity Bean の例題   ↑top
 ここでは、インストール時についてくるサンプルプログラムを使ってコンテナ管理 Entity Bean の動作確認をしてみます。

 日本語の解説が必要であれば、BEA社のHPを参照します。(ただし、Ver4.0.3付属のサンプルについての説明になっており、Ver4.5.1のサンプルとはファイル名などを含めて若干の違いがあり、注意が必要です)

 サンプルプログラムは以下にあります。
  C:\weblogic\examples\ejb\basic\containerManaged
 
 
2. DeploymentDescriptor.txt の編集   ↑top
 サンプルプログラムでは、データベースを用いた永続化を行う設定になっていますが、ここでは簡略化のために File による永続化に変更して動作確認することにします。メモ帳などで編集してかまいません。

    (persistentStoreProperties
      persistentStoreType         file  <− jdbc を file に修正しています
      ; Note: the value of the persistentStoreType must be lowercase.

;     persistentStoreClassName
      (file                  <− (jdbc で始まり、); end jdbc で終わる部分を削除し、変わりにこの3行を挿入する
         persistentDirectoryRoot  C:\mystore
      ); end file
    ); end persistentStoreProperties
 
 
3. コンパイル   ↑top
 コンパイルのためのスクリプトが用意されているのでそれを利用します。

C:\>cd \weblogic\examples\ejb
C:\weblogic\examples\ejb>build basic containerManaged

 環境が設定されていないため、以下のメッセージが表示されます。

  Building the examples is dependent on setting certain variables
  in your environment. Please run the setEnv.cmd script
  (found in the \weblogic directory) before running this script.

 環境設定用のスクリプト (setEnv) を実行し、再度コンパイルしてみます。

C:\weblogic\examples\ejb>cd ../../..
C:\weblogic>setEnv
C:\weblogic>cd examples\ejb
C:\weblogic\examples\ejb>build basic containerManaged

=== End of building basic\containerManaged EJBean example ===
で終われば成功で、以下のファイルが作成されます。

C:\weblogic\myserver\ejb_basic_containerManaged.jar

これはインストール時に既に用意されているようですが、一部修正を加えているので再コンパイルが必要です
 
 
4. プロパティファイルの編集   ↑top
 C:\weblogic\weblogic.properties の以下の行のコメントをはずしてください。実際はいくつかの設定がカンマでつながっているので、この2行をコピーして追加してもいいでしょう。

  weblogic.ejb.deploy=\
         C:/weblogic/myserver/ejb_basic_containerManaged.jar
 
 
5. WebLogic Server の起動   ↑top
 WebLogic Server を立ち上げます。このとき EJB も展開されます。

    Windowsのスタートメニュー  -> WebLogic 4.5.1 -> WebLogic Server

 WebLogic Console を用いて、EJBが立ち上がっていることを確認します。

    Windowsのスタートメニュー  -> WebLogic 4.5.1 -> WebLogic Console で Console を起動し、
    myserver -> distributedObjects -> ejb -> deployedBeanscontianerManaged accountHome が存在することを確認します。
 
 
6. クライアントプログラムの実行   ↑top
 以下を実行します。classがないとか言われたときは、\weblogic\setEnv を実行します。
 
C:\weblogic>java examples.ejb.basic.containerManaged.Client

Beginning containerManaged.Client...

Looking up account 10020...

Did not find 10020
Account 10020 being created; opening balance is $3000.0

Part A: Depositing $100.0
Current balance is $3100.0

Withdrawing amount greater than current balance. Expecting an exception...

【わざと起こさせた異常を検出】
Received expected Processing Error:
examples.ejb.basic.containerManaged.ProcessingErrorException: Request to withdraw $3110.0; is more than balance $3100.0 in account 10020

Start server side stack trace:
examples.ejb.basic.containerManaged.ProcessingErrorException: Request to withdraw $3110.0; is more than balance $3100.0 in account 10020
        at java.lang.Throwable.<init>(Compiled Code)
        at java.lang.Exception.<init>(Compiled Code)
        at examples.ejb.basic.containerManaged.ProcessingErrorException.<init>(ProcessingErrorException.java:24)
        at examples.ejb.basic.containerManaged.AccountBean.withdraw(AccountBean.java:198)
        at examples.ejb.basic.containerManaged.AccountBeanEOImpl.withdraw(AccountBeanEOImpl.java:160)
        at examples.ejb.basic.containerManaged.AccountBeanEOImpl_WLSkel.invoke(AccountBeanEOImpl_WLSkel.java:141)
        at weblogic.rmi.extensions.BasicServerObjectAdapter.invoke(BasicServerObjectAdapter.java:261)
        at weblogic.rmi.extensions.BasicRequestHandler.handleRequest(BasicRequestHandler.java:49)
        at weblogic.rmi.extensions.BasicRequestDispatcher$BasicExecuteRequest.execute(BasicRequestDispatcher.java:163)
        at weblogic.t3.srvr.ExecuteThread.run(Compiled Code)
End  server side stack trace
 

Part B: Creating 5 new accounts...

Created account: 9407625995190; balance is $0.0; type: Checking
Created account: 9407625995191; balance is $100.0; type: Checking
Created account: 9407625995192; balance is $200.0; type: Checking
Created account: 9407625995193; balance is $300.0; type: Checking
Created account: 9407625995194; balance is $400.0; type: Checking

5 accounts successfully created

Creating an account with a null balance...

Querying for accounts with a balance greater than 50.0...ordered by Account ID

:::::::::::::: Unexpected Error :::::::::::::::::
javax.ejb.FinderException: store doesn't support finders

Start server side stack trace:
javax.ejb.FinderException: store doesn't support finders
        at java.lang.Throwable.<init>(Compiled Code)
        at java.lang.Exception.<init>(Compiled Code)
        at javax.ejb.FinderException.<init>(FinderException.java:44)
        at examples.ejb.basic.containerManaged.AccountBeanHomeImpl.findOrderedBigAccounts(AccountBeanHomeImpl.java:167)
        at examples.ejb.basic.containerManaged.AccountBeanHomeImpl_WLSkel.invoke(AccountBeanHomeImpl_WLSkel.java:218)
        at weblogic.rmi.extensions.BasicServerObjectAdapter.invoke(BasicServerObjectAdapter.java:261)
        at weblogic.rmi.extensions.BasicRequestHandler.handleRequest(BasicRequestHandler.java:49)
        at weblogic.rmi.extensions.BasicRequestDispatcher$BasicExecuteRequest.execute(BasicRequestDispatcher.java:163)
        at weblogic.t3.srvr.ExecuteThread.run(Compiled Code)
End  server side stack trace

        at weblogic.rmi.extensions.BasicRequest.sendReceive(BasicRequest.java:44)
        at examples.ejb.basic.containerManaged.AccountBeanHomeImpl_WLStub.findOrderedBigAccounts(AccountBeanHomeImpl_WLStub.java:480)
        at examples.ejb.basic.containerManaged.AccountBeanHomeImpl_ServiceStub.findOrderedBigAccounts(Compiled Code)
        at examples.ejb.basic.containerManaged.Client.main(Compiled Code)

End containerManaged.Client...

Part B の方で、期待しない異常(Unexpected Error)が発生していますが、原因はよくわかりません。分かりしだい記述していきます。

この動作により、生成されたEJBのインスタンスが以下のファイルに永続化されます
C:\mystore\examples_ejb_basic_containerManaged_AccountBean.dat
C:\mystore\examples_ejb_basic_containerManaged_AccountBeanTmp.dat

【2回目の実行】
C:\weblogic\examples\ejb>java examples.ejb.basic.containerManaged.Client

Beginning containerManaged.Client...

Looking up account 10020...

Account 10020 found; balance is $3100.0; type: Checking

Part A: Depositing $100.0
Current balance is $3200.0

Withdrawing amount greater than current balance. Expecting an exception...

Received expected Processing Error:
examples.ejb.basic.containerManaged.ProcessingErrorException: Request to withdraw $3210.0; is more than balance $3200.0
in account 10020

Start server side stack trace:
examples.ejb.basic.containerManaged.ProcessingErrorException: Request to withdraw $3210.0; is more than balance $3200.0
in account 10020
        at java.lang.Throwable.<init>(Compiled Code)
        at java.lang.Exception.<init>(Compiled Code)
        at examples.ejb.basic.containerManaged.ProcessingErrorException.<init>(ProcessingErrorException.java:24)
        at examples.ejb.basic.containerManaged.AccountBean.withdraw(AccountBean.java:198)
        at examples.ejb.basic.containerManaged.AccountBeanEOImpl.withdraw(AccountBeanEOImpl.java:160)
        at examples.ejb.basic.containerManaged.AccountBeanEOImpl_WLSkel.invoke(AccountBeanEOImpl_WLSkel.java:141)
        at weblogic.rmi.extensions.BasicServerObjectAdapter.invoke(BasicServerObjectAdapter.java:261)
        at weblogic.rmi.extensions.BasicRequestHandler.handleRequest(BasicRequestHandler.java:49)
        at weblogic.rmi.extensions.BasicRequestDispatcher$BasicExecuteRequest.execute(BasicRequestDispatcher.java:163)
        at weblogic.t3.srvr.ExecuteThread.run(Compiled Code)
End  server side stack trace

(Part B については省略しています)
 
7. Servlet からの実行   ↑top
 先ほどは、クライアント用のJavaプログラムからEJBを呼ぶ例でしたが、今度はサーブレットからEJBを呼んでみます。ソースファイル名を以下に示します。  
  C\weblogic\examples\ejb\containerManaged\Servlet.java

 このファイルは、実は 「3.コンパイル」 の項で既にパッケージ化されており、改めてコンパイルする必要はありません。

  1.  サーブレットを有効にするために、C:\weblogic\weblogic.properties の以下の行のコメントをはずしてください。547行目あたりです。

  2. weblogic.httpd.register.containerManaged=\
        examples.ejb.basic.containerManaged.Servlet

  3. WebLogic Server を再起動します

  4. WebLogic Server の終了方法がよく分からないので、起動時に現れるコマンドプロンプトの×を押しています。
    起動は、スタートメニューから行います。
     
  5. ブラウザから呼んでみます

  6. お好きなブラウザを立ち上げ、URL として、「http://localhost:7001/containerManaged」 を入力します。

    クライアント用のJavaプログラムを実行したときと同様の結果が、ブラウザに表示されれば成功です。なぜか、先ほどのPart Bでの異常は発生しません。