|
|
A Tour Through WebLogic Workshop 8.1: Westside Auto Sales Java Message Service TopicsWhen a user purchases a car, we want to order another car from the manufacturer to replace the car purchased. However, we don't need to order the car immediately. In fact, it's more important to return a confirmation to the user as soon as possible. We really just want to send a message to the car ordering system that a new car needs to be ordered whenever they can get around to it. This scenario is perfect for JMS because of the disconnected nature of messaging. We will build two controls to enable this: a Java control to send messages to the topic, and a message-driven EJB to pick the messages up and initiate the ordering of the new vehicle. The first thing we need is a way to get messages to the JMS topic. We can easily do this by creating a JMS control. Right-click the Controls directory and select "New Java Control". Choose "JMS" and name it "MessageSender". The message type will be "Text/XMLBean" and the destination type is "Topic". Browse for the send-jndi-name and choose "WASTopic" (the topic we created in the Workshop domain earlier). Ignore the receive topic (since our EJB will receive the message) and scroll down to click on "browse" for the connection-factory (use the default: "weblogic.jws.jms.QueueConnectionFactory"). Click "Create". Now, switch back to the WASControl and in design view, drag the new "MessageSender.jcx" onto the control. All we need to do to send a message to the JMS topic is add the following in our purchase car method (new code in italics):
Now we just need something to listen for the message. We will do this with a message-driven EJB. Back in our EJB project, right-click on the "Westside" folder and choose "New/Message-Driven Bean". We will name this "ListenerBean.ejb". First we need to tell the bean which topic to listen to. In the EJBGen properties pane, change the destination-type to "javax.jms.Topic" and the destination-jndi-name to "WASTopic". We will add code later to order the new vehicle once we build our Web service. For now, just add the following code to the
Let's give it a try. First save any dirty files and build your EJB project again. Now fire up the application, go to inventory, select a car, and click the "buy" link. You should get a confirmation that you successfully purchased the car along with your bill. Again, a new car will be added to the inventory of the same make and model with a status of "Waiting to be ordered". If you go to your WebLogic Server window, you should see a message in the console like the following:
The content of the message is the SKU of the car that was purchased. Web ServicesThe next part of the application to build is the manufacturer's Web service that allows for ordering of cars. We'll create this as a separate application. Close the Westside application, then choose File/New/Application and create a new empty application called "CarSource". Now right-click on "CarSource" and choose New\Project\Web Service Project. Call the new project "CarSourceWS". This is the Web service that will receive the request for a new car. To create a new Web service in the Web service project, right-click on the CarSourceWS project and choose New/Web Service. Call the new Web service "CarSourceWS.jws". We will have two different methods for ordering cars - one for Hondas, and one for all other cars. We will do this to show how you can use XQuery to map between two different XML schemas. In design view of CarSourceWS.jws, right-click and add a method called " XML Schemas and XMLBeansTo communicate between our Westside application and the
Rather than using this XML directly, we will use an XMLBean to abstract the XML into a Java class. To do this, create a new schema project called "Schemas" under the main "CarSource" folder. Right-click on this new project and choose New/XML Schema called "OrderCar.xsd". Paste the code above into this new file and click on "Save". WebLogic Workshop immediately creates an XMLBean for this schema. We will use this new XMLBean in the orderOther method of our Web service. Modify the method declaration to the following:
Again, hit Alt+Enter to add the appropriate import statement for Database ControlWhen we receive the order for the new car, we will record the order in the database, start the timer to wait 10 seconds, and then retrieve the information back out of the database and send the it to the requestor's callback. We will need a database control to talk to our data source. Right-click on the CarSourceWS folder and create a new folder called "CSControls". Now, right-click on this new folder and choose New/Java Control/Database. Name it "CSDataAccess" and then browse for the WASDataSource. Finally, click "create". We need three methods in this control:
Notice in the Now, to use this control in our Web service, drag and drop the control onto the design view of the Web service. This gives us a new private variable to access the control called " |