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-2015, 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.CacheClearResponse;
import com.hazelcast.cache.impl.CacheEventListenerAdaptor;
import com.hazelcast.cache.impl.CacheProxyUtil;
import com.hazelcast.cache.impl.CacheSyncListenerCompleter;
import com.hazelcast.cache.impl.client.CacheAddInvalidationListenerRequest;
import com.hazelcast.cache.impl.client.CacheBatchInvalidationMessage;
import com.hazelcast.cache.impl.client.CacheClearRequest;
import com.hazelcast.cache.impl.client.CacheGetAndRemoveRequest;
import com.hazelcast.cache.impl.client.CacheGetAndReplaceRequest;
import com.hazelcast.cache.impl.client.CacheInvalidationMessage;
import com.hazelcast.cache.impl.client.CachePutIfAbsentRequest;
import com.hazelcast.cache.impl.client.CachePutRequest;
import com.hazelcast.cache.impl.client.CacheRemoveInvalidationListenerRequest;
import com.hazelcast.cache.impl.client.CacheRemoveRequest;
import com.hazelcast.cache.impl.client.CacheReplaceRequest;
import com.hazelcast.cache.impl.client.CacheSingleInvalidationMessage;
import com.hazelcast.cache.impl.client.CompletionAwareCacheRequest;
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.HazelcastClientInstanceImpl;
import com.hazelcast.client.impl.client.BaseClientRemoveListenerRequest;
import com.hazelcast.client.impl.client.ClientRequest;
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.config.CacheConfig;
import com.hazelcast.config.InMemoryFormat;
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 com.hazelcast.util.executor.DelegatingFuture;
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.List;
import java.util.Map;
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 java.util.concurrent.atomic.AtomicInteger;
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);
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;
private final AtomicInteger completionIdCounter = new AtomicInteger();
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 close() {
if (nearCache != null) {
removeInvalidationListener();
nearCacheManager.clearNearCache(nearCache.getName());
}
if (statisticsEnabled) {
statistics.clear();
}
super.close();
}
@Override
public void destroy() {
if (nearCache != null) {
removeInvalidationListener();
nearCacheManager.destroyNearCache(nearCache.getName());
}
if (statisticsEnabled) {
statistics.clear();
}
super.destroy();
}
protected ClientInvocationFuture invoke(ClientRequest req, int partitionId, boolean completionOperation) {
Integer completionId = null;
if (completionOperation) {
completionId = registerCompletionLatch(1);
if (req instanceof CompletionAwareCacheRequest) {
((CompletionAwareCacheRequest) req).setCompletionId(completionId);
}
}
try {
HazelcastClientInstanceImpl client = (HazelcastClientInstanceImpl) clientContext.getHazelcastInstance();
final ClientInvocation clientInvocation = new ClientInvocation(client, req, partitionId);
final ClientInvocationFuture f = clientInvocation.invoke();
f.setResponseDeserialized(true);
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(ClientRequest req, Data keyData, boolean completionOperation) {
int partitionId = clientContext.getPartitionService().getPartitionId(keyData);
return invoke(req, partitionId, completionOperation);
}
protected T getSafely(Future future) {
try {
return future.get();
} catch (Throwable throwable) {
throw ExceptionUtil.rethrow(throwable);
}
}
protected ICompletableFuture removeAsyncInternal(K key, V oldValue, final boolean hasOldValue,
final boolean isGet, boolean withCompletionEvent,
boolean async) {
final long start = System.nanoTime();
ensureOpen();
if (hasOldValue) {
validateNotNull(key, oldValue);
CacheProxyUtil.validateConfiguredTypes(cacheConfig, key, oldValue);
} else {
validateNotNull(key);
CacheProxyUtil.validateConfiguredTypes(cacheConfig, key);
}
final Data keyData = toData(key);
final Data oldValueData = oldValue != null ? toData(oldValue) : null;
ClientRequest request;
InMemoryFormat inMemoryFormat = cacheConfig.getInMemoryFormat();
if (isGet) {
request = new CacheGetAndRemoveRequest(nameWithPrefix, keyData, inMemoryFormat);
} else {
request = new CacheRemoveRequest(nameWithPrefix, keyData, oldValueData, inMemoryFormat);
}
ClientInvocationFuture future;
try {
future = invoke(request, keyData, withCompletionEvent);
invalidateNearCache(keyData);
} catch (Exception e) {
throw ExceptionUtil.rethrow(e);
}
DelegatingFuture delegatingFuture = new DelegatingFuture(future, clientContext.getSerializationService());
if (async && statisticsEnabled) {
delegatingFuture.andThen(new ExecutionCallback