Step 5: Add a JMS and EJB Control

In this step, you will take the data you have gathered and request help from an in-house application to generate a credit score. You will then pass the credit score to an Enterprise Java Bean (EJB) that will measure the applicant's credit risk.

To generate the credit score, you will use an in-house credit scoring application designed to do just this sort of work. To make this legacy application available to other components, it has been exposed to other parts of the company through a messaging system. That's how your web service will get to it. You will send an XML message to the application with applicant data, and receive an XML message with 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 because 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 Java Message Service (JMS), provided with WebLogic Server, supports several ways for applications to use messages. In point-to-point messaging, which you will use here, the message's sender and receiver are both clients of the messaging system (or "JMS provider"). The first client begins the exchange by sending a message to a queue. A queue is a kind of message waypoint to which other clients are listening. When the message arrives at the queue, the receiving client picks it up and reads it. You will use the JMS control to send a message (containing the data you have collected) to a queue that the credit scoring application is listening to. The application will send its response message (containing a credit score) to a queue that your service is listening to.

The tasks in this step are:

- To add a JMS control for requesting a credit score

- To add XML maps for JMS control

- To edit the callback handlers

- To add an EJB control for requesting a credit approval rating

- To add code that requests credit validation

- To launch the Test View

To Add a JMS Control for Requesting a Credit Score

1. If you are not in Design View, click the Design View tab.

2. From the Add Control drop-down list, select Add JMS Control. The Add JMS Control dialog appears.

3. Enter values as shown in the following illustration:

Note that the message type is XML Map. XML maps are a powerful tool for turning your Java types into XML. These will be covered in more detail later in this tutorial.

4. Click Create.

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

To Add XML Maps to the JMS Control

1. Double-click the arrow corresponding to the sendMessage method. The Edit Maps and Interface dialog appears.

2. Confirm that the Message XML tab is selected.

3. Edit the contents of the Java field so that it contains the following:

    public void sendMessage(int availableCredit, boolean bankruptcy)

Using this method, you will send data about the applicant to the credit scoring application.

4. Leaving the dialog open, edit the contents of the top box so it contains the following:

    <score_request>
        <credit_remaining>{availableCredit}</credit_remaining>
        <is_bankrupt>{bankruptcy}</is_bankrupt>
    </score_request>

Note: This code represents an XML map. The <score_request>, <credit_remaining>, and <is_bankrupt> elements define the XML expected by the credit scoring application. Notice that the text in curly braces matches parameters of the declaration. This tells WebLogic Server how to insert the values passed to the sendMessage method into the XML. The XML is then sent to the application.

5. Click OK.

6. Double-click the arrow corresponding to the receiveMessage callback handler. The Edit Maps and Interface dialog appears.

7. On the Message XML tab, edit the XML map code as follows:

    <score_response>
        <calculated_score>{score}</calculated_score>
    </score_response>

8. Edit the Java code as follows:

    public void receiveMessage(int score)

This adds an XML map to the callback handler. The map matches the format of the expected XML response. Placing the score parameter in the curly braces tells WebLogic Server to pass the returned data to the callback handler's score parameter.

9. Click OK.

To Edit the Callback Handlers

1. In Design View, click the name of the creditCardDataResult callback handler to view its source code.

2. Edit the creditCardDataResult callback handler code so that it appears as follows. Make sure to delete the line of code shown crossed out below.

        private void creditCardReportControl_creditCardDataResult(CreditCard[] cards)
        {
            for(int i = 0; i < cards.length; i++)
            {
                m_currentApplicant.availableCCCredit += cards[i].availableCredit;
            }
            /*
             * Use the JMS control to send the available credit and bankruptcy information to the credit
             * scoring application.
             */
            creditScoreJMS.sendMessage(m_currentApplicant.availableCCCredit,
                m_currentApplicant.currentlyBankrupt);
            callback.onCreditReportDone(m_currentApplicant, null);
        }

