All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hazelcast.client.cache.impl.HazelcastClientCacheManager Maven / Gradle / Ivy

There is a newer version: 3.6.8
Show newest version
/*
 * 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.HazelcastCachingProvider;
import com.hazelcast.cache.impl.AbstractHazelcastCacheManager;
import com.hazelcast.cache.impl.CacheProxyUtil;
import com.hazelcast.cache.impl.CacheService;
import com.hazelcast.cache.impl.ICacheInternal;
import com.hazelcast.cache.impl.client.CacheCreateConfigRequest;
import com.hazelcast.cache.impl.client.CacheGetConfigRequest;
import com.hazelcast.cache.impl.client.CacheManagementConfigRequest;
import com.hazelcast.cache.impl.nearcache.NearCacheManager;
import com.hazelcast.client.impl.HazelcastClientInstanceImpl;
import com.hazelcast.client.impl.HazelcastClientProxy;
import com.hazelcast.client.impl.client.ClientRequest;
import com.hazelcast.client.spi.ClientContext;
import com.hazelcast.client.spi.impl.ClientInvocation;
import com.hazelcast.config.CacheConfig;
import com.hazelcast.config.InMemoryFormat;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.Member;
import com.hazelcast.nio.Address;
import com.hazelcast.spi.impl.SerializableList;
import com.hazelcast.util.ExceptionUtil;
import com.hazelcast.util.FutureUtil;

import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import static com.hazelcast.util.Preconditions.checkNotNull;

/**
 * CacheManager implementation for client side
 * 

* Provides client side cacheManager functionality */ public final class HazelcastClientCacheManager extends AbstractHazelcastCacheManager { private final ClientContext clientContext; private final ConcurrentMap configs = new ConcurrentHashMap(); public HazelcastClientCacheManager(HazelcastClientCachingProvider cachingProvider, HazelcastInstance hazelcastInstance, URI uri, ClassLoader classLoader, Properties properties) { super(cachingProvider, hazelcastInstance, uri, classLoader, properties); final ClientCacheDistributedObject setupRef = hazelcastInstance.getDistributedObject(CacheService.SERVICE_NAME, "setupRef"); this.clientContext = setupRef.getClientContext(); } @Override public void enableManagement(String cacheName, boolean enabled) { enableStatisticManagementOnNodes(cacheName, false, enabled); } @Override public void enableStatistics(String cacheName, boolean enabled) { enableStatisticManagementOnNodes(cacheName, true, enabled); } private void enableStatisticManagementOnNodes(String cacheName, boolean statOrMan, boolean enabled) { checkIfManagerNotClosed(); checkNotNull(cacheName, "cacheName cannot be null"); Collection members = clientContext.getClusterService().getMemberList(); Collection futures = new ArrayList(); HazelcastClientInstanceImpl client = (HazelcastClientInstanceImpl) clientContext.getHazelcastInstance(); for (Member member : members) { try { Address address = member.getAddress(); CacheManagementConfigRequest request = new CacheManagementConfigRequest(getCacheNameWithPrefix(cacheName), statOrMan, enabled, address); ClientInvocation clientInvocation = new ClientInvocation(client, request, address); Future future = clientInvocation.invoke(); futures.add(future); } catch (Exception e) { ExceptionUtil.sneakyThrow(e); } } // Make sure all configs are created FutureUtil.waitWithDeadline(futures, CacheProxyUtil.AWAIT_COMPLETION_TIMEOUT_SECONDS, TimeUnit.SECONDS); } @Override protected CacheConfig getCacheConfigLocal(String cacheName) { return configs.get(cacheName); } @Override protected void addCacheConfigIfAbsent(CacheConfig cacheConfig) { configs.putIfAbsent(cacheConfig.getNameWithPrefix(), cacheConfig); } @Override protected void removeCacheConfigFromLocal(String cacheName) { configs.remove(cacheName); } @Override protected CacheConfig getCacheConfigFromPartition(String cacheName, String simpleCacheName) { ClientRequest request = new CacheGetConfigRequest(cacheName, simpleCacheName, InMemoryFormat.BINARY); HazelcastClientInstanceImpl client = (HazelcastClientInstanceImpl) clientContext.getHazelcastInstance(); try { int partitionId = clientContext.getPartitionService().getPartitionId(cacheName); ClientInvocation clientInvocation = new ClientInvocation(client, request, partitionId); Future future = clientInvocation.invoke(); return clientContext.getSerializationService().toObject(future.get()); } catch (Exception e) { throw ExceptionUtil.rethrow(e); } } @Override protected CacheConfig createConfigOnPartition(CacheConfig cacheConfig) { HazelcastClientInstanceImpl client = (HazelcastClientInstanceImpl) clientContext.getHazelcastInstance(); try { int partitionId = clientContext.getPartitionService().getPartitionId(cacheConfig.getNameWithPrefix()); CacheCreateConfigRequest request = new CacheCreateConfigRequest(cacheConfig, false, false, partitionId); ClientInvocation clientInvocation = new ClientInvocation(client, request, partitionId); Future future = clientInvocation.invoke(); return (CacheConfig) clientContext.getSerializationService().toObject(future.get()); } catch (Exception e) { throw ExceptionUtil.rethrow(e); } } @Override protected ICacheInternal createCacheProxy(CacheConfig cacheConfig) { return new ClientCacheProxy(cacheConfig, clientContext, this); } @Override protected CacheConfig findConfig(String cacheName, String simpleCacheName, boolean createAlsoOnOthers, boolean syncCreate) { CacheConfig config = configs.get(cacheName); if (config == null) { // If cache config not found, try to find it from partition config = getCacheConfigFromPartition(cacheName, simpleCacheName); if (config != null) { // Cache config possibly is not exist on other nodes, so create also on them if absent createConfig(cacheName, config, createAlsoOnOthers, syncCreate); } } return config; } @Override protected CacheConfig createConfig(String cacheName, CacheConfig config, boolean createAlsoOnOthers, boolean syncCreate) { CacheConfig currentCacheConfig = configs.get(cacheName); HazelcastClientInstanceImpl client = (HazelcastClientInstanceImpl) clientContext.getHazelcastInstance(); try { int partitionId = clientContext.getPartitionService().getPartitionId(config.getNameWithPrefix()); CacheCreateConfigRequest request = new CacheCreateConfigRequest(config, createAlsoOnOthers, false, partitionId); ClientInvocation clientInvocation = new ClientInvocation(client, request, partitionId); Future future = clientInvocation.invoke(); if (syncCreate) { return (CacheConfig) clientContext.getSerializationService().toObject(future.get()); } else { return currentCacheConfig; } } catch (Exception e) { throw ExceptionUtil.rethrow(e); } } @Override public T unwrap(Class clazz) { if (HazelcastClientCacheManager.class.isAssignableFrom(clazz)) { return (T) this; } throw new IllegalArgumentException(); } protected void postClose() { if (properties.getProperty(HazelcastCachingProvider.HAZELCAST_CONFIG_LOCATION) != null) { hazelcastInstance.shutdown(); } } /** * Gets the related {@link NearCacheManager} with the underlying client instance. * * @return the related {@link NearCacheManager} with the underlying client instance */ public NearCacheManager getNearCacheManager() { if (hazelcastInstance instanceof HazelcastClientInstanceImpl) { return ((HazelcastClientInstanceImpl) hazelcastInstance).getNearCacheManager(); } else if (hazelcastInstance instanceof HazelcastClientProxy) { HazelcastClientInstanceImpl clientInstance = ((HazelcastClientProxy) hazelcastInstance).client; if (clientInstance != null) { return clientInstance.getNearCacheManager(); } else { return null; } } else { return null; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy