This module is a
dropin
replacement for EhCacheImpl or MemcachedImpl from Play.
It allow your application to have a clustered cache when you can’t install a memcached server on your platform.
It’s also a great free open source In-Memory Data Grid
See Hazelcast Documentation for more details.
To install Hazelcast Plugin module, you do like every other Play Modules:
play install hazelcast[-version]
- The Play 1.x way (in application.conf)
module.hazelcast=${play.path}/modules/hazelcast-[version]
- The Play 1.2.x way (in dependencies.yml)
require:
- play -> hazelcast [version]
You can disable the module in your application by usinga parameter in application.conf
:
hazelcast.disabled=true
The value defaults to false.
hazelcast.configFile=conf/hazelcast-dev.xml
You can specify the configuration file to be used. The value defaults to conf/hazelcast.xml
All the standard Play annotations or methods work seamlessly with Hazelcast instead of EhCache or Memcached.
CacheFor("1h")
Cache.xxx()
- Etc…
See Play Framework Cache Documentation for
CacheFor("1h")
and provided Play!Cache.xxx()
usage.
@Inject private static HazelcastInstance hazel;
@Inject private static ExecutorService executor;
@Named("MyQueue") private static Queue myQueue;
- Distributed Queues (Entries are consumed by one and only one JVM)
- Distributed Topics (Entries are consumed by all JVM)
- Distributed Events
- Distributed AtomicNumber provider
- Distributed ExecutorService
- Distributed ID Generator (ID are UNIQUE in all cluster nodes)
- Distributed List (to share a list in the cluster between many JVM)
- Distributed Lock (to apply a lock on an object and share between JVM)
- Distributed Set
- Distributed Map/MultiMap (Maps are shared between JMV, MultiMap allow for multiple values for the same key)
- Distributed Transaction (affect transactions on clustered objects, not database!)
You can modify hazelcast.xml to your need to create more Queues or Map and modify the default ExecutorService parameters. Just copy the hazelcast.xml file from the module conf directory to your application conf directory. This is also the file that allow you to change from multicast to TCPIP static cluster definition.
- Queue
- Topic
- AtomicNumber
- ExecutorService
- Set
- Map
- MultiMap
- IdGenerator
- Transaction
- ExecutorService
- PartitionService
- Event
1 Use this to get a Transaction object then you can begin()
or commit()
or rollback()
operations on cluster.
2 Use this to lock an object across the cluster.
3 Use this if you absolutly NEED to directly get the Default Hazelcast instance.
- application.conf
hibernate.cache.region.factory_class=com.hazelcast.hibernate.HazelcastCacheRegionFactory
hibernate.cache.use_query_cache=false
hibernate.cache.use_minimal_puts=true
hibernate.cache.use_second_level_cache=true
hibernate.cache.region_prefix=myApp
hibernate.cache.use_structured_entries=true
- dependencies.yml
# Application dependencies
require:
- play
- play -> hazelcast 0.4
- com.hazelcast -> hazelcast-hibernate 1.9.4.4
- Entity class annotation
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "model")
- hazelcast.xml – add following map definition
<map name="myApp.model">
<backup-count>1</backup-count>
<time-to-live-seconds>0</time-to-live-seconds>
<max-idle-seconds>3600</max-idle-seconds>
<eviction-policy>LRU</eviction-policy>
<max-size policy="cluster_wide_map_size">10000</max-size>
<eviction-percentage>25</eviction-percentage>
</map>
I’d like to see some basic Play! features use Hazel capabilities: