Search in sources :

Example 6 with ExecutionCallback

use of com.hazelcast.core.ExecutionCallback in project hazelcast by hazelcast.

the class ClientExecutorServiceSubmitTest method submitRunnable_withExecutionCallback.

@Test
public void submitRunnable_withExecutionCallback() {
    IExecutorService service = client.getExecutorService(randomString());
    final CountDownLatch responseLatch = new CountDownLatch(1);
    String mapName = randomString();
    Runnable runnable = new MapPutRunnable(mapName);
    MemberSelector selector = new SelectAllMembers();
    service.submit(runnable, selector, new ExecutionCallback() {

        public void onResponse(Object response) {
            responseLatch.countDown();
        }

        public void onFailure(Throwable t) {
        }
    });
    IMap map = client.getMap(mapName);
    assertOpenEventually("responseLatch", responseLatch);
    assertEquals(1, map.size());
}
Also used : IMap(com.hazelcast.core.IMap) MemberSelector(com.hazelcast.core.MemberSelector) MapPutPartitionAwareRunnable(com.hazelcast.client.executor.tasks.MapPutPartitionAwareRunnable) MapPutRunnable(com.hazelcast.client.executor.tasks.MapPutRunnable) MapPutRunnable(com.hazelcast.client.executor.tasks.MapPutRunnable) SelectAllMembers(com.hazelcast.client.executor.tasks.SelectAllMembers) IExecutorService(com.hazelcast.core.IExecutorService) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) CountDownLatch(java.util.concurrent.CountDownLatch) MultiExecutionCallback(com.hazelcast.core.MultiExecutionCallback) ExecutionCallback(com.hazelcast.core.ExecutionCallback) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 7 with ExecutionCallback

use of com.hazelcast.core.ExecutionCallback in project hazelcast by hazelcast.

the class ClientExecutorServiceSubmitTest method testSubmitCallableToMember_withExecutionCallback.

@Test
public void testSubmitCallableToMember_withExecutionCallback() throws Exception {
    IExecutorService service = client.getExecutorService(randomString());
    Callable getUuidCallable = new GetMemberUuidTask();
    Member member = server.getCluster().getLocalMember();
    final CountDownLatch responseLatch = new CountDownLatch(1);
    final AtomicReference<Object> result = new AtomicReference<Object>();
    service.submitToMember(getUuidCallable, member, new ExecutionCallback() {

        @Override
        public void onResponse(Object response) {
            result.set(response);
            responseLatch.countDown();
        }

        @Override
        public void onFailure(Throwable t) {
        }
    });
    assertOpenEventually("responseLatch", responseLatch);
    assertEquals(member.getUuid(), result.get());
}
Also used : GetMemberUuidTask(com.hazelcast.client.executor.tasks.GetMemberUuidTask) AtomicReference(java.util.concurrent.atomic.AtomicReference) IExecutorService(com.hazelcast.core.IExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) Member(com.hazelcast.core.Member) NullCallable(com.hazelcast.client.executor.tasks.NullCallable) Callable(java.util.concurrent.Callable) MapPutPartitionAwareCallable(com.hazelcast.client.executor.tasks.MapPutPartitionAwareCallable) AppendCallable(com.hazelcast.client.executor.tasks.AppendCallable) MultiExecutionCallback(com.hazelcast.core.MultiExecutionCallback) ExecutionCallback(com.hazelcast.core.ExecutionCallback) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 8 with ExecutionCallback

use of com.hazelcast.core.ExecutionCallback in project hazelcast by hazelcast.

the class ClientMapNearCacheTest method testAfterSubmitToKeyWithCallbackKeyIsInvalidatedFromNearCache.

