Search in sources :

Example 21 with IScheduledFuture

use of com.hazelcast.scheduledexecutor.IScheduledFuture in project Payara by payara.

the class ClusterExecutionService method schedule.

/**
 * Schedules a Callable object to be run in the future on the specified Member
 * @param <V> The type of the result
 * @param memberUUIDs The members to schedule the task on
 * @param callable The Callable Object
 * @param delay The delay before running the task
 * @param unit The time unit of the delay
 * @return A Future containing the result
 */
public <V extends Serializable> Map<String, ScheduledTaskFuture<V>> schedule(Collection<String> memberUUIDs, Callable<V> callable, long delay, TimeUnit unit) {
    HashMap<String, ScheduledTaskFuture<V>> result = new HashMap<>(2);
    if (hzCore.isEnabled()) {
        Collection<Member> toSubmitTo = selectMembers(memberUUIDs);
        IScheduledExecutorService scheduledExecutorService = hzCore.getInstance().getScheduledExecutorService(HazelcastCore.SCHEDULED_CLUSTER_EXECUTOR_SERVICE_NAME);
        Map<Member, IScheduledFuture<V>> schedule = scheduledExecutorService.scheduleOnMembers(callable, toSubmitTo, delay, unit);
        for (Member member : schedule.keySet()) {
            result.put(member.getUuid(), new ScheduledTaskFuture<>(schedule.get(member)));
        }
    }
    return result;
}
Also used : IScheduledFuture(com.hazelcast.scheduledexecutor.IScheduledFuture) IScheduledExecutorService(com.hazelcast.scheduledexecutor.IScheduledExecutorService) HashMap(java.util.HashMap) Member(com.hazelcast.core.Member)

Example 22 with IScheduledFuture

use of com.hazelcast.scheduledexecutor.IScheduledFuture in project Payara by payara.

the class ClusterExecutionService method scheduleAtFixedRate.

/**
 * Schedules a Callable object to be run in the future on the specified Member
 * @param memberUUIDs The members to schedule the task on
 * @param runnable The Runnable Object
 * @param delay The delay before running the task
 * @param period The period of the fixed rate
 * @param unit The time unit of the delay
 * @return A Future containing the result
 */
public Map<String, ScheduledTaskFuture<?>> scheduleAtFixedRate(Collection<String> memberUUIDs, Runnable runnable, long delay, long period, TimeUnit unit) {
    HashMap<String, ScheduledTaskFuture<?>> result = new HashMap<>(2);
    if (hzCore.isEnabled()) {
        Collection<Member> toSubmitTo = selectMembers(memberUUIDs);
        IScheduledExecutorService scheduledExecutorService = hzCore.getInstance().getScheduledExecutorService(HazelcastCore.SCHEDULED_CLUSTER_EXECUTOR_SERVICE_NAME);
        Map<Member, IScheduledFuture<?>> schedule = scheduledExecutorService.scheduleOnMembersAtFixedRate(runnable, toSubmitTo, delay, period, unit);
        for (Member member : schedule.keySet()) {
            result.put(member.getUuid(), new ScheduledTaskFuture<>(schedule.get(member)));
        }
    }
    return result;
}
Also used : IScheduledFuture(com.hazelcast.scheduledexecutor.IScheduledFuture) IScheduledExecutorService(com.hazelcast.scheduledexecutor.IScheduledExecutorService) HashMap(java.util.HashMap) Member(com.hazelcast.core.Member)

Example 23 with IScheduledFuture

use of com.hazelcast.scheduledexecutor.IScheduledFuture in project hazelcast by hazelcast.

the class ScheduledExecutorServiceProxy method scheduleOnMembersAtFixedRate.

@Nonnull
@Override
public <V> Map<Member, IScheduledFuture<V>> scheduleOnMembersAtFixedRate(@Nonnull Runnable command, @Nonnull Collection<Member> members, long initialDelay, long period, @Nonnull TimeUnit unit) {
    checkNotNull(command, "Command is null");
    checkNotNull(members, "Members is null");
    checkNotNull(unit, "Unit is null");
    command = initializeManagedContext(command);
    String name = extractNameOrGenerateOne(command);
    ScheduledRunnableAdapter<?> adapter = createScheduledRunnableAdapter(command);
    Map<Member, IScheduledFuture<V>> futures = createHashMapAdapter(members.size());
    for (Member member : members) {
        TaskDefinition definition = new TaskDefinition(TaskDefinition.Type.AT_FIXED_RATE, name, adapter, initialDelay, period, unit, false);
        futures.put(member, submitOnMemberSync(name, new ScheduleTaskOperation(getName(), definition), member));
    }
    return futures;
}
Also used : IScheduledFuture(com.hazelcast.scheduledexecutor.IScheduledFuture) ScheduleTaskOperation(com.hazelcast.scheduledexecutor.impl.operations.ScheduleTaskOperation) Member(com.hazelcast.cluster.Member) Nonnull(javax.annotation.Nonnull)

