Article
Enterprise JavaBeans, A Primer: Part 3, A Tour of the Java Pet Store
Placing an Order
To purchase a bulldog, for example, follow these steps:
- On the home page, select Dogs.
- Click Bulldog.
- Click Add to Cart in the row for the Male Adult Bulldog.
- Click Check Out. A form for your billing and shipping information appears with pre-filled information.
- Click Submit.
When you complete the order, the Storefront sends the order info asynchronously to the order processing center (OPC).
Using the Admin Client
To run the administration module, go to http://localhost:8000/admin/. Launch the administrator, and look at the pending and non-pending orders tabs. The bulldog order will be non-pending, because it was less than $500, and all orders under $500 are automatically approved.
Using the Supplier Client
The supplier and its inventory are managed using a simple Web interface. Be sure to first logout of the admin client, before using the supplier.
To manage the supplier and its inventory, go to http://localhost:8000/supplier/. Look around the inventory; click Display Inventory at the home screen. A table shows the quantity of each item in the inventory.
An Analysis of the Pet Store Application
Let's take an in-depth look at the Pet Store application's architecture and expose its structure so we can understand how it's put together. If we can gain enough knowledge and experience, we might be able to use the application as a platform for some self-directed learning about J2EE.
The Java Pet Store Demo consists of the following high-level module:
- The front page (storefront) presents a typical user interface for a Web application. Customers use the storefront to place orders for pets.
- The order processing center (OPC) module receives orders from the storefront.
- The supplier module fulfills orders from the OPC inventory and invoices the OPC.
- The admin module presents an administrator interface in a JFC/Swing front-end. Administrators use the admin module to examine pending orders and approve or deny them.
Now, let's take a look at the types of EJBs employed by the Pet Store application and the roles they play.
Stateless Session Beans
ShoppingClientFacadeLocal– Provides a facade to all of the EJBs related to a shopping clientOrderFulfillmentFacade– Facade used by Supplier Order MDB – called every time the supplier gets a PO and will save a PO, try to fulfill an order, create an Invoice for a shipped order, and any other activities needed to fulfil an order when a purchase order arrivesAsyncSender– converts shopping cart contents and customer data into an XML message representing an order, and sends the message to the Order Processing CenterOrderProcessingCenterAdminFacade– the facade that OPC uses to handle admin client requestsProcessManager– provides methods to view and modify information for a particular order workflow processCatalog– provides a programmatic interface to the catalogSignOn– creates and authenticates system usersUniqueIdGenerator– creates globally unique object identifiers
Stateful Session Beans
ShoppingController– controls all the shopping activities that happen for a particular client sessionShoppingClientFacade– acts as an intercessory between a shopping client and any business-logic objectsShoppingCart– maintains the contents of an individual user's virtual shopping cart
Entity Beans
Inventory– represents the inventory for a given itemCreditCard– tracks card number, card type, and expiration dateAddress– tracks two lines of street address, state, zip code, and countryContactInfo– tracks family and given name, telephone, email, and addressLineItem– represents line-items in an InvoicePurchaseOrder– represents purchase orders from customersCustomer– tracks customer ID (primary key), account, and profileAccount– tracks account status, credit card, and contact infoProfile– tracks preferred language, category, list preference, and banner preferenceUser– represents a user who may sign on to the system; tracks a user name and passwordCounter– represents a counter with a specific prefix; used only byUniqueIdGeneratorto manage series of unique numbersSupplierOrder– represents orders sent from suppliers
Message-Driven Beans
SupplierOrder– processesPurchaseOrdersfrom the Java Pet StorePurchaseOrder– processes PurchaseOrders from the Java Pet StoreInvoice– processes Invoices from the SupplierOrderApproval– processesOrderApprovalsfrom the AdminMailInvoice– processes Invoices received from the SupplierMailOrderApproval– processes receiving an Order ApprovalMailCompletedOrder– processes a completed orderMailer– receives mail messages from the mailer serviceOPCAdminFacade– exposes the interface for the Admin client from OPCManager– provides functionality to view and modify sign-on information for a particular manager
The source code for each EJB is located in an ejb directory under the src directory for each module, as in the following:
<PETSTORE_INSTALL_DIR>/src/apps/opc/src/com/sun/j2ee/blueprints/opc /ejb<PETSTORE_INSTALL_DIR>/src/apps/opc/src/com/sun/j2ee/blueprints/opc/admin/ejb<PETSTORE_INSTALL_DIR>/src/apps/petstore/src/com/sun/j2ee/blueprints/petstore/controller/ejb<PETSTORE_INSTALL_DIR>/src/apps/supplier/src/com/sun/j2ee/blueprints/supplier/inventory/ejb
Browse around at the source code for each of the EJBs and pay attention to the way the interfaces are used and reused.
Summary
The Pet Store application offers a full-featured demonstration of the J2EE platform and provides a quick start to building any typical Web application or distributed application. The Pet Store's extensive use of EJBs allows business-logic developer's the opportunity to view many types of interactions and uses for the power of the EJB architecture.