The bookstore webapp example demonstrates how to:
- Run Mule ESB inside a Webapp
- Use Mule with the Servlet transport
- Use Mule with CXF web services
Note: The bookstore webapp example was added in Mule 2.2 as an evolution of the older Bookstore Example. The bookstore webapp example is still in beta. Please file a JIRA for any problems you encounter, or better yet, submit fixes for them! For more information, see Getting Involved.
|To watch a webinar that walks through the bookstore example, go to http://www.mulesoft.com/webinar-make-web-applications-do-more-mule.|
The bookstore example includes the following:
- A bookstore catalog service with a public interface for browsing the catalog and an admin interface for adding books to the catalog
- An order service that allows customers to order books
- An email notification service that sends an email when an order has been placed.
- A data warehouse service that tracks statistics on the books, such as the number of books sold, total revenue, and best sellers.
The example is contained within three modules:
- The API module contains the domain objects and interfaces used by the two webapps.
- The Admin webapp includes the Mule configuration file and the service definitions. This module is hosted behind the firewall.
- The Bookstore webapp includes the JSPs for displaying the catalog to customers outside of the firewall.
Following is an overview of the architecture:
To run the Bookstore example, run the mvn tomcat:run command in the admin-web and bookstore-web subdirectories under the <MuleHome>\examples\bookstore directory. The applications will then be available at the following URLs:
These services are defined in the bookstore-config.xml file in the examples\bookstore\admin-web\src\main\resources directory. All four services are in a single model called Bookstore:
When an administrator adds a book in the Admin application, the information in the web page is sent as an HTTP request using the POST method <form method="POST" action="../catalog"> over the Servlet transport to servlet://catalog. Mule then transforms the HTTP request to a Book object, which is passed via the addBook method to the Catalog service.
The Admin webapp web.xml file, located in the examples\bookstore\admin-web\src\main\webapp\WEB-INF directory under your Mule directory, provides the Mule configuration as a context parameter so that Mule can be started within the webapp:
The JSP used to construct the web page, admin.jsp, is located in the examples\bookstore\admin-web\src\main\webapp directory.
When a customer searches the catalog through the bookstore web application, the getBooks() method is called on the Catalog service. The request is sent over the CXF transport cxf:http://localhost:8777/services/catalog, which marshalls the request between Java and WSDL/XML. The Catalog service replies with the Book object, which is sent back as an XML response marshalled by CXF, and the result is displayed in the HTML page.
The Bookstore webapp web.xml file, located in the examples\bookstore\bookstore-web\src\main\webapp\WEB-INF directory under your Mule directory, loads the catalog JSP to display in the HTML page to customers:
The HTML page looks like this:
The Java classes for the Admin webapp are located in examples\bookstore\admin-web\src\main\java\org\mule\example\bookstore. The CatalogServiceImpl.java class defines the methods for adding and getting books. The OrderServiceImpl.java class defines the orderBook() method. The DataWarehouse.java class defines methods for updating statistics on the page, retrieving the best seller, and printing the statistics on the page.
The domain objects such as Book and Order are defined in Java classes in examples\bookstore\api\src\main\java\org\mule\example\bookstore.
In addition to defining the various methods, the Java files also contain annotations. @WebService lets CXF know that this is a JAX-WS service. For example, CatalogServiceImpl.java contains the following annotation:
The serviceName attribute specifies that the service name in the WSDL will be "CatalogService". The endpointInterface attribute will control what interface CXF uses to build your WSDL. If this is not specified, CXF will use your web service implementation class to generate the WSDL and therefore expects any @WebParam and @WebResult annotations to be located there.
The @WebResult and @WebParam annotations are used to define the parameter names in the WSDL (Java does not store parameter names in the class files, so they must be supplied with annotations):
The examples\bookstore\admin-web\src\main\java\org\mule\example\bookstore\transformers directory contains the transformers that do the following:
- Adds a book to the response message and wraps it in an HTML body using the HTML template in examples\bookstore\admin-web\src\main\java\org\mule\example\bookstore\web.
- Composes an e-mail notification message to be sent based on the Book Order. The <string-to-email-transformer> from the Email transport is then used to convert the text to an email message.
- Transforms a Map of HttpRequest parameters into a Book object.