Polish along with debugging changes.

This commit is contained in:
John Blum
2017-06-07 20:37:37 -07:00
parent bf40caedbf
commit 6064dffe4a
6 changed files with 313 additions and 172 deletions

View File

@@ -54,8 +54,8 @@ task runGemFireServer() {
String[] commandLine = [ String[] commandLine = [
'java', '-server', '-ea', '-classpath', classpath, 'java', '-server', '-ea', '-classpath', classpath,
//"-Dgemfire.log-file=gemfire-server.log", "-Dgemfire.log-file=gemfire-server.log",
//"-Dgemfire.log-level=config", "-Dgemfire.log-level=config",
"-Dspring-session-data-gemfire.cache.server.port=$port", "-Dspring-session-data-gemfire.cache.server.port=$port",
"-Dspring-session-data-gemfire.log.level=" "-Dspring-session-data-gemfire.log.level="
+ System.getProperty('spring-session-data-gemfire.log.level', 'warning'), + System.getProperty('spring-session-data-gemfire.log.level', 'warning'),

View File

@@ -61,18 +61,22 @@ public class AttributeTests {
@Test @Test
public void noAttributes() { public void noAttributes() {
HomePage home = HomePage.go(this.driver, HomePage.class);
assertThat(home.attributes().size()).isEqualTo(0); HomePage homePage = HomePage.go(this.driver, HomePage.class);
homePage.assertAt();
assertThat(homePage.attributes().size()).isEqualTo(0);
} }
@Test @Test
public void createAttribute() { public void createAttribute() {
HomePage home = HomePage.go(this.driver, HomePage.class);
home = home.form() HomePage homePage = HomePage.go(this.driver, HomePage.class);
.attributeName("a")
.attributeValue("b") homePage = homePage.assertAt().form().attributeName("a").attributeValue("b").submit(HomePage.class);
.submit(HomePage.class);
assertThat(home.attributes()).extracting("attributeName").containsOnly("requestCount", "a"); assertThat(homePage.attributes()).extracting("attributeName").containsOnly("requestCount", "a");
assertThat(home.attributes()).extracting("attributeValue").containsOnly("1", "b"); assertThat(homePage.attributes()).extracting("attributeValue").containsOnly("1", "b");
} }
} }

View File

@@ -16,6 +16,8 @@
package sample.pages; package sample.pages;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@@ -29,16 +31,18 @@ import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory; import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.pagefactory.DefaultElementLocatorFactory; import org.openqa.selenium.support.pagefactory.DefaultElementLocatorFactory;
import static org.assertj.core.api.Assertions.assertThat;
/** /**
* @author Eddú Meléndez * @author Eddú Meléndez
* @author Rob Winch * @author Rob Winch
*/ */
@SuppressWarnings("unused")
public class HomePage { public class HomePage {
private WebDriver driver; private WebDriver driver;
@FindBy(tagName = "title")
WebElement title;
@FindBy(tagName = "form") @FindBy(tagName = "form")
WebElement form; WebElement form;
@@ -49,19 +53,28 @@ public class HomePage {
public HomePage(WebDriver driver) { public HomePage(WebDriver driver) {
this.driver = driver; this.driver = driver;
this.attributes = new ArrayList<Attribute>(); this.attributes = new ArrayList<>();
}
private static void get(WebDriver driver, String get) {
String baseUrl = "http://localhost:" + System.getProperty("app.port", "8080");
driver.get(baseUrl + get);
} }
public static <T> T go(WebDriver driver, Class<T> page) { public static <T> T go(WebDriver driver, Class<T> page) {
get(driver, "/"); get(driver, "/");
return PageFactory.initElements(driver, page); return PageFactory.initElements(driver, page);
} }
private static void get(WebDriver driver, String get) {
String baseUrl = String.format("http://localhost:%s", System.getProperty("app.port", "8080"));
driver.get(baseUrl + get);
}
public HomePage assertAt() {
assertThat(this.driver.getTitle()).isEqualTo("Session Attributes");
return this;
}
public void containCookie(String cookieName) { public void containCookie(String cookieName) {
Set<Cookie> cookies = this.driver.manage().getCookies(); Set<Cookie> cookies = this.driver.manage().getCookies();
assertThat(cookies).extracting("name").contains(cookieName); assertThat(cookies).extracting("name").contains(cookieName);
@@ -72,27 +85,35 @@ public class HomePage {
assertThat(cookies).extracting("name").doesNotContain(cookieName); assertThat(cookies).extracting("name").doesNotContain(cookieName);
} }
public HomePage logout() {
WebElement logout = this.driver
.findElement(By.cssSelector("input[type=\"submit\"]"));
logout.click();
return PageFactory.initElements(this.driver, HomePage.class);
}
public List<Attribute> attributes() {
List<Attribute> rows = new ArrayList<Attribute>();
for (WebElement tr : this.trs) {
rows.add(new Attribute(tr));
}
this.attributes.addAll(rows);
return this.attributes;
}
public Form form() { public Form form() {
return new Form(this.form); return new Form(this.form);
} }
public List<Attribute> attributes() {
List<Attribute> rows = new ArrayList<>();
for (WebElement tr : this.trs) {
rows.add(new Attribute(tr));
}
this.attributes.addAll(rows);
return this.attributes;
}
public HomePage logout() {
WebElement logout = this.driver.findElement(By.cssSelector("input[type=\"submit\"]"));
logout.click();
return PageFactory.initElements(this.driver, HomePage.class);
}
public String title() {
return this.title.getText();
}
public class Form { public class Form {
@FindBy(name = "attributeName") @FindBy(name = "attributeName")
WebElement attributeName; WebElement attributeName;
@@ -123,6 +144,7 @@ public class HomePage {
} }
public static class Attribute { public static class Attribute {
@FindBy(xpath = ".//td[1]") @FindBy(xpath = ".//td[1]")
WebElement attributeName; WebElement attributeName;
@@ -147,5 +169,4 @@ public class HomePage {
return this.attributeValue.getText(); return this.attributeValue.getText();
} }
} }
} }

View File

@@ -16,21 +16,28 @@
package sample.client; package sample.client;
import static java.util.Arrays.stream;
import static org.springframework.data.gemfire.util.ArrayUtils.nullSafeArray;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import org.apache.geode.cache.Region; import org.apache.geode.cache.Region;
import org.apache.geode.cache.client.ClientCache; import org.apache.geode.cache.client.ClientCache;
import org.apache.geode.cache.client.ClientRegionShortcut;
import org.apache.geode.cache.client.Pool; import org.apache.geode.cache.client.Pool;
import org.apache.geode.cache.client.PoolManager; import org.apache.geode.cache.client.PoolManager;
import org.apache.geode.cache.client.internal.PoolImpl; import org.apache.geode.cache.client.internal.PoolImpl;
@@ -44,8 +51,10 @@ import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.data.gemfire.client.ClientCacheFactoryBean; import org.springframework.data.gemfire.client.ClientCacheFactoryBean;
import org.springframework.data.gemfire.client.ClientRegionFactoryBean;
import org.springframework.data.gemfire.config.xml.GemfireConstants; import org.springframework.data.gemfire.config.xml.GemfireConstants;
import org.springframework.data.gemfire.support.ConnectionEndpoint; import org.springframework.data.gemfire.support.ConnectionEndpoint;
import org.springframework.data.gemfire.util.CollectionUtils; import org.springframework.data.gemfire.util.CollectionUtils;
@@ -55,10 +64,13 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap; import org.springframework.ui.ModelMap;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/** /**
* A Spring Boot, GemFire cache client, web application that reveals the current state of the HTTP Session. * A Spring Boot, GemFire cache client, web application that reveals the current state of the HTTP Session.
@@ -76,15 +88,16 @@ import org.springframework.web.bind.annotation.ResponseBody;
*/ */
// tag::class[] // tag::class[]
@SpringBootApplication @SpringBootApplication
@EnableGemFireHttpSession(poolName = "DEFAULT")// <1>
@Controller @Controller
@EnableGemFireHttpSession(poolName = "DEFAULT")// <1>
@SuppressWarnings("unused")
public class Application { public class Application {
static final long DEFAULT_TIMEOUT = TimeUnit.SECONDS.toMillis(60); static final long DEFAULT_TIMEOUT = TimeUnit.SECONDS.toMillis(60);
static final CountDownLatch LATCH = new CountDownLatch(1); 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 GEMFIRE_DEFAULT_POOL_NAME = "DEFAULT"; static final String GEMFIRE_DEFAULT_POOL_NAME = "DEFAULT";
static final String INDEX_TEMPLATE_VIEW_NAME = "index"; static final String INDEX_TEMPLATE_VIEW_NAME = "index";
static final String PING_RESPONSE = "PONG"; static final String PING_RESPONSE = "PONG";
@@ -94,166 +107,216 @@ public class Application {
SpringApplication.run(Application.class, args); SpringApplication.run(Application.class, args);
} }
@Bean @Configuration
static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() { static class GemFireConfiguration {
return new PropertySourcesPlaceholderConfigurer();
}
Properties gemfireProperties() { // <2> @Bean
Properties gemfireProperties = new Properties(); static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
gemfireProperties.setProperty("name", applicationName()); Properties gemfireProperties() { // <2>
//gemfireProperties.setProperty("log-file", "gemfire-client.log");
gemfireProperties.setProperty("log-level", logLevel());
return gemfireProperties; Properties gemfireProperties = new Properties();
}
String applicationName() { gemfireProperties.setProperty("name", applicationName());
return "spring-session-data-gemfire-boot-sample.".concat(getClass().getSimpleName()); //gemfireProperties.setProperty("log-file", "gemfire-client.log");
} gemfireProperties.setProperty("log-level", logLevel());
String logLevel() { return gemfireProperties;
return System.getProperty("spring-session-data-gemfire.log.level", DEFAULT_GEMFIRE_LOG_LEVEL); }
}
@Bean String applicationName() {
ClientCacheFactoryBean gemfireCache( return "spring-session-data-gemfire-boot-sample.".concat(getClass().getSimpleName());
@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(); String logLevel() {
return System.getProperty("spring-session-data-gemfire.log.level", DEFAULT_GEMFIRE_LOG_LEVEL);
}
gemfireCache.setClose(true); @Bean
gemfireCache.setProperties(gemfireProperties()); ClientCacheFactoryBean gemfireCache(
@Value("${spring-session-data-gemfire.cache.server.host:localhost}") String host,
@Value("${spring-session-data-gemfire.cache.server.port:40404}") int port) { // <3>
// GemFire Pool settings <4> ClientCacheFactoryBean gemfireCache = new ClientCacheFactoryBean();
gemfireCache.setKeepAlive(false);
gemfireCache.setPingInterval(TimeUnit.SECONDS.toMillis(5));
gemfireCache.setReadTimeout(intValue(TimeUnit.SECONDS.toMillis(15)));
gemfireCache.setRetryAttempts(1);
gemfireCache.setSubscriptionEnabled(true);
gemfireCache.setThreadLocalConnections(false);
gemfireCache.setServers(Collections.singletonList(newConnectionEndpoint(host, port)));
registerClientMembershipListener(); // <5> gemfireCache.setClose(true);
gemfireCache.setProperties(gemfireProperties());
return gemfireCache; // GemFire Pool settings <4>
} gemfireCache.setKeepAlive(false);
gemfireCache.setPingInterval(TimeUnit.SECONDS.toMillis(5));
gemfireCache.setReadTimeout(intValue(TimeUnit.SECONDS.toMillis(15)));
gemfireCache.setRetryAttempts(1);
gemfireCache.setSubscriptionEnabled(true);
gemfireCache.setThreadLocalConnections(false);
gemfireCache.setServers(Collections.singletonList(newConnectionEndpoint(host, port)));
int intValue(Number number) { registerClientMembershipListener(); // <5>
return number.intValue();
}
ConnectionEndpoint newConnectionEndpoint(String host, int port) { return gemfireCache;
return new ConnectionEndpoint(host, port); }
}
void registerClientMembershipListener() { int intValue(Number number) {
ClientMembership.registerClientMembershipListener(new ClientMembershipListenerAdapter() { return number.intValue();
@Override }
public void memberJoined(ClientMembershipEvent event) {
LATCH.countDown();
}
});
}
@Bean ConnectionEndpoint newConnectionEndpoint(String host, int port) {
BeanPostProcessor gemfireClientServerReadyBeanPostProcessor( return new ConnectionEndpoint(host, port);
@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() { void registerClientMembershipListener() {
private final AtomicBoolean checkGemFireServerIsRunning = new AtomicBoolean(true); ClientMembership.registerClientMembershipListener(new ClientMembershipListenerAdapter() {
private final AtomicReference<Pool> defaultPool = new AtomicReference<Pool>(null);
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { @Override
if (shouldCheckWhetherGemFireServerIsRunning(bean, beanName)) { public void memberJoined(ClientMembershipEvent event) {
try { LATCH.countDown();
validateCacheClientNotified(); }
validateCacheClientSubscriptionQueueConnectionEstablished(); });
} }
catch (InterruptedException e) {
Thread.currentThread().interrupt(); @Bean(name = "Example")
ClientRegionFactoryBean<String, Object> exampleRegion(ClientCache gemfireCache) {
ClientRegionFactoryBean<String, Object> exampleRegionFactory = new ClientRegionFactoryBean<>();
exampleRegionFactory.setCache(gemfireCache);
exampleRegionFactory.setClose(false);
exampleRegionFactory.setShortcut(ClientRegionShortcut.PROXY);
return exampleRegionFactory;
}
@Bean
BeanPostProcessor gemfireClientServerReadyBeanPostProcessor(
@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() {
private final AtomicBoolean checkGemFireServerIsRunning = new AtomicBoolean(true);
private final AtomicReference<Pool> defaultPool = new AtomicReference<>(null);
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (shouldCheckWhetherGemFireServerIsRunning(bean, beanName)) {
try {
validateCacheClientNotified();
validateCacheClientSubscriptionQueueConnectionEstablished();
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} }
return bean;
} }
return bean; private boolean shouldCheckWhetherGemFireServerIsRunning(Object bean, String beanName) {
}
private boolean shouldCheckWhetherGemFireServerIsRunning(Object bean, String beanName) { return (isGemFireRegion(bean, beanName)
return (isGemFireRegion(bean, beanName) ? checkGemFireServerIsRunning.compareAndSet(true, false)
? checkGemFireServerIsRunning.compareAndSet(true, false) : whenGemFireCache(bean, beanName));
: whenGemFireCache(bean, beanName));
}
private boolean isGemFireRegion(Object bean, String beanName) {
return (GemFireHttpSessionConfiguration.DEFAULT_SPRING_SESSION_GEMFIRE_REGION_NAME.equals(beanName)
|| bean instanceof Region);
}
private boolean whenGemFireCache(Object bean, String beanName) {
if (bean instanceof ClientCache) {
defaultPool.compareAndSet(null, ((ClientCache) bean).getDefaultPool());
} }
return false; private boolean isGemFireRegion(Object bean, String beanName) {
}
private void validateCacheClientNotified() throws InterruptedException { return (GemFireHttpSessionConfiguration.DEFAULT_SPRING_SESSION_GEMFIRE_REGION_NAME.equals(beanName)
boolean didNotTimeout = LATCH.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS); || bean instanceof Region);
}
Assert.state(didNotTimeout, String.format( private boolean whenGemFireCache(Object bean, String beanName) {
"GemFire Cache Server failed to start on host [%s] and port [%d]", host, port));
}
@SuppressWarnings("all") if (bean instanceof ClientCache) {
private void validateCacheClientSubscriptionQueueConnectionEstablished() throws InterruptedException { defaultPool.compareAndSet(null, ((ClientCache) bean).getDefaultPool());
boolean cacheClientSubscriptionQueueConnectionEstablished = false; }
Pool pool = defaultIfNull(this.defaultPool.get(), GemfireConstants.DEFAULT_GEMFIRE_POOL_NAME, return false;
GEMFIRE_DEFAULT_POOL_NAME); }
if (pool instanceof PoolImpl) { private void validateCacheClientNotified() throws InterruptedException {
long timeout = (System.currentTimeMillis() + DEFAULT_TIMEOUT);
while (System.currentTimeMillis() < timeout boolean didNotTimeout = LATCH.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
&& !((PoolImpl) pool).isPrimaryUpdaterAlive()) {
Assert.state(didNotTimeout, String.format(
"GemFire Cache Server failed to start on host [%s] and port [%d]", host, port));
}
@SuppressWarnings("all")
private void validateCacheClientSubscriptionQueueConnectionEstablished() throws InterruptedException {
boolean cacheClientSubscriptionQueueConnectionEstablished = false;
Pool pool = defaultIfNull(this.defaultPool.get(), GemfireConstants.DEFAULT_GEMFIRE_POOL_NAME,
GEMFIRE_DEFAULT_POOL_NAME);
if (pool instanceof PoolImpl) {
long timeout = (System.currentTimeMillis() + DEFAULT_TIMEOUT);
while (System.currentTimeMillis() < timeout
&& !((PoolImpl) pool).isPrimaryUpdaterAlive()) {
synchronized (pool) {
TimeUnit.MILLISECONDS.timedWait(pool, 500L);
}
synchronized (pool) {
TimeUnit.MILLISECONDS.timedWait(pool, 500L);
} }
cacheClientSubscriptionQueueConnectionEstablished |=
((PoolImpl) pool).isPrimaryUpdaterAlive();
} }
cacheClientSubscriptionQueueConnectionEstablished |= Assert.state(cacheClientSubscriptionQueueConnectionEstablished, String.format(
((PoolImpl) pool).isPrimaryUpdaterAlive(); "Cache client subscription queue connection not established; GemFire Pool was [%s];"
+ " GemFire Pool configuration was [locators = %s, servers = %s]",
pool, pool.getLocators(), pool.getServers()));
} }
Assert.state(cacheClientSubscriptionQueueConnectionEstablished, String.format( private Pool defaultIfNull(Pool pool, String... poolNames) {
"Cache client subscription queue connection not established; GemFire Pool was [%s];"
+ " GemFire Pool configuration was [locators = %s, servers = %s]",
pool, pool.getLocators(), pool.getServers()));
}
private Pool defaultIfNull(Pool pool, String... poolNames) { AtomicReference<Pool> poolRef = new AtomicReference<>(null);
for (String poolName : poolNames) {
pool = (pool != null ? pool : PoolManager.find(poolName)); stream(nullSafeArray(poolNames, String.class)).forEach(poolName ->
poolRef.set(Optional.ofNullable(pool).orElseGet(() -> PoolManager.find(poolName))));
return poolRef.get();
} }
return pool; public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
} return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { };
return bean; }
}
};
} }
@RequestMapping("/") @Configuration
public String index() { // <6> static class SpringWebMvcConfiguration {
return INDEX_TEMPLATE_VIEW_NAME;
@Bean
public WebMvcConfigurer webMvcConfig() { // <6>
return new WebMvcConfigurer() {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName(INDEX_TEMPLATE_VIEW_NAME);
}
};
}
}
@Resource(name = "Example")
private Region<String, Object> example;
@ExceptionHandler
@ResponseBody
public String errorHandler(Throwable error) {
StringWriter writer = new StringWriter();
error.printStackTrace(new PrintWriter(writer));
return writer.toString();
} }
@RequestMapping(method = RequestMethod.GET, path = "/ping") @RequestMapping(method = RequestMethod.GET, path = "/ping")
@@ -262,6 +325,19 @@ public class Application {
return PING_RESPONSE; return PING_RESPONSE;
} }
@RequestMapping(method = RequestMethod.GET, path = "/time")
@ResponseBody
public String time() {
return String.valueOf(this.example.get("time"));
}
/*
@RequestMapping("/")
public String index() { // <6>
return INDEX_TEMPLATE_VIEW_NAME;
}
*/
@RequestMapping(method = RequestMethod.POST, path = "/session") @RequestMapping(method = RequestMethod.POST, path = "/session")
public String session(HttpSession session, ModelMap modelMap, public String session(HttpSession session, ModelMap modelMap,
@RequestParam(name = "attributeName", required = false) String name, @RequestParam(name = "attributeName", required = false) String name,
@@ -276,6 +352,7 @@ public class Application {
/* (non-Javadoc) */ /* (non-Javadoc) */
@SuppressWarnings("all") @SuppressWarnings("all")
HttpSession updateRequestCount(HttpSession session) { HttpSession updateRequestCount(HttpSession session) {
synchronized (session) { synchronized (session) {
Integer currentRequestCount = (Integer) session.getAttribute(REQUEST_COUNT_ATTRIBUTE_NAME); Integer currentRequestCount = (Integer) session.getAttribute(REQUEST_COUNT_ATTRIBUTE_NAME);
session.setAttribute(REQUEST_COUNT_ATTRIBUTE_NAME, nullSafeIncrement(currentRequestCount)); session.setAttribute(REQUEST_COUNT_ATTRIBUTE_NAME, nullSafeIncrement(currentRequestCount));
@@ -290,11 +367,12 @@ public class Application {
/* (non-Javadoc) */ /* (non-Javadoc) */
int nullSafeIntValue(Number value) { int nullSafeIntValue(Number value) {
return (value != null ? value.intValue() : 0); return Optional.ofNullable(value).map(Number::intValue).orElse(0);
} }
/* (non-Javadoc) */ /* (non-Javadoc) */
HttpSession setAttribute(HttpSession session, String attributeName, String attributeValue) { HttpSession setAttribute(HttpSession session, String attributeName, String attributeValue) {
if (isSet(attributeName, attributeValue)) { if (isSet(attributeName, attributeValue)) {
session.setAttribute(attributeName, attributeValue); session.setAttribute(attributeName, attributeValue);
} }
@@ -304,6 +382,7 @@ public class Application {
/* (non-Javadoc) */ /* (non-Javadoc) */
boolean isSet(String... values) { boolean isSet(String... values) {
boolean set = true; boolean set = true;
for (String value : values) { for (String value : values) {
@@ -315,7 +394,8 @@ public class Application {
/* (non-Javadoc) */ /* (non-Javadoc) */
Map<String, String> attributes(HttpSession session) { Map<String, String> attributes(HttpSession session) {
Map<String, String> sessionAttributes = new HashMap<String, String>();
Map<String, String> sessionAttributes = new HashMap<>();
for (String attributeName : toIterable(session.getAttributeNames())) { for (String attributeName : toIterable(session.getAttributeNames())) {
sessionAttributes.put(attributeName, String.valueOf(session.getAttribute(attributeName))); sessionAttributes.put(attributeName, String.valueOf(session.getAttribute(attributeName)));
@@ -325,12 +405,9 @@ public class Application {
} }
/* (non-Javadoc) */ /* (non-Javadoc) */
<T> Iterable<T> toIterable(final Enumeration<T> enumeration) { <T> Iterable<T> toIterable(Enumeration<T> enumeration) {
return new Iterable<T>() {
public Iterator<T> iterator() { return () -> Optional.ofNullable(enumeration).map(CollectionUtils::toIterator)
return (enumeration == null ? Collections.<T>emptyIterator() .orElseGet(Collections::emptyIterator);
: CollectionUtils.toIterator(enumeration));
}
};
} }
} }

View File

@@ -16,17 +16,26 @@
package sample.server; package sample.server;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.apache.geode.cache.Cache; import javax.annotation.Resource;
import org.apache.geode.cache.Cache;
import org.apache.geode.cache.Region;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.data.gemfire.CacheFactoryBean; import org.springframework.data.gemfire.CacheFactoryBean;
import org.springframework.data.gemfire.ReplicatedRegionFactoryBean;
import org.springframework.data.gemfire.server.CacheServerFactoryBean; import org.springframework.data.gemfire.server.CacheServerFactoryBean;
import org.springframework.session.data.gemfire.config.annotation.web.http.EnableGemFireHttpSession; import org.springframework.session.data.gemfire.config.annotation.web.http.EnableGemFireHttpSession;
@@ -44,13 +53,14 @@ import org.springframework.session.data.gemfire.config.annotation.web.http.Enabl
// tag::class[] // tag::class[]
@SpringBootApplication @SpringBootApplication
@EnableGemFireHttpSession(maxInactiveIntervalInSeconds = 20) // <1> @EnableGemFireHttpSession(maxInactiveIntervalInSeconds = 20) // <1>
@SuppressWarnings("unused")
public class GemFireServer { public class GemFireServer {
static final String DEFAULT_GEMFIRE_LOG_LEVEL = "warning"; static final String DEFAULT_GEMFIRE_LOG_LEVEL = "config";
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(GemFireServer.class); SpringApplication springApplication = new SpringApplication(GemFireServer.class);
springApplication.setWebEnvironment(false); springApplication.setWebApplicationType(WebApplicationType.NONE);
springApplication.run(args); springApplication.run(args);
} }
@@ -60,13 +70,14 @@ public class GemFireServer {
} }
Properties gemfireProperties() { // <2> Properties gemfireProperties() { // <2>
Properties gemfireProperties = new Properties(); Properties gemfireProperties = new Properties();
gemfireProperties.setProperty("name", applicationName()); gemfireProperties.setProperty("name", applicationName());
//gemfireProperties.setProperty("log-file", "gemfire-server.log"); //gemfireProperties.setProperty("log-file", "gemfire-server.log");
gemfireProperties.setProperty("log-level", logLevel()); gemfireProperties.setProperty("log-level", logLevel());
//gemfireProperties.setProperty("jmx-manager", "true"); gemfireProperties.setProperty("jmx-manager", "true");
//gemfireProperties.setProperty("jmx-manager-start", "true"); gemfireProperties.setProperty("jmx-manager-start", "true");
return gemfireProperties; return gemfireProperties;
} }
@@ -81,6 +92,7 @@ public class GemFireServer {
@Bean @Bean
CacheFactoryBean gemfireCache() { // <3> CacheFactoryBean gemfireCache() { // <3>
CacheFactoryBean gemfireCache = new CacheFactoryBean(); CacheFactoryBean gemfireCache = new CacheFactoryBean();
gemfireCache.setClose(true); gemfireCache.setClose(true);
@@ -93,7 +105,7 @@ public class GemFireServer {
CacheServerFactoryBean gemfireCacheServer(Cache gemfireCache, CacheServerFactoryBean gemfireCacheServer(Cache gemfireCache,
@Value("${spring-session-data-gemfire.cache.server.bind-address:localhost}") String bindAddress, @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.hostname-for-clients:localhost}") String hostnameForClients,
@Value("${spring-session-data-gemfire.cache.server.port:12480}") int port) { // <4> @Value("${spring-session-data-gemfire.cache.server.port:40404}") int port) { // <4>
CacheServerFactoryBean gemfireCacheServer = new CacheServerFactoryBean(); CacheServerFactoryBean gemfireCacheServer = new CacheServerFactoryBean();
@@ -106,5 +118,30 @@ public class GemFireServer {
return gemfireCacheServer; return gemfireCacheServer;
} }
@Bean(name = "Example")
ReplicatedRegionFactoryBean<String, Object> exampleRegion(Cache gemfireCache) {
ReplicatedRegionFactoryBean<String, Object> exampleRegionFactory = new ReplicatedRegionFactoryBean<>();
exampleRegionFactory.setCache(gemfireCache);
exampleRegionFactory.setClose(false);
return exampleRegionFactory;
}
@Resource(name = "Example")
private Region<String, Object> example;
@Bean
CommandLineRunner exampleInitializer() {
return args -> {
String key = "time";
example.put(key, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:ss a")));
System.err.printf("Example.get(%s) is [%s]%n", key, example.get(key));
};
}
} }
// end::class[] // end::class[]

View File

@@ -0,0 +1,2 @@
#server.error.whitelabel.enabled=false
spring.thymeleaf.prefix=classpath*:/templates/