|
每种 EJB 组件(会话、实体和消息驱动的)都有 home 接口。home 接口是 bean 的操作基础;一旦您找到它,就可以使用该 bean 的功能。EJB 应用程序依靠 JNDI 查找来访问其 bean 的 home 接口。因为 EJB 应用程序往往运行多个 bean,并且因为许多组件中经常使用 JNDI 查找,所以应用程序大部分性能开销都花费在这些查找上。
先看个典型的EJB查找: public boolean buyItems(PaymentInfo paymentInfo, String storeName, List items) { // Load up the initial context思考:新建InitialContext()是否必要,很可能其它地方的代码已经装入了上下文 Context ctx = new InitialContext();
// Look up a bean's home interface Object obj = ctx.lookup("java:comp/env/ejb/PurchaseHome"); PurchaseHome purchaseHome = (PurchaseHome)PortableRemoteObject.narrow(obj, PurchaseHome.class); Purchase purchase = purchaseHome.create(paymentInfo);
// Work on the bean for (Iterator i = items.iterator(); i.hasNext(); ) { purchase.addItem((Item)i.next()); }
// Look up another bean Object obj = ctx.lookup("java:comp/env/ejb/InventoryHome"); InventoryHome inventoryHome = (InventoryHome)PortableRemoteObject.narrow(obj, InventoryHome.class); Inventory inventory = inventoryHome.findByStoreName(storeName);
// Work on the bean for (Iterator i = items.iterator(); i.hasNext(); ) inventory.markAsSold((Item)i.next()); }
// Do some other stuff }
清单 2. 高速缓存 InitialContext 实例 public static Context getInitialContext() { if (initialContext == null) { initialContext = new InitialContext(); }
return initialContext; }
我们每次调用 lookup() 方法时都会执行一次新查找,并返回 bean 的 home 接口的新实例。至少,JNDI 查找通常是这样编码的。但如果每个 bean 都只有一个 home 接口,并在多个组件上共享这个接口,这样不是更好吗?
我们可以高速缓存每个单独的 bean 引用,而不是反复查找 PurchaseHome 或 InventoryHome 的 home 接口;这是一种解决方案。但我们真正想要的是一种更通用的机制:在 EJB 应用程序中高速缓存 home 接口。
清单 3. EJB home 接口工厂 package com.ibm.ejb;
import java.util.Map; import javax.ejb.EJBHome; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException;
public class EJBHomeFactory {
private static EJBHomeFactory;
private Map homeInterfaces; private Context context;
// This is private, and can't be instantiated directly private EJBHomeFactory() throws NamingException { homeInterfaces = new HashMap();
// Get the context for caching purposes context = new InitialContext();
/** * In non-J2EE applications, you might need to load up * a properties file and get this context manually. I've * kept this simple for demonstration purposes. */ }
public static EJBHomeFactory getInstance() throws NamingException { // Not completely thread-safe, but good enough // (see note in article) if (instance == null) { instance = new EJBHomeFactory(); } return instance; }
public EJBHome lookup(String jndiName, Class homeInterfaceClass) throws NamingException {
// See if we already have this interface cached EJBHome homeInterface = (EJBHome)homeInterfaces.get(homeClass);
// If not, look up with the supplied JNDI name if (homeInterface == null) { Object obj = context.lookup(jndiName); homeInterface = (EJBHome)PortableRemoteObject.narrow(obj, homeInterfaceClass);
// If this is a new ref, save for caching purposes homeInterfaces.put(homeInterfaceClass, homeInterface); } return homeInterface; } }
EJBHomeFactory 类内幕 home 接口工厂的关键在 homeInterfaces 映射中。该映射存储了供使用的每个 bean 的 home 接口;这样,home 接口实例可以反复使用。您还应注意,映射中的关键并不是传递到 lookup() 方法的 JNDI 名称。将同一 home 接口绑定到不同 JNDI 名称是很常见的,但这样做会在您的映射中产生副本。通过依靠类本身,您就可以确保最终不会为同一个 bean 创建多个 home 接口。
改进的 EJB 查找 public boolean buyItems(PaymentInfo paymentInfo, String storeName, List items) {
EJBHomeFactory f = EJBHomeFactory.getInstance();
PurchaseHome purchaseHome = (PurchaseHome)f.lookup("java:comp/env/ejb/PurchaseHome", PurchaseHome.class); Purchase purchase = purchaseHome.create(paymentInfo);
// Work on the bean for (Iterator i = items.iterator(); i.hasNext(); ) { purchase.addItem((Item)i.next()); }
InventoryHome inventoryHome = (InventoryHome)f.lookup("java:comp/env/ejb/InventoryHome", InventoryHome.class); Inventory inventory = inventoryHome.findByStoreName(storeName);
// Work on the bean for (Iterator i = items.iterator(); i.hasNext(); ) { inventory.markAsSold((Item)i.next()); }
// Do some other stuff }
以上工厂优化的 EJB 查找将执行得更快。您第一次使用这个新类时,将花费所有正常查找开销(假定应用程序的其它部分没有付出过这种开销),但将来的所有 JNDI 查找都将继续使用原先的查找结果。还有必要指出,home 接口工厂不会干扰您容器的bean 管理。容器管理的是 bean 实例,而不是这些 bean 实例的 home 接口。您的容器还将管理实例交换,以及其它您希望它执行的任何优化。
|