This is an automated email from the ASF dual-hosted git repository.
upthewaterspout pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode-examples.git
The following commit(s) were added to refs/heads/develop by this push:
new a4b09ab GEODE-3887: Cleaning up cache listener example (#27)
a4b09ab is described below
commit a4b09abb5827d4dfd44b15eb1690e5f5796a3075
Author: Dan Smith <[email protected]>
AuthorDate: Mon Oct 23 10:44:42 2017 -0700
GEODE-3887: Cleaning up cache listener example (#27)
Moving the cache listener to the server, removing some unecessary cruft
from tests and example.
---
listener/README.md | 2 +-
listener/scripts/start.gfsh | 6 +-
.../apache/geode/examples/listener/Example.java | 31 ++----
.../examples/listener/ExampleCacheListener.java | 13 +--
.../listener/ExampleCacheListenerTest.java | 44 ---------
.../geode/examples/listener/ExampleTest.java | 7 +-
.../org/geode/examples/util/TestEntryEvent.java | 108 ---------------------
7 files changed, 18 insertions(+), 193 deletions(-)
diff --git a/listener/README.md b/listener/README.md
index 6010925..f11b3b6 100644
--- a/listener/README.md
+++ b/listener/README.md
@@ -19,7 +19,7 @@ limitations under the License.
This is a simple example that demonstrates the use of a cache listener to
capture modifications to a region.
-A cache listener is added to a region before the region is created. _After_ an
event (e.g., create a new entry, update an existing entry) occurs on that
region, the cache listener has the appropriate handler method invoked, e.g.,
`afterCreate()` for creating a new entry. This method invocation can _not_
affect the operation on the region.
+A cache listener is added to a region when the region is created. _After_ an
event (e.g., create a new entry, update an existing entry) occurs on that
region, the cache listener has the appropriate handler method invoked, e.g.,
`afterCreate()` for creating a new entry. This method invocation can _not_
affect the operation on the region.
In this example, a cache listener is installed that captures all of the
creation events for the region. A number of entries are created in the region.
The cache listener is notified of each creation and adds it to its queue of
events. In other applications, the event could either be persisted to some
other data store (i.e., write-behind) or a notification about the activity
could be sent via some other mechanism.
diff --git a/listener/scripts/start.gfsh b/listener/scripts/start.gfsh
index 0173ff9..48c43c0 100755
--- a/listener/scripts/start.gfsh
+++ b/listener/scripts/start.gfsh
@@ -16,9 +16,9 @@
#
start locator --name=locator --bind-address=127.0.0.1
-start server --name=server1 --locators=127.0.0.1[10334] --server-port=0
-start server --name=server2 --locators=127.0.0.1[10334] --server-port=0
+start server --name=server1 --locators=127.0.0.1[10334] --server-port=0
--classpath=../build/classes/main
+start server --name=server2 --locators=127.0.0.1[10334] --server-port=0
--classpath=../build/classes/main
list members
-create region --name=example-region --type=REPLICATE
+create region --name=example-region --type=REPLICATE
--cache-listener=org.apache.geode.examples.listener.ExampleCacheListener
describe region --name=example-region
diff --git
a/listener/src/main/java/org/apache/geode/examples/listener/Example.java
b/listener/src/main/java/org/apache/geode/examples/listener/Example.java
index fbbe000..d23b2da 100644
--- a/listener/src/main/java/org/apache/geode/examples/listener/Example.java
+++ b/listener/src/main/java/org/apache/geode/examples/listener/Example.java
@@ -17,10 +17,10 @@ package org.apache.geode.examples.listener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
+import java.util.Map;
import java.util.Queue;
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
-import java.util.function.Consumer;
import java.util.stream.IntStream;
import org.apache.geode.cache.CacheListener;
@@ -31,14 +31,9 @@ import org.apache.geode.cache.client.ClientCacheFactory;
import org.apache.geode.cache.client.ClientRegionFactory;
import org.apache.geode.cache.client.ClientRegionShortcut;
-public class Example implements Consumer<Region<Integer, String>> {
+public class Example {
public static final int ITERATIONS = 100;
- private final CacheListener cacheListener;
-
- private Queue<EntryEvent<Integer, String>> events =
- new ArrayBlockingQueue<EntryEvent<Integer, String>>(100, true);
-
public static void main(String[] args) {
// connect to the locator using default port 10334
ClientCache cache = new ClientCacheFactory().addPoolLocator("127.0.0.1",
10334)
@@ -48,26 +43,13 @@ public class Example implements Consumer<Region<Integer,
String>> {
// create a local region that matches the server region
ClientRegionFactory<Integer, String> clientRegionFactory =
- cache.<Integer,
String>createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY);
- clientRegionFactory.addCacheListener(example.getCacheListener());
+ cache.createClientRegionFactory(ClientRegionShortcut.PROXY);
Region<Integer, String> region =
clientRegionFactory.create("example-region");
- example.accept(region);
+ example.putEntries(region);
cache.close();
}
- Example() {
- cacheListener = new ExampleCacheListener(events);
- }
-
- CacheListener getCacheListener() {
- return cacheListener;
- }
-
- Queue<EntryEvent<Integer, String>> getEvents() {
- return events;
- }
-
private Collection<Integer> generateIntegers() {
IntStream stream = new Random().ints(0, ITERATIONS);
Iterator<Integer> iterator = stream.iterator();
@@ -81,8 +63,7 @@ public class Example implements Consumer<Region<Integer,
String>> {
return integers;
}
- @Override
- public void accept(Region<Integer, String> region) {
+ public void putEntries(Map<Integer, String> region) {
Collection<Integer> integers = generateIntegers();
Iterator<Integer> iterator = integers.iterator();
@@ -90,6 +71,6 @@ public class Example implements Consumer<Region<Integer,
String>> {
Integer integer = iterator.next();
region.put(integer, integer.toString());
}
- System.out.println("Created " + region.size() + " entries.");
+ System.out.println("Created " + integers.size() + " entries.");
}
}
diff --git
a/listener/src/main/java/org/apache/geode/examples/listener/ExampleCacheListener.java
b/listener/src/main/java/org/apache/geode/examples/listener/ExampleCacheListener.java
index b91506a..6da3b8d 100644
---
a/listener/src/main/java/org/apache/geode/examples/listener/ExampleCacheListener.java
+++
b/listener/src/main/java/org/apache/geode/examples/listener/ExampleCacheListener.java
@@ -14,6 +14,7 @@
*/
package org.apache.geode.examples.listener;
+import java.util.LinkedList;
import java.util.Queue;
import org.apache.geode.cache.CacheListener;
@@ -22,18 +23,10 @@ import org.apache.geode.cache.RegionEvent;
import org.apache.geode.cache.util.CacheListenerAdapter;
public class ExampleCacheListener extends CacheListenerAdapter<Integer,
String> {
- private final Queue<EntryEvent<Integer, String>> events;
-
- public ExampleCacheListener(Queue<EntryEvent<Integer, String>> events) {
- this.events = events;
- }
-
- public Queue<EntryEvent<Integer, String>> getEvents() {
- return events;
- }
+ public ExampleCacheListener() {}
@Override
public void afterCreate(EntryEvent<Integer, String> event) {
- events.add(event);
+ System.out.println("received create for key " + event.getKey());
}
}
diff --git
a/listener/src/test/java/org/apache/geode/examples/listener/ExampleCacheListenerTest.java
b/listener/src/test/java/org/apache/geode/examples/listener/ExampleCacheListenerTest.java
deleted file mode 100644
index a260614..0000000
---
a/listener/src/test/java/org/apache/geode/examples/listener/ExampleCacheListenerTest.java
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
contributor license
- * agreements. See the NOTICE file distributed with this work for additional
information regarding
- * copyright ownership. The ASF licenses this file to You 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.apache.geode.examples.listener;
-
-import static org.junit.Assert.assertEquals;
-
-import java.util.LinkedList;
-import java.util.Queue;
-
-import org.geode.examples.util.TestEntryEvent;
-import org.junit.Test;
-
-import org.apache.geode.cache.EntryEvent;
-import org.apache.geode.cache.Operation;
-
-public class ExampleCacheListenerTest {
- @Test
- public void testAfterCreate() {
- Queue<EntryEvent<Integer, String>> events = new LinkedList<>();
- events.add(new TestEntryEvent<>(null, Operation.CREATE, 1, "one", "uno"));
- events.add(new TestEntryEvent<>(null, Operation.CREATE, 2, "two", "dos"));
- events.add(new TestEntryEvent<>(null, Operation.CREATE, 3, "three",
"tres"));
-
- ExampleCacheListener cacheListener =
- new ExampleCacheListener(new LinkedList<EntryEvent<Integer,
String>>());
- for (EntryEvent<Integer, String> event : events) {
- cacheListener.afterCreate(event);
- }
-
- assertEquals(events, cacheListener.getEvents());
- }
-}
diff --git
a/listener/src/test/java/org/apache/geode/examples/listener/ExampleTest.java
b/listener/src/test/java/org/apache/geode/examples/listener/ExampleTest.java
index e17aacf..fccda67 100644
--- a/listener/src/test/java/org/apache/geode/examples/listener/ExampleTest.java
+++ b/listener/src/test/java/org/apache/geode/examples/listener/ExampleTest.java
@@ -16,6 +16,9 @@ package org.apache.geode.examples.listener;
import static org.junit.Assert.assertEquals;
+import java.util.HashMap;
+import java.util.Map;
+
import org.apache.geode.cache.Region;
import org.geode.examples.util.Mocks;
import org.junit.Test;
@@ -24,8 +27,8 @@ public class ExampleTest {
@Test
public void testExample() throws Exception {
Example example = new Example();
- Region<Integer, String> region = Mocks.region("example-region");
- example.accept(region);
+ Map<Integer, String> region = new HashMap<>();
+ example.putEntries(region);
assertEquals(Example.ITERATIONS, region.size());
}
diff --git a/src/test/java/org/geode/examples/util/TestEntryEvent.java
b/src/test/java/org/geode/examples/util/TestEntryEvent.java
deleted file mode 100644
index 7fe0d58..0000000
--- a/src/test/java/org/geode/examples/util/TestEntryEvent.java
+++ /dev/null
@@ -1,108 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
contributor license
- * agreements. See the NOTICE file distributed with this work for additional
information regarding
- * copyright ownership. The ASF licenses this file to You 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.geode.examples.util;
-
-import org.apache.geode.cache.EntryEvent;
-import org.apache.geode.cache.Operation;
-import org.apache.geode.cache.Region;
-import org.apache.geode.cache.SerializedCacheValue;
-import org.apache.geode.cache.TransactionId;
-import org.apache.geode.distributed.DistributedMember;
-
-public class TestEntryEvent<K, V> implements EntryEvent<K, V> {
- final Region region;
- final Operation operation;
- final K key;
- final V oldValue;
- final V newValue;
-
- public TestEntryEvent(Region region, Operation operation, K key, V oldValue,
V newValue) {
- this.region = region;
- this.operation = operation;
- this.key = key;
- this.oldValue = oldValue;
- this.newValue = newValue;
- }
-
- @Override
- public K getKey() {
- return key;
- }
-
- @Override
- public V getOldValue() {
- return oldValue;
- }
-
- @Override
- public SerializedCacheValue getSerializedOldValue() {
- return null;
- }
-
- @Override
- public V getNewValue() {
- return newValue;
- }
-
- @Override
- public SerializedCacheValue getSerializedNewValue() {
- return null;
- }
-
- @Override
- public TransactionId getTransactionId() {
- return null;
- }
-
- @Override
- public boolean hasClientOrigin() {
- return false;
- }
-
- @Override
- public boolean isOldValueAvailable() {
- return true;
- }
-
- @Override
- public Region getRegion() {
- return region;
- }
-
- @Override
- public Operation getOperation() {
- return operation;
- }
-
- @Override
- public Object getCallbackArgument() {
- return null;
- }
-
- @Override
- public boolean isCallbackArgumentAvailable() {
- return false;
- }
-
- @Override
- public boolean isOriginRemote() {
- return false;
- }
-
- @Override
- public DistributedMember getDistributedMember() {
- return null;
- }
-}
--
To stop receiving notification emails like this one, please contact
['"[email protected]" <[email protected]>'].