use of javax.cache.CacheManager in project hazelcast by hazelcast.
the class CacheResourceTest method testCloseableCacheWriter.
@Test
public void testCloseableCacheWriter() throws InterruptedException {
CachingProvider provider = HazelcastServerCachingProvider.createCachingProvider(factory.newHazelcastInstance());
CacheManager cacheManager = provider.getCacheManager();
CloseableCacheWriter writer = new CloseableCacheWriter();
Factory<CloseableCacheWriter> writerFactory = FactoryBuilder.factoryOf(writer);
CompleteConfiguration<Object, Object> configuration = new CacheConfig().setCacheWriterFactory(writerFactory).setWriteThrough(true);
Cache<Object, Object> cache = cacheManager.createCache("test", configuration);
// trigger partition assignment
cache.get("key");
factory.newHazelcastInstance();
for (int i = 0; i < 1000; i++) {
cache.put(i, i);
LockSupport.parkNanos(1000);
}
assertFalse("CacheWriter should not be closed!", writer.closed);
}
use of javax.cache.CacheManager in project hazelcast by hazelcast.
the class CacheSerializationTest method test_CacheReplicationOperation_serialization.
@Test
public void test_CacheReplicationOperation_serialization() throws Exception {
TestHazelcastInstanceFactory factory = new TestHazelcastInstanceFactory(1);
HazelcastInstance hazelcastInstance = factory.newHazelcastInstance();
try {
CachingProvider provider = HazelcastServerCachingProvider.createCachingProvider(hazelcastInstance);
CacheManager manager = provider.getCacheManager();
CompleteConfiguration configuration = new MutableConfiguration();
Cache cache1 = manager.createCache("cache1", configuration);
Cache cache2 = manager.createCache("cache2", configuration);
Cache cache3 = manager.createCache("cache3", configuration);
for (int i = 0; i < 1000; i++) {
cache1.put("key" + i, i);
cache2.put("key" + i, i);
cache3.put("key" + i, i);
}
HazelcastInstanceProxy proxy = (HazelcastInstanceProxy) hazelcastInstance;
Field original = HazelcastInstanceProxy.class.getDeclaredField("original");
original.setAccessible(true);
HazelcastInstanceImpl impl = (HazelcastInstanceImpl) original.get(proxy);
NodeEngineImpl nodeEngine = impl.node.nodeEngine;
CacheService cacheService = nodeEngine.getService(CacheService.SERVICE_NAME);
int partitionCount = nodeEngine.getPartitionService().getPartitionCount();
for (int partitionId = 0; partitionId < partitionCount; partitionId++) {
CachePartitionSegment segment = cacheService.getSegment(partitionId);
CacheReplicationOperation operation = new CacheReplicationOperation(segment, 1);
Data serialized = service.toData(operation);
try {
service.toObject(serialized);
} catch (Exception e) {
throw new Exception("Partition: " + partitionId, e);
}
}
} finally {
factory.shutdownAll();
}
}
use of javax.cache.CacheManager in project hazelcast by hazelcast.
the class CacheHazelcastInstanceAwareTest method test_inject_hazelcastInstance_to_expiryPolicy_if_itIs_hazelcastInstanceAware.
@Test
public void test_inject_hazelcastInstance_to_expiryPolicy_if_itIs_hazelcastInstanceAware() {
long id1 = generateUniqueHazelcastInjectionId();
long id2 = generateUniqueHazelcastInjectionId();
CacheConfig<Integer, Integer> cacheConfig = createCacheConfig(CACHE_NAME);
cacheConfig.setExpiryPolicyFactory(new HazelcastInstanceAwareExpiryPolicyFactory(id1, id2));
HazelcastInstance hazelcastInstance = createInstance();
CacheManager cacheManager = createCachingProvider(hazelcastInstance).getCacheManager();
Cache<Integer, Integer> cache = cacheManager.createCache(CACHE_NAME, cacheConfig);
cache.put(1, 1);
assertEquals("Hazelcast instance has not been injected into expiry policy factory!", Boolean.TRUE, HAZELCAST_INSTANCE_INJECTION_RESULT_MAP.get(id1));
assertEquals("Hazelcast instance has not been injected into expiry policy!", Boolean.TRUE, HAZELCAST_INSTANCE_INJECTION_RESULT_MAP.get(id2));
}
use of javax.cache.CacheManager in project hazelcast by hazelcast.
the class CacheHazelcastInstanceAwareTest method test_inject_hazelcastInstance_to_partitionLostListener_if_itIs_hazelcastInstanceAware.
@Test
public void test_inject_hazelcastInstance_to_partitionLostListener_if_itIs_hazelcastInstanceAware() {
long id = generateUniqueHazelcastInjectionId();
CacheConfig<Integer, Integer> cacheConfig = createCacheConfig(CACHE_NAME);
HazelcastInstance hazelcastInstance = createInstance();
CacheManager cacheManager = createCachingProvider(hazelcastInstance).getCacheManager();
ICache<Integer, Integer> cache = (ICache<Integer, Integer>) cacheManager.createCache(CACHE_NAME, cacheConfig);
cache.addPartitionLostListener(new HazelcastInstanceAwareCachePartitionLostListener(id));
assertEquals("Hazelcast instance has not been injected into partition lost listener!", Boolean.TRUE, HAZELCAST_INSTANCE_INJECTION_RESULT_MAP.get(id));
}
use of javax.cache.CacheManager in project hazelcast by hazelcast.
the class CachePartitionLostListenerStressTest method testCachePartitionLostListener.
@Test
public void testCachePartitionLostListener() {
List<HazelcastInstance> instances = getCreatedInstancesShuffledAfterWarmedUp();
List<HazelcastInstance> survivingInstances = new ArrayList<HazelcastInstance>(instances);
List<HazelcastInstance> terminatingInstances = survivingInstances.subList(0, numberOfNodesToCrash);
survivingInstances = survivingInstances.subList(numberOfNodesToCrash, instances.size());
HazelcastInstance instance = survivingInstances.get(0);
HazelcastServerCachingProvider cachingProvider = createCachingProvider(instance);
CacheManager cacheManager = cachingProvider.getCacheManager();
List<EventCollectingCachePartitionLostListener> listeners = registerListeners(cacheManager);
if (withData) {
for (int i = 0; i < getNodeCount(); i++) {
Cache<Integer, Integer> cache = cacheManager.getCache(getIthCacheName(i));
for (int j = 0; j < getCacheEntryCount(); j++) {
cache.put(j, j);
}
}
}
final String log = "Surviving: " + survivingInstances + " Terminating: " + terminatingInstances;
Map<Integer, Integer> survivingPartitions = getMinReplicaIndicesByPartitionId(survivingInstances);
stopInstances(terminatingInstances, nodeLeaveType);
waitAllForSafeState(survivingInstances);
if (shouldExpectPartitionLostEvents) {
for (int i = 0; i < getNodeCount(); i++) {
assertListenerInvocationsEventually(log, i, numberOfNodesToCrash, listeners.get(i), survivingPartitions);
}
} else {
for (final EventCollectingCachePartitionLostListener listener : listeners) {
assertTrueAllTheTime(new AssertTask() {
@Override
public void run() throws Exception {
assertTrue(listener.getEvents().isEmpty());
}
}, 1);
}
}
for (int i = 0; i < getNodeCount(); i++) {
cacheManager.destroyCache(getIthCacheName(i));
}
cacheManager.close();
cachingProvider.close();
}
Aggregations