From b1865dab3adf8796330b743ed6ddcd7bc7295b92 Mon Sep 17 00:00:00 2001 From: Venil Noronha Date: Mon, 18 Sep 2017 01:24:34 -0700 Subject: [PATCH] INT-4313: GemfireMetadataSt: Add Listener support JIRA: https://jira.springsource.org/browse/INT-4313 * Add `GemfireCacheListener` in `GemfireMetadataStore` to delegate cache events to `MetadataStoreListener`s. * Add GemfireMetadataStore cache listener tests. * Update ascii doc. * Addresses review comments. * Some polishing to Java 8 style * Some code style polishing * Minor Doc improvement --- .../metadata/GemfireMetadataStore.java | 58 ++++++- ...emfireMetadataStoreCacheListenerTests.java | 158 ++++++++++++++++++ src/reference/asciidoc/gemfire.adoc | 19 ++- src/reference/asciidoc/whats-new.adoc | 10 +- 4 files changed, 236 insertions(+), 9 deletions(-) create mode 100644 spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/metadata/GemfireMetadataStoreCacheListenerTests.java diff --git a/spring-integration-gemfire/src/main/java/org/springframework/integration/gemfire/metadata/GemfireMetadataStore.java b/spring-integration-gemfire/src/main/java/org/springframework/integration/gemfire/metadata/GemfireMetadataStore.java index ef5951d1ce..bbceb9cd03 100644 --- a/spring-integration-gemfire/src/main/java/org/springframework/integration/gemfire/metadata/GemfireMetadataStore.java +++ b/spring-integration-gemfire/src/main/java/org/springframework/integration/gemfire/metadata/GemfireMetadataStore.java @@ -16,11 +16,19 @@ package org.springframework.integration.gemfire.metadata; +import java.util.List; +import java.util.Objects; +import java.util.concurrent.CopyOnWriteArrayList; + import org.apache.geode.cache.Cache; +import org.apache.geode.cache.EntryEvent; import org.apache.geode.cache.Region; import org.apache.geode.cache.Scope; +import org.apache.geode.cache.util.CacheListenerAdapter; import org.springframework.integration.metadata.ConcurrentMetadataStore; +import org.springframework.integration.metadata.ListenableMetadataStore; +import org.springframework.integration.metadata.MetadataStoreListener; import org.springframework.util.Assert; /** @@ -30,22 +38,30 @@ import org.springframework.util.Assert; * restarts. * * @author Artem Bilan + * @author Venil Noronha + * * @since 4.0 */ -public class GemfireMetadataStore implements ConcurrentMetadataStore { +public class GemfireMetadataStore implements ListenableMetadataStore { public static final String KEY = "MetaData"; + private final GemfireCacheListener cacheListener = new GemfireCacheListener(); + private final Region region; public GemfireMetadataStore(Cache cache) { - Assert.notNull(cache, "'cache' must not be null"); - this.region = cache.createRegionFactory().setScope(Scope.LOCAL).create(KEY); + this(Objects.requireNonNull(cache, "'cache' must not be null") + .createRegionFactory() + .setScope(Scope.LOCAL) + .create(KEY)); } public GemfireMetadataStore(Region region) { Assert.notNull(region, "'region' must not be null"); this.region = region; + this.region.getAttributesMutator() + .addCacheListener(this.cacheListener); } @Override @@ -82,4 +98,40 @@ public class GemfireMetadataStore implements ConcurrentMetadataStore { return this.region.remove(key); } + @Override + public void addListener(MetadataStoreListener listener) { + Assert.notNull(listener, "'listener' must not be null"); + this.cacheListener.listeners.add(listener); + } + + @Override + public void removeListener(MetadataStoreListener listener) { + this.cacheListener.listeners.remove(listener); + } + + private static class GemfireCacheListener extends CacheListenerAdapter { + + private final List listeners = new CopyOnWriteArrayList<>(); + + GemfireCacheListener() { + super(); + } + + @Override + public void afterCreate(EntryEvent event) { + this.listeners.forEach(listener -> listener.onAdd(event.getKey(), event.getNewValue())); + } + + @Override + public void afterUpdate(EntryEvent event) { + this.listeners.forEach(listener -> listener.onUpdate(event.getKey(), event.getNewValue())); + } + + @Override + public void afterDestroy(EntryEvent event) { + this.listeners.forEach(listener -> listener.onRemove(event.getKey(), event.getOldValue())); + } + + } + } diff --git a/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/metadata/GemfireMetadataStoreCacheListenerTests.java b/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/metadata/GemfireMetadataStoreCacheListenerTests.java new file mode 100644 index 0000000000..1b992f88b5 --- /dev/null +++ b/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/metadata/GemfireMetadataStoreCacheListenerTests.java @@ -0,0 +1,158 @@ +/* + * Copyright 2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.gemfire.metadata; + +import static org.junit.Assert.assertEquals; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.geode.cache.Cache; +import org.apache.geode.cache.CacheFactory; +import org.apache.geode.cache.Region; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import org.springframework.integration.metadata.MetadataStoreListenerAdapter; +import org.springframework.util.Assert; + +/** + * @author Venil Noronha + * + * @since 5.0 + * + */ +public class GemfireMetadataStoreCacheListenerTests { + + private static Cache cache; + + private static GemfireMetadataStore metadataStore; + + private static Region region; + + @BeforeClass + public static void startUp() throws Exception { + cache = new CacheFactory().create(); + metadataStore = new GemfireMetadataStore(cache); + region = cache.getRegion(GemfireMetadataStore.KEY); + } + + @AfterClass + public static void cleanUp() { + if (region != null) { + region.close(); + } + if (cache != null) { + cache.close(); + Assert.isTrue(cache.isClosed(), "Cache did not close after close() call"); + } + } + + @Before + @After + public void setup() { + if (region != null) { + region.clear(); + } + } + + @Test + public void testAdd() throws InterruptedException { + String testKey = "key"; + String testValue = "value"; + + CountDownLatch latch = new CountDownLatch(1); + AtomicReference actualKey = new AtomicReference<>(); + AtomicReference actualValue = new AtomicReference<>(); + metadataStore.addListener(new MetadataStoreListenerAdapter() { + + @Override + public void onAdd(String key, String value) { + actualKey.set(key); + actualValue.set(value); + latch.countDown(); + } + + }); + + metadataStore.put(testKey, testValue); + latch.await(10, TimeUnit.SECONDS); + + assertEquals(testKey, actualKey.get()); + assertEquals(testValue, actualValue.get()); + } + + @Test + public void testRemove() throws InterruptedException { + String testKey = "key"; + String testValue = "value"; + + CountDownLatch latch = new CountDownLatch(1); + AtomicReference actualKey = new AtomicReference<>(); + AtomicReference actualValue = new AtomicReference<>(); + metadataStore.addListener(new MetadataStoreListenerAdapter() { + + @Override + public void onRemove(String key, String oldValue) { + actualKey.set(key); + actualValue.set(oldValue); + latch.countDown(); + } + + }); + + metadataStore.put(testKey, testValue); + metadataStore.remove(testKey); + latch.await(10, TimeUnit.SECONDS); + + assertEquals(testKey, actualKey.get()); + assertEquals(testValue, actualValue.get()); + } + + @Test + public void testUpdate() throws InterruptedException { + String testKey = "key"; + String testValue = "value"; + String testNewValue = "new-value"; + + CountDownLatch latch = new CountDownLatch(1); + AtomicReference actualKey = new AtomicReference<>(); + AtomicReference actualValue = new AtomicReference<>(); + metadataStore.addListener(new MetadataStoreListenerAdapter() { + + @Override + public void onUpdate(String key, String newValue) { + actualKey.set(key); + actualValue.set(newValue); + latch.countDown(); + } + + }); + + metadataStore.put(testKey, testValue); + metadataStore.put(testKey, testNewValue); + latch.await(10, TimeUnit.SECONDS); + + assertEquals(testKey, actualKey.get()); + assertEquals(testNewValue, actualValue.get()); + } + +} diff --git a/src/reference/asciidoc/gemfire.adoc b/src/reference/asciidoc/gemfire.adoc index 313a8eb84a..399138d05f 100644 --- a/src/reference/asciidoc/gemfire.adoc +++ b/src/reference/asciidoc/gemfire.adoc @@ -192,7 +192,7 @@ Another constructor requires `Cache` and the `Region` will be created with `GLOB [[gemfire-metadata-store]] === Gemfire Metadata Store -As of _Spring Integration 4.0_, a new Gemfire-based `MetadataStore` (<>) implementation is available. +As of _version 4.0_, a new Gemfire-based `MetadataStore` (<>) implementation is available. The `GemfireMetadataStore` can be used to maintain metadata state across application restarts. This new `MetadataStore` implementation can be used with adapters such as: @@ -202,11 +202,24 @@ This new `MetadataStore` implementation can be used with adapters such as: * <> * <> - - In order to instruct these adapters to use the new `GemfireMetadataStore`, simply declare a Spring bean using the bean name *metadataStore*. The _Twitter Inbound Channel Adapter_ and the _Feed Inbound Channel Adapter_ will both automatically pick up and use the declared `GemfireMetadataStore`. NOTE: The `GemfireMetadataStore` also implements `ConcurrentMetadataStore`, allowing it to be reliably shared across multiple application instances where only one instance will be allowed to store or modify a key's value. These methods give various levels of concurrency guarantees based on the scope and data policy of the region. They are implemented in the peer cache and client/server cache but are disallowed in peer Regions having NORMAL or EMPTY data policies. + +NOTE: Since _version 5.0_, the `GemfireMetadataStore` also implements `ListenableMetadataStore`, allowing users to listen to cache events by providing `MetadataStoreListener` instances to the store: + +[source,java] +---- +GemfireMetadataStore metadataStore = new GemfireMetadataStore(cache); +metadataStore.addListener(new MetadataStoreListenerAdapter() { + + @Override + public void onAdd(String key, String value) { + ... + } + +}); +---- diff --git a/src/reference/asciidoc/whats-new.adoc b/src/reference/asciidoc/whats-new.adoc index 56c9da63ff..a37df6187b 100644 --- a/src/reference/asciidoc/whats-new.adoc +++ b/src/reference/asciidoc/whats-new.adoc @@ -95,6 +95,10 @@ The `ObjectToMapTransformer` can now be supplied with a customised `JsonObjectMa See <> for more information. +The `@GlobalChannelInterceptor` annotation and `` now support negative patterns (via `!` prepending) for component names matching. + +See <> for more information. + ==== Gateway Changes The gateway now correctly sets the `errorChannel` header when the gateway method has a `void` return type and an error channel is provided. @@ -273,8 +277,8 @@ A `ByteArrayElasticRawDeserializer` has been added without `maxMessageSize` cont See <> for more information. -==== GlobalChannelInterceptor changes +==== Gemfire Changes -The `@GlobalChannelInterceptor` annotation and `` now support negative patterns (via `!` prepending) for component names matching. +The `GemfireMetadataStore` now implements `ListenableMetadataStore`, allowing users to listen to cache events by providing `MetadataStoreListener` instances to the store. -See <> for more information. +See <> for more information.