Configuration - hazelcast/hazelcast-scala GitHub Wiki
The Scala API for Hazelcast adds just a few implicit methods to the Config
and ClientConfig
classes.
Properties
Most of the System properties have been made into type-safe method calls, which use scala.concurrent.duration.Duration
for time related properties and com.hazelcast.memory.MemorySize
for memory related properties. The latter also has an implicit conversion for Int
, so it can be written inline.
E.g. to set the hazelcast.socket.receive.buffer.size
property, you don't need to know the expected unit (kilobytes), but can instead set it explicitly, like this:
conf.setSocketReceiveBufferSize(1.megabytes)
Same with hazelcast.partition.migration.timeout
, you provide the unit explicitly:
conf.setPartitionMigrationTimeout(5.minutes)
Instance
Once the configuration is complete, call newInstance()
, or getInstance()
for named instances:
val hz = conf.newInstance()
Or for ClientConfig
:
import com.hazelcast.Scala.client._
val hz = conf.newClient()
Event listeners
Listening to events can be done either ad hoc (un/subscribe on demand) or at configuration time, which is for the lifetime of all instances created with that configuration.
E.g. to print a message for all IMap
s created:
conf.onDistributedObjectEvent() {
case DistributedObjectCreated(name, imap: IMap[_, _]) =>
println(s"IMap created: $name")
}
Or listen for all distributed objects (IMap/Multimap/IQueue, etc) named "foobar":
conf.onDistributedObjectEvent() {
case DistributedObjectCreated("foobar", distObj) =>
println(s"Another 'foobar' created: ${distObj.getServiceName}")
}
Listen to all clients connecting and disconnecting to cluster:
conf.onClient() {
case ClientConnected(client) =>
println(s"Connected: ${client.getSocketAddress}")
case ClientDisconnected(client) =>
println(s"Disconnected: ${client.getSocketAddress}")
}