Search in sources :

Example 1 with ClientMessageDecoder

use of com.hazelcast.client.impl.ClientMessageDecoder in project hazelcast by hazelcast.

the class ClientRingbufferProxy method onInitialize.

@Override
protected void onInitialize() {
    String partitionKey = StringPartitioningStrategy.getPartitionKey(name);
    partitionId = getContext().getPartitionService().getPartitionId(partitionKey);
    final SerializationService serializationService = getContext().getSerializationService();
    readManyAsyncResponseDecoder = new ClientMessageDecoder() {

        @Override
        public PortableReadResultSet decodeClientMessage(ClientMessage clientMessage) {
            final RingbufferReadManyCodec.ResponseParameters responseParameters = RingbufferReadManyCodec.decodeResponse(clientMessage);
            PortableReadResultSet readResultSet = new PortableReadResultSet(responseParameters.readCount, responseParameters.items);
            readResultSet.setSerializationService(serializationService);
            return readResultSet;
        }
    };
}
Also used : ClientMessageDecoder(com.hazelcast.client.impl.ClientMessageDecoder) PortableReadResultSet(com.hazelcast.ringbuffer.impl.client.PortableReadResultSet) SerializationService(com.hazelcast.spi.serialization.SerializationService) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage)

Example 2 with ClientMessageDecoder

use of com.hazelcast.client.impl.ClientMessageDecoder 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)

Aggregations

ClientMessageDecoder (com.hazelcast.client.impl.ClientMessageDecoder)2 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)2 ClientInvocation (com.hazelcast.client.spi.impl.ClientInvocation)1 ClientInvocationFuture (com.hazelcast.client.spi.impl.ClientInvocationFuture)1 PortableReadResultSet (com.hazelcast.ringbuffer.impl.client.PortableReadResultSet)1 SerializationService (com.hazelcast.spi.serialization.SerializationService)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 CacheLoaderException (javax.cache.integration.CacheLoaderException)1