+ improvements to the Gemfire sample
SGF-3
This commit is contained in:
@@ -16,13 +16,18 @@
|
||||
|
||||
package org.springframework.data.gemfire.samples.helloworld;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Scanner;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import com.gemstone.gemfire.cache.Region;
|
||||
|
||||
/**
|
||||
* @author Costin Leau
|
||||
*/
|
||||
@@ -30,33 +35,47 @@ public class CommandProcessor {
|
||||
|
||||
private static final Log log = LogFactory.getLog(CommandProcessor.class);
|
||||
|
||||
private static String help = initHelp();
|
||||
private static String EMPTY = "";
|
||||
|
||||
boolean threadActive;
|
||||
private Thread thread;
|
||||
|
||||
private Region<String, String> region;
|
||||
|
||||
public CommandProcessor(Region<String, String> region) {
|
||||
this.region = region;
|
||||
}
|
||||
|
||||
void start() {
|
||||
if (thread == null) {
|
||||
threadActive = false;
|
||||
threadActive = true;
|
||||
thread = new Thread(new Task(), "cmd-processor");
|
||||
thread.start();
|
||||
}
|
||||
}
|
||||
|
||||
void stop() throws Exception {
|
||||
threadActive = true;
|
||||
thread.join(3 * 1000);
|
||||
threadActive = false;
|
||||
thread.join();
|
||||
}
|
||||
|
||||
void awaitCommands() throws Exception {
|
||||
thread.join();
|
||||
}
|
||||
|
||||
private class Task implements Runnable {
|
||||
|
||||
public void run() {
|
||||
System.out.println("Hello World!");
|
||||
System.out.println("Want to interact with the world ? ...");
|
||||
System.out.println(help);
|
||||
try {
|
||||
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
|
||||
while (threadActive) {
|
||||
if (br.ready()) {
|
||||
String line = br.readLine();
|
||||
log.info("Read line " + line);
|
||||
process(br.readLine());
|
||||
}
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
@@ -65,4 +84,74 @@ public class CommandProcessor {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String initHelp() {
|
||||
try {
|
||||
InputStream stream = CommandProcessor.class.getResourceAsStream("help.txt");
|
||||
byte[] buffer = new byte[stream.available() > 0 ? stream.available() : 300];
|
||||
|
||||
BufferedInputStream bf = new BufferedInputStream(stream);
|
||||
bf.read(buffer);
|
||||
return new String(buffer);
|
||||
} catch (IOException io) {
|
||||
throw new IllegalStateException("Cannot read help file");
|
||||
}
|
||||
}
|
||||
|
||||
String process(String line) throws Exception {
|
||||
Scanner sc = new Scanner(line);
|
||||
String command = sc.next();
|
||||
|
||||
// parse commands w/o arguments
|
||||
if ("exit".equalsIgnoreCase(command)) {
|
||||
threadActive = false;
|
||||
return EMPTY;
|
||||
}
|
||||
if ("help".equalsIgnoreCase(command)) {
|
||||
return help;
|
||||
}
|
||||
if ("size".equalsIgnoreCase(command)) {
|
||||
return EMPTY + region.size();
|
||||
}
|
||||
if ("clear".equalsIgnoreCase(command)) {
|
||||
region.clear();
|
||||
return EMPTY;
|
||||
}
|
||||
if ("keys".equalsIgnoreCase(command)) {
|
||||
return region.keySet().toString();
|
||||
}
|
||||
if ("values".equalsIgnoreCase(command)) {
|
||||
return region.values().toString();
|
||||
}
|
||||
|
||||
String arg = sc.next();
|
||||
|
||||
// commands w/ 1 arg
|
||||
if ("containsKey".equalsIgnoreCase(command)) {
|
||||
return EMPTY + region.containsKey(arg);
|
||||
}
|
||||
if ("containsValue".equalsIgnoreCase(command)) {
|
||||
return EMPTY + region.containsValue(arg);
|
||||
}
|
||||
if ("query".equalsIgnoreCase(command)) {
|
||||
return region.query(command).toString();
|
||||
}
|
||||
if ("get".equalsIgnoreCase(command)) {
|
||||
return region.get(arg);
|
||||
}
|
||||
if ("remove".equalsIgnoreCase(command)) {
|
||||
return region.remove(arg);
|
||||
}
|
||||
|
||||
// commands w/ 2 args
|
||||
|
||||
String arg2 = sc.next();
|
||||
|
||||
if ("put".equalsIgnoreCase(command)) {
|
||||
return region.put(arg, arg2);
|
||||
}
|
||||
|
||||
sc.close();
|
||||
return "unknown command - run 'help' for available commands";
|
||||
}
|
||||
}
|
||||
@@ -39,7 +39,7 @@ public class HelloWorld {
|
||||
private static final Log log = LogFactory.getLog(HelloWorld.class);
|
||||
|
||||
// inject the region
|
||||
@Resource
|
||||
@Resource(name = "hw-region")
|
||||
private Region<String, String> region;
|
||||
|
||||
// re-inject the region (as a Map)
|
||||
@@ -54,7 +54,7 @@ public class HelloWorld {
|
||||
void start() {
|
||||
log.info("Member " + region.getCache().getDistributedSystem().getDistributedMember().getId()
|
||||
+ " connecting to region [" + region.getName() + "]");
|
||||
processor = new CommandProcessor();
|
||||
processor = new CommandProcessor(region);
|
||||
processor.start();
|
||||
}
|
||||
|
||||
@@ -64,4 +64,12 @@ public class HelloWorld {
|
||||
+ " disconnecting from region [" + region.getName() + "]");
|
||||
processor.stop();
|
||||
}
|
||||
|
||||
public void greetWorld() {
|
||||
try {
|
||||
processor.awaitCommands();
|
||||
} catch (Exception ex) {
|
||||
throw new IllegalStateException("Cannot greet world", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -45,5 +45,9 @@ public class Main {
|
||||
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext(res);
|
||||
// shutdown the context along with the VM
|
||||
ctx.registerShutdownHook();
|
||||
|
||||
// call greet world to prevent the thread from ending
|
||||
HelloWorld bean = ctx.getBean(HelloWorld.class);
|
||||
bean.greetWorld();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
Supported commands are:
|
||||
|
||||
get <key> - retrieves an entry (by key) from the grid
|
||||
put <key> <value> - puts a new entry into the grid
|
||||
remove <key> - removes an entry (by key) from the grid
|
||||
size - returns the size of the grid
|
||||
clear - removes all mapping in the grid
|
||||
keys - returns the keys contained by the grid
|
||||
values - returns the values contained by the grid
|
||||
containsKey <key> - indicates if the given key is contained by the grid
|
||||
containsValue <value> - indicates if the given value is contained by the grid
|
||||
|
||||
query <query> - executes a query on the grid
|
||||
|
||||
help - this info
|
||||
exit - this node exists
|
||||
Reference in New Issue
Block a user