hazelcast 搭建,是否可以创建Hazelcast ExecutorService在集群的子集上执行?

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值