use of com.tangosol.net.NamedCache in project micronaut-coherence by micronaut-projects.
the class CoherenceHttpSessionTest method shouldCreateSession.
@Test
void shouldCreateSession() throws Exception {
String cacheName = context.getProperty("micronaut.session.http.coherence.cache-name", String.class).orElse("http-sessions");
NamedCache cache = coherence.getSession().getCache(cacheName);
CoherenceSessionStore sessionStore = context.getBean(CoherenceSessionStore.class);
// create and save new session
CoherenceSessionStore.CoherenceHttpSession session = sessionStore.newSession();
session.put("username", "fred");
session.put("foo", new Foo("Fred", 10));
CoherenceSessionStore.CoherenceHttpSession saved = sessionStore.save(session).get();
assertThat(cache.size(), is(1));
assertNotNull(saved);
assertFalse(saved.isExpired());
assertNotNull(saved.getMaxInactiveInterval());
assertNotNull(saved.getCreationTime());
assertNotNull(saved.getId());
assertEquals("fred", saved.get("username").get());
assertThat(saved.get("foo").get(), instanceOf(Foo.class));
assertEquals("Fred", ((Foo) saved.get("foo").get()).getName());
assertEquals(10, ((Foo) saved.get("foo").get()).getAge());
// locate session
CoherenceSessionStore.CoherenceHttpSession retrieved = sessionStore.findSession(saved.getId()).get().get();
// is session valid
assertNotNull(retrieved);
assertFalse(retrieved.isExpired());
assertNotNull(retrieved.getMaxInactiveInterval());
assertNotNull(retrieved.getCreationTime());
assertNotNull(retrieved.getId());
assertThat(retrieved.get("foo", Foo.class).get(), instanceOf(Foo.class));
assertEquals("fred", retrieved.get("username", String.class).get());
assertEquals("Fred", retrieved.get("foo", Foo.class).get().getName());
assertEquals(10, retrieved.get("foo", Foo.class).get().getAge());
// modify session
retrieved.remove("username");
retrieved.put("more", "stuff");
Instant now = Instant.now();
retrieved.setLastAccessedTime(now);
retrieved.setMaxInactiveInterval(Duration.of(10, ChronoUnit.MINUTES));
sessionStore.save(retrieved).get();
retrieved = sessionStore.findSession(retrieved.getId()).get().get();
// is session valid
assertNotNull(retrieved);
assertFalse(retrieved.isExpired());
assertEquals(Duration.of(10, ChronoUnit.MINUTES), retrieved.getMaxInactiveInterval());
assertEquals(retrieved.getCreationTime().getLong(ChronoField.MILLI_OF_SECOND), saved.getCreationTime().getLong(ChronoField.MILLI_OF_SECOND));
assertTrue(retrieved.getLastAccessedTime().isAfter(now));
assertNotNull(retrieved.getId());
assertFalse(retrieved.contains("username"));
assertEquals("stuff", retrieved.get("more", String.class).get());
assertEquals("Fred", retrieved.get("foo", Foo.class).get().getName());
assertEquals(10, retrieved.get("foo", Foo.class).get().getAge());
// delete session
assertTrue(sessionStore.deleteSession(saved.getId()).get());
Optional<CoherenceSessionStore.CoherenceHttpSession> found = sessionStore.findSession(saved.getId()).get();
assertFalse(found.isPresent());
}
use of com.tangosol.net.NamedCache in project teiid by teiid.
the class CoherenceConnectionImpl method add.
public void add(Object key, Object value) throws ResourceException {
NamedCache sourceCache = getCache();
if (sourceCache.containsKey(key)) {
throw new ResourceException("Unable to add object for key: " + key + " to cache " + this.cacheName + ", because it already exist");
}
TransactionMap tmap = CacheFactory.getLocalTransaction(sourceCache);
tmap.setTransactionIsolation(TransactionMap.TRANSACTION_REPEATABLE_GET);
tmap.setConcurrency(TransactionMap.CONCUR_PESSIMISTIC);
tmap.begin();
try {
tmap.put(key, value);
tmap.prepare();
tmap.commit();
} catch (Exception e) {
throw new ResourceException(e);
}
sourceCache = getCache();
if (!sourceCache.containsKey(key)) {
throw new ResourceException("Problem adding object for key: " + key + " to the cache " + this.cacheName + ", object not found after add");
}
}
use of com.tangosol.net.NamedCache in project teiid by teiid.
the class CoherenceConnectionImpl method remove.
public void remove(Object key) throws ResourceException {
System.out.println("Remove: " + key);
NamedCache sourceCache = getCache();
if (!sourceCache.containsKey(key)) {
throw new ResourceException("Unable to remove object for key: " + key + " from cache " + this.cacheName + ", because it doesn't exist");
}
TransactionMap tmap = CacheFactory.getLocalTransaction(sourceCache);
tmap.setTransactionIsolation(TransactionMap.TRANSACTION_REPEATABLE_GET);
tmap.setConcurrency(TransactionMap.CONCUR_OPTIMISTIC);
tmap.begin();
try {
tmap.remove(key);
tmap.prepare();
tmap.commit();
} catch (Exception e) {
throw new ResourceException(e);
}
if (getCache().containsKey(key)) {
throw new ResourceException("Unable to remove object for key: " + key + " from the cache " + this.cacheName);
}
}
use of com.tangosol.net.NamedCache in project teiid by teiid.
the class CoherenceConnectionImpl method getCache.
private NamedCache getCache() {
NamedCache sourceCache = CacheFactory.getCache(this.cacheName, this.getClass().getClassLoader());
// $NON-NLS-1$
LogManager.logDetail(LogConstants.CTX_CONNECTOR, "Coherence NamedCache " + cacheName + " has been obtained.");
return sourceCache;
}
use of com.tangosol.net.NamedCache in project coherence-visualvm by oracle.
the class AbstractDataRetrieverTest method testCacheData.
/**
* Test the retrieval of CacheData via the VisualVMModel.
*/
private void testCacheData() throws Exception {
final int INSERT1_COUNT = 10000;
final int INSERT2_COUNT = 7500;
final int INSERT3_COUNT = 100;
final int DATA_SIZE = 250;
List<Map.Entry<Object, Data>> cacheData;
List<Map.Entry<Object, Data>> cacheDetailData;
List<Map.Entry<Object, Data>> cacheStorageData;
VisualVMModel model = getModel();
RequestSender requestSender = getRequestSender();
assertClusterReady();
waitForRefresh();
// refresh the statistics
model.refreshStatistics(requestSender);
cacheData = model.getData(VisualVMModel.DataType.CACHE);
setCurrentDataType(VisualVMModel.DataType.CACHE);
setClientProperties();
ExtensibleConfigurableCacheFactory eccf = getECCF();
// now add 2 caches on 2 different services - note we are connecting via Extend
NamedCache nc1 = eccf.ensureCache(DIST1_CACHE, null);
NamedCache nc2 = eccf.ensureCache(DIST2_CACHE, null);
NamedCache nc3 = eccf.ensureCache(REPL_CACHE, null);
populateRandomData(nc1, INSERT1_COUNT, DATA_SIZE);
populateRandomData(nc2, INSERT2_COUNT, DATA_SIZE);
populateRandomData(nc3, INSERT3_COUNT, DATA_SIZE);
assertTrue(nc1.size() == INSERT1_COUNT);
assertTrue(nc2.size() == INSERT2_COUNT);
assertTrue(nc3.size() == INSERT3_COUNT);
waitForRefresh();
model.refreshStatistics(requestSender);
cacheData = model.getData(VisualVMModel.DataType.CACHE);
Map.Entry<Object, Data> distCache1 = getData(cacheData, new Pair<>(DIST1_SERVICE, DIST1_CACHE));
Map.Entry<Object, Data> distCache2 = getData(cacheData, new Pair<>(DIST2_SERVICE, DIST2_CACHE));
Map.Entry<Object, Data> replCache1 = getData(cacheData, new Pair<>(REPLICATED_SERVICE, REPL_CACHE));
// validate the data returned where its deterministic
validateColumnNotNull(CacheData.SIZE, distCache1);
validateColumn(CacheData.CACHE_NAME, distCache1, getCacheName(DIST1_SERVICE, DIST1_CACHE));
validateColumnNotNull(CacheData.SIZE, distCache2);
validateColumn(CacheData.CACHE_NAME, distCache2, getCacheName(DIST2_SERVICE, DIST2_CACHE));
validateColumnNotNull(CacheData.SIZE, replCache1);
validateColumn(CacheData.CACHE_NAME, replCache1, getCacheName(REPLICATED_SERVICE, REPL_CACHE));
validateColumn(CacheData.UNIT_CALCULATOR, distCache1, "BINARY");
// select the first service, which should then generate both CacheDetailData and
// CacheStorageManagerData on next refresh
model.setSelectedCache(new Pair<>(DIST1_SERVICE, DIST1_CACHE));
// do 2 gets
nc1.get(0);
nc1.get(INSERT1_COUNT - 1);
waitForRefresh();
model.refreshStatistics(getRequestSender());
cacheDetailData = model.getData(VisualVMModel.DataType.CACHE_DETAIL);
cacheStorageData = model.getData(VisualVMModel.DataType.CACHE_STORAGE_MANAGER);
setCurrentDataType(VisualVMModel.DataType.CACHE_DETAIL);
validateData(VisualVMModel.DataType.CACHE_DETAIL, cacheDetailData, 2);
setCurrentDataType(VisualVMModel.DataType.CACHE_STORAGE_MANAGER);
validateData(VisualVMModel.DataType.CACHE_STORAGE_MANAGER, cacheStorageData, 2);
// validate the CacheDetail data
Map.Entry<Object, Data> entryDetail1 = cacheDetailData.get(0);
Map.Entry<Object, Data> entryDetail2 = cacheDetailData.get(1);
validateColumn(CacheDetailData.NODE_ID, entryDetail1, 1);
validateColumn(CacheDetailData.NODE_ID, entryDetail2, 2);
// call the dependent tests as we can't guarantee execution order in JUnit
testPersistenceData();
testProxyData();
if (isCommercial()) {
testElasticData();
}
}
Aggregations