Example 24 with IScheduledFuture

use of com.hazelcast.scheduledexecutor.IScheduledFuture in project hazelcast by hazelcast.

the class ScheduledExecutorServiceProxy method accumulateTaskHandlersAsScheduledFutures.

@SuppressWarnings("unchecked")
private <V> void accumulateTaskHandlersAsScheduledFutures(Map<Member, List<IScheduledFuture<V>>> accumulator, Map<?, ?> taskHandlersMap) {
    ClusterService clusterService = getNodeEngine().getClusterService();
    IPartitionService partitionService = getNodeEngine().getPartitionService();
    for (Map.Entry<?, ?> entry : taskHandlersMap.entrySet()) {
        Member owner;
        Object key = entry.getKey();
        if (key instanceof Number) {
            owner = clusterService.getMember(partitionService.getPartitionOwner((Integer) key));
        } else {
            owner = (Member) key;
        }
        List<ScheduledTaskHandler> handlers = (List<ScheduledTaskHandler>) entry.getValue();
        List<IScheduledFuture<V>> futures = new ArrayList<>();
        for (ScheduledTaskHandler handler : handlers) {
            IScheduledFuture future = new ScheduledFutureProxy(handler, this);
            futures.add(initializeManagedContext(future));
        }
        if (accumulator.containsKey(owner)) {
            List<IScheduledFuture<V>> memberFutures = accumulator.get(owner);
            memberFutures.addAll(futures);
        } else {
            accumulator.put(owner, futures);
        }
    }
}
Also used : IPartitionService(com.hazelcast.internal.partition.IPartitionService) ArrayList(java.util.ArrayList) IScheduledFuture(com.hazelcast.scheduledexecutor.IScheduledFuture) ClusterService(com.hazelcast.internal.cluster.ClusterService) ScheduledTaskHandler(com.hazelcast.scheduledexecutor.ScheduledTaskHandler) AbstractDistributedObject(com.hazelcast.spi.impl.AbstractDistributedObject) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) MapUtil.createHashMap(com.hazelcast.internal.util.MapUtil.createHashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Member(com.hazelcast.cluster.Member)

Example 25 with IScheduledFuture

use of com.hazelcast.scheduledexecutor.IScheduledFuture in project hazelcast by hazelcast.

the class ScheduledExecutorServiceProxy method scheduleOnMembers.

@Nonnull
@Override
public <V> Map<Member, IScheduledFuture<V>> scheduleOnMembers(@Nonnull Callable<V> command, @Nonnull Collection<Member> members, long delay, @Nonnull TimeUnit unit) {
    checkNotNull(command, "Command is null");
    checkNotNull(members, "Members is null");
    checkNotNull(unit, "Unit is null");
    command = initializeManagedContext(command);
    String name = extractNameOrGenerateOne(command);
    Map<Member, IScheduledFuture<V>> futures = createHashMap(members.size());
    boolean autoDisposable = isAutoDisposable(command);
    for (Member member : members) {
        TaskDefinition<V> definition = new TaskDefinition<>(TaskDefinition.Type.SINGLE_RUN, name, command, delay, unit, autoDisposable);
        futures.put(member, submitOnMemberSync(name, new ScheduleTaskOperation(getName(), definition), member));
    }
    return futures;
}
Also used : IScheduledFuture(com.hazelcast.scheduledexecutor.IScheduledFuture) ScheduleTaskOperation(com.hazelcast.scheduledexecutor.impl.operations.ScheduleTaskOperation) Member(com.hazelcast.cluster.Member) Nonnull(javax.annotation.Nonnull)

Aggregations

IScheduledFuture (com.hazelcast.scheduledexecutor.IScheduledFuture)48 IScheduledExecutorService (com.hazelcast.scheduledexecutor.IScheduledExecutorService)39 HazelcastInstance (com.hazelcast.core.HazelcastInstance)35 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)34 Test (org.junit.Test)34 QuickTest (com.hazelcast.test.annotation.QuickTest)27 Config (com.hazelcast.config.Config)16 ScheduledExecutorConfig (com.hazelcast.config.ScheduledExecutorConfig)16 Member (com.hazelcast.cluster.Member)14 ICountDownLatch (com.hazelcast.cp.ICountDownLatch)14 HashMap (java.util.HashMap)12 ArrayList (java.util.ArrayList)10 ScheduledTaskHandler (com.hazelcast.scheduledexecutor.ScheduledTaskHandler)7 SlowTest (com.hazelcast.test.annotation.SlowTest)7 Member (com.hazelcast.core.Member)5 TaskDefinition (com.hazelcast.scheduledexecutor.impl.TaskDefinition)5 List (java.util.List)5 Map (java.util.Map)5 Nonnull (javax.annotation.Nonnull)4 LinkedList (java.util.LinkedList)3