+ updated sample
- removed GridToWorldWriter
This commit is contained in:
@@ -51,7 +51,7 @@ public class CacheLogger extends CacheListenerAdapter<Object, Object> {
|
||||
Object value = event.getNewValue();
|
||||
|
||||
if (event.getOperation().isUpdate()) {
|
||||
return "[" + key + "=" + event.getOldValue() + " to " + event.getNewValue() + "]";
|
||||
return "[" + key + "] from [" + event.getOldValue() + "] to [" + event.getNewValue() + "]";
|
||||
}
|
||||
return "[" + key + "=" + value + "]";
|
||||
}
|
||||
|
||||
@@ -22,6 +22,9 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Scanner;
|
||||
import java.util.Set;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
@@ -43,6 +46,8 @@ import com.gemstone.gemfire.cache.Region;
|
||||
@Component
|
||||
public class CommandProcessor {
|
||||
|
||||
private static final Pattern COM = Pattern.compile("query|exit|help|size|clear|keys|values|map|containsKey|containsValue|get|remove|put");
|
||||
|
||||
private static final Log log = LogFactory.getLog(CommandProcessor.class);
|
||||
|
||||
private static String help = initHelp();
|
||||
@@ -120,15 +125,16 @@ public class CommandProcessor {
|
||||
final Scanner sc = new Scanner(line);
|
||||
|
||||
return template.execute(new GemfireCallback<String>() {
|
||||
|
||||
public String doInGemfire(Region reg) throws GemFireCheckedException, GemFireException {
|
||||
Region<String, String> region = reg;
|
||||
|
||||
if (!sc.hasNext()) {
|
||||
return "Invalid command";
|
||||
if (!sc.hasNext(COM)) {
|
||||
return "Invalid command - type 'help' for supported operations";
|
||||
}
|
||||
String command = sc.next();
|
||||
|
||||
|
||||
String arg1 = (sc.hasNext() ? sc.next() : null);
|
||||
String arg2 = (sc.hasNext() ? sc.next() : null);
|
||||
|
||||
// query shortcut
|
||||
if ("query".equalsIgnoreCase(command)) {
|
||||
@@ -149,7 +155,7 @@ public class CommandProcessor {
|
||||
}
|
||||
if ("clear".equalsIgnoreCase(command)) {
|
||||
region.clear();
|
||||
return "Clearing grid";
|
||||
return "Clearing grid..";
|
||||
}
|
||||
if ("keys".equalsIgnoreCase(command)) {
|
||||
return region.keySet().toString();
|
||||
@@ -158,36 +164,40 @@ public class CommandProcessor {
|
||||
return region.values().toString();
|
||||
}
|
||||
|
||||
if (!sc.hasNext()) {
|
||||
return "Argument(s) needed";
|
||||
}
|
||||
if ("map".equalsIgnoreCase(command)) {
|
||||
Set<Entry<String, String>> entrySet = region.entrySet();
|
||||
if (entrySet.size() == 0)
|
||||
return "[]";
|
||||
|
||||
String arg = sc.next();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (Entry<String, String> entry : entrySet) {
|
||||
sb.append("[");
|
||||
sb.append(entry.getKey());
|
||||
sb.append("=");
|
||||
sb.append(entry.getValue());
|
||||
sb.append("] ");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
// commands w/ 1 arg
|
||||
if ("containsKey".equalsIgnoreCase(command)) {
|
||||
return EMPTY + region.containsKey(arg);
|
||||
return EMPTY + region.containsKey(arg1);
|
||||
}
|
||||
if ("containsValue".equalsIgnoreCase(command)) {
|
||||
return EMPTY + region.containsValue(arg);
|
||||
return EMPTY + region.containsValue(arg1);
|
||||
}
|
||||
if ("get".equalsIgnoreCase(command)) {
|
||||
return region.get(arg);
|
||||
return region.get(arg1);
|
||||
}
|
||||
if ("remove".equalsIgnoreCase(command)) {
|
||||
return region.remove(arg);
|
||||
return region.remove(arg1);
|
||||
}
|
||||
|
||||
// commands w/ 2 args
|
||||
|
||||
if (!sc.hasNext()) {
|
||||
return "2 Argument needed";
|
||||
}
|
||||
|
||||
String arg2 = sc.next();
|
||||
|
||||
if ("put".equalsIgnoreCase(command)) {
|
||||
return region.put(arg, arg2);
|
||||
return region.put(arg1, arg2);
|
||||
}
|
||||
|
||||
sc.close();
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010 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.data.gemfire.samples.helloworld;
|
||||
|
||||
import com.gemstone.gemfire.cache.util.CacheWriterAdapter;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class GridToWorldWriter extends CacheWriterAdapter<Object, Object> {
|
||||
|
||||
|
||||
}
|
||||
@@ -37,7 +37,7 @@ public class HelloWorld {
|
||||
private static final Log log = LogFactory.getLog(HelloWorld.class);
|
||||
|
||||
// inject the region
|
||||
@Resource(name = "hw-region")
|
||||
@Resource(name = "myWorld")
|
||||
private Region<String, String> region;
|
||||
|
||||
@Resource
|
||||
|
||||
@@ -4,12 +4,11 @@
|
||||
xmlns:p="http://www.springframework.org/schema/p"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
|
||||
<import resource="cache-context.xml"/>
|
||||
|
||||
<!-- find beans by scanning the classpath -->
|
||||
<context:component-scan base-package="org.springframework.data.gemfire.samples.helloworld"/>
|
||||
|
||||
<bean id="gemfireTemplate" class="org.springframework.data.gemfire.GemfireTemplate" p:region-ref="hw-region"/>
|
||||
|
||||
</beans>
|
||||
|
||||
@@ -15,13 +15,11 @@
|
||||
|
||||
<!-- hello world region -->
|
||||
<!-- since no name is given, the region will be named after the bean -->
|
||||
<bean id="hw-region" class="org.springframework.data.gemfire.RegionFactoryBean" p:cache-ref="cache" p:dataPolicy="REPLICATE" p:scope="DISTRIBUTED_ACK">
|
||||
<bean id="myWorld" class="org.springframework.data.gemfire.RegionFactoryBean" p:cache-ref="cache" p:dataPolicy="REPLICATE" p:scope="DISTRIBUTED_ACK">
|
||||
<property name="cacheListeners">
|
||||
<bean class="org.springframework.data.gemfire.samples.helloworld.CacheLogger"/>
|
||||
</property>
|
||||
<property name="cacheWriter">
|
||||
<bean class="org.springframework.data.gemfire.samples.helloworld.GridToWorldWriter"/>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
|
||||
<bean id="gemfireTemplate" class="org.springframework.data.gemfire.GemfireTemplate" p:region-ref="myWorld"/>
|
||||
</beans>
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
log-level=warning
|
||||
name=Spring GemFire World
|
||||
|
||||
@@ -57,6 +57,13 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -150,6 +150,10 @@ public class GemfireTemplate extends GemfireAccessor {
|
||||
new CloseSuppressingInvocationHandler(region));
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
// Convenience methods for load, save, delete
|
||||
//-------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Invocation handler that suppresses close calls on GemFire Regions.
|
||||
* @see Region#close()
|
||||
|
||||
Reference in New Issue
Block a user