3. In Source View, immediately beneath the Source View tab, from the class drop-down list, select creditScoreJMS, as shown here:

4. To the right of the class drop-down list, from the member drop-down list, select receiveMessage, as shown here.

The source code for the JMS control's callback handler appears.

5. Edit the receiveMessage callback handler code so that it appears as follows:

    private void creditScoreJMS_receiveMessage(int score)
    {
        /* Store the score returned with other data about the applicant. */
        m_currentApplicant.creditScore = score;
    }

To Add an EJB Control for Requesting a Credit Approval Rating

Now that you have a way to retrieve a credit score, you will use the score to determine whether the applicant deserves credit approval. For this you will use an existing stateless session Enterprise Java Bean (EJB). EJBs are Java software components of enterprise applications. The Java 2 Enterprise Edition (J2EE) Specification defines the types and capabilities of EJBs as well as the environment (or container) in which EJBs are deployed and execute. From a software developer’s point of view, there are two aspects to EJBs: development and deployment of EJBs; and use of EJBs from client software. WebLogic Workshop provides the EJB control as a simplified way to act as a client of an existing EJB from within a web service.
The ValidateCredit EJB you will be accessing is designed to take the credit score and return response about the applicant's credit worthiness. To access the ValidateCredit bean, you will add an EJB control.

Note: In order for you add an EJB control to a web service project, the compiled home and remote interfaces for the EJB must be in the project also. For the sake of the tutorial, the JAR file containing the ValidateCredit bean has already been copied to the WEB-INF\lib folder of the samples project. Having the bean's JAR file in the WEB-INF\lib folder ensures that WebLogic Workshop can find these interfaces. (For more information, see Creating a New EJB Control.)

1. If you are not in Design View, click the Design View tab.

2. From the Add Control drop-down list, select Add EJB Control. The Add EJB Control dialog appears.

3. Enter values as shown in the following illustration. Browse for the jndi-name value required in Step 3 of the dialog. When you select it from the list and click Select, the home interface and bean interface values will be added automatically.

4. Click Create.

You can see that WebLogic Workshop has added a validateCreditEJB control to your design, as shown below. The control shows that the EJB exposes two methods: create and validate. The validate method is the one you will use (all EJBs expose a create method that is used by WebLogic Server; you will not need to call this).
Now you need to connect the score received through the creditScore JMS control with the validateCredit EJB control. You will do this by updating the JMS control's callback handler. The code you add will send the credit score to the EJB for validation.

To Add Code that Requests Credit Validation

1. If you are not in Design View, click the Design View tab.

2. Click the name of the receiveMessage callback handler. Its source code appears in Source View.

3. Edit the receiveMessage code so that it appears as follows:

    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);
        /*
         * Send the Applicant object, now complete with all the data the client requested,
         * to the client using the callback.
         */
        callback.onCreditReportDone(m_currentApplicant, "Credit score received.");
    }

You've added a lot of functionality in these last steps. Now it's time to try it out and see if it all works.

To Launch Test View

1. Click the Start button. As before, this launches your browser to display the Test View.

2. When Test View launches, type one of the following nine-digit numbers in the taxID box for requestCreditReportAsync, as shown below:
   123456789, 111111111, 222222222, 333333333, 444444444, 555555555

3. Click requestCreditReportAsync.

2. Click the Refresh button to reload Test View until callback.onCreditReportDone appears in the Message Log. Your screen should resemble the following:

Notice that the <creditScore> and <approvalLevel> elements of the response now have values in them. This means that in just the few steps of this topic, you have connected the Investigate web service with a JMS messaging system and an Enterprise Java Bean.

In the next step you will enhance the service's design to return its result in a more generic format, making it more broadly useful.

Related Topics

Using a Control
JMS Control: Using Java Message Service Queues and Topics from Your Web Service
EJB Control: Using Enterprise Java Beans from a Web Service
Click one of the following arrows to navigate through the tutorial.