作为一名更熟悉 .NET的人员来讲对于Bean比较陌生,但认真思考J2EE中讲到的各种规范.NET也应该具备,因为这毕竟是企业级开发共有的要求。
1.理解Stateful Session Bean
比较好的一篇文章是http://www.jguru.com/faq/view.jsp?EID=917。方便起见原文拷贝在此以便学习。
Richard Monson-Haefel
A stateful session bean is an enterprise bean (EJB component) that acts as a server-side extension of the client that uses it. The stateful session bean is created by a client and will work for only that client until the client connection is dropped or the bean is explicitly removed.
The stateful session bean is EJB component that implements the javax.ejb.SessionBean interface and is deployed with the declarative attribute "stateful". Stateful session beans are called "stateful" because they maintain a conversational state with the client. In other words, they have state or instance fields that can be initialized and changed by the client with each method invocation. The bean can use the conversational state as it process business methods invoked by the client.
Stateful session beans are usually developed to act as agents for the client, managing the interaction of other beans and performing work on behalf of the client application. An example is a shopping cart stateful session bean that tracks a client's product choices and can execute a sale when requested. Below is partial example of a stateful session bean that is used as a shopping cart.
public class ShoppingCartBean implements javax.ejb.SessionBean { // instance fields; The conversational state Vector products = new Vector( ); Customer customer; // initializes bean with reference to customer bean public void ejbCreate(Customer cust){ customer cust; } //add a product to shopping cart public void addItem(Product item) { products.addElement(item); } //charge customer for products and create a shipping order public void executeSale(){ // get the customer's credit card object CreditCard card = customer.getCreditCard( ); // calculate a total price from products chosen double total; for(int i = 0; i products.size(); i++){ Product product = (Product)products.elementAt(i); total += product.getPrice( ); } // get reference to the CreditService bean and process the customers charge CreditService cardSwipper = ... get credit card service bean Charge charge = cardSwipper.charge(CreditCard, total); // get reference to the OrderHome and create a new shipping order bean (record) OrderHome orderHome = ... get the Order beans Home object Order order = orderHome.create(customer, products, charge); } ... }
In the example above the ShoppingCartBean
keeps track of the Customer
and the items chosen by the client application. This is the bean's conversational state. Once the client application is ready to execute a sale, the ShoppingCartBean
manages the interactions of the Customer, Product, CreditServer,
and Order
beans in a workflow that results in a charge against the customers credit card and the creation of a shipping order. The ShoppingCartBean
behaves as an agent for the client managing the interaction of these other beans, tracking chosen items, and executing the sale. The below table identifies the beans types used in the ShoppingCartBean
example above.
Bean Name | Bean Type |
ShoppingCartBean | Stateful Session |
CreditService | Stateless Session |
Customer | Entity |
Product | Entity |
Order | Entity |
From the clients perspective only the Customer
and ShoppingCart
beans are visible, because the client application works with the bean's remote interface while the bean itself resides on the server. This means that the client need only be concerned with the business methods made public by the ShoppingCartBean
. Below is an example of the client applications view of the ShoppingCartBean
. (Note: The client interacts with the beans remote interface not the bean itself. The remote interface is called the ShoppingCart
.)
// somewhere in the client application (applet, servlet, etc.) // obtain the home objects (factories) for the Customer and ShoppingCart beans CustomerHome custHm = ... get CustomerHome ShoppingCartHome shpCrtHm = ... get ShoppingCartHome // locate the bean representing the customer named, Bob Johnsten Customer customer = custHome.findByName("Bob","Johnsten"); // create a new ShoppingCart initialized with the Bob Johnsten Customer bean ShoppingCart shoppingCart = shpCrtHm.create(customer); ... Product product = ... get reference to bean representing the product chosen // add product to customer's shopping cart shoppingCart.addItem(product); ... //charge customer for products and create a shipping order shoppingCart.executeSale( );
Session beans like the shoppingCartBean
only live as long as the client maintains a connection. In other words, they represent some aspect of the client's current session with the system and die when the client ends that session. Session beans are generally not fault tolerant. A system failure or shut down will result in the death of session bean and a loss of any conversation state it maintained prior to the failure. (This is a conceptual lifecycle of a session bean. Some vendors can make them more fault tolerant and longer lasting).
这篇文章介绍的非常清晰,这里只强调一点,用户必然能够从session中获取stateful session bean,这才完全符合stateful, session两层意思。前一段时间做过一个项目是关于工单的,后台使用状态模式,包含一个workitemContext, 前台调用后台时每一次使用都要初始化,然后设置属性等,现在看来应该使用stateful session bean,将之放在session里,这样用户第一次使用工单功能时创建这个context,以后都从session中获取直接使用了。
2. Entity Bean
关于实体bean,推荐Core J2EE Patterns中的Business Object. 首先需要理解的是业务对象都是有状态的,所以BO都是为某一个用户服务的。其次是与BO相关的都是企业级相关的问题,例如:security, transaction, caching, pooling, concurrency, data Synchronization, Persistence. 这些通用的问题对于系统的可重用性,可维护性,性能都非常重要。