ステップ 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 クライアント ポールにクレジット申込者の完全なプロファイルが返されます。

  1. デザイン ビューを表示していない場合は、[デザイン ビュー] タブをクリックします。

  2. 次のように、[メンバー変数] を右クリックして [メンバー変数の追加] を選択します。

[新しいメンバー変数] ダイアログが表示されます。

  1. [変数名] フィールドに次のように m_isInvestigationComplete と入力します。

  2. [] ドロップダウン リストの値が次のように boolean になっていることを確認します。

  1. [OK] をクリックします。

  2. [メンバー変数] を右クリックして [メンバー変数の追加] を選択します。[新しいメンバー変数] ダイアログが表示されます。

  3. 次のように、[変数名] フィールドに「m_callbackURL」と入力し、[型] ドロップダウン リストで [String] を選択します。

  4. [OK] をクリックします。

  5. [requestCreditReportAsync] をクリックします。ソース ビューに requestCreditReportAsync のコードが表示されます。  

  6. 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 + ".";
            }
        }
    }
  1. デザイン ビューを表示していない場合は、[デザイン ビュー] タブをクリックします。receiveMessage(JMS コントロール上のコールバック ハンドラ)をクリックします。ソース ビューに creditScoreJWS_receiveMessage コードが表示されます。

  2. コードを次のように編集します。

    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;
        }
    }
  1. デザイン ビューを表示していない場合は、[デザイン ビュー] タブをクリックします。[cancelInvestigation] をクリックします。ソース ビューに cancelInvestigation のコードが表示されます。

  2. コードを次のように編集します。

    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.";
        }
    }
  1. デザイン ビューを表示していない場合は、[デザイン ビュー] タブをクリックします。[onTimeout] をクリックします。ソース ビューに creditCardReportTimer_onTimout コードが表示されます。

  2. コードを次のように編集します。

    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.";
        }
    }
  1. [ソース ビュー] タブをクリックします。次のように、[ソース ビュー] タブのすぐ下にある [クラス] ドロップダウン リストから [context] を選択します。

  2. 次のように、クラス ドロップダウン リストの右側にある メンバー ドロップダウン リストから [onException] を選択します。

    JwsContext の onException コールバック ハンドラのソース コードが表示されます。

  3. コードを次のように編集します。

    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.";
        }
    }
  1. 次のメソッドを 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;
    }
  1. 〔Ctrl〕+〔S〕を押して Investigate.jws を保存します。

Web サービスのプロキシ クラスを保存するには

このタスクでは、Web サービスと Java コンソール クライアントとのインタフェースを構成する、JAR(Java Application Archive)ファイルを保存します。この JAR ファイルには、Web サービスのプロキシを構成する Java クラスが記述されています。クライアントでは、このプロキシを介して実際の Web サービスを呼び出すことができます。

  1. 〔F5〕を押して Web サービスをコンパイルします。Web ブラウザにテスト ビューが表示されます。

  2. ブラウザ ウィンドウのテスト ビューの上部には、[Overview]、[Console]、[Test Form]、[Test XML]、および [Warnings] の 5 つのタブが表示されます。ここでは、[Overview] タブをクリックします。

  3. 次のように、[Web Service Clients] の下の [Java Proxy] をクリックします。


次のような [ファイルのダウンロード] ダイアログが表示されます。

  1. [保存] をクリックすると、[名前を付けて保存] ダイアログが表示されます。

  2. [保存する場所] ドロップダウン リストで

    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


    を選択します。

  3. [ファイルの種類] で次のように [すべてのファイル] が選択されていることを確認します。

  1. [保存] をクリックすると、Investigate.jar が保存され、[ダウンロードの完了] ダイアログが表示されます。

  2. [閉じる] をクリックします。

環境変数 BEA_HOME および PATH を編集するには

このステップでは、2 つの環境変数にパスを設定します。これらの環境変数を設定することで、ユーザのマシンは Java クライアント アプリケーションをコンパイルするためのプログラムを見つけることが可能になります。  

  1. コマンド プロンプト ウィンドウを開きます。

  2. WebLogic Server 7.0 をインストールしたディレクトリを指定します。WebLogic Server 7.0 のインストール時に明示的に指定しなかった場合は、インストール ディレクトリは C:\bea です。

  3. コマンド プロンプトで、次のコマンドを入力します。 [BEA インストール ディレクトリ] の部分は、インストール ディレクトリで置き換えます。 WebLogic Server 7.0 を C:\bea にインストールした場合は、set BEA_HOME=C:\bea と入力します。

set BEA_HOME=[BEA インストール ディレクトリ]
  1. 〔Enter〕 を押します。
  2. コマンド プロンプトで、次のコマンドを入力します。
set PATH=%PATH%;%BEA_HOME%\jdk131_03\bin
このコマンドを入力することで、ユーザのマシンが Java クライアント アプリケーションをコンパイルするためのプログラムを見つけることが可能になります。
  1. 〔Enter〕 を押します。

Java クライアント アプリケーションをコンパイルおよび実行するには

以上の作業で JAR ファイルが適切に配置され、環境変数が設定されたので、Java クライアント アプリケーションをコンパイルおよび実行する準備が整いました。

  1. 先ほどのコマンド プロンプトで次のコマンドを入力します。

cd %BEA_HOME%\weblogic700\samples\workshop\applications\samples\tutorials\java_client
  1. 〔Enter〕 を押します。

  2. Java コンソール クライアントをコンパイルするには、次のコマンドを入力します。

compile
  1. 〔Enter〕 を押します。

  2. Java コンソール クライアントを実行するには、次のコマンドを入力します。

run
  1. 〔Enter〕 を押します。

  2. 納税者 ID 番号を入力するよう求められたら、次の 9 桁の数字のうちの 1 つを入力します。

123456789, 111111111, 222222222, 333333333, 444444444, 555555555
  1. 〔Enter〕 を押します。

クライアントは、クレジット レポートが用意されるまで 1 秒ごとにポーリングします。用意ができた時点で Java コンソール アプリケーションによって結果が表示されます。

注意 : クライアントではコールバックをサポートしていないという内容の警告が表示される場合があります。ただし、Java コンソール クライアントの場合、Web サービスの応答を受信する際にコールバックに依存しないので、この警告は無視しても構いません。

チュートリアルの前後のステップに移動するには、次のいずれかの矢印をクリックしてください。