Search in sources :

Example 11 with NamedCache

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);
            }
        }
    }
}
Also used : MapEventFilter(com.tangosol.util.filter.MapEventFilter) MapEventTransformer(com.tangosol.util.MapEventTransformer) MapEventTransformerFilter(com.tangosol.util.filter.MapEventTransformerFilter) NamedCache(com.tangosol.net.NamedCache) Filter(com.tangosol.util.Filter) MapEventFilter(com.tangosol.util.filter.MapEventFilter) MapEventTransformerFilter(com.tangosol.util.filter.MapEventTransformerFilter) Session(com.tangosol.net.Session) CoherenceEventListener(com.oracle.coherence.spring.event.CoherenceEventListener)

Example 12 with NamedCache

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");
}
Also used : BeanFactory(org.springframework.beans.factory.BeanFactory) NamedCache(com.tangosol.net.NamedCache) Test(org.junit.jupiter.api.Test)

Example 13 with NamedCache

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);
}
Also used : LocalCache(com.tangosol.net.cache.LocalCache) AbstractCacheLoader(com.tangosol.net.cache.AbstractCacheLoader) BeanFactory(org.springframework.beans.factory.BeanFactory) Map(java.util.Map) NamedCache(com.tangosol.net.NamedCache) Test(org.junit.jupiter.api.Test)

Example 14 with NamedCache

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"));
    }
}
Also used : AvailablePortIterator(com.oracle.bedrock.runtime.network.AvailablePortIterator) ClusterPort(com.oracle.bedrock.runtime.coherence.options.ClusterPort) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Capture(com.oracle.bedrock.util.Capture) NamedCache(com.tangosol.net.NamedCache) AbstractTest(com.oracle.bedrock.testsupport.junit.AbstractTest) Test(org.junit.Test)

Example 15 with NamedCache

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"));
}
Also used : ConfigurableCacheFactory(com.tangosol.net.ConfigurableCacheFactory) NamedCache(com.tangosol.net.NamedCache) Test(org.junit.Test)

Aggregations

NamedCache (com.tangosol.net.NamedCache)39 Test (org.junit.jupiter.api.Test)19 Map (java.util.Map)11 Test (org.junit.Test)6 BeanFactory (org.springframework.beans.factory.BeanFactory)6 ConfigurableCacheFactory (com.tangosol.net.ConfigurableCacheFactory)5 CacheMap (com.tangosol.net.cache.CacheMap)4 AbstractTest (com.oracle.bedrock.testsupport.junit.AbstractTest)3 VisualVMModel (com.oracle.coherence.plugin.visualvm.VisualVMModel)3 RequestSender (com.oracle.coherence.plugin.visualvm.helper.RequestSender)3 CacheData (com.oracle.coherence.plugin.visualvm.tablemodel.model.CacheData)3 CacheDetailData (com.oracle.coherence.plugin.visualvm.tablemodel.model.CacheDetailData)3 ClusterData (com.oracle.coherence.plugin.visualvm.tablemodel.model.ClusterData)3 Data (com.oracle.coherence.plugin.visualvm.tablemodel.model.Data)3 FederationData (com.oracle.coherence.plugin.visualvm.tablemodel.model.FederationData)3 FederationDestinationDetailsData (com.oracle.coherence.plugin.visualvm.tablemodel.model.FederationDestinationDetailsData)3 FederationOriginDetailsData (com.oracle.coherence.plugin.visualvm.tablemodel.model.FederationOriginDetailsData)3 MemberData (com.oracle.coherence.plugin.visualvm.tablemodel.model.MemberData)3 PersistenceData (com.oracle.coherence.plugin.visualvm.tablemodel.model.PersistenceData)3 ProxyData (com.oracle.coherence.plugin.visualvm.tablemodel.model.ProxyData)3