0

Below is my scenario from Application perspective.

We have 2 applications (.war) files will be running in a same instance of Application server (mostly Tomcat 8), In production we may deploy App1 on 100 servers and App2 only on 50 server out of those 100 (The App2 does not need to be distributed so much)

Now this 2 applications (.war) depends on a common custom jar (some utility classes)

I am planning to use Jcache API and hazelcast implementation in our apps. I have added following dependency in my pom.xml

<!-- JSR 107 JCache -->
    <dependency>
        <groupId>javax.cache</groupId>
        <artifactId>cache-api</artifactId>
        <version>1.0.0</version>
    </dependency>
    <!-- Hazelcast dependency -->
    <dependency>
        <groupId>com.hazelcast</groupId>
        <artifactId>hazelcast</artifactId>
        <version>3.4</version>
    </dependency>

Plan is to write a utility CacheManager in this common custom jar which will be shared by App1 and App2.

I am planning to use only the hazelcast server provider as I am doing in-memory cluster i.e. the caching will be in application memory.

Below is the snippet of my code.

public class PPCacheManager {

// Loads the default CacheProvider (HazelCast) from hazelcast.xml which is
// in classpath
private static CachingProvider defaultCachingProvider = Caching.getCachingProvider(); //
// Loads the default CacheManager from hazelcast.xml which is in classpath
private static CacheManager defaultCacheManager = defaultCachingProvider.getCacheManager();
// Some more code goes here...

My hazelast.xml

<hazelcast xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.hazelcast.com/schema/config
                           http://www.hazelcast.com/schema/config/hazelcast-config-3.4.xsd"
       xmlns="http://www.hazelcast.com/schema/config">



<cache name="commonClientCache">
    <key-type class-name="java.lang.String"></key-type>
    <value-type class-name="java.lang.Object"></value-type>
    <statistics-enabled>true</statistics-enabled>
    <management-enabled>true</management-enabled>
    <read-through>true</read-through>
</cache>
</hazelcast>

Now I have several question around this approach.

  1. Is this a good way to implement the in memory caching (currently we are not looking for cluster caching), should this code be in the common custom jar or somewhere else?

  2. There is some master data from DB which I am planning to load (both applications need this data) so not sure how and where I should load this data into memory. Note: I do not want to do lazy loading; I want to load this master data very first.

  3. Where should I add the cache shutdown code to avoid memory leak issues, as this cache is shared by both the applications.

Update

Also by implementing this approach will I have 2 copies of cache each for application or a single copy will be shared across both?

I have already implemented this approach in my application and from Hazelcast management console I can see that there is only 1 cache is created but it says GET is executed on this cache twice.

4

1 回答 1

0

Hazelcast 是您想要做的事情的完美解决方案。绝对没有延迟加载。如果您有共享内存,则不需要这样的东西。

就你在一个(Tomcat)JVM 中有多少个实例而言,如果你实例化 Hazelcast 两次,你就会有两个。它会自动增加端口。但是,只要集群名称相同,两者都将属于同一个集群(您称为“缓存”)。所以除了看起来有点傻(在单个 JVM 上进行分片),你很好。为避免这种情况,您可以配置其中一场战争来实例化 HazelcastClient。实用程序 jar 可以相同。它应该都在一些例如 Spring 配置中——每场战争都会有它自己的副本。或者您可以将该配置放入两个外部目录并添加到 catalina 类路径。

关闭代码属于您实例化 Hazelcast 的同一个地方,即您的两次战争将有两个关闭调用。您可以在任何高级配置(或自动装配)bean 的 Spring 的 destroy() 中执行此操作,或者将其放入 Web App 会话侦听器中。

于 2015-06-06T23:02:26.523 回答