Solution is using pessimistic locks implemented on MongoDB in order to choose a leader.
Run several instances of TaskRunnerExample and turn them off one by one in order to observe a behaviour
For more information read through this post http://vedmiediev.github.io/2016/01/31/simple-leader-election/