How do you share Java Caching System (JCS) resource across multiple EJB

痴心易碎 提交于 2019-12-06 06:42:06

问题


I am using JCS to store the ldap search results which should be shared by multiple EJB. I have created a singleton class to initialize JCS only once but due to EJB's classloader, it's been initialized multiple times with its own copy. so search resources are not shared.

How are you guys resolving issue where you need to share the cache across multiple beans? I am looking for cache within JVM. (Not the remote e.g memcached etc.).

Glassfish is used as an application server.


回答1:


I haven't been able to test it yet, but I think that one of the techniques explained in the "Circumventing Class Loader Isolation" chapter of the Application Development Guide for the version of Glassfish you are using may solve you problem.


Short version, at least valid for versions 2-3-4 : use the Common Classloader (what exactly this common classloader does and its relation to the other classloaders is explained in the same manual). There are several ways to do this:

  • copy the jar to domain-dir/lib
  • or copy the jar to as-install/lib
  • or run asadmin add-library --type common /path/to/your.jar (will only work in version 4 iirc)

There are several questions here on SO that are related to "Circumventing Class Loader Isolation" (just use that search term), look there for examples and more discussion.




回答2:


Simply put, the singleton will likely "live" where your caching implementation class lives, as that's the classloader in the hierarchy that "owns" the class.

So, if each EJB is separately deployed, with their own copy of the cache lib jar, they'll each get their own copy.

If your beans are deployed in a composite EAR, sharing a single instance of the lib jar, then that cache will be shared across the beans in the EAR.

If you remove the lib from the deployment completely, and put it outside the container ($DOMAIN/lib/ext for example), then that cache will be shared by EVERYTHING in the domain (EJBs, EARs, WARs, etc.).



来源:https://stackoverflow.com/questions/1498011/how-do-you-share-java-caching-system-jcs-resource-across-multiple-ejb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!