Fixes GemFire client/server integratione tests issue when setting JAVA_TOOL_OPTIONS env var
Fixes gh-727
This commit is contained in:
@@ -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() {
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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<Object, AbstractGemFireOperationsSessionRepository.GemFireSession> 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user