enhanced cache server fork to sync on the existence of a file

This commit is contained in:
David Turanski
2011-08-26 07:30:17 -04:00
parent 5902ece645
commit 480ff15c09
2 changed files with 43 additions and 6 deletions

View File

@@ -17,6 +17,7 @@
package org.springframework.data.gemfire;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
@@ -29,6 +30,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
* @author Costin Leau
*/
public class ForkUtil {
private static String TEMP_DIR = System.getProperty("java.io.tmpdir");
public static OutputStream cloneJVM(String argument) {
String cp = System.getProperty("java.class.path");
@@ -95,12 +98,42 @@ public class ForkUtil {
}
public static OutputStream cacheServer() {
OutputStream os = cloneJVM("org.springframework.data.gemfire.fork.CacheServerProcess");
try {
Thread.sleep(8000);
} catch (InterruptedException ex) {
// ignore and move on
String className = "org.springframework.data.gemfire.fork.CacheServerProcess";
if (controlFileExists(className)) {
deleteControlFile(className);
}
OutputStream os = cloneJVM(className);
int maxTime = 30000;
int time = 0;
while (!controlFileExists(className) && time < maxTime) {
try {
Thread.sleep(500);
time += 500;
}
catch (InterruptedException ex) {
// ignore and move on
}
}
if (controlFileExists(className)){
System.out.println("Started cache server");
} else {
throw new RuntimeException("could not fork cache server");
}
return os;
}
public static boolean deleteControlFile(String name) {
String path = TEMP_DIR + File.separator + name;
return new File(path).delete();
}
public static boolean createControlFile(String name) throws IOException {
String path = TEMP_DIR + File.separator + name;
return new File(path).createNewFile();
}
public static boolean controlFileExists(String name) {
String path = TEMP_DIR + File.separator + name;
return new File(path).exists();
}
}

View File

@@ -20,6 +20,9 @@ import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;
import org.springframework.data.gemfire.ForkUtil;
import com.gemstone.gemfire.cache.AttributesFactory;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
@@ -57,7 +60,8 @@ public class CacheServerProcess {
server.setNotifyBySubscription(true);
server.start();
ForkUtil.createControlFile(CacheServerProcess.class.getName());
System.out.println("Waiting for signal");
// wait for signal
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));