# Step 4: Add a JMS Control

In this step, you will add functionality to generate a credit score for the loan applicant.

To generate the credit score, you will use an in-house, legacy credit scoring application. (The legacy application is already running on WebLogic Server as part of the Workshop sample domain: this is the server you selected when you created the Tutorial: Java Control application.) To make this legacy application available to other components, it has been exposed through a Java messaging service (JMS). You will send an XML message to the credit scoring application containing loan applicant data, and receive an XML message containing a credit score.

Messaging systems are often used to create bridges between otherwise incompatible software components. Messaging is also useful when asynchronous communication is needed, since the requesting component doesn't have to wait for a response in order to continue working. Here, asynchrony is useful because you have no way of knowing how long the credit scoring application will take to respond to your request. For more information about messaging, see Overview: Messaging Systems and JMS.

The tasks in this step are:

- To add a JMS control
- To add code to construct an XML message
- To add code to invoke the JMS control
- To edit the callback handler to parse the XML response
- To test the Investigate Java Control

To Add a JMS Control

1. On the Application tab, double click InvestigateImpl.jcs. (Don't confuse your Java control with its test web service!)
2. If necessary, click the Design View tab.
3. From the Insert menu select Controls --> JMS.
   The Insert Control - JMS dialog appears.
4. Enter values as shown in the following illustration.
   Note: Use the Browse buttons in the dialog below to fill in the send-jndi-name, receive-jndi-name, and connection-factory fields.
   Note: The value for the connection-factory field is not fully displayed by the illustration below. If you enter the value manually instead of using the Browse button, you must enter weblogic.jws.jms.QueueConnectionFactory.

5. Click Create.

Your Java control now includes the creditScoreJMS control, as shown in the following illustration.

To Add Code to Construct an XML Message

The credit score application exposed over JMS is designed to expect an incoming XML message of the following shape.

    <score_request>
      <credit_remaining>[integer_value]</credit_remaining>
      <is_bankrupt>[boolean_value]</is_bankrupt>
    </score_request>

In this task you will add a method that constructs a message of this shape using XMLBeans compiled from the schema file ScoreMessageJMS.xsd, a schema file already included in the Schemas project.

1. Confirm that InvestigateImpl.jcs is displayed in the main work area.
2. Click the Source View tab.
3. Add the following import statements to the list of import statements at the top of InvestigateImpl.jcs, directly underneath the package declaration.

       import org.openuri.bea.samples.workshop.ScoreRequestDocument;
       import org.openuri.bea.samples.workshop.ScoreRequestDocument.ScoreRequest;
       import org.openuri.bea.samples.workshop.ScoreResponseDocument;

   Note: these XMLBean classes know how to parse and construct the XML documents that form the JMS traffic between the Investigate Java control and the legacy credit scoring application. These XMLBean classes were produced by the compilation of the schema file ScoreMessageJMS.xsd.
4. Add the following method to the body of InvestigateImpl.jcs.

           /**
            * This method uses the XMLBean ScoreRequestDocument (compiled from the XML schema
            * ScoreMessageJMS.xsd) to construct a string of the following form.
            *
            * <score_request>
            *   <credit_remaining>[integer_value]</credit_remaining>
            *   <is_bankrupt>[boolean_value]</is_bankrupt>
            * </score_request>
            */
           private String makeMessageToJMS(int availableCredit, boolean currentlyBankrupt)
           {
               ScoreRequestDocument reqDoc = ScoreRequestDocument.Factory.newInstance();
               ScoreRequest req = reqDoc.addNewScoreRequest();
               req.setCreditRemaining((short)availableCredit);
               req.setIsBankrupt(currentlyBankrupt);
               String str = reqDoc.xmlText();
               return reqDoc.xmlText();
           }

5. Press Ctrl+S to save your work.

To Add Code to Invoke the JMS Control

In this task you will add code to invoke the JMS control.

1. Confirm that InvestigateImpl.jcs is displayed in the main work area.
2. Click the Design View tab.
3. Click the link text for the onCreditCardDataReady callback handler.
4. Edit the creditCardReportControl_onCreditCardDataReady callback handler code so it appears as follows. Code to edit appears in red. Make sure to delete the lines of code shown crossed out below.

           public void creditCardReportControl_onCreditCardDataReady(java.lang.String cardData)
           {
               try
               {
                   /*
                    * When a report is delivered, extract the relevant credit card info,
                    * and store it in m_currentApplicant
                    */
                   CreditCardDataDocument cardInfo = CreditCardDataDocument.Factory.parse(cardData);
                   CustomerType[] customer = cardInfo.getCreditCardData().getCustomerArray();
                   CardType[] cards = customer[0].getCardArray();
                   for(int i = 0; i < cards.length; i++)
                   {
                       m_currentApplicant.availableCCCredit += cards[i].getAvailableCredit();
                   }

                   /*
                    * Send the complete report back to the client.
                    */
                   // callback.onCreditReportDone(m_currentApplicant);

               }
               catch(XmlException xe)
               {
                   /*
                    * If there is a problem with extracting the credit card info,
                    * store an error message in m_currentApplicant.
                    */
                   m_currentApplicant.message =  "There was an error retrieving the credit card information. Please call (555) 555-5555 for assistance.";

                    /*
                     * Send an error message back to the client.
                     */
                    callback.onCreditReportDone(m_currentApplicant);
               }

               /*
                * Construct and send an XML message through the JMS control
                * to the credit scoring application.
                */
               String messageToJMSControl = makeMessageToJMS(m_currentApplicant.availableCCCredit, m_currentApplicant.currentlyBankrupt);
               creditScoreJMS.subscribe();
               creditScoreJMS.sendTextMessage(messageToJMSControl);
           }

To Edit the Callback Handler to Parse the XML Response

In this task you will use the XBeans classes to parse the XML response from the JMS control and extract the credit score.

1. Confirm that InvestigateImpl.jcs is displayed in the main work area.
2. Click the Design View tab.
3. Click the link text for the receiveTextMessage callback handler.
4. Edit the creditScoreJMS_receiveTextMessage callback handler code so that it appears as follows:

           public void creditScoreJMS_receiveTextMessage(java.lang.String payload)
           {
               try
               {
                   /*
                    * Extract the the credit score from the JMS response string
                    * and store it in m_currentApplicant.
                    */
                   ScoreResponseDocument scoreInfo = ScoreResponseDocument.Factory.parse(payload);
                   m_currentApplicant.creditScore = scoreInfo.getScoreResponse().getCalculatedScore();

                   /*
                    * Send the complete report back to the client.
                    */
                   callback.onCreditReportDone(m_currentApplicant);
               }
               catch(XmlException e)
               {
                   m_currentApplicant.message = "There was a problem retrieving the credit score.  Please call (555) 555-5555.";

                  /*
                   * Send the the error message back to the client.
                   */
                  callback.onCreditReportDone(m_currentApplicant);
               }
           }

5. Click Ctrl+S to save your work.

To Test the Investigate Java Control

Note: Due to a problem in WebLogic Workshop, before you test the Investigate control again, you may need to dirty the InvestigateTest.jws web service and save it again. This will ensure that the test web service is redeployed to the server and that it exhibits the expected changes. You can do this by simply adding a comment to the source code for InvestigateTest.jws.

1. From the Window menu, select InvestigateTest.jws.
2. Click the Start button, shown here:

   Workshop builds InvestigateTest.jws and launches the Workshop Test Browser.
3. In the Workshop Test Browser, in the taxID field, enter the 9 digit number 555555555 and click the requestCreditReport button.

   Note: Use one of the following (9 digit) taxID's to test your Java control throughout the tutorial:

   123456789, 111111111, 222222222, 333333333, 444444444, and 555555555.

4. Click Refresh until callback.onCreditReportDone appears in the Message Log. Click callback.onCreditReportDone.
   Note that a new piece of information appears in the applicant profile: <creditScore>810</creditScore>. This new piece of information was returned by the legacy credit scoring application exposed over JMS.

5. Return to WebLogic Workshop, and click the Stop button to close the Workshop Test Browser.

Related Topics

JMS Control

Getting Started with XMLBeans

Click one of the following arrows to navigate through the tutorial: