pet store中得servicelocator的疑问

starry 2003-11-27 12:26:51
pet store中得servicelocator在web和ejb中各有一个,其中web中的使用了cache,而ejb中未使用。有资料说是single model使然,我不太理解,请多多讲解一下。
...全文
67 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
starry 2003-12-18
  • 打赏
  • 举报
回复
那ejb调用singleton,第一次查找资源将从jndi中查找并存入缓存,以后的调用直接从缓存中读取。不行么?不从jndi中查找资源
ZeroC 2003-12-17
  • 打赏
  • 举报
回复
ejb的是多实例的,用于从jndi中查找资源
web的是singleton(不懂的话看下singleton模式),第一次查找资源将从jndi中查找并存入缓存,以后的调用直接从缓存中读取
ZeroC 2003-12-17
  • 打赏
  • 举报
回复
web:
/**
* 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;
}
......
ZeroC 2003-12-17
  • 打赏
  • 举报
回复
ejb:
/**
* This class is an implementation of the Service Locator pattern. It is
* used to looukup resources such as EJBHomes, JMS Destinations, etc.
*/
public class ServiceLocator {

private InitialContext ic;

private static ServiceLocator me;

public ServiceLocator() throws ServiceLocatorException {
try {
ic = new InitialContext();
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
}


/**
* will get the ejb Local home factory.
* clients need to cast to the type of EJBHome they desire
*
* @return the Local EJB Home corresponding to the homeName
*/
public EJBLocalHome getLocalHome(String jndiHomeName) throws ServiceLocatorException {
EJBLocalHome home = null;
try {
home = (EJBLocalHome) ic.lookup(jndiHomeName);
} catch (NamingException ne) {
throw new ServiceLocatorException(ne);
} catch (Exception e) {
throw new ServiceLocatorException(e);
}
return home;
}

/**
* will get the ejb Remote home factory.
* 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 {
Object objref = ic.lookup(jndiHomeName);
Object obj = PortableRemoteObject.narrow(objref, className);
home = (EJBHome)obj;
} 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 {
factory = (QueueConnectionFactory) ic.lookup(qConnFactoryName);
} 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 {
queue =(Queue)ic.lookup(queueName);
} 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 {
factory = (TopicConnectionFactory) ic.lookup(topicConnFactoryName);
} 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 {
topic = (Topic)ic.lookup(topicName);
} 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 {
dataSource = (DataSource)ic.lookup(dataSourceName);
} 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 ;
}

}
segl 2003-12-17
  • 打赏
  • 举报
回复
帮你顶!
starry 2003-12-16
  • 打赏
  • 举报
回复
没人知道???
starry 2003-11-30
  • 打赏
  • 举报
回复
up

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