@Test
public void testAfterSubmitToKeyWithCallbackKeyIsInvalidatedFromNearCache() throws Exception {
    final int mapSize = 1000;
    String mapName = randomMapName("testAfterSubmitToKeyWithCallbackKeyIsInvalidatedFromNearCache");
    Random random = new Random();
    HazelcastInstance member = hazelcastFactory.newHazelcastInstance(newConfig());
    IMap memberMap = member.getMap(mapName);
    populateMap(memberMap, mapSize);
    HazelcastInstance client = getClient(hazelcastFactory, newInvalidationOnChangeEnabledNearCacheConfig(mapName));
    final IMap<Integer, Integer> clientMap = client.getMap(mapName);
    populateNearCache(clientMap, mapSize);
    final CountDownLatch latch = new CountDownLatch(1);
    ExecutionCallback<Integer> callback = new ExecutionCallback<Integer>() {

        @Override
        public void onResponse(Integer response) {
            latch.countDown();
        }

        @Override
        public void onFailure(Throwable t) {
        }
    };
    int randomKey = random.nextInt(mapSize);
    clientMap.submitToKey(randomKey, new IncrementEntryProcessor(), callback);
    latch.await(3, TimeUnit.SECONDS);
    assertTrueEventually(new AssertTask() {

        public void run() {
            assertThatOwnedEntryCountEquals(clientMap, mapSize - 1);
        }
    });
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IMap(com.hazelcast.core.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Random(java.util.Random) AssertTask(com.hazelcast.test.AssertTask) ExecutionCallback(com.hazelcast.core.ExecutionCallback) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 9 with ExecutionCallback

use of com.hazelcast.core.ExecutionCallback in project hazelcast by hazelcast.

the class ClientInvocationTest method executionCallback_FailOnShutdown.

@Test
public void executionCallback_FailOnShutdown() {
    HazelcastInstance server = hazelcastFactory.newHazelcastInstance();
    HazelcastInstance client = hazelcastFactory.newHazelcastClient();
    final CountDownLatch disconnectedLatch = new CountDownLatch(1);
    IMap<Object, Object> map = client.getMap(randomName());
    client.getLifecycleService().addLifecycleListener(new LifecycleListener() {

        @Override
        public void stateChanged(LifecycleEvent event) {
            if (event.getState() == LifecycleEvent.LifecycleState.CLIENT_DISCONNECTED) {
                disconnectedLatch.countDown();
            }
        }
    });
    server.shutdown();
    assertOpenEventually(disconnectedLatch);
    int n = 100;
    final CountDownLatch errorLatch = new CountDownLatch(n);
    for (int i = 0; i < n; i++) {
        try {
            map.submitToKey(randomString(), new DummyEntryProcessor(), new ExecutionCallback() {

                @Override
                public void onResponse(Object response) {
                }

                @Override
                public void onFailure(Throwable t) {
                    errorLatch.countDown();
                }
            });
        } catch (Exception e) {
            errorLatch.countDown();
        }
    }
    assertOpenEventually("Not all of the requests failed", errorLatch);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) LifecycleEvent(com.hazelcast.core.LifecycleEvent) LifecycleListener(com.hazelcast.core.LifecycleListener) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionCallback(com.hazelcast.core.ExecutionCallback) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 10 with ExecutionCallback

use of com.hazelcast.core.ExecutionCallback in project hazelcast by hazelcast.

the class TestManagedContext method testRunnableTask.

@Test
public void testRunnableTask() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
    instance1.getExecutorService("test").submitToMember(new SomeRunnableTask(), instance2.getCluster().getLocalMember(), new ExecutionCallback() {

        public void onResponse(Object response) {
            latch.countDown();
        }

        public void onFailure(Throwable t) {
            error.set(t);
            latch.countDown();
        }
    });
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    Throwable throwable = error.get();
    if (throwable != null) {
        sneakyThrow(throwable);
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutionCallback(com.hazelcast.core.ExecutionCallback) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

ExecutionCallback (com.hazelcast.core.ExecutionCallback)44 QuickTest (com.hazelcast.test.annotation.QuickTest)39 Test (org.junit.Test)39 ParallelTest (com.hazelcast.test.annotation.ParallelTest)37 CountDownLatch (java.util.concurrent.CountDownLatch)24 MultiExecutionCallback (com.hazelcast.core.MultiExecutionCallback)11 IMap (com.hazelcast.core.IMap)10 IExecutorService (com.hazelcast.core.IExecutorService)9 HazelcastTestSupport.randomString (com.hazelcast.test.HazelcastTestSupport.randomString)9 MapPutPartitionAwareRunnable (com.hazelcast.client.executor.tasks.MapPutPartitionAwareRunnable)8 MapPutRunnable (com.hazelcast.client.executor.tasks.MapPutRunnable)8 HazelcastInstance (com.hazelcast.core.HazelcastInstance)7 Member (com.hazelcast.core.Member)7 AssertTask (com.hazelcast.test.AssertTask)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 MemberLeftException (com.hazelcast.core.MemberLeftException)4 ExpectedRuntimeException (com.hazelcast.test.ExpectedRuntimeException)4 ExecutionException (java.util.concurrent.ExecutionException)4 Config (com.hazelcast.config.Config)3 DurableExecutorService (com.hazelcast.durableexecutor.DurableExecutorService)3