ステップ 8: クライアント アプリケーション
このステップでは、Web サービスを呼び出す Java コンソール クライアントを作成します。現在、設計している Web サービスはコールバックを受信するクライアント機能をサポートしますが、コールバックを受信できるクライアントの作成については、このチュートリアルでは説明しません。その代わりに、ポーリング インタフェースをサポートするように Web サービスを変更します。つまり、Web サービスに変更を加えて、定期的にクライアントが Web サービスに結果を要求(ポーリング)できるようにします。
このステップでのタスクは次のとおりです。
ポーリング インタフェースをサポートするために Web サービスを変更するには
この節では、いつ完全な結果が Java クライアントに返されるかを Web サービスで認識できるよう、Web サービスに m_isInvestigationComplete という新しいメンバー変数を追加します。このメンバー変数は、申込者のクレジット プロファイルの作成タスクが Web サービスで完了したかどうかを記録します。m_isInvestigationComplete が false の場合、Java クライアント コンソールが情報をポーリングするたびに Web サービスから null 応答が返されます。m_isInvestigation が true になると、Web サービスから Java クライアント ポールにクレジット申込者の完全なプロファイルが返されます。
デザイン ビューを表示していない場合は、[デザイン ビュー] タブをクリックします。
次のように、[メンバー変数] を右クリックして [メンバー変数の追加] を選択します。
[新しいメンバー変数] ダイアログが表示されます。
[変数名] フィールドに次のように m_isInvestigationComplete と入力します。
[型] ドロップダウン リストの値が次のように boolean になっていることを確認します。
[OK] をクリックします。
[メンバー変数] を右クリックして [メンバー変数の追加] を選択します。[新しいメンバー変数] ダイアログが表示されます。
次のように、[変数名] フィールドに「m_callbackURL」と入力し、[型] ドロップダウン リストで [String] を選択します。
[OK] をクリックします。
[requestCreditReportAsync] をクリックします。ソース ビューに requestCreditReportAsync のコードが表示されます。
requestCreditReportAsync を次のように編集します。
public void requestCreditReportAsynch(String taxID, boolean useCallbacks) throws java.sql.SQLException { if(useCallbacks) { m_callbackURL = context.getCallbackLocation(); } /* * Query the database via the database control */ Applicant dbApplicant = bankruptciesDB.checkForBankruptcies(taxID); /* * If the database contains data on the current applicant, * assign the retrieved values to m_currentApplicant. */ if(dbApplicant != null) { m_currentApplicant = dbApplicant; creditCardReportControl.getCreditCardData(taxID); creditCardReportTimer.start(); } /* * If the database contains no data on the current applicant, * inform the client. */ else { /* * If the member variable m_callbackURL has value other than null, * then send a message to the client via the callback */ if( m_callbackURL != null ) { callback.onCreditReportDone(null, "No bankruptcy data found on applicant " + taxID + "."); } /* * If the member variable m_callbackURL has the value null, * then the client cannot accept callbacks. * Make the message available to the client through the synchronous method * checkForResults. */ else { /* * Mark the investigation as complete by setting the isInvestigationComplete * field to "true". Client's that cannot accept callbacks will call checkForResults * to poll for investigation results; m_isInvestigationComplete is used to determine * what checkForResults should return. */ m_isInvestigationComplete = true; m_currentApplicant.approvalLevel = "No bankruptcy data found on applicant " + taxID + "."; } } }
デザイン ビューを表示していない場合は、[デザイン ビュー] タブをクリックします。receiveMessage(JMS コントロール上のコールバック ハンドラ)をクリックします。ソース ビューに creditScoreJWS_receiveMessage コードが表示されます。
コードを次のように編集します。
private void creditScoreJMS_receiveMessage(int score) throws java.rmi.RemoteException { m_currentApplicant.creditScore = score; /* * Pass the credit score to the EJB's validate method. Store the value returned * with other applicant data. */ m_currentApplicant.approvalLevel = validateCreditEJB.validate(m_currentApplicant.creditScore); /* * If the member variable m_callbackURL has value other than null, * then send a message to the client via the callback */ if( m_callbackURL != null ) { callback.onCreditReportDone(m_currentApplicant, "Credit score received."); } /* * If the member variable m_callbackURL has the value null, * then the client cannot accept callbacks. * Make the message available to the client through the synchronous method * checkForResults. */ else { /* * Mark the investigation as complete by setting the isInvestigationComplete * field to "true". Client's that cannot accept callbacks will call checkForResults * to poll for investigation results; m_isInvestigationComplete is used to determine * what checkForResults should return. */ m_isInvestigationComplete = true; } }
デザイン ビューを表示していない場合は、[デザイン ビュー] タブをクリックします。[cancelInvestigation] をクリックします。ソース ビューに cancelInvestigation のコードが表示されます。
コードを次のように編集します。
public void cancelInvestigation() { /* Cancel the request to the credit card company because it is now unnecessary.*/ creditCardReportControl.cancelRequest(); /* * If the member variable m_callbackURL has a value other than null, * then send a message to the client via the callback */ if( m_callbackURL != null ) { callback.onCreditReportDone(null, "Investigation canceled at client's request."); } /* * If the member variable m_callbackURL has the value null, * then the client cannot accept callbacks. * Make the message available to the client through the synchronous method * checkForResults. */ else { /* * Mark the investigation as complete by setting the isInvestigationComplete * field to "true". Client's that cannot accept callbacks will call checkForResults * to poll for investigation results; m_isInvestigationComplete is used * to determine what checkForResults should return. */ m_isInvestigationComplete = true; m_currentApplicant.approvalLevel = "Investigation canceled at client's request."; } }
デザイン ビューを表示していない場合は、[デザイン ビュー] タブをクリックします。[onTimeout] をクリックします。ソース ビューに creditCardReportTimer_onTimout コードが表示されます。
コードを次のように編集します。
private void creditCardReportTimer_onTimeout(long time) { /* Because the credit card service has not yet returned, cancel the request.*/ creditCardReportControl.cancelRequest(); /* * If the member variable m_callbackURL has a value other than null, * then send a message to the client via the callback */ if( m_callbackURL != null ) { callback.onCreditReportDone(null, "Unable to retrieve credit card information."); } /* * If the member variable m_callbackURL has the value null, * then the client cannot accept callbacks. * Make the message available to the client through the synchronous method * checkForResults. */ else { /* * Mark the investigation as complete by setting the isInvestigationComplete * field to "true". Clients that cannot accept callbacks will call checkForResults * to poll for investigation results; m_isInvestigationComplete is used to determine * what checkForResults should return. */ m_isInvestigationComplete = true; m_currentApplicant.approvalLevel = "Unable to retrieve credit card information."; } }
[ソース ビュー] タブをクリックします。次のように、[ソース ビュー] タブのすぐ下にある [クラス] ドロップダウン リストから [context] を選択します。
次のように、クラス ドロップダウン リストの右側にある メンバー ドロップダウン リストから [onException] を選択します。
JwsContext の onException コールバック ハンドラのソース コードが表示されます。
コードを次のように編集します。
public void context_onException(Exception e, String methodName, Object[] arguments) { /* Create a logger variable to use for logging messages. Assigning it the * "Investigate" category name will make it easier to find messages from this * service in the log file. */ Logger logger = context.getLogger("Investigate"); /* Log an error message, giving the name of the method that threw the * exception and a stack trace from the exception object. */ logger.error("Exception in " + methodName + ": " + e); /* * If the member variable m_callbackURL has a value other than null, * then send a message to the client via the callback */ if( m_callbackURL != null ) { callback.onCreditReportDone(null, "Unable to respond to request at this time. " + "Please contact us at 555-5555."); } /* * If the member variable m_callbackURL has the value null, * then the client cannot accept callbacks. * Make the message available to the client through the synchronous method * checkForResults. */ else { /* * Mark the investigation as complete by setting the isInvestigationComplete * field to "true". Client's that cannot accept callbacks will call checkForResults * to poll for investigation results; m_isInvestigationComplete is used to determine * what checkForResults should return. */ m_isInvestigationComplete = true; m_currentApplicant.approvalLevel = "Unable to respond to request at this time. " + "Please contact us at 555-5555."; } }
次のメソッドを Web サービスに追加します。 このメソッドをソース ビューに直接コピー アンド ベーストできます。
/** * @jws:operation * @jws:conversation phase="continue" */ public Applicant checkForResults() { /* * Clients that do not support callbacks can invoke this method to receive complete results * on a credit applicant. * This method will return null if the Investigate web service has not completed its profile * of an applicant. It will return a full profile on an applicant once the web service has * completed its investigation. */ Applicant retval = null; if(m_isInvestigationComplete == true){ retval = m_currentApplicant; context.finishConversation(); } return retval; }
〔Ctrl〕+〔S〕を押して Investigate.jws を保存します。
このタスクでは、Web サービスと Java コンソール クライアントとのインタフェースを構成する、JAR(Java Application Archive)ファイルを保存します。この JAR ファイルには、Web サービスのプロキシを構成する Java クラスが記述されています。クライアントでは、このプロキシを介して実際の Web サービスを呼び出すことができます。
〔F5〕を押して Web サービスをコンパイルします。Web ブラウザにテスト ビューが表示されます。
ブラウザ ウィンドウのテスト ビューの上部には、[Overview]、[Console]、[Test Form]、[Test XML]、および [Warnings] の 5 つのタブが表示されます。ここでは、[Overview] タブをクリックします。
次のように、[Web Service Clients] の下の [Java Proxy] をクリックします。
次のような [ファイルのダウンロード] ダイアログが表示されます。
[保存] をクリックすると、[名前を付けて保存] ダイアログが表示されます。
[保存する場所] ドロップダウン リストで
BEA_HOME\weblogic700\samples\workshop\applications\samples\WEB-INF\lib
を選択します。
注意 : BEA_HOME は WebLogic Server 7.0 をインストールしたディレクトリです。 WebLogic Server 7.0 を C:\bea にインストールした場合は、
C:\bea\weblogic700\samples\workshop\applications\samples\WEB-INF\lib
を選択します。
[ファイルの種類] で次のように [すべてのファイル] が選択されていることを確認します。
[保存] をクリックすると、Investigate.jar が保存され、[ダウンロードの完了] ダイアログが表示されます。
[閉じる] をクリックします。
環境変数 BEA_HOME および PATH を編集するには
このステップでは、2 つの環境変数にパスを設定します。これらの環境変数を設定することで、ユーザのマシンは Java クライアント アプリケーションをコンパイルするためのプログラムを見つけることが可能になります。
コマンド プロンプト ウィンドウを開きます。
WebLogic Server 7.0 をインストールしたディレクトリを指定します。WebLogic Server 7.0 のインストール時に明示的に指定しなかった場合は、インストール ディレクトリは C:\bea です。
コマンド プロンプトで、次のコマンドを入力します。 [BEA インストール ディレクトリ] の部分は、インストール ディレクトリで置き換えます。 WebLogic Server 7.0 を C:\bea にインストールした場合は、set BEA_HOME=C:\bea と入力します。
set BEA_HOME=[BEA インストール ディレクトリ]
set PATH=%PATH%;%BEA_HOME%\jdk131_03\binこのコマンドを入力することで、ユーザのマシンが Java クライアント アプリケーションをコンパイルするためのプログラムを見つけることが可能になります。
Java クライアント アプリケーションをコンパイルおよび実行するには
以上の作業で JAR ファイルが適切に配置され、環境変数が設定されたので、Java クライアント アプリケーションをコンパイルおよび実行する準備が整いました。
先ほどのコマンド プロンプトで次のコマンドを入力します。
cd %BEA_HOME%\weblogic700\samples\workshop\applications\samples\tutorials\java_client
〔Enter〕 を押します。
Java コンソール クライアントをコンパイルするには、次のコマンドを入力します。
compile
〔Enter〕 を押します。
Java コンソール クライアントを実行するには、次のコマンドを入力します。
run
〔Enter〕 を押します。
納税者 ID 番号を入力するよう求められたら、次の 9 桁の数字のうちの 1 つを入力します。
123456789, 111111111, 222222222, 333333333, 444444444, 555555555
〔Enter〕 を押します。
クライアントは、クレジット レポートが用意されるまで 1 秒ごとにポーリングします。用意ができた時点で Java コンソール アプリケーションによって結果が表示されます。
注意 : クライアントではコールバックをサポートしていないという内容の警告が表示される場合があります。ただし、Java コンソール クライアントの場合、Web サービスの応答を受信する際にコールバックに依存しないので、この警告は無視しても構いません。
チュートリアルの前後のステップに移動するには、次のいずれかの矢印をクリックしてください。