Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.client.cache.impl;
import com.hazelcast.cache.impl.CacheEventData;
import com.hazelcast.cache.impl.CacheEventListenerAdaptor;
import com.hazelcast.cache.impl.CacheProxyUtil;
import com.hazelcast.cache.impl.CacheSyncListenerCompleter;
import com.hazelcast.cache.impl.nearcache.NearCache;
import com.hazelcast.cache.impl.nearcache.NearCacheContext;
import com.hazelcast.cache.impl.nearcache.NearCacheExecutor;
import com.hazelcast.cache.impl.nearcache.NearCacheManager;
import com.hazelcast.cache.impl.operation.MutableOperation;
import com.hazelcast.client.impl.ClientMessageDecoder;
import com.hazelcast.client.impl.HazelcastClientInstanceImpl;
import com.hazelcast.client.impl.protocol.ClientMessage;
import com.hazelcast.client.impl.protocol.codec.CacheAddEntryListenerCodec;
import com.hazelcast.client.impl.protocol.codec.CacheAddInvalidationListenerCodec;
import com.hazelcast.client.impl.protocol.codec.CacheClearCodec;
import com.hazelcast.client.impl.protocol.codec.CacheGetAndRemoveCodec;
import com.hazelcast.client.impl.protocol.codec.CacheGetAndReplaceCodec;
import com.hazelcast.client.impl.protocol.codec.CachePutCodec;
import com.hazelcast.client.impl.protocol.codec.CachePutIfAbsentCodec;
import com.hazelcast.client.impl.protocol.codec.CacheRemoveAllCodec;
import com.hazelcast.client.impl.protocol.codec.CacheRemoveAllKeysCodec;
import com.hazelcast.client.impl.protocol.codec.CacheRemoveCodec;
import com.hazelcast.client.impl.protocol.codec.CacheRemoveEntryListenerCodec;
import com.hazelcast.client.impl.protocol.codec.CacheReplaceCodec;
import com.hazelcast.client.spi.ClientContext;
import com.hazelcast.client.spi.ClientExecutionService;
import com.hazelcast.client.spi.ClientListenerService;
import com.hazelcast.client.spi.EventHandler;
import com.hazelcast.client.spi.impl.ClientInvocation;
import com.hazelcast.client.spi.impl.ClientInvocationFuture;
import com.hazelcast.client.spi.impl.ListenerMessageCodec;
import com.hazelcast.client.util.ClientDelegatingFuture;
import com.hazelcast.config.CacheConfig;
import com.hazelcast.config.NearCacheConfig;
import com.hazelcast.core.Client;
import com.hazelcast.core.ExecutionCallback;
import com.hazelcast.core.HazelcastInstanceNotActiveException;
import com.hazelcast.core.ICompletableFuture;
import com.hazelcast.nio.serialization.Data;
import com.hazelcast.util.ExceptionUtil;
import com.hazelcast.util.executor.CompletedFuture;
import javax.cache.CacheException;
import javax.cache.configuration.CacheEntryListenerConfiguration;
import javax.cache.expiry.ExpiryPolicy;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import static com.hazelcast.cache.impl.CacheProxyUtil.validateNotNull;
import static com.hazelcast.cache.impl.operation.MutableOperation.IGNORE_COMPLETION;
/**
* Abstract {@link com.hazelcast.cache.ICache} implementation which provides shared internal implementations
* of cache operations like put, replace, remove and invoke. These internal implementations are delegated
* by actual cache methods.
*
*
Note: this partial implementation is used by client.
*
* @param the type of key
* @param the type of value
*/
abstract class AbstractClientInternalCacheProxy
extends AbstractClientCacheProxyBase
implements CacheSyncListenerCompleter {
private static final long MAX_COMPLETION_LATCH_WAIT_TIME = TimeUnit.MINUTES.toMillis(5);
private static final long COMPLETION_LATCH_WAIT_TIME_STEP = TimeUnit.SECONDS.toMillis(1);
private static final ClientMessageDecoder getAndRemoveResponseDecoder = new ClientMessageDecoder() {
@Override
public T decodeClientMessage(ClientMessage clientMessage) {
return (T) CacheGetAndRemoveCodec.decodeResponse(clientMessage).response;
}
};
private static final ClientMessageDecoder removeResponseDecoder = new ClientMessageDecoder() {
@Override
public T decodeClientMessage(ClientMessage clientMessage) {
return (T) Boolean.valueOf(CacheRemoveCodec.decodeResponse(clientMessage).response);
}
};
private static final ClientMessageDecoder replaceResponseDecoder = new ClientMessageDecoder() {
@Override
public T decodeClientMessage(ClientMessage clientMessage) {
return (T) CacheReplaceCodec.decodeResponse(clientMessage).response;
}
};
private static final ClientMessageDecoder getAndReplaceResponseDecoder = new ClientMessageDecoder() {
@Override
public T decodeClientMessage(ClientMessage clientMessage) {
return (T) CacheGetAndReplaceCodec.decodeResponse(clientMessage).response;
}
};
private static final ClientMessageDecoder putResponseDecoder = new ClientMessageDecoder() {
@Override
public T decodeClientMessage(ClientMessage clientMessage) {
return (T) CachePutCodec.decodeResponse(clientMessage).response;
}
};
private static final ClientMessageDecoder putIfAbsentResponseDecoder = new ClientMessageDecoder() {
@Override
public T decodeClientMessage(ClientMessage clientMessage) {
return (T) Boolean.valueOf(CachePutIfAbsentCodec.decodeResponse(clientMessage).response);
}
};
protected final HazelcastClientCacheManager cacheManager;
protected final NearCacheManager nearCacheManager;
// Object => Data or
protected NearCache nearCache;
protected String nearCacheMembershipRegistrationId;
protected final ClientCacheStatisticsImpl statistics;
protected final boolean statisticsEnabled;
protected boolean cacheOnUpdate;
private final ConcurrentMap asyncListenerRegistrations;
private final ConcurrentMap syncListenerRegistrations;
private final ConcurrentMap syncLocks;
protected AbstractClientInternalCacheProxy(CacheConfig cacheConfig, ClientContext clientContext,
HazelcastClientCacheManager cacheManager) {
super(cacheConfig, clientContext);
this.cacheManager = cacheManager;
this.nearCacheManager = clientContext.getNearCacheManager();
this.asyncListenerRegistrations = new ConcurrentHashMap();
this.syncListenerRegistrations = new ConcurrentHashMap();
this.syncLocks = new ConcurrentHashMap();
initNearCache();
if (nearCache != null) {
this.statistics = new ClientCacheStatisticsImpl(System.currentTimeMillis(),
nearCache.getNearCacheStats());
} else {
this.statistics = new ClientCacheStatisticsImpl(System.currentTimeMillis());
}
this.statisticsEnabled = cacheConfig.isStatisticsEnabled();
}
private void initNearCache() {
NearCacheConfig nearCacheConfig = clientContext.getClientConfig().getNearCacheConfig(name);
if (nearCacheConfig != null) {
cacheOnUpdate = nearCacheConfig.getLocalUpdatePolicy() == NearCacheConfig.LocalUpdatePolicy.CACHE;
NearCacheContext nearCacheContext =
new NearCacheContext(nearCacheManager,
clientContext.getSerializationService(),
createNearCacheExecutor(clientContext.getExecutionService()));
nearCache = nearCacheManager.getOrCreateNearCache(nameWithPrefix, nearCacheConfig, nearCacheContext);
registerInvalidationListener();
}
}
private static class ClientNearCacheExecutor
implements NearCacheExecutor {
private ClientExecutionService clientExecutionService;
private ClientNearCacheExecutor(ClientExecutionService clientExecutionService) {
this.clientExecutionService = clientExecutionService;
}
@Override
public ScheduledFuture> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
return clientExecutionService.scheduleWithFixedDelay(command, initialDelay, delay, unit);
}
}
private NearCacheExecutor createNearCacheExecutor(ClientExecutionService clientExecutionService) {
return new ClientNearCacheExecutor(clientExecutionService);
}
@Override
public void postClose() {
if (nearCache != null) {
removeInvalidationListener();
nearCacheManager.clearNearCache(nearCache.getName());
}
if (statisticsEnabled) {
statistics.clear();
}
}
@Override
protected void postDestroy() {
if (nearCache != null) {
removeInvalidationListener();
nearCacheManager.destroyNearCache(nearCache.getName());
}
if (statisticsEnabled) {
statistics.clear();
}
cacheManager.removeCache(cacheConfig.getName(), false);
}
protected ClientInvocationFuture invoke(ClientMessage req, int partitionId, int completionId) {
final boolean completionOperation = completionId != -1;
if (completionOperation) {
registerCompletionLatch(completionId, 1);
}
try {
HazelcastClientInstanceImpl client = (HazelcastClientInstanceImpl) clientContext.getHazelcastInstance();
final ClientInvocation clientInvocation = new ClientInvocation(client, req, partitionId);
ClientInvocationFuture f = clientInvocation.invoke();
if (completionOperation) {
waitCompletionLatch(completionId, f);
}
return f;
} catch (Throwable e) {
if (e instanceof IllegalStateException) {
close();
}
if (completionOperation) {
deregisterCompletionLatch(completionId);
}
throw ExceptionUtil.rethrowAllowedTypeFirst(e, CacheException.class);
}
}
protected ClientInvocationFuture invoke(ClientMessage req, Data keyData, int completionId) {
int partitionId = clientContext.getPartitionService().getPartitionId(keyData);
return invoke(req, partitionId, completionId);
}
protected T getSafely(Future future) {
try {
return future.get();
} catch (Throwable throwable) {
throw ExceptionUtil.rethrow(throwable);
}
}
protected ICompletableFuture getAndRemoveAsyncInternal(K key, boolean withCompletionEvent, boolean async) {
final long start = System.nanoTime();
ensureOpen();
validateNotNull(key);
CacheProxyUtil.validateConfiguredTypes(cacheConfig, key);
final Data keyData = toData(key);
final int completionId = withCompletionEvent ? nextCompletionId() : -1;
ClientMessage request = CacheGetAndRemoveCodec.encodeRequest(nameWithPrefix, keyData, completionId);
ClientInvocationFuture future;
try {
future = invoke(request, keyData, completionId);
invalidateNearCache(keyData);
} catch (Exception e) {
throw ExceptionUtil.rethrow(e);
}
ClientDelegatingFuture delegatingFuture =
new ClientDelegatingFuture(future, clientContext.getSerializationService(),
getAndRemoveResponseDecoder);
if (async && statisticsEnabled) {
delegatingFuture.andThen(new ExecutionCallback