Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

updating continuous query example to the cq container

  • Loading branch information...
commit 1ffca5bdee46f6882ac07aaac705b51865e4bc4b 1 parent 03170ce
@d4v3r d4v3r authored
View
49 vfgf-quickstart/src/main/java/quickstart/ContinuousQueryListenerImpl.java
@@ -0,0 +1,49 @@
+package quickstart;
+
+import com.gemstone.gemfire.cache.Operation;
+import com.gemstone.gemfire.cache.query.CqEvent;
+
+public class ContinuousQueryListenerImpl {
+
+ public void handleEvent(CqEvent event) {
+ Operation baseOperation = event.getBaseOperation();
+ Operation queryOperation = event.getQueryOperation();
+
+ String baseOp = "";
+ String queryOp = "";
+
+ if (baseOperation.isUpdate()) {
+ baseOp = " Update";
+ }
+ else if (baseOperation.isCreate()) {
+ baseOp = " Create";
+ }
+ else if (baseOperation.isDestroy()) {
+ baseOp = " Destroy";
+ }
+ else if (baseOperation.isInvalidate()) {
+ baseOp = " Invalidate";
+ }
+
+ if (queryOperation.isUpdate()) {
+ queryOp = " Update";
+ }
+ else if (queryOperation.isCreate()) {
+ queryOp = " Create";
+ }
+ else if (queryOperation.isDestroy()) {
+ queryOp = " Destroy";
+ }
+
+ StringBuffer eventLog = new StringBuffer();
+ eventLog.append("\n "
+ + "CqListener:\n Received cq event for entry: "
+ + event.getKey() + ", "
+ + ((event.getNewValue()) != null ? event.getNewValue() : "")
+ + "\n" + " With BaseOperation =" + baseOp
+ + " and QueryOperation =" + queryOp + "\n");
+ System.out.print(eventLog.toString());
+
+ }
+
+}
View
47 vfgf-quickstart/src/main/java/quickstart/CqClient.java
@@ -6,16 +6,9 @@
import javax.annotation.Resource;
+import org.springframework.data.gemfire.GemfireTemplate;
import org.springframework.stereotype.Component;
-import com.gemstone.gemfire.cache.Region;
-import com.gemstone.gemfire.cache.client.ClientCache;
-import com.gemstone.gemfire.cache.query.CqAttributes;
-import com.gemstone.gemfire.cache.query.CqAttributesFactory;
-import com.gemstone.gemfire.cache.query.CqListener;
-import com.gemstone.gemfire.cache.query.CqQuery;
-import com.gemstone.gemfire.cache.query.QueryService;
-
/**
* In this example of hierarchical caching, the server listens on a port for
* client requests and updates. The client creates and executes CQ on the server
@@ -28,12 +21,8 @@
@Component
public class CqClient {
- // inject the region
- @Resource(name = "exampleRegion")
- private Region<String, String> exampleRegion;
-
- @Resource(name = "gemfire-cache")
- private ClientCache cache;
+ @Resource(name = "exampleRegionTemplate")
+ private GemfireTemplate exampleRegionTemplate;
private final BufferedReader stdinReader;
@@ -43,28 +32,6 @@ public CqClient() {
public void run() throws Exception {
- // Get the query service
- QueryService queryService = cache.getQueryService();
-
- // Create CQ Attributes.
- CqAttributesFactory cqAf = new CqAttributesFactory();
-
- // Initialize and set CqListener.
- CqListener[] cqListeners = { new SimpleCqListener() };
- cqAf.initCqListeners(cqListeners);
- CqAttributes cqa = cqAf.create();
-
- // Construct a new CQ.
- String cqName = "MyCq";
- String query = "SELECT * FROM /exampleRegion e where e='ClientFirstValue'";
- System.out.println("Creating CQ \"" + cqName + "\" with query");
- System.out.println("\"" + query + "\"");
-
- // Execute the Cq. This registers the cq on the server.
- System.out.println("Executing CQ \"" + cqName + "\"...");
- CqQuery myCq = queryService.newCq(cqName, query, cqa);
- myCq.execute();
-
System.out
.println("This client will update the server cache and its CQ listener will");
System.out.println("get events for any changes to the CQ result set.");
@@ -83,7 +50,7 @@ public void run() throws Exception {
System.out
.println("This satisfies the query, so the CqListener will report a query");
System.out.println("creation event from the server cache.");
- exampleRegion.put("key1", "ClientFirstValue");
+ exampleRegionTemplate.put("key1", "ClientFirstValue");
// Wait for the events to come through so the screen output makes sense
Thread.sleep(2000);
@@ -99,7 +66,7 @@ public void run() throws Exception {
.println("This removes key1 from the CQ result set, to the CQListener will");
System.out.println("report a query destroy event.");
- exampleRegion.put("key1", "ClientSecondValue");
+ exampleRegionTemplate.put("key1", "ClientSecondValue");
Thread.sleep(2000);
pressEnterToContinue();
@@ -113,7 +80,7 @@ public void run() throws Exception {
.println("This adds key1 back into the CQ result set, to the CQListener will");
System.out.println("report a query create event.");
- exampleRegion.put("key1", "ClientFirstValue");
+ exampleRegionTemplate.put("key1", "ClientFirstValue");
Thread.sleep(2000);
pressEnterToContinue();
@@ -127,7 +94,7 @@ public void run() throws Exception {
.println("This removes key1 from the result set, to the CQListener will");
System.out.println("report a query destroy event.");
- exampleRegion.destroy("key1");
+ exampleRegionTemplate.remove("key1");
Thread.sleep(2000);
pressEnterToContinue();
View
21 vfgf-quickstart/src/main/resources/cq-client-cache-context.xml
@@ -1,21 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:util="http://www.springframework.org/schema/util"
- xmlns:gfe="http://www.springframework.org/schema/gemfire"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
+ xmlns:util="http://www.springframework.org/schema/util" xmlns:gfe="http://www.springframework.org/schema/gemfire"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire.xsd">
<!-- GemFire cache bean -->
- <gfe:client-cache pool-name="client"/>
+ <gfe:client-cache pool-name="client" />
<gfe:pool subscription-enabled="true" id="client">
- <gfe:server host="localhost" port="40404"/>
+ <gfe:server host="localhost" port="40404" />
</gfe:pool>
<!-- create region -->
- <gfe:client-region id="exampleRegion" shortcut="CACHING_PROXY" pool-name="client"/>
-
+ <gfe:client-region id="exampleRegion" shortcut="CACHING_PROXY"
+ pool-name="client" />
+
+ <bean id="exampleRegionTemplate" class="org.springframework.data.gemfire.GemfireTemplate" p:region-ref="exampleRegion"/>
+ <gfe:cq-listener-container>
+ <gfe:listener ref="listener" query="SELECT * FROM /exampleRegion e where e='ClientFirstValue'"/>
+ </gfe:cq-listener-container>
+
+ <bean id="listener" class="quickstart.ContinuousQueryListenerImpl" />
</beans>
Please sign in to comment.
Something went wrong with that request. Please try again.