I'm trying to setup a distributed executor service with hazelcast for my project. Some tasks which will run can only be completed on machines with OS specific utilities. Is there a way to submit a task that will run once on a subset of the cluster? Or to register that it should be used with a specific executor service?
Looking at the API there are a number of options to submit a task, but all the options for submitting to multiple members will run the task on all of those members, not one of them.
I have looked at the javadoc and seen that there are a number of ways to submit Runnable objects to the executor service:
void executeOnMember(Runnable command, Member member) - this only allows me to specify one member, I need to specify a group of members.
void executeOnMembers(Runnable command, Collection members) - This allows me to specify a collection of members, but runs the task multiple times instead of just once.
void submitToMembers(Runnable task, Collection members, MultiExecutionCallback callback) - again this specifies a collection of members, but it will run the task multiple times.
解决方案
I'm going to copy the comment as answer, because this question was closed for some time for reasons beyond me.
I would make use of the IExecutorService.executeOnMember. If you want to execute on one of the members of a subset of members, select one member randomly or using some kind of loadbalancing policy (e.g. round robin.. or detect actual load) and then executeOnMember.
You could also wrap the IExecutorService by an Executor decorator that takes care of this:
class OsSpecificExecutor implements Executor{
private IExecutorService ex;
public void execute(Runnable task){
Set targetMembers = getYourTargetMembers()
Member member = random(targetMembers)
ex.executeOnMember(task, member);
}
}
This way you can use it as a normal executor, but underneath you have control on which members + optional load balancing.