diff --git a/samples/boot/gemfire/build.gradle b/samples/boot/gemfire/build.gradle index 93c3e2c6..1cb900a9 100644 --- a/samples/boot/gemfire/build.gradle +++ b/samples/boot/gemfire/build.gradle @@ -41,24 +41,34 @@ springBoot { task runGemFireServer() { doLast { - println 'STARTING GEMFIRE SERVER...' - ext.port = reservePort() + println "Starting GemFire Server on port [$port] ..." + + def out = new StringBuilder() + def err = new StringBuilder() + String classpath = sourceSets.main.runtimeClasspath.collect { it }.join(File.pathSeparator) - String[] commandLine = ['java', '-server', '-ea', '-classpath', classpath, - "-Dgemfire.cache.server.port=$port", - //"-Dgemfire.log-file=gemfire-server.log", - "-Dgemfire.log-level=" + System.getProperty('gemfire.log.level', 'config'), - 'sample.server.GemFireServer'] + String[] commandLine = [ + 'java', '-server', '-ea', '-classpath', classpath, + //"-Dgemfire.log-file=gemfire-server.log", + //"-Dgemfire.log-level=config", + "-Dspring-session-data-gemfire.cache.server.port=$port", + "-Dspring-session-data-gemfire.log.level=" + + System.getProperty('spring-session-data-gemfire.log.level', 'config'), + 'sample.server.GemFireServer' + ] //println commandLine ext.process = commandLine.execute() - process.in.close() - process.out.close() - process.err.close() + //ext.process = new ProcessBuilder().command(commandLine).redirectErrorStream(true).start(); + + ext.process.consumeProcessOutput(out, err) + + //println 'OUT: ' + out + //println 'ERR: ' + err } } @@ -67,12 +77,15 @@ integrationTest { dependsOn runGemFireServer doFirst { def port = reservePort() - systemProperties['server.port'] = port systemProperties['management.port'] = 0 - systemProperties['gemfire.cache.server.port'] = runGemFireServer.port + systemProperties['server.port'] = port + //systemProperties['gemfire.log-file'] = "gemfire-client.log" + //systemProperties['gemfire.log-level'] = "config" + systemProperties['spring-session-data-gemfire.cache.server.port'] = runGemFireServer.port + systemProperties['spring-session-data-gemfire.log.level'] = System.getProperty("spring-session-data-gemfire.log.level", "warning") } doLast { - println 'STOPPING GEMFIRE SERVER...' + println 'Stopping GemFire Server...' runGemFireServer.process?.destroyForcibly() } } diff --git a/samples/boot/gemfire/src/main/java/sample/client/Application.java b/samples/boot/gemfire/src/main/java/sample/client/Application.java index a3c0d541..13f6cf38 100644 --- a/samples/boot/gemfire/src/main/java/sample/client/Application.java +++ b/samples/boot/gemfire/src/main/java/sample/client/Application.java @@ -69,7 +69,7 @@ import org.springframework.web.bind.annotation.ResponseBody; */ // tag::class[] @SpringBootApplication -@EnableGemFireHttpSession // <1> +@EnableGemFireHttpSession(poolName = "DEFAULT")// <1> @Controller public class Application { @@ -77,7 +77,7 @@ public class Application { static final CountDownLatch LATCH = new CountDownLatch(1); - static final String DEFAULT_GEMFIRE_LOG_LEVEL = "config"; + static final String DEFAULT_GEMFIRE_LOG_LEVEL = "warning"; static final String INDEX_TEMPLATE_VIEW_NAME = "index"; static final String PING_RESPONSE = "PONG"; static final String REQUEST_COUNT_ATTRIBUTE_NAME = "requestCount"; @@ -102,18 +102,17 @@ public class Application { } String applicationName() { - return "samples:httpsession-gemfire-boot:" - .concat(getClass().getSimpleName()); + return "spring-session-data-gemfire-boot-sample.".concat(getClass().getSimpleName()); } String logLevel() { - return System.getProperty("gemfire.log-level", DEFAULT_GEMFIRE_LOG_LEVEL); + return System.getProperty("spring-session-data-gemfire.log.level", DEFAULT_GEMFIRE_LOG_LEVEL); } @Bean ClientCacheFactoryBean gemfireCache( - @Value("${gemfire.cache.server.host:localhost}") String host, - @Value("${gemfire.cache.server.port:12480}") int port) { // <3> + @Value("${spring-session-data-gemfire.cache.server.host:localhost}") String host, + @Value("${spring-session-data-gemfire.cache.server.port:12480}") int port) { // <3> ClientCacheFactoryBean gemfireCache = new ClientCacheFactoryBean(); @@ -153,8 +152,8 @@ public class Application { @Bean BeanPostProcessor gemfireCacheServerReadyBeanPostProcessor( - @Value("${gemfire.cache.server.host:localhost}") final String host, - @Value("${gemfire.cache.server.port:12480}") final int port) { // <5> + @Value("${spring-session-data-gemfire.cache.server.host:localhost}") final String host, + @Value("${spring-session-data-gemfire.cache.server.port:12480}") final int port) { // <5> return new BeanPostProcessor() { diff --git a/samples/boot/gemfire/src/main/java/sample/server/GemFireServer.java b/samples/boot/gemfire/src/main/java/sample/server/GemFireServer.java index 16c490e0..83b964a5 100644 --- a/samples/boot/gemfire/src/main/java/sample/server/GemFireServer.java +++ b/samples/boot/gemfire/src/main/java/sample/server/GemFireServer.java @@ -46,7 +46,7 @@ import org.springframework.session.data.gemfire.config.annotation.web.http.Enabl @EnableGemFireHttpSession(maxInactiveIntervalInSeconds = 20) // <1> public class GemFireServer { - static final String DEFAULT_GEMFIRE_LOG_LEVEL = "config"; + static final String DEFAULT_GEMFIRE_LOG_LEVEL = "warning"; public static void main(String[] args) { SpringApplication springApplication = new SpringApplication(GemFireServer.class); @@ -63,7 +63,6 @@ public class GemFireServer { Properties gemfireProperties = new Properties(); gemfireProperties.setProperty("name", applicationName()); - gemfireProperties.setProperty("mcast-port", "0"); //gemfireProperties.setProperty("log-file", "gemfire-server.log"); gemfireProperties.setProperty("log-level", logLevel()); //gemfireProperties.setProperty("jmx-manager", "true"); @@ -73,11 +72,11 @@ public class GemFireServer { } String applicationName() { - return "samples:httpsession-gemfire-boot:".concat(getClass().getSimpleName()); + return "spring-session-data-gemfire-boot-sample:".concat(getClass().getSimpleName()); } String logLevel() { - return System.getProperty("gemfire.log-level", DEFAULT_GEMFIRE_LOG_LEVEL); + return System.getProperty("spring-session-data-gemfire.log.level", DEFAULT_GEMFIRE_LOG_LEVEL); } @Bean @@ -92,9 +91,9 @@ public class GemFireServer { @Bean CacheServerFactoryBean gemfireCacheServer(Cache gemfireCache, - @Value("${gemfire.cache.server.bind-address:localhost}") String bindAddress, - @Value("${gemfire.cache.server.hostname-for-clients:localhost}") String hostnameForClients, - @Value("${gemfire.cache.server.port:12480}") int port) { // <4> + @Value("${spring-session-data-gemfire.cache.server.bind-address:localhost}") String bindAddress, + @Value("${spring-session-data-gemfire.cache.server.hostname-for-clients:localhost}") String hostnameForClients, + @Value("${spring-session-data-gemfire.cache.server.port:12480}") int port) { // <4> CacheServerFactoryBean gemfireCacheServer = new CacheServerFactoryBean(); diff --git a/samples/boot/gemfire/src/main/java/sample/server/NativeGemFireServer.java b/samples/boot/gemfire/src/main/java/sample/server/NativeGemFireServer.java new file mode 100644 index 00000000..20977d17 --- /dev/null +++ b/samples/boot/gemfire/src/main/java/sample/server/NativeGemFireServer.java @@ -0,0 +1,186 @@ +/* + * Copyright 2014-2016 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 sample.server; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.Properties; + +import com.gemstone.gemfire.cache.Cache; +import com.gemstone.gemfire.cache.CacheFactory; +import com.gemstone.gemfire.cache.ExpirationAction; +import com.gemstone.gemfire.cache.ExpirationAttributes; +import com.gemstone.gemfire.cache.Region; +import com.gemstone.gemfire.cache.RegionFactory; +import com.gemstone.gemfire.cache.RegionShortcut; +import com.gemstone.gemfire.cache.server.CacheServer; + +import org.springframework.session.data.gemfire.AbstractGemFireOperationsSessionRepository; +import org.springframework.session.data.gemfire.config.annotation.web.http.GemFireHttpSessionConfiguration; +import org.springframework.util.StringUtils; + +/** + * The {@link NativeGemFireServer} class uses the GemFire API to create a GemFire (cache) instance. + * + * @author John Blum + * @see com.gemstone.gemfire.cache.Cache + * @see com.gemstone.gemfire.cache.Region + * @see com.gemstone.gemfire.cache.server.CacheServer + * @see org.springframework.session.data.gemfire.config.annotation.web.http.GemFireHttpSessionConfiguration + * @since 1.3.0 + */ +@SuppressWarnings("unused") +public final class NativeGemFireServer implements Runnable { + + private static final int GEMFIRE_CACHE_SERVER_PORT = + Integer.getInteger("spring-session-data-gemfire.cache.server.port", 12480); + + private static final String GEMFIRE_CACHE_SERVER_HOST = "localhost"; + private static final String GEMFIRE_CACHE_SERVER_HOSTNAME_FOR_CLIENTS = GEMFIRE_CACHE_SERVER_HOST; + private static final String GEMFIRE_LOG_FILENAME_PATTERN = + String.format("%s", NativeGemFireServer.class.getSimpleName()).concat("-%s.log"); + + public static void main(String[] args) { + newNativeGemFireServer(args).run(); + } + + private final String[] args; + + private static File newGemFireLogFile(String suffix) { + return new File(String.format(GEMFIRE_LOG_FILENAME_PATTERN, suffix)); + } + + private static NativeGemFireServer newNativeGemFireServer(String[] args) { + return new NativeGemFireServer(args); + } + + private static String[] nullSafeStringArray(String[] array) { + return (array != null ? array.clone() : new String[0]); + } + + private static void writeStringTo(File file, String fileContents) { + PrintWriter fileWriter = null; + + try { + fileWriter = new PrintWriter(new BufferedWriter(new FileWriter(file, true)), true); + fileWriter.println(fileContents); + fileWriter.flush(); + } + catch (IOException e) { + throw new RuntimeException(String.format("Failed to write [%s] to file [%s]", fileContents, file), e); + } + finally { + if (fileWriter != null) { + fileWriter.close(); + } + } + } + private NativeGemFireServer(String[] args) { + this.args = nullSafeStringArray(args); + } + + /** + * @inheritDoc + */ + public void run() { + run(this.args); + } + + private void run(String[] args) { + try { + writeStringTo(newGemFireLogFile("stdout"), "Before"); + + registerShutdownHook(addCacheServer(createRegion(gemfireCache( + gemfireProperties(applicationName()))))); + + writeStringTo(newGemFireLogFile("stdout"), "After"); + } + catch (Throwable e) { + writeStringTo(newGemFireLogFile("stderr"), e.toString()); + } + } + + private String applicationName() { + return applicationName(null); + } + + private String applicationName(String applicationName) { + return StringUtils.hasText(applicationName) ? applicationName + : "spring-session-data-gemfire.boot.sample." + NativeGemFireServer.class.getSimpleName(); + } + + private Properties gemfireProperties(String applicationName) { + Properties gemfireProperties = new Properties(); + + gemfireProperties.setProperty("name", applicationName); + gemfireProperties.setProperty("log-file", "gemfire-server.log"); + gemfireProperties.setProperty("log-level", "config"); + //gemfireProperties.setProperty("jmx-manager", "true"); + //gemfireProperties.setProperty("jmx-manager-start", "true"); + + return gemfireProperties; + } + + private Cache gemfireCache(Properties gemfireProperties) { + return new CacheFactory(gemfireProperties).create(); + } + + private Cache createRegion(Cache gemfireCache) { + RegionFactory regionFactory = + gemfireCache.createRegionFactory(RegionShortcut.PARTITION); + + regionFactory.setKeyConstraint(Object.class); + regionFactory.setValueConstraint(AbstractGemFireOperationsSessionRepository.GemFireSession.class); + regionFactory.setStatisticsEnabled(true); + regionFactory.setEntryIdleTimeout(newExpirationAttributes(1800, ExpirationAction.INVALIDATE)); + + Region region = regionFactory.create( + GemFireHttpSessionConfiguration.DEFAULT_SPRING_SESSION_GEMFIRE_REGION_NAME); + + return gemfireCache; + } + + private ExpirationAttributes newExpirationAttributes(int expirationTime, ExpirationAction expirationAction) { + return new ExpirationAttributes(expirationTime, expirationAction); + } + + private Cache addCacheServer(Cache gemfireCache) throws IOException { + CacheServer cacheServer = gemfireCache.addCacheServer(); + + cacheServer.setBindAddress(GEMFIRE_CACHE_SERVER_HOST); + cacheServer.setHostnameForClients(GEMFIRE_CACHE_SERVER_HOSTNAME_FOR_CLIENTS); + cacheServer.setPort(GEMFIRE_CACHE_SERVER_PORT); + cacheServer.start(); + + return gemfireCache; + } + + private Cache registerShutdownHook(final Cache gemfireCache) { + Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { + public void run() { + if (gemfireCache != null) { + gemfireCache.close(); + } + } + })); + + return gemfireCache; + } +} diff --git a/samples/javaconfig/gemfire-clientserver/build.gradle b/samples/javaconfig/gemfire-clientserver/build.gradle index 446ebfed..1bef0b3b 100644 --- a/samples/javaconfig/gemfire-clientserver/build.gradle +++ b/samples/javaconfig/gemfire-clientserver/build.gradle @@ -1,7 +1,7 @@ apply from: JAVA_GRADLE +apply from: SAMPLE_GRADLE apply from: TOMCAT_7_GRADLE apply plugin: "application" -apply from: SAMPLE_GRADLE dependencies { compile project(':spring-session-data-gemfire'), @@ -38,27 +38,34 @@ task availablePort() { task runGemFireServer(dependsOn: availablePort) { doLast { - println 'STARTING GEMFIRE SERVER...' + println "Starting GemFire Server on port [$port]..." + + def out = new StringBuilder() + def err = new StringBuilder() String classpath = sourceSets.main.runtimeClasspath.collect { it }.join(File.pathSeparator) + String gemfireLogLevel = System.getProperty('sample.httpsession.gemfire.log-level', 'config') - String[] commandLine = ['java', '-server', '-ea', - "-Dspring.session.data.gemfire.port=$port", - "-Dsample.httpsession.gemfire.log-level=" - + System.getProperty('sample.httpsession.gemfire.log-level', 'warning'), - '-classpath', classpath, 'sample.ServerConfig'] + String[] commandLine = [ + 'java', '-server', '-ea', '-classpath', classpath, + "-Dspring.session.data.gemfire.port=$port", + "-Dsample.httpsession.gemfire.log-level=" + gemfireLogLevel, + 'sample.ServerConfig' + ] //println commandLine process = commandLine.execute() - process.in.close() - process.out.close() - process.err.close() + + process.consumeProcessOutput(out, err) + + //println 'OUT: ' + out + //println 'ERR: ' + err } } integrationTest.doLast { - println 'STOPPING GEMFIRE SERVER...' + println 'Stopping GemFire Server...' process?.destroyForcibly() } diff --git a/samples/javaconfig/gemfire-clientserver/src/main/java/sample/ClientConfig.java b/samples/javaconfig/gemfire-clientserver/src/main/java/sample/ClientConfig.java index 66a3e077..d55d4d17 100644 --- a/samples/javaconfig/gemfire-clientserver/src/main/java/sample/ClientConfig.java +++ b/samples/javaconfig/gemfire-clientserver/src/main/java/sample/ClientConfig.java @@ -44,7 +44,9 @@ public class ClientConfig { static final CountDownLatch LATCH = new CountDownLatch(1); - static final String DEFAULT_GEMFIRE_LOG_LEVEL = "warning"; + static final String DEFAULT_GEMFIRE_LOG_LEVEL = "config"; + static final String PROXY_HOST = "dummy.example.com"; + static final String PROXY_PORT = "3128"; @Bean static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() { @@ -54,6 +56,7 @@ public class ClientConfig { Properties gemfireProperties() { // <2> Properties gemfireProperties = new Properties(); gemfireProperties.setProperty("name", applicationName()); + // gemfireProperties.setProperty("log-file", "gemfire-client.log"); gemfireProperties.setProperty("log-level", logLevel()); return gemfireProperties; } diff --git a/samples/javaconfig/gemfire-clientserver/src/main/java/sample/ServerConfig.java b/samples/javaconfig/gemfire-clientserver/src/main/java/sample/ServerConfig.java index 28f7115f..91c0541f 100644 --- a/samples/javaconfig/gemfire-clientserver/src/main/java/sample/ServerConfig.java +++ b/samples/javaconfig/gemfire-clientserver/src/main/java/sample/ServerConfig.java @@ -36,13 +36,12 @@ public class ServerConfig { static final int SERVER_PORT = 12480; - static final String DEFAULT_GEMFIRE_LOG_LEVEL = "warning"; + static final String DEFAULT_GEMFIRE_LOG_LEVEL = "config"; static final String SERVER_HOST = "localhost"; @SuppressWarnings("resource") public static void main(String[] args) throws IOException { // <5> - new AnnotationConfigApplicationContext(ServerConfig.class) - .registerShutdownHook(); + new AnnotationConfigApplicationContext(ServerConfig.class).registerShutdownHook(); } @Bean @@ -55,21 +54,20 @@ public class ServerConfig { gemfireProperties.setProperty("name", applicationName()); gemfireProperties.setProperty("mcast-port", "0"); + // gemfireProperties.setProperty("log-file", "gemfire-server.log"); gemfireProperties.setProperty("log-level", logLevel()); - gemfireProperties.setProperty("jmx-manager", "true"); - gemfireProperties.setProperty("jmx-manager-start", "true"); + // gemfireProperties.setProperty("jmx-manager", "true"); + // gemfireProperties.setProperty("jmx-manager-start", "true"); return gemfireProperties; } String applicationName() { - return "samples:httpsession-gemfire-clientserver:" - .concat(getClass().getSimpleName()); + return "samples:httpsession-gemfire-clientserver:".concat(getClass().getSimpleName()); } String logLevel() { - return System.getProperty("sample.httpsession.gemfire.log-level", - DEFAULT_GEMFIRE_LOG_LEVEL); + return System.getProperty("sample.httpsession.gemfire.log-level", DEFAULT_GEMFIRE_LOG_LEVEL); } @Bean diff --git a/samples/xml/gemfire-clientserver/build.gradle b/samples/xml/gemfire-clientserver/build.gradle index 63362ada..8bd4b8a0 100644 --- a/samples/xml/gemfire-clientserver/build.gradle +++ b/samples/xml/gemfire-clientserver/build.gradle @@ -1,7 +1,7 @@ apply from: JAVA_GRADLE +apply from: SAMPLE_GRADLE apply from: TOMCAT_7_GRADLE apply plugin: "application" -apply from: SAMPLE_GRADLE dependencies { compile project(':spring-session-data-gemfire'), @@ -37,7 +37,10 @@ task availablePort() { task runGemFireServer(dependsOn: availablePort) { doLast { - println 'STARTING GEMFIRE SERVER...' + println "Starting GemFire Server on port [$port]..." + + def out = new StringBuilder() + def err = new StringBuilder() String classpath = sourceSets.main.runtimeClasspath.collect { it }.join(File.pathSeparator) @@ -50,14 +53,16 @@ task runGemFireServer(dependsOn: availablePort) { //println commandLine process = commandLine.execute() - process.in.close() - process.out.close() - process.err.close() + + process.consumeProcessOutput(out, err) + + //println 'OUT: ' + out + //println 'ERR: ' + err } } integrationTest.doLast { - println 'STOPPING GEMFIRE SERVER...' + println 'Stopping GemFire Server...' process?.destroyForcibly() } diff --git a/samples/xml/gemfire-clientserver/src/main/java/sample/Application.java b/samples/xml/gemfire-clientserver/src/main/java/sample/Application.java index 6e627c5d..9dfbceaf 100644 --- a/samples/xml/gemfire-clientserver/src/main/java/sample/Application.java +++ b/samples/xml/gemfire-clientserver/src/main/java/sample/Application.java @@ -27,8 +27,7 @@ import org.springframework.context.annotation.ImportResource; public class Application { public static void main(final String[] args) { - new AnnotationConfigApplicationContext(Application.class) - .registerShutdownHook(); + new AnnotationConfigApplicationContext(Application.class).registerShutdownHook(); } } // tag::end[] diff --git a/spring-session/build.gradle b/spring-session/build.gradle index 127f7a42..3b6228ff 100644 --- a/spring-session/build.gradle +++ b/spring-session/build.gradle @@ -51,7 +51,7 @@ dependencies { dependencyManagement { springIoTestRuntime { imports { - mavenBom "io.spring.platform:platform-bom:${springIoVersion}" + mavenBom "io.spring.platform:platform-bom:$springIoVersion" } } }