Skip to content

Executor service

Nils Kilden-Pedersen edited this page Jul 14, 2016 · 4 revisions

Hazelcast provides distributed compute through the IExecutorService.

The Scala API provides two overloaded methods, submit, one for running on a single member, which returns a Future[T], and one for running on multiple members, returning Map[Member, Future[T]]. Both provide access to the local Hazelcast instance.

A task submission is implemented as a lambda and can be submitted either to a single member or to multiple members, e.g. this will submit the 2 + 2 computation to a single arbitrary member of the cluster:

val exec = hz.getExecutorService("distributed-threadpool")
val result: Future[Int] = exec.submit(ToOne) { _ =>
  2 + 2
}
result.foreach(println)

We can also submit to the member that holds a given key, to ensure data locality:

val key = "abc"
val result = exec.submit(ToKeyOwner(key)) { hz =>
  val numbers = hz.getMap[String, Int]("numberMap")
  val num = numbers.get(key)
  num + 42
}
result.onComplete {
  case Success(result) => println(result)
  case Failure(e) => e.printStackTrace()
}

The following member selectors are available:

One member

  • exec.submit(ToOne), submits to an arbitrary member
  • exec.submit(ToLocal), submits to the member calling submit. NOTE: This is a no-op when called from a client
  • exec.submit(ToOneWhere(_.isLiteMember)), submits to one member that matches the function, in this example a lite member.
  • exec.submit(ToMember(member)), submits to specific member.
  • exec.submit(ToKeyOwner(key)), submits to member that owns the key, as determined by the deterministic hashing.

Multiple members

  • exec.submit(ToAll), submits to all members of the cluster.
  • exec.submit(ToMembers(member1, member2, etc)), submits to specific members.
  • exec.submit(ToMembersWhere(_.isLiteMember)), submits to all members that match the function, in this example all lite members.

See the page on User context for access to local non-Hazelcast resources.

Clone this wiki locally