This is an automated email from the ASF dual-hosted git repository.

jchen21 pushed a commit to branch feature/continuous-query-examples
in repository https://gitbox.apache.org/repos/asf/geode-examples.git

commit ae15b638ead5e49e098d42d1b7f0b0853e44e18a
Author: Patrick Johnson <kcirtap1...@gmail.com>
AuthorDate: Wed Jun 20 17:30:47 2018 -0700

    added example for continuous query
    
    Signed-off-by: Jianxia Chen <jche...@apache.org>
---
 cq/scripts/start.gfsh                              | 26 ++++++++
 cq/scripts/stop.gfsh                               | 19 ++++++
 .../java/org/apache/geode_examples/cq/Example.java | 74 ++++++++++++++++++++++
 .../geode_examples/cq/TradeEventListener.java      | 39 ++++++++++++
 4 files changed, 158 insertions(+)

diff --git a/cq/scripts/start.gfsh b/cq/scripts/start.gfsh
new file mode 100755
index 0000000..e06d1d2
--- /dev/null
+++ b/cq/scripts/start.gfsh
@@ -0,0 +1,26 @@
+#
+# 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.
+#
+
+start locator --name=locator --bind-address=127.0.0.1
+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 --skip-if-exists=true \
+    --enable-statistics=true \
+    --entry-time-to-live-expiration=10 
--entry-time-to-live-expiration-action=local-destroy
+describe region --name=example-region
diff --git a/cq/scripts/stop.gfsh b/cq/scripts/stop.gfsh
new file mode 100755
index 0000000..15cd93c
--- /dev/null
+++ b/cq/scripts/stop.gfsh
@@ -0,0 +1,19 @@
+#
+# 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.
+#
+
+connect --locator=127.0.0.1[10334]
+shutdown --include-locators=true
diff --git a/cq/src/main/java/org/apache/geode_examples/cq/Example.java 
b/cq/src/main/java/org/apache/geode_examples/cq/Example.java
new file mode 100644
index 0000000..8200e01
--- /dev/null
+++ b/cq/src/main/java/org/apache/geode_examples/cq/Example.java
@@ -0,0 +1,74 @@
+/*
+ * 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.expiration;
+
+import org.apache.geode.cache.Region;
+import org.apache.geode.cache.client.ClientCache;
+import org.apache.geode.cache.client.ClientCacheFactory;
+import org.apache.geode.cache.client.ClientRegionFactory;
+import org.apache.geode.cache.client.ClientRegionShortcut;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
+import java.util.Map;
+import java.util.Random;
+import java.util.Set;
+import java.util.stream.IntStream;
+
+public class Example {
+
+    public static void main(String[] args) {
+        // Get cache and queryService - refs to local cache and QueryService
+        // Create client /tradeOrder region configured to talk to the server
+
+        // Create CqAttribute using CqAttributeFactory
+        CqAttributesFactory cqf = new CqAttributesFactory();
+
+        // Create a listener and add it to the CQ attributes callback defined 
below
+        CqListener tradeEventListener = new TradeEventListener();
+        cqf.addCqListener(tradeEventListener);
+        CqAttributes cqa = cqf.create();
+        // Name of the CQ and its query
+        String cqName = "priceTracker";
+        String queryStr = "SELECT * FROM /tradeOrder t where t.price > 100.00";
+
+        // Create the CqQuery
+        CqQuery priceTracker = queryService.newCq(cqName, queryStr, cqa);
+
+        try
+        {  // Execute CQ, getting the optional initial result set
+            // Without the initial result set, the call is 
priceTracker.execute();
+            SelectResults sResults = priceTracker.executeWithInitialResults();
+            for (Object o : sResults) {
+                Struct s = (Struct) o;
+                TradeOrder to = (TradeOrder) s.get("value");
+                System.out.println("Intial result includes: " + to);
+            }
+        }
+        catch (Exception ex)
+        {
+            ex.printStackTrace();
+        }
+        // Now the CQ is running on the server, sending CqEvents to the 
listener
+
+        // End of life for the CQ - clear up resources by closing
+        priceTracker.close();
+    }
+}
diff --git 
a/cq/src/main/java/org/apache/geode_examples/cq/TradeEventListener.java 
b/cq/src/main/java/org/apache/geode_examples/cq/TradeEventListener.java
new file mode 100644
index 0000000..e1ba0dd
--- /dev/null
+++ b/cq/src/main/java/org/apache/geode_examples/cq/TradeEventListener.java
@@ -0,0 +1,39 @@
+public class TradeEventListener implements CqStatusListener
+{
+    public void onEvent(CqEvent cqEvent)
+    {
+        // org.apache.geode.cache Operation associated with the query op
+        Operation queryOperation = cqEvent.getQueryOperation();
+        // key and new value from the event
+        Object key = cqEvent.getKey();
+        TradeOrder tradeOrder = (TradeOrder)cqEvent.getNewValue();
+        if (queryOperation.isUpdate())
+        {
+            // update data on the screen for the trade order . . .
+        }
+        else if (queryOperation.isCreate())
+        {
+            // add the trade order to the screen . . .
+        }
+        else if (queryOperation.isDestroy())
+        {
+            // remove the trade order from the screen . . .
+        }
+    }
+    public void onError(CqEvent cqEvent)
+    {
+        // handle the error
+    }
+    // From CacheCallback public void close()
+    {
+        // close the output screen for the trades . . .
+    }
+
+    public void onCqConnected() {
+        //Display connected symbol
+    }
+
+    public void onCqDisconnected() {
+        //Display disconnected symbol
+    }
+}
\ No newline at end of file

Reply via email to