use of javax.cache.CacheManager in project hazelcast by hazelcast.
the class ClientCacheNearCacheConfigTest method native_backed_nearCache_throws_illegalArgumentException_whenNoNativeConfigAvailable.
@Test(expected = IllegalArgumentException.class)
public void native_backed_nearCache_throws_illegalArgumentException_whenNoNativeConfigAvailable() throws Exception {
String xml = "<hazelcast-client xmlns=\"http://www.hazelcast.com/schema/client-config\">" + "<near-cache name=\"test\">" + "<in-memory-format>NATIVE</in-memory-format>" + "</near-cache></hazelcast-client>";
ClientConfig clientConfig = buildConfig(xml);
HazelcastClientProxy client = (HazelcastClientProxy) factory.newHazelcastClient(clientConfig);
CachingProvider clientCachingProvider = HazelcastClientCachingProvider.createCachingProvider(client);
CacheManager cacheManager = clientCachingProvider.getCacheManager();
cacheManager.createCache("test", newCacheConfig());
}
use of javax.cache.CacheManager in project hazelcast by hazelcast.
the class AbstractClientCachePartitionIteratorTest method getCacheProxy.
private <K, V> ClientCacheProxy<K, V> getCacheProxy() {
String cacheName = randomString();
CacheManager cacheManager = cachingProvider.getCacheManager();
CacheConfig<K, V> config = new CacheConfig<K, V>();
config.getEvictionConfig().setMaximumSizePolicy(EvictionConfig.MaxSizePolicy.ENTRY_COUNT).setSize(10000000);
return (ClientCacheProxy<K, V>) cacheManager.createCache(cacheName, config);
}
use of javax.cache.CacheManager in project hazelcast by hazelcast.
the class ClientCacheConfigTest method cacheManagerByLocationFileTest.
@Test
public void cacheManagerByLocationFileTest() throws URISyntaxException {
assertEquals(0, HazelcastClient.getAllHazelcastClients().size());
URI uri = new URI("MY-SCOPE");
String urlStr = configUrl1.toString();
assertEquals("file", urlStr.substring(0, 4));
Properties properties = new Properties();
properties.setProperty(HazelcastCachingProvider.HAZELCAST_CONFIG_LOCATION, urlStr);
CacheManager cacheManager = Caching.getCachingProvider().getCacheManager(uri, null, properties);
assertNotNull(cacheManager);
URI uri2 = new URI("MY-SCOPE-OTHER");
String urlStr2 = configUrl2.toString();
assertEquals("file", urlStr2.substring(0, 4));
Properties properties2 = new Properties();
properties2.setProperty(HazelcastCachingProvider.HAZELCAST_CONFIG_LOCATION, urlStr2);
CacheManager cacheManager2 = Caching.getCachingProvider().getCacheManager(uri2, null, properties2);
assertNotNull(cacheManager2);
assertEquals(2, HazelcastClient.getAllHazelcastClients().size());
Caching.getCachingProvider().close();
}
use of javax.cache.CacheManager in project hazelcast by hazelcast.
the class ClientCacheConfigTest method cacheManagerByInstanceNameTest.
@Test
public void cacheManagerByInstanceNameTest() throws URISyntaxException {
assertEquals(0, HazelcastClient.getAllHazelcastClients().size());
String instanceName = "ClientInstanceTest";
ClientConfig clientConfig = new ClientConfig();
clientConfig.getGroupConfig().setName("cluster1");
clientConfig.getGroupConfig().setPassword("cluster1pass");
clientConfig.setInstanceName(instanceName);
HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
assertEquals(instanceName, client.getName());
URI uri1 = new URI("MY-SCOPE");
Properties properties = new Properties();
properties.setProperty(HazelcastCachingProvider.HAZELCAST_INSTANCE_NAME, instanceName);
CacheManager cacheManager = Caching.getCachingProvider().getCacheManager(uri1, null, properties);
assertNotNull(cacheManager);
assertEquals(1, HazelcastClient.getAllHazelcastClients().size());
client.shutdown();
Caching.getCachingProvider().close();
}
use of javax.cache.CacheManager in project hazelcast by hazelcast.
the class InvalidationMemberAddRemoveTest method ensure_nearCachedClient_and_member_data_sync_eventually.
@Test
public void ensure_nearCachedClient_and_member_data_sync_eventually() throws Exception {
final AtomicBoolean stopTest = new AtomicBoolean();
final Config config = createConfig();
hazelcastFactory.newHazelcastInstance(config);
CachingProvider provider = HazelcastServerCachingProvider.createCachingProvider(serverInstance);
final CacheManager serverCacheManager = provider.getCacheManager();
// populated from member.
final Cache<Integer, Integer> memberCache = serverCacheManager.createCache(DEFAULT_CACHE_NAME, createCacheConfig(BINARY));
for (int i = 0; i < KEY_COUNT; i++) {
memberCache.put(i, i);
}
ClientConfig clientConfig = createClientConfig();
clientConfig.addNearCacheConfig(createNearCacheConfig(BINARY));
HazelcastClientProxy client = (HazelcastClientProxy) hazelcastFactory.newHazelcastClient(clientConfig);
CachingProvider clientCachingProvider = HazelcastClientCachingProvider.createCachingProvider(client);
final Cache<Integer, Integer> clientCache = clientCachingProvider.getCacheManager().createCache(DEFAULT_CACHE_NAME, createCacheConfig(BINARY));
ArrayList<Thread> threads = new ArrayList<Thread>();
// continuously adds and removes member
Thread shadowMember = new Thread(new Runnable() {
@Override
public void run() {
while (!stopTest.get()) {
HazelcastInstance member = hazelcastFactory.newHazelcastInstance(config);
sleepSeconds(5);
member.getLifecycleService().terminate();
}
}
});
threads.add(shadowMember);
for (int i = 0; i < NEAR_CACHE_POPULATOR_THREAD_COUNT; i++) {
// populates client near-cache
Thread populateClientNearCache = new Thread(new Runnable() {
public void run() {
while (!stopTest.get()) {
for (int i = 0; i < KEY_COUNT; i++) {
clientCache.get(i);
}
}
}
});
threads.add(populateClientNearCache);
}
// updates data from member.
Thread putFromMember = new Thread(new Runnable() {
public void run() {
while (!stopTest.get()) {
int key = getInt(KEY_COUNT);
int value = getInt(Integer.MAX_VALUE);
memberCache.put(key, value);
sleepAtLeastMillis(2);
}
}
});
threads.add(putFromMember);
Thread clearFromMember = new Thread(new Runnable() {
public void run() {
while (!stopTest.get()) {
memberCache.clear();
sleepSeconds(3);
}
}
});
threads.add(clearFromMember);
// start threads
for (Thread thread : threads) {
thread.start();
}
// stress system some seconds
sleepSeconds(TEST_RUN_SECONDS);
//stop threads
stopTest.set(true);
for (Thread thread : threads) {
thread.join();
}
assertTrueEventually(new AssertTask() {
@Override
public void run() throws Exception {
for (int i = 0; i < KEY_COUNT; i++) {
Integer valueSeenFromMember = memberCache.get(i);
Integer valueSeenFromClient = clientCache.get(i);
String msg = createFailureMessage(i);
assertEquals(msg, valueSeenFromMember, valueSeenFromClient);
}
}
private String createFailureMessage(int i) {
InternalPartitionService partitionService = getPartitionService(serverInstance);
Data keyData = getSerializationService(serverInstance).toData(i);
int partitionId = partitionService.getPartitionId(keyData);
AbstractNearCacheRecordStore nearCacheRecordStore = getAbstractNearCacheRecordStore();
NearCacheRecord record = nearCacheRecordStore.getRecord(keyData);
long recordSequence = record == null ? NO_SEQUENCE : record.getInvalidationSequence();
MetaDataGenerator metaDataGenerator = getMetaDataGenerator();
long memberSequence = metaDataGenerator.currentSequence("/hz/" + DEFAULT_CACHE_NAME, partitionId);
MetaDataContainer metaDataContainer = nearCacheRecordStore.getStaleReadDetector().getMetaDataContainer(keyData);
return String.format("partition=%d, onRecordSequence=%d, latestSequence=%d, staleSequence=%d, memberSequence=%d", partitionService.getPartitionId(keyData), recordSequence, metaDataContainer.getSequence(), metaDataContainer.getStaleSequence(), memberSequence);
}
private MetaDataGenerator getMetaDataGenerator() {
CacheEventHandler cacheEventHandler = ((CacheService) ((CacheProxy) memberCache).getService()).getCacheEventHandler();
return cacheEventHandler.getMetaDataGenerator();
}
private AbstractNearCacheRecordStore getAbstractNearCacheRecordStore() {
DefaultNearCache defaultNearCache = (DefaultNearCache) ((ClientCacheProxy) clientCache).getNearCache().unwrap(DefaultNearCache.class);
return (AbstractNearCacheRecordStore) defaultNearCache.getNearCacheRecordStore();
}
});
}
Aggregations