Search in sources :

Example 16 with ClientInvocationFuture

use of com.hazelcast.client.spi.impl.ClientInvocationFuture in project hazelcast by hazelcast.

the class ClientRingbufferProxy method addAllAsync.

@Override
public ICompletableFuture<Long> addAllAsync(Collection<? extends E> collection, OverflowPolicy overflowPolicy) {
    checkNotNull(collection, "collection can't be null");
    checkNotNull(overflowPolicy, "overflowPolicy can't be null");
    checkFalse(collection.isEmpty(), "collection can't be empty");
    checkTrue(collection.size() <= MAX_BATCH_SIZE, "collection can't be larger than " + MAX_BATCH_SIZE);
    Collection<Data> dataCollection = CollectionUtil.objectToDataCollection(collection, getSerializationService());
    ClientMessage request = RingbufferAddAllCodec.encodeRequest(name, dataCollection, overflowPolicy.getId());
    try {
        ClientInvocationFuture invocationFuture = new ClientInvocation(getClient(), request, partitionId).invoke();
        return new ClientDelegatingFuture<Long>(invocationFuture, getContext().getSerializationService(), ADD_ALL_ASYNC_RESPONSE_DECODER);
    } catch (Exception e) {
        throw ExceptionUtil.rethrow(e);
    }
}
Also used : ClientDelegatingFuture(com.hazelcast.client.util.ClientDelegatingFuture) Data(com.hazelcast.nio.serialization.Data) ClientInvocation(com.hazelcast.client.spi.impl.ClientInvocation) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) StaleSequenceException(com.hazelcast.ringbuffer.StaleSequenceException) ExecutionException(java.util.concurrent.ExecutionException) ClientInvocationFuture(com.hazelcast.client.spi.impl.ClientInvocationFuture)

Example 17 with ClientInvocationFuture

use of com.hazelcast.client.spi.impl.ClientInvocationFuture in project hazelcast by hazelcast.

the class ClientRingbufferProxy method addAsync.

@Override
public ICompletableFuture<Long> addAsync(E item, OverflowPolicy overflowPolicy) {
    checkNotNull(item, "item can't be null");
    checkNotNull(overflowPolicy, "overflowPolicy can't be null");
    Data element = toData(item);
    ClientMessage request = RingbufferAddCodec.encodeRequest(name, overflowPolicy.getId(), element);
    try {
        ClientInvocationFuture invocationFuture = new ClientInvocation(getClient(), request, partitionId).invoke();
        return new ClientDelegatingFuture<Long>(invocationFuture, getContext().getSerializationService(), ADD_ASYNC_ASYNC_RESPONSE_DECODER);
    } catch (Exception e) {
        throw ExceptionUtil.rethrow(e);
    }
}
Also used : ClientDelegatingFuture(com.hazelcast.client.util.ClientDelegatingFuture) Data(com.hazelcast.nio.serialization.Data) ClientInvocation(com.hazelcast.client.spi.impl.ClientInvocation) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) StaleSequenceException(com.hazelcast.ringbuffer.StaleSequenceException) ExecutionException(java.util.concurrent.ExecutionException) ClientInvocationFuture(com.hazelcast.client.spi.impl.ClientInvocationFuture)

Example 18 with ClientInvocationFuture

use of com.hazelcast.client.spi.impl.ClientInvocationFuture in project hazelcast by hazelcast.

the class ClientAddressCancellableDelegatingFuture method invokeCancelRequest.

private boolean invokeCancelRequest(boolean mayInterruptIfRunning) throws InterruptedException {
    waitForRequestToBeSend();
    ClientInvocation clientInvocation;
    final HazelcastClientInstanceImpl client = (HazelcastClientInstanceImpl) context.getHazelcastInstance();
    ClientMessage request = ExecutorServiceCancelOnAddressCodec.encodeRequest(uuid, target, mayInterruptIfRunning);
    clientInvocation = new ClientInvocation(client, request, target);
    try {
        ClientInvocationFuture f = clientInvocation.invoke();
        return ExecutorServiceCancelOnAddressCodec.decodeResponse(f.get()).response;
    } catch (Exception e) {
        throw rethrow(e);
    }
}
Also used : HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl) ClientInvocation(com.hazelcast.client.spi.impl.ClientInvocation) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) CancellationException(java.util.concurrent.CancellationException) ClientInvocationFuture(com.hazelcast.client.spi.impl.ClientInvocationFuture)

Example 19 with ClientInvocationFuture

use of com.hazelcast.client.spi.impl.ClientInvocationFuture in project hazelcast by hazelcast.

the class CallbackAwareClientDelegatingFutureTest method test_CallbackAwareClientDelegatingFuture.

private void test_CallbackAwareClientDelegatingFuture(boolean timeout, boolean error) throws ExecutionException, InterruptedException {
    if (timeout && error) {
        throw new IllegalArgumentException("Only one of the `timeout` and `error` parameters can be enabled at the same time!");
    }
    int timeoutMillis = timeout ? 5000 : -1;
    createCache(timeoutMillis, error);
    ClientMessage getRequest = createGetRequest(1);
    ClientMessageDecoder decoder = CACHE_GET_RESPONSE_DECODER;
    ClientInvocation invocation = new ClientInvocation(client, getRequest, 0);
    ClientInvocationFuture invocationFuture = invocation.invoke();
    final AtomicBoolean responseCalled = new AtomicBoolean();
    final AtomicBoolean failureCalled = new AtomicBoolean();
    OneShotExecutionCallback callback = new OneShotExecutionCallback() {

        @Override
        protected void onResponseInternal(Object response) {
            responseCalled.set(true);
        }

        @Override
        protected void onFailureInternal(Throwable t) {
            failureCalled.set(true);
        }
    };
    CallbackAwareClientDelegatingFuture callbackAwareInvocationFuture = new CallbackAwareClientDelegatingFuture(invocationFuture, client.getSerializationService(), decoder, callback);
    if (timeoutMillis > 0) {
        try {
            callbackAwareInvocationFuture.get(timeoutMillis / 2, TimeUnit.MILLISECONDS);
            fail("Timeout expected!");
        } catch (TimeoutException e) {
            // Timeout expected
            assertTrue(failureCalled.get());
            assertFalse(responseCalled.get());
        }
    } else {
        if (error) {
            try {
                callbackAwareInvocationFuture.get();
                fail("CacheLoaderException expected!");
            } catch (ExecutionException e) {
                // Exception expected
                assertTrue(e.getCause() instanceof CacheLoaderException);
                assertTrue(failureCalled.get());
                assertFalse(responseCalled.get());
            }
        } else {
            try {
                callbackAwareInvocationFuture.get();
                assertTrue(responseCalled.get());
                assertFalse(failureCalled.get());
            } catch (CacheLoaderException e) {
                fail("CacheLoaderException not expected!");
            }
        }
    }
}
Also used : ClientInvocation(com.hazelcast.client.spi.impl.ClientInvocation) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) ClientInvocationFuture(com.hazelcast.client.spi.impl.ClientInvocationFuture) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClientMessageDecoder(com.hazelcast.client.impl.ClientMessageDecoder) CacheLoaderException(javax.cache.integration.CacheLoaderException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 20 with ClientInvocationFuture

use of com.hazelcast.client.spi.impl.ClientInvocationFuture in project hazelcast by hazelcast.

the class ClientRingbufferProxy method readManyAsync.

@Override
public ICompletableFuture<ReadResultSet<E>> readManyAsync(long startSequence, int minCount, int maxCount, IFunction<E, Boolean> filter) {
    checkSequence(startSequence);
    checkNotNegative(minCount, "minCount can't be smaller than 0");
    checkTrue(maxCount >= minCount, "maxCount should be equal or larger than minCount");
    checkTrue(minCount <= capacity(), "the minCount should be smaller than or equal to the capacity");
    checkTrue(maxCount <= MAX_BATCH_SIZE, "maxCount can't be larger than " + MAX_BATCH_SIZE);
    ClientMessage request = RingbufferReadManyCodec.encodeRequest(name, startSequence, minCount, maxCount, toData(filter));
    try {
        ClientInvocationFuture invocationFuture = new ClientInvocation(getClient(), request, partitionId).invoke();
        return new ClientDelegatingFuture<ReadResultSet<E>>(invocationFuture, getContext().getSerializationService(), readManyAsyncResponseDecoder);
    } catch (Exception e) {
        throw ExceptionUtil.rethrow(e);
    }
}
Also used : ClientDelegatingFuture(com.hazelcast.client.util.ClientDelegatingFuture) MAX_BATCH_SIZE(com.hazelcast.ringbuffer.impl.RingbufferProxy.MAX_BATCH_SIZE) ClientInvocation(com.hazelcast.client.spi.impl.ClientInvocation) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) StaleSequenceException(com.hazelcast.ringbuffer.StaleSequenceException) ExecutionException(java.util.concurrent.ExecutionException) ClientInvocationFuture(com.hazelcast.client.spi.impl.ClientInvocationFuture)

Aggregations

ClientInvocationFuture (com.hazelcast.client.spi.impl.ClientInvocationFuture)36 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)31 ClientInvocation (com.hazelcast.client.spi.impl.ClientInvocation)21 ClientDelegatingFuture (com.hazelcast.client.util.ClientDelegatingFuture)17 Data (com.hazelcast.nio.serialization.Data)9 SerializationService (com.hazelcast.spi.serialization.SerializationService)9 ExecutionException (java.util.concurrent.ExecutionException)9 CacheException (javax.cache.CacheException)7 CacheEventData (com.hazelcast.cache.impl.CacheEventData)6 HazelcastClientInstanceImpl (com.hazelcast.client.impl.HazelcastClientInstanceImpl)6 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)5 HazelcastException (com.hazelcast.core.HazelcastException)3 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)3 StaleSequenceException (com.hazelcast.ringbuffer.StaleSequenceException)3 ClientConnection (com.hazelcast.client.connection.nio.ClientConnection)2 EventHandler (com.hazelcast.client.spi.EventHandler)2 Connection (com.hazelcast.nio.Connection)2 CancellationException (java.util.concurrent.CancellationException)2 AuthenticationException (com.hazelcast.client.AuthenticationException)1 ClientMessageDecoder (com.hazelcast.client.impl.ClientMessageDecoder)1