Search in sources :

Example 26 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<UUID, ScheduledTaskFuture<?>> scheduleAtFixedRate(Collection<UUID> memberUUIDs, Runnable runnable, long delay, long period, TimeUnit unit) {
    HashMap<UUID, 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<Object>> schedule = scheduledExecutorService.<Object>scheduleOnMembersAtFixedRate(runnable, toSubmitTo, delay, period, unit);
        for (Entry<Member, IScheduledFuture<Object>> entry : schedule.entrySet()) {
            Member member = entry.getKey();
            result.put(member.getUuid(), new ScheduledTaskFuture<>(entry.getValue()));
        }
    }
    return result;
}
Also used : IScheduledFuture(com.hazelcast.scheduledexecutor.IScheduledFuture) IScheduledExecutorService(com.hazelcast.scheduledexecutor.IScheduledExecutorService) HashMap(java.util.HashMap) UUID(java.util.UUID) Member(com.hazelcast.cluster.Member)

Example 27 with IScheduledFuture

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

the class ClientScheduledExecutorProxy 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");
    String name = extractNameOrGenerateOne(command);
    Callable adapter = createScheduledRunnableAdapter(command);
    Map<Member, IScheduledFuture<V>> futures = new HashMap<>();
    for (Member member : members) {
        TaskDefinition definition = new TaskDefinition(TaskDefinition.Type.AT_FIXED_RATE, name, adapter, initialDelay, period, unit, false);
        futures.put(member, scheduleOnMember(name, member, definition));
    }
    return futures;
}
Also used : IScheduledFuture(com.hazelcast.scheduledexecutor.IScheduledFuture) TaskDefinition(com.hazelcast.scheduledexecutor.impl.TaskDefinition) HashMap(java.util.HashMap) Member(com.hazelcast.cluster.Member) Callable(java.util.concurrent.Callable) Nonnull(javax.annotation.Nonnull)

Example 28 with IScheduledFuture

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

the class ClientScheduledExecutorProxy 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");
    String name = extractNameOrGenerateOne(command);
    Map<Member, IScheduledFuture<V>> futures = new HashMap<>();
    boolean autoDisposable = isAutoDisposable(command);
    for (Member member : members) {
        TaskDefinition definition = new TaskDefinition(TaskDefinition.Type.SINGLE_RUN, name, command, delay, unit, autoDisposable);
        futures.put(member, scheduleOnMember(name, member, definition));
    }
    return futures;
}
Also used : IScheduledFuture(com.hazelcast.scheduledexecutor.IScheduledFuture) TaskDefinition(com.hazelcast.scheduledexecutor.impl.TaskDefinition) HashMap(java.util.HashMap) Member(com.hazelcast.cluster.Member) Nonnull(javax.annotation.Nonnull)

Example 29 with IScheduledFuture

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

the class ScheduledExecutorServiceBasicTest method config.

@Test
public void config() {
    String schedulerName = ANY_EXECUTOR_NAME;
    ScheduledExecutorConfig sec = new ScheduledExecutorConfig().setName(schedulerName).setDurability(5).setPoolSize(24);
    Config config = new Config().addScheduledExecutorConfig(sec);
    HazelcastInstance[] instances = createClusterWithCount(1, config);
    IScheduledFuture future = instances[0].getScheduledExecutorService(schedulerName).schedule(new PlainCallableTask(), 0, SECONDS);
    NodeEngineImpl nodeEngine = getNodeEngineImpl(instances[0]);
    ManagedExecutorService mes = (ManagedExecutorService) nodeEngine.getExecutionService().getScheduledDurable(sec.getName());
    DistributedScheduledExecutorService dses = nodeEngine.getService(DistributedScheduledExecutorService.SERVICE_NAME);
    assertNotNull(mes);
    assertEquals(24, mes.getMaximumPoolSize());
    assertEquals(5, dses.getPartition(future.getHandler().getPartitionId()).getOrCreateContainer(schedulerName).getDurability());
    assertEquals(1, dses.getPartition(future.getHandler().getPartitionId()).getOrCreateContainer("other").getDurability());
}
Also used : IScheduledFuture(com.hazelcast.scheduledexecutor.IScheduledFuture) NodeEngineImpl(com.hazelcast.spi.impl.NodeEngineImpl) Accessors.getNodeEngineImpl(com.hazelcast.test.Accessors.getNodeEngineImpl) ManagedExecutorService(com.hazelcast.internal.util.executor.ManagedExecutorService) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ScheduledExecutorConfig(com.hazelcast.config.ScheduledExecutorConfig) Config(com.hazelcast.config.Config) ScheduledExecutorConfig(com.hazelcast.config.ScheduledExecutorConfig) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 30 with IScheduledFuture

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

the class ScheduledExecutorServiceBasicTest method schedule_andCancel.

@Test
public void schedule_andCancel() {
    HazelcastInstance[] instances = createClusterWithCount(2);
    ICountDownLatch latch = instances[0].getCPSubsystem().getCountDownLatch("latch");
    latch.trySetCount(1);
    IScheduledExecutorService executorService = getScheduledExecutor(instances, ANY_EXECUTOR_NAME);
    IScheduledFuture future = executorService.scheduleAtFixedRate(new ICountdownLatchRunnableTask("latch"), 1, 1, SECONDS);
    sleepSeconds(5);
    assertFalse(future.isCancelled());
    assertFalse(future.isDone());
    future.cancel(false);
    assertTrue(future.isCancelled());
    assertTrue(future.isDone());
}
Also used : IScheduledFuture(com.hazelcast.scheduledexecutor.IScheduledFuture) HazelcastInstance(com.hazelcast.core.HazelcastInstance) IScheduledExecutorService(com.hazelcast.scheduledexecutor.IScheduledExecutorService) ICountDownLatch(com.hazelcast.cp.ICountDownLatch) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

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