diff --git a/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/fork/CacheServerProcess.java b/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/fork/CacheServerProcess.java index 745a61eaa4..d862425ad4 100644 --- a/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/fork/CacheServerProcess.java +++ b/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/fork/CacheServerProcess.java @@ -61,7 +61,7 @@ public class CacheServerProcess { server.setNotifyBySubscription(true); System.out.println("Starting server"); server.start(); - + ForkUtil.createControlFile(CacheServerProcess.class.getName()); System.out.println("Waiting for shutdown"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); diff --git a/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/fork/ForkUtil.java b/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/fork/ForkUtil.java index 57bee0bf72..19d9b56d9e 100644 --- a/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/fork/ForkUtil.java +++ b/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/fork/ForkUtil.java @@ -17,12 +17,12 @@ package org.springframework.integration.gemfire.fork; import java.io.BufferedReader; +import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.util.concurrent.atomic.AtomicBoolean; - /** * Utility for forking Java processes. Modified from the SGF version for SI * @@ -33,21 +33,26 @@ import java.util.concurrent.atomic.AtomicBoolean; */ 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"); String home = System.getProperty("java.home"); Process proc = null; - String java = home + "/bin/java ".replace("\\","/");; + String java = home + "/bin/java ".replace("\\", "/"); + ; String argCp = "-cp " + cp; String argClass = argument; String cmd = java + argCp + " " + argClass; try { - //ProcessBuilder builder = new ProcessBuilder(cmd, argCp, argClass); - //builder.redirectErrorStream(true); + // ProcessBuilder builder = new ProcessBuilder(cmd, argCp, + // argClass); + // builder.redirectErrorStream(true); proc = Runtime.getRuntime().exec(cmd); - } catch (IOException ioe) { + } + catch (IOException ioe) { throw new IllegalStateException("Cannot start command " + cmd, ioe); } @@ -68,14 +73,15 @@ public class ForkUtil { } Thread.sleep(200); } while (run.get()); - } catch (Exception ex) { + } + catch (Exception ex) { // ignore and exit } } }); reader.start(); - + Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { @@ -86,7 +92,8 @@ public class ForkUtil { try { p.waitFor(); - } catch (InterruptedException e) { + } + catch (InterruptedException e) { // ignore } System.out.println("Fork stopped"); @@ -97,12 +104,43 @@ public class ForkUtil { } public static OutputStream cacheServer() { - OutputStream os = cloneJVM("org.springframework.integration.gemfire.fork.CacheServerProcess"); - try { - Thread.sleep(8000); - } catch (InterruptedException ex) { - // ignore and move on + String className = "org.springframework.integration.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(); + } + } \ No newline at end of file diff --git a/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/inbound/CacheListeningMessageProducerTests.java b/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/inbound/CacheListeningMessageProducerTests.java index 44127816d6..441d6bb460 100644 --- a/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/inbound/CacheListeningMessageProducerTests.java +++ b/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/inbound/CacheListeningMessageProducerTests.java @@ -21,7 +21,6 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import org.junit.Test; - import org.springframework.data.gemfire.CacheFactoryBean; import org.springframework.data.gemfire.RegionFactoryBean; import org.springframework.integration.Message; @@ -40,7 +39,7 @@ public class CacheListeningMessageProducerTests { public void receiveNewValuePayloadForCreateEvent() throws Exception { CacheFactoryBean cacheFactoryBean = new CacheFactoryBean(); cacheFactoryBean.afterPropertiesSet(); - Cache cache = cacheFactoryBean.getObject(); + Cache cache = (Cache)cacheFactoryBean.getObject(); RegionFactoryBean regionFactoryBean = new RegionFactoryBean(); regionFactoryBean.setName("test.receiveNewValuePayloadForCreateEvent"); regionFactoryBean.setCache(cache); @@ -63,7 +62,7 @@ public class CacheListeningMessageProducerTests { public void receiveNewValuePayloadForUpdateEvent() throws Exception { CacheFactoryBean cacheFactoryBean = new CacheFactoryBean(); cacheFactoryBean.afterPropertiesSet(); - Cache cache = cacheFactoryBean.getObject(); + Cache cache = (Cache)cacheFactoryBean.getObject(); RegionFactoryBean regionFactoryBean = new RegionFactoryBean(); regionFactoryBean.setName("test.receiveNewValuePayloadForUpdateEvent"); regionFactoryBean.setCache(cache); @@ -90,7 +89,7 @@ public class CacheListeningMessageProducerTests { public void receiveOldValuePayloadForDestroyEvent() throws Exception { CacheFactoryBean cacheFactoryBean = new CacheFactoryBean(); cacheFactoryBean.afterPropertiesSet(); - Cache cache = cacheFactoryBean.getObject(); + Cache cache = (Cache)cacheFactoryBean.getObject(); RegionFactoryBean regionFactoryBean = new RegionFactoryBean(); regionFactoryBean.setName("test.receiveOldValuePayloadForDestroyEvent"); regionFactoryBean.setCache(cache); @@ -116,7 +115,7 @@ public class CacheListeningMessageProducerTests { public void receiveOldValuePayloadForInvalidateEvent() throws Exception { CacheFactoryBean cacheFactoryBean = new CacheFactoryBean(); cacheFactoryBean.afterPropertiesSet(); - Cache cache = cacheFactoryBean.getObject(); + Cache cache = (Cache)cacheFactoryBean.getObject(); RegionFactoryBean regionFactoryBean = new RegionFactoryBean(); regionFactoryBean.setName("test.receiveOldValuePayloadForDestroyEvent"); regionFactoryBean.setCache(cache); diff --git a/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/outbound/CacheWritingMessageHandlerTests.java b/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/outbound/CacheWritingMessageHandlerTests.java index 45ecb95114..1bccd71e11 100644 --- a/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/outbound/CacheWritingMessageHandlerTests.java +++ b/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/outbound/CacheWritingMessageHandlerTests.java @@ -43,7 +43,7 @@ public class CacheWritingMessageHandlerTests { public void mapPayloadWritesToCache() throws Exception { CacheFactoryBean cacheFactoryBean = new CacheFactoryBean(); cacheFactoryBean.afterPropertiesSet(); - Cache cache = cacheFactoryBean.getObject(); + Cache cache = (Cache)cacheFactoryBean.getObject(); RegionFactoryBean regionFactoryBean = new RegionFactoryBean(); regionFactoryBean.setName("test.mapPayloadWritesToCache"); regionFactoryBean.setCache(cache); @@ -63,7 +63,7 @@ public class CacheWritingMessageHandlerTests { public void ExpressionsWriteToCache() throws Exception { CacheFactoryBean cacheFactoryBean = new CacheFactoryBean(); cacheFactoryBean.afterPropertiesSet(); - Cache cache = cacheFactoryBean.getObject(); + Cache cache = (Cache)cacheFactoryBean.getObject(); RegionFactoryBean regionFactoryBean = new RegionFactoryBean(); regionFactoryBean.setName("test.expressionsWriteToCache"); regionFactoryBean.setCache(cache); diff --git a/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/store/GemfireMessageStoreTests.java b/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/store/GemfireMessageStoreTests.java index 27b969f0f6..254a9d6850 100644 --- a/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/store/GemfireMessageStoreTests.java +++ b/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/store/GemfireMessageStoreTests.java @@ -41,7 +41,7 @@ public class GemfireMessageStoreTests { public void addAndGetMessage() throws Exception { CacheFactoryBean cacheFactoryBean = new CacheFactoryBean(); cacheFactoryBean.afterPropertiesSet(); - Cache cache = cacheFactoryBean.getObject(); + Cache cache = (Cache)cacheFactoryBean.getObject(); RegionFactoryBean> regionFactoryBean = new RegionFactoryBean>(); regionFactoryBean.setName("test.addAndGetMessage"); regionFactoryBean.setCache(cache); diff --git a/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/store/messagegroupstore/GemfireMessageGroupStoreTestConfiguration.java b/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/store/messagegroupstore/GemfireMessageGroupStoreTestConfiguration.java index 43602cbd10..dcf3e2585b 100644 --- a/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/store/messagegroupstore/GemfireMessageGroupStoreTestConfiguration.java +++ b/spring-integration-gemfire/src/test/java/org/springframework/integration/gemfire/store/messagegroupstore/GemfireMessageGroupStoreTestConfiguration.java @@ -70,7 +70,7 @@ public class GemfireMessageGroupStoreTestConfiguration { public Cache cache() throws Throwable { CacheFactoryBean cacheFactoryBean = new CacheFactoryBean(); cacheFactoryBean.afterPropertiesSet(); - return cacheFactoryBean.getObject(); + return (Cache)cacheFactoryBean.getObject(); } @Bean