Development of business reservation system based on EJB technology
Author:Eve Cole
Update Time:2009-07-02 17:12:35
Technology has been increasingly used in the development of large-scale network systems. In this article, the author will introduce the definition of EJB (Enterprise Java Beans), the application system structure model based on EJB technology, and the content and classification of EJB components. Finally, combined with the EJB-based Developed a business reservation system using structural model and EJB components.
EJB is not technically a "product", but a technical specification. SUN's definition of EJB is: The structure of EJB is a component structure for developing and configuring component-based distributed business applications. Applications developed with EJB structures are scalable, transactional, and multi-user secure. These applications may only need to be written once, but can be deployed on a task server platform that supports the EJB specification. In general, EJB is a standard server-side component model for component transaction monitoring.
System structure model based on EJB technology
The EJB structure is a server-side component structure, which is a hierarchical structure. Its structural model is shown in Figure 1. This structural model can usually be divided into customer layer, business logic layer and data layer. The author will give a brief introduction to this below.
Figure 1: EJB-based application structure model
The client layer is mainly used to meet various access requirements for the entire system and handle the following tasks:
Receive user input, analyze and check user input and process accordingly;
Displays the running results transmitted from the server-side high-level.
The client layer usually consists of client processes, which are dynamically created and destroyed by the browser.
Business logic layer This is the most critical part of the entire system. The business logic layer is usually divided into two layers. The upper layer is the request receiving layer (commonly called the Web layer), which is used to receive requests from the browser and transfer the requests to the bottom layer for processing. The processing results are sent to the browser. These processes mainly consist of JSP pages, Web-based Applets, and Servlets that display HTML pages. The bottom layer is the request processing layer (generally called the EJB layer), which includes the listening process, processing process and database operation process. It is responsible for processing customer requests from the request receiving layer and processing them, and at the same time passing the request results to the request receiving layer. , if necessary, the processing results need to be handed over to the data layer for storage.
The data layer mainly provides data services for the business logic layer, such as storing the processing results of the business logic layer and returning the data results retrieved by the business logic layer. It is also used to shield changes in the data source, so that when the database changes, we only need to modify it. Just use the statement to connect to the data source.
Classification of EJB components Normally, there are two basic types of server-side EJB components: Entity Bean and Session Bean.
Figure 2: Architecture of business reservation system
Entity Beans are models built for real-world objects, which are usually persistent records in a database. Entity Bean builds a model for business concepts that can be expressed as nouns. It describes both the state of real-world objects and their behavior, and allows developers to encapsulate data and business rules related to specific concepts. Session Bean is an Enterprise Bean created through Home Interface and dedicated to client connections. Session bean instances are generally not shared with other clients. Session Bean is an extension of the client application and is responsible for managing the entire process or task. Session Beans can manage the interaction between Entity Beans, describing how they work together to complete a special task.
Entity beans can be divided into container-managed beans and beans managed using beans based on the way they manage persistence. Container-managed beans are automatically managed by the EJB container. The container knows how the fields of the Bean instance are mapped to the database, and automatically manages the insertion, update, and deletion of entity-related data in the database; use Beans to manage persistence. Beans need to do all this explicitly. Bean developers must write code to operate the database. The EJB container only tells the Bean instance when it can safely insert, update, and delete data in the database. In addition, it does not provide any other help. The bean instance does all the persistence work itself.
Session Beans can be divided into stateless beans and stateful beans according to whether they are stateful. Stateless Session Beans tend to be versatile and reusable; stateful Session Beans are extensions of client applications that complete tasks on behalf of clients and maintain client-related status.
Development of Business Reservation System The cabin reservation system is a business reservation system based on EJB component technology developed on the J2EE platform. Its main process is that after the user logs in, he will be led through the customer selection page and navigation selection page in turn, and will provide the customer with Select an available cabin (obtain the list of available cabins from the TravelAgentBean. The listAvailableCabin() method of TravelAgentBean is called by the Servlet that generates this web page. The cabin list will be used to create an HTML list box on the web page loaded into the user's browser) , when the user selects a cabin and submits the selection, an HTTP request will be sent to the EJB server (Websphere Application Server). After the server receives the request, it dispatches it to the ReservationServlet. This Servlet calls the TravelAgent.BookPassage() method. To make the actual booking, the tag information returned by the BookPassage() method will be used to create another web page that is sent back to the user's browser. If the reservation is successful, ProcessPaymentServlet will call the payment method in ProcessPaymentBean to implement the charging process for the customer. Its specific structure is shown in Figure 2.
The EJB components in the reservation system mainly include the following parts:
CabinBean: Entity Bean, the primary key is CabinPK, which is an entity Bean used to encapsulate the cabin of a ship in the real world.
CustomerBean: Entity Bean, the primary key is CustomerPK, which is an entity Bean used to encapsulate consumers who need to book cruise cabins in the real world.
CruiseBean: Entity Bean, the primary key is CruisePK, which is an entity Bean used to encapsulate ship routes in the real world. ReservationBean: Entity Bean, the primary key is CruiseID, CabinID. It represents an unchanged record in the database, that is, a reservation. It records the historical events of the reservation system. It is mainly used to prevent double booking, that is, two customers book the same route. The reason for this problem is that there is a gap between the time when the customer selects the cabin and route and the time when the bookPassage() method is called. TravelAgentBean: Stateful session bean, a session bean responsible for the workflow of booking flight cabins. It encapsulates the process of completing a booking operation for a route and is used in client applications of travel agents around the world. TravelAgentBean not only meets the needs of consumers to book tickets, but also provides information about the remaining cabins during the voyage. In order to complete this task, the Bean needs to know which route, cabin, and customer the reservation is made up of. After collecting this information, the bookPassage() method completes the booking process. It is responsible for the billing of the customer account. On the correct route Book the selected cabin on the correct ship and generate a ticket for the customer through the Ticket class. Here, we need to use the CreditCard class to store relevant information about the customer's credit card. At the same time, the ListAvailableCabins() method is used to display the available cabins that have not been booked.
ProcessPaymentBean: Stateless session bean, which is the process of charging consumers in a transaction system. It defines three transaction methods for check, cash and credit card payment methods, namely ByCheck(), ByCash() and ByCredit().
Program code example of the business reservation system. The entire business reservation system was developed under IBM VisualAge for Java. In this IDE development environment, entity beans are much easier to develop than session beans. The following uses TravelAgentBean as an example to introduce Development process of EJB components:
1. TravelAgent remote interface It provides a method to set the route and cabin ID that the customer wants to book. Additionally, the boolPassage() method is set up to bill the customer for their reservation and generate a ticket for the customer. The specific code is as follows:
package com.titan.travelagent;
import java.rmi.RemoteException;
import javax.ejb.FinderException;
import com.titan.cruise.Cruise;
import com.titan.customer.Customer;
import com.titan.processpayment.CreditCard;
public interface TravelAgent extends javax.ejb.EJBObject
{
public void setCruiseID(int cruise) throws RemoteException, FinderException;
public int getCruiseID( ) throws RemoteException, IncompleteConversationalState;
public void setCabinID(int cabin) throws RemoteException, FinderException;
public int getCabinID()throws RemoteException, IncompleteConversationalState;
public int getCustomerID()throws RemoteException, IncompleteConversationalState;
public Ticket boolPassage(CreditCard card,double price) throws RemoteException, IncompleteConversationalState;
}
2. TravelAgent Home interface
The TravelAgent Home interface code is as follows:
puckage com.titan.tracelagent;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import com.titan.customer.Customer;
public interface TravelAgentHome extends javax.ejb.EJBHome {
public TravelAgent create(Customer cust) throws RemoteException,CreateException;}
3. The TravelAgent Bean class needs to implement all the behaviors in the TravelAgent's remote interface and Home interface. Due to space limitations, this article will not introduce its implementation code. Interested readers can complete it by themselves.
Through the above steps, we have completed the development of the EJB component of a business reservation system.