论坛首页 Java企业应用论坛

关于Cache EJB Home Remote 接口问题

浏览 6749 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2004-09-20  
最近用 Jive 中的 Cahce object,  缓存了EJB Home  接口,  但通过 Jprofile发现如果能缓存 EJB Remote 接口,  系统性能还会有大的提升.  

But I know most of us just cache EJB Home interface.  We didn't cache EJB remote interface.  I post thread on  Java Sun  Web site. http://forum.java.sun.com/thread.jsp?forum=13&thread=494121

不知大家是否遇到这类问题?   My codes are listed as follows:

	Object instance = null;
	try {
	  Object bean = cacheLookup.get(service);;
	  //Cache EJBHome to save lookup EJBHome time
	  if(bean == null);{
		bean = ctx.lookup(service);;
		cacheLookup.add(service, bean);;
	  }
     
	  try {
		bean = javax.rmi.PortableRemoteObject.narrow(bean, Class.forName(service + "Home"););;
	  } catch(Exception cce); {
		if(service.endsWith("Service");); {
		  service = service.substring(0,service.lastIndexOf("Service"););;
		} else {
		  service += "Service";
		}
		service += "Home";
		bean = javax.rmi.PortableRemoteObject.narrow(bean, Class.forName(service););;
	  }

      Object instanceStub = cacheStub.get(service);;
      if(instanceStub == null);{ 
	    Class params[] = new Class[0];
	    Method m = bean.getClass();.getDeclaredMethod("create", params);;
	    instance = m.invoke(bean, null);;
		cacheStub.add(service, instance);;
      }else{
		instance = instanceStub;
      }	  
	  
	} catch(Exception e); {
	  e.printStackTrace();;
	  throw new ServiceException(e.getMessage(););;
	}
	return instance;
  }

   发表时间:2004-09-20  
目前我的做法是只cache home interface。home interface的缓存能够避免反复JNDI lookUp引起的消耗。我觉得没有必要cache remote interface——因为bean的实例已经由容器管理了,你没有必要再自己搞个东西把它的远程引用存起来,说不定还会搅乱容器的对象池管理。比如说——如果有多个客户端各自都抢了几个bean缓存起来——EJB容器不是要多new几个根本不用的bean了么?
0 请登录后投票
   发表时间:2004-09-20  
monk 写道
目前我的做法是只cache home interface。home interface的缓存能够避免反复JNDI lookUp引起的消耗。我觉得没有必要cache remote interface——因为bean的实例已经由容器管理了,你没有必要再自己搞个东西把它的远程引用存起来,说不定还会搅乱容器的对象池管理。比如说——如果有多个客户端各自都抢了几个bean缓存起来——EJB容器不是要多new几个根本不用的bean了么?


Thanks, monk.

The toppest codes are located in Web container.  Actually, it is service locator pattern in J2EE.  Why do I want to cache  远程引用?  Because line 26
Method m = bean.getClass();.getDeclaredMethod("create", params);; 

It is a reflection, which is performance overhead. 

引用
如果有多个客户端各自都抢了几个bean缓存起来

I don't think so.  Because  HashTable's key is the same.  That means, only one 远程引用 exists in web container.  What I worry about is,   if two threads use the same  远程引用,   what will happend?  If it works, why not cache  EJBHome and 远程引用EJBObject together?
0 请登录后投票
   发表时间:2004-09-20  
No one knows what will if two threads use the same remote bean handle. I guess whether the remote bean is thread-safe  maybe is a Container-dependent thing - It might work on WAS but crash on JBOSS ? That problem bothers me too. So I don't cache remote bean, just for sure.
0 请登录后投票
   发表时间:2004-09-20  
monk 写道
No one knows what will if two threads use the same remote bean handle. I guess whether the remote bean is thread-safe  maybe is a Container-dependent thing - It might work on WAS but crash on JBOSS ? That problem bothers me too. So I don't cache remote bean, just for sure.


My EJB Container is Weblogic.  I decompile the classes that weblogic.ejbc generated.   I found there are no synchronized method in these generated proxy classes.  So I think you are right, monk.  Seems Caching remote bean is not good smart choice for updating process. But I still believe reading or requiring is not bad approache.
0 请登录后投票
   发表时间:2004-09-20  
EJB的远程接口能缓存么?
   EJB的远程接口所返回的只是一个存根,简单地讲,只是一个ejb bean实现的一个远程代理引用。缓存它没有任何用,因为它本身无任何状态信息,唯一的可能就是,呵呵,报错。
0 请登录后投票
   发表时间:2004-09-21  
凤舞凰扬 写道
EJB的远程接口能缓存么?
   EJB的远程接口所返回的只是一个存根,简单地讲,只是一个ejb bean实现的一个远程代理引用。缓存它没有任何用,因为它本身无任何状态信息,唯一的可能就是,呵呵,报错。


Could you please try it and then tell us if 报错?  
Also,  the reason why I want to cache EJBOjbect is that we used reflection to return EJBObject.  Reflection is time-consuming but gives us flexibility.   

Please take just 1 min to read toppest codes. You will know why.

My opinion is:

If your ejbhome.create() method is not the bottle neck in your applicaton. Just forget it. However, if so,  we'd better consider caching EJBObject to deal with reading method in EJB.

Any different opinions are welcome. 
0 请登录后投票
   发表时间:2004-09-21  
绝大多数应用服务器都会帮你做这件事情,即使你再缓存一次,除了增大内存开销,似乎没有任何好处。
0 请登录后投票
   发表时间:2004-09-21  
bruce 写道

Could you please try it and then tell us if 报错?  
Also,  the reason why I want to cache EJBOjbect is that we used reflection to return EJBObject.  Reflection is time-consuming but gives us flexibility.   

Please take just 1 min to read toppest codes. You will know why.

My opinion is:

If your ejbhome.create() method is not the bottle neck in your applicaton. Just forget it. However, if so,  we'd better consider caching EJBObject to deal with reading method in EJB.

Any different opinions are welcome. 

     呵呵,我当然试过,两年多前的我就曾尝试过,在容易没有对EJB进行钝化的时候,当然不会错,但是如果服务器钝化了bean的实例,而你想通过所谓的远程缓存引用来方面,就会错了(其实要想早点错,最好的方式就是用remote接口,而不是local接口)。如果楼上,不相信,尽管尝试吧`
     最后,建议楼上还是去看看robbin的一篇关于EJB原理的帖子,好好了解和深入一下了。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics