内容摘要:在这篇技巧文章中,我们将研究一些最常用的 JNDI 优化。特别地,我们将向您展示如何将高速缓存和通用助手类组合使用,以创建针对 JNDI 开销的工厂风格的解决方案。
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;
}
}
责编:豆豆技术应用
正在加载评论...
- Java新闻
- Java入门教程
- Java开发工具
- J2EE
- J2SE
- J2ME
- EJB/Spring
- Applet/Swing
- Servlet/JSP
- Struts/Hibernate
- JDBC/JDO
- Ajax
- 认证考试
- JAVA对象比较器Comparator
- Java 6 RowSet 使用完全剖析
- 深入了解"Java"中的“异常机制”
- 讨论5种跟踪Java执行的方法
- Java专业术语标准化规范
- java中线程概念描述
- JAVA中的反射机制详解
- Java语言灵巧指针与垃圾回收
- 讲述java语言中内部类的研究
- Java对于Cookie的操作详解