JiveJdon Community Forums
在线80人   首页   主题总表   培训咨询   精华   查搜   注册    登陆
首页 » 论坛 » J2EE/JavaEE/JEE/EJB/JSF等技术讨论
???en_US.forumThreadPrev.name??? 上一主题
  Go back to the topic 返回本主题   Go back to the topic listing返回主题列表
???en_US.forumThreadNext.name??? 下一主题
Go 总共有 8 回复 / 1
 发表新帖子   回复该主题贴
ccxanadu

悄悄话
发表文章: 23
注册时间: 2003年07月30日 14:45
关于jndi优化的讨论 2003年08月27日 15:43 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
标签列表
大家讨论一下关于jndi优化方面的问题吧,
高速缓存jndi查询出来的home接口或引用接口对性能提升非常大,
优化jndi非常有必要,不知各位DX有什么好的方法和建议?
ccxanadu

悄悄话
发表文章: 23
注册时间: 2003年07月30日 14:45
Re: 关于jndi优化的讨论 2003年08月27日 17:26 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
大家从来不优化jndi吗?
magician

悄悄话
发表文章: 27
注册时间: 2003年05月23日 17:32
Re: 关于jndi优化的讨论 2003年08月27日 17:34 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
flyweight模式

具体实现就是:

对于无状态的接口,用可同步容器存储,键值可以任意,只要你认识
对于有状态,最好键值上标示其状态,以保证取到的是指定状态的接口

一般都是用Map来做这个容器

不知道大家是不是
magician

悄悄话
发表文章: 27
注册时间: 2003年05月23日 17:32
Re: 关于jndi优化的讨论 2003年08月27日 17:34 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
flyweight模式

具体实现就是:

对于无状态的接口,用可同步容器存储,键值可以任意,只要你认识
对于有状态,最好键值上标示其状态,以保证取到的是指定状态的接口

一般都是用Map来做这个容器

不知道大家是不是
anonymous

悄悄话
发表文章: 0
注册时间:
Re: 关于jndi优化的讨论 2003年08月27日 19:21 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
http://www-900.ibm.com/developerWorks/cn/java/j-ejb0924/index.shtml
EJB 最佳实践:工业强度的 JNDI 优化
banq

悄悄话
发表文章: 9290
注册时间: 2002年08月03日 17:08
Re: 关于jndi优化的讨论 2003年08月27日 20:20 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
petstore中的Servicelocator类很不错,实现了缓冲优化,代表Service Locator模式。
banq

悄悄话
发表文章: 9290
注册时间: 2002年08月03日 17:08
Re: 关于jndi优化的讨论 2003年08月27日 20:22 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
代码很简单,将Home缓存到cache中,以后每次从cache中取出。
更进一步 EJBLocalObject也可以缓存,这样大大增加EJB处理速度。

Service Locator如下:


package com.jdon.servicelocator.web;


import java.util.HashMap;
import java.util.Map;
import java.util.Collections;
import java.net.URL;


import javax.ejb.EJBHome;
import javax.ejb.EJBLocalHome;
import javax.jms.QueueConnectionFactory;
import javax.jms.Queue;
import javax.jms.TopicConnectionFactory;
import javax.jms.Topic;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.sql.DataSource;

import com.jdon.servicelocator.ServiceLocatorException;

/**
* This class is an implementation of the Service Locator pattern. It is
* used to looukup resources such as EJBHomes, JMS Destinations, etc.
* This implementation uses the "singleton" strategy and also the "caching"
* strategy.
* This implementation is intended to be used on the web tier and
* not on the EJB tier.
*/

public class ServiceLocator {

private InitialContext ic;
private Map cache;
//used to hold references to EJBHomes/JMS Resources for re-use

private static ServiceLocator me;

static {
try {
me = new ServiceLocator();
} catch(ServiceLocatorException se) {
System.err.println(se);
se.printStackTrace(System.err);
}
}

private ServiceLocator() throws ServiceLocatorException {
try {
ic = new InitialContext();
cache = Collections.synchronizedMap(new HashMap());
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
}

static public ServiceLocator getInstance() {
return me;
}






/**
* will get the EJB Local home factory. If this EJB home factory has already been
* clients need to cast to the type of EJBHome they desire
*
* @return the EJB Home corresponding to the homeName
*/

public EJBLocalHome getLocalHome(String jndiHomeName) throws ServiceLocatorException {
EJBLocalHome home = null;
try {
if (cache.containsKey(jndiHomeName)) {
home = (EJBLocalHome) cache.get(jndiHomeName);
} else {
home = (EJBLocalHome) ic.lookup(jndiHomeName);
cache.put(jndiHomeName, home);
}
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
return home;
}

/**
* will get the EJB Remote home factory. If this EJB home factory has already been
* clients need to cast to the type of EJBHome they desire
*
* @return the EJB Home corresponding to the homeName
*/

public EJBHome getRemoteHome(String jndiHomeName, Class className) throws ServiceLocatorException {
EJBHome home = null;
try {
if (cache.containsKey(jndiHomeName)) {
home = (EJBHome) cache.get(jndiHomeName);
} else {
Object objref = ic.lookup(jndiHomeName);
Object obj = PortableRemoteObject.narrow(objref, className);
home = (EJBHome)obj;
cache.put(jndiHomeName, home);
}
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}

return home;
}


/**
* @return the factory for the factory to get queue connections from
*/

public QueueConnectionFactory getQueueConnectionFactory(String qConnFactoryName)
throws ServiceLocatorException {
QueueConnectionFactory factory = null;
try {
if (cache.containsKey(qConnFactoryName)) {
factory = (QueueConnectionFactory) cache.get(qConnFactoryName);
} else {
factory = (QueueConnectionFactory) ic.lookup(qConnFactoryName);
cache.put(qConnFactoryName, factory);
}
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
return factory;
}


/**
* @return the Queue Destination to send messages to
*/

public Queue getQueue(String queueName) throws ServiceLocatorException {
Queue queue = null;
try {
if (cache.containsKey(queueName)) {
queue = (Queue) cache.get(queueName);
} else {
queue =(Queue)ic.lookup(queueName);
cache.put(queueName, queue);
}
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}

return queue;
}


/**
* This method helps in obtaining the topic factory
* @return the factory for the factory to get topic connections from
*/

public TopicConnectionFactory getTopicConnectionFactory(String topicConnFactoryName) throws ServiceLocatorException {
TopicConnectionFactory factory = null;
try {
if (cache.containsKey(topicConnFactoryName)) {
factory = (TopicConnectionFactory) cache.get(topicConnFactoryName);
} else {
factory = (TopicConnectionFactory) ic.lookup(topicConnFactoryName);
cache.put(topicConnFactoryName, factory);
}
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
return factory;
}

/**
* This method obtains the topc itself for a caller
* @return the Topic Destination to send messages to
*/

public Topic getTopic(String topicName) throws ServiceLocatorException {
Topic topic = null;
try {
if (cache.containsKey(topicName)) {
topic = (Topic) cache.get(topicName);
} else {
topic = (Topic)ic.lookup(topicName);
cache.put(topicName, topic);
}
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
return topic;
}

/**
* This method obtains the datasource itself for a caller
* @return the DataSource corresponding to the name parameter
*/

public DataSource getDataSource(String dataSourceName) throws ServiceLocatorException {
DataSource dataSource = null;
try {
if (cache.containsKey(dataSourceName)) {
dataSource = (DataSource) cache.get(dataSourceName);
} else {
dataSource = (DataSource)ic.lookup(dataSourceName);
cache.put(dataSourceName, dataSource );
}
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
return dataSource;
}

/**
* @return the URL value corresponding
* to the env entry name.
*/

public URL getUrl(String envName) throws ServiceLocatorException {
URL url = null;
try {
url = (URL)ic.lookup(envName);
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}

return url;
}

/**
* @return the boolean value corresponding
* to the env entry such as SEND_CONFIRMATION_MAIL property.
*/

public boolean getBoolean(String envName) throws ServiceLocatorException {
Boolean bool = null;
try {
bool = (Boolean)ic.lookup(envName);
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
return bool.booleanValue();
}

/**
* @return the String value corresponding
* to the env entry name.
*/

public String getString(String envName) throws ServiceLocatorException {
String envEntry = null;
try {
envEntry = (String)ic.lookup(envName);
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
return envEntry ;
}

}

ccxanadu

悄悄话
发表文章: 23
注册时间: 2003年07月30日 14:45
Re: 关于jndi优化的讨论 2003年08月27日 21:08 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
admire banq :)
将Home缓存到cache中我觉得应该不会有问题,
因为复用home引用不会干扰容器的bean管理,
但是缓存EJBLocalObject是否会干扰容器管理?
另外,如果cache中home引用或EJBLocalObject引用
万一失效了怎么办?EJBObject为什么不缓存
banq

悄悄话
发表文章: 9290
注册时间: 2002年08月03日 17:08
Re: 关于jndi优化的讨论 2003年08月28日 16:27 到本帖网址 加入本帖到收藏夹 发送到手机 回复该主题
缓存EJBLocalObject一般不会干扰容器管理,这种做法对于有状态Bean尤其适合。
这个主题有 8 回复 / 1Go
???en_US.forumThreadPrev.name??? 上一主题
  Go back to the topic 返回本主题   Go back to the topic listing返回主题列表    返回页首返回页首
???en_US.forumThreadNext.name??? 下一主题
热点TAG: AOP cache 缓存 DDD EJB 集群 设计模式 Hibernate IOC JiveJdon OO RBAC Seam Spring Struts
google yahoo 新浪ViVi 365Key网摘 天极网摘 CSDN网摘 添加到百度搜藏 POCO网摘 博采网摘
查询本论坛内 回复超过的热门帖子
     回复该主题贴
标题
 
粗体 斜体 下划线 插入图片 插入代码 插入url链接 插入附件
内容
 

手机阅读 add to google add to yahoo
解惑之道在J道 ,打造中国最具影响力的的企业软件社区
OpenSource JIVEJDON v3.0 Powered by JdonFramework Code © 2002-08 jdon.com
anti spam