use of com.tangosol.net.NamedCache in project coherence-spring by coherence-community.
the class MapListenerRegistrationBean method registerMapListeners.
/**
* Listen for {@link com.tangosol.net.events.partition.cache.CacheLifecycleEvent.Type#CREATED Created}
* {@link com.tangosol.net.events.partition.cache.CacheLifecycleEvent CacheLifecycleEvents}
* and register relevant map listeners when caches are created.
* @param event the {@link com.tangosol.net.events.partition.cache.CacheLifecycleEvent}
*/
@CoherenceEventListener
@SuppressWarnings({ "rawtypes", "unchecked" })
void registerMapListeners(@Created CacheLifecycleEvent event) {
String cacheName = event.getCacheName();
String eventScope = event.getScopeName();
String eventSession = event.getSessionName();
String eventService = event.getServiceName();
Set<AnnotatedMapListener<?, ?>> setListeners = getMapListeners(removeScope(eventService), cacheName);
Session session = Coherence.findSession(eventSession).orElseThrow(() -> new IllegalStateException("Cannot find a Session with name " + eventSession));
NamedCache cache = session.getCache(cacheName);
for (AnnotatedMapListener<?, ?> listener : setListeners) {
if (listener.hasFilterAnnotation()) {
// ensure that the listener's filter has been resolved as this
// was not possible as discovery time.
listener.setFilter(this.filterService.resolve(listener.getFilterAnnotations()));
}
if (listener.hasTransformerAnnotation()) {
// ensure that the listener's transformer has been resolved as this
// was not possible at discovery time.
listener.resolveTransformer(this.mapEventTransformerService);
}
String sScope = listener.getScopeName();
boolean fScopeOK = sScope == null || sScope.equals(eventScope);
String sSession = listener.getSession();
boolean fSessionOK = sSession == null || sSession.equals(eventSession);
if (fScopeOK && fSessionOK) {
Filter filter = listener.getFilter();
if (filter != null && !(filter instanceof MapEventFilter)) {
filter = new MapEventFilter(MapEventFilter.E_ALL, filter);
}
MapEventTransformer transformer = listener.getTransformer();
if (transformer != null) {
filter = new MapEventTransformerFilter(filter, transformer);
}
try {
boolean fLite = listener.isLiteEvent();
if (listener.isSynchronous()) {
cache.addMapListener(listener.synchronous(), filter, fLite);
} else {
cache.addMapListener(listener, filter, fLite);
}
} catch (Exception ex) {
throw Exceptions.ensureRuntimeException(ex);
}
}
}
}
use of com.tangosol.net.NamedCache in project coherence-spring by coherence-community.
the class SpringApplicationTests method testCacheStore.
/**
* Test the use of Spring to inject a CacheStore.
*/
@Test
public void testCacheStore() {
String[] asCacheNames = new String[] { "CacheStore", "CacheStorePull" };
for (String sCacheName : asCacheNames) {
NamedCache cache = this.session.getCache(sCacheName);
// the CacheStore provided by Spring is an instance of MapCacheStore
// which has an internal map that contains the entry <"key", "value">
assertThat("value").isEqualTo(cache.get("key"));
// this asserts that the {cache-name} macro succeeded in injecting
// the cache name to the cache store (see StubNamedCacheStore)
assertThat(sCacheName).isEqualTo(cache.get(StubNamedCacheStore.CACHE_NAME_KEY));
}
BeanFactory beanFactory = this.session.getResourceRegistry().getResource(BeanFactory.class);
StubNamedCacheStore cs = beanFactory.getBean("mapCacheStorePull", StubNamedCacheStore.class);
assertThat(cs.getSpelValue()).isEqualTo("Prosper");
}
use of com.tangosol.net.NamedCache in project coherence-spring by coherence-community.
the class SpringApplicationTests method testManualRegistration.
/**
* Test the registration of a bean factory and injection of a backing map.
*/
@Test
public void testManualRegistration() {
// this local cache will be used as a backing map
LocalCache localCache = new LocalCache(100, 0, new AbstractCacheLoader() {
@Override
public Object load(Object oKey) {
return ExternalizableHelper.toBinary("mock");
}
});
// instead of creating a Spring application context, create
// a simple mock BeanFactory that returns the local cache
// created above
BeanFactory factory = mock(BeanFactory.class);
when(factory.getBean("localBackingMap")).thenReturn(localCache);
// register the mock BeanFactory with the cache factory so that
// it is used as the backing map (see the cache config file)
this.session.getResourceRegistry().registerResource(BeanFactory.class, "mock", factory);
NamedCache namedCache = this.session.getCache("CacheCustomBackingMap");
// cache loader always returns the same value
assertThat("mock").isEqualTo(namedCache.get("key"));
// assert backing map properties
Map mapBacking = namedCache.getCacheService().getBackingMapManager().getContext().getBackingMapContext("CacheCustomBackingMap").getBackingMap();
assertThat(LocalCache.class).isEqualTo(mapBacking.getClass());
assertThat(100).isEqualTo(((LocalCache) mapBacking).getHighUnits());
assertThat(localCache).isEqualTo(mapBacking);
}
use of com.tangosol.net.NamedCache in project oracle-bedrock by coherence-community.
the class AbstractCoherenceClusterBuilderTest method shouldFailOverNamedCache.
/**
* Ensure that a {@link NamedCache} produced by a {@link CoherenceCluster} {@link CoherenceClusterMember}
* is failed over to another {@link CoherenceClusterMember} when the original {@link CoherenceClusterMember}
* is closed.
*/
@Test
public void shouldFailOverNamedCache() {
Capture<Integer> wkaPort = new Capture<>(LocalPlatform.get().getAvailablePorts());
final int CLUSTER_SIZE = 3;
String localHost = System.getProperty("tangosol.coherence.localhost", "127.0.0.1");
AvailablePortIterator availablePorts = LocalPlatform.get().getAvailablePorts();
ClusterPort clusterPort = ClusterPort.of(new Capture<>(availablePorts));
CoherenceClusterBuilder builder = new CoherenceClusterBuilder();
builder.include(CLUSTER_SIZE, CoherenceClusterMember.class, WellKnownAddress.of(localHost, wkaPort), clusterPort, LocalHost.of(localHost, wkaPort), ClusterName.of("FailOver"), DisplayName.of("DCS"));
try (CoherenceCluster cluster = builder.build(getPlatform(), Console.system())) {
assertThat(invoking(cluster).getClusterSize(), is(CLUSTER_SIZE));
// acquire a NamedCache from a specific cluster member
CoherenceClusterMember member = cluster.get("DCS-1");
NamedCache cache = member.getCache("dist-example");
// use the cache to put some data
cache.put("message", "hello");
assertThat(cluster.get("DCS-2").getCache("dist-example").get("message"), is("hello"));
// close the cluster member
member.close();
// ensure that it's not in the cluster
assertThat(invoking(cluster).getClusterSize(), is(CLUSTER_SIZE - 1));
// attempt to use the cache
assertThat(invoking(cache).get("message"), is("hello"));
}
}
use of com.tangosol.net.NamedCache in project oracle-bedrock by coherence-community.
the class CoherenceClusterResourceTest method shouldCreateStorageDisabledMemberSession.
/**
* Ensure that a {@link StorageDisabledMember} session can be created against the {@link CoherenceClusterResource}.
*/
@Test
public void shouldCreateStorageDisabledMemberSession() {
ConfigurableCacheFactory session = coherenceResource.createSession(SessionBuilders.storageDisabledMember());
NamedCache cache = session.ensureCache("dist-example", null);
Eventually.assertThat(coherenceResource.getCluster().getClusterSize(), is(4));
cache.put("message", "hello world");
Eventually.assertThat(invoking(coherenceResource.getCluster().getCache("dist-example")).get("message"), is((Object) "hello world"));
}
Aggregations