From 6064dffe4a4628585ce6a60ebebcae1a7cd9263c Mon Sep 17 00:00:00 2001 From: John Blum Date: Wed, 7 Jun 2017 20:37:37 -0700 Subject: [PATCH] Polish along with debugging changes. --- .../spring-session-sample-boot-gemfire.gradle | 4 +- .../java/sample/AttributeTests.java | 22 +- .../java/sample/pages/HomePage.java | 71 ++-- .../main/java/sample/client/Application.java | 337 +++++++++++------- .../java/sample/server/GemFireServer.java | 49 ++- .../src/main/resources/application.properties | 2 + 6 files changed, 313 insertions(+), 172 deletions(-) create mode 100644 samples/boot/gemfire/src/main/resources/application.properties diff --git a/samples/boot/gemfire/spring-session-sample-boot-gemfire.gradle b/samples/boot/gemfire/spring-session-sample-boot-gemfire.gradle index abc7185..29b5282 100644 --- a/samples/boot/gemfire/spring-session-sample-boot-gemfire.gradle +++ b/samples/boot/gemfire/spring-session-sample-boot-gemfire.gradle @@ -54,8 +54,8 @@ task runGemFireServer() { String[] commandLine = [ 'java', '-server', '-ea', '-classpath', classpath, - //"-Dgemfire.log-file=gemfire-server.log", - //"-Dgemfire.log-level=config", + "-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', 'warning'), diff --git a/samples/boot/gemfire/src/integration-test/java/sample/AttributeTests.java b/samples/boot/gemfire/src/integration-test/java/sample/AttributeTests.java index 4ca02aa..c89da36 100644 --- a/samples/boot/gemfire/src/integration-test/java/sample/AttributeTests.java +++ b/samples/boot/gemfire/src/integration-test/java/sample/AttributeTests.java @@ -61,18 +61,22 @@ public class AttributeTests { @Test 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 public void createAttribute() { - HomePage home = HomePage.go(this.driver, HomePage.class); - home = home.form() - .attributeName("a") - .attributeValue("b") - .submit(HomePage.class); - assertThat(home.attributes()).extracting("attributeName").containsOnly("requestCount", "a"); - assertThat(home.attributes()).extracting("attributeValue").containsOnly("1", "b"); + + HomePage homePage = HomePage.go(this.driver, HomePage.class); + + homePage = homePage.assertAt().form().attributeName("a").attributeValue("b").submit(HomePage.class); + + assertThat(homePage.attributes()).extracting("attributeName").containsOnly("requestCount", "a"); + assertThat(homePage.attributes()).extracting("attributeValue").containsOnly("1", "b"); } } diff --git a/samples/boot/gemfire/src/integration-test/java/sample/pages/HomePage.java b/samples/boot/gemfire/src/integration-test/java/sample/pages/HomePage.java index 0f43fda..6774366 100644 --- a/samples/boot/gemfire/src/integration-test/java/sample/pages/HomePage.java +++ b/samples/boot/gemfire/src/integration-test/java/sample/pages/HomePage.java @@ -16,6 +16,8 @@ package sample.pages; +import static org.assertj.core.api.Assertions.assertThat; + import java.util.ArrayList; import java.util.List; 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.DefaultElementLocatorFactory; -import static org.assertj.core.api.Assertions.assertThat; - /** * @author EddĂș MelĂ©ndez * @author Rob Winch */ +@SuppressWarnings("unused") public class HomePage { private WebDriver driver; + @FindBy(tagName = "title") + WebElement title; + @FindBy(tagName = "form") WebElement form; @@ -49,19 +53,28 @@ public class HomePage { public HomePage(WebDriver driver) { this.driver = driver; - 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); + this.attributes = new ArrayList<>(); } public static T go(WebDriver driver, Class page) { + get(driver, "/"); + 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) { Set cookies = this.driver.manage().getCookies(); assertThat(cookies).extracting("name").contains(cookieName); @@ -72,27 +85,35 @@ public class HomePage { 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 attributes() { - List rows = new ArrayList(); - for (WebElement tr : this.trs) { - rows.add(new Attribute(tr)); - } - this.attributes.addAll(rows); - return this.attributes; - } - public Form form() { return new Form(this.form); } + public List attributes() { + + List 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 { + @FindBy(name = "attributeName") WebElement attributeName; @@ -123,6 +144,7 @@ public class HomePage { } public static class Attribute { + @FindBy(xpath = ".//td[1]") WebElement attributeName; @@ -147,5 +169,4 @@ public class HomePage { return this.attributeValue.getText(); } } - } 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 b296d2a..3b3b53f 100644 --- a/samples/boot/gemfire/src/main/java/sample/client/Application.java +++ b/samples/boot/gemfire/src/main/java/sample/client/Application.java @@ -16,21 +16,28 @@ 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.Enumeration; import java.util.HashMap; -import java.util.Iterator; import java.util.Map; +import java.util.Optional; import java.util.Properties; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; +import javax.annotation.Resource; import javax.servlet.http.HttpSession; import org.apache.geode.cache.Region; 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.PoolManager; 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.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; 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.support.ConnectionEndpoint; import org.springframework.data.gemfire.util.CollectionUtils; @@ -55,10 +64,13 @@ import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.util.Assert; 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.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; 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. @@ -76,15 +88,16 @@ import org.springframework.web.bind.annotation.ResponseBody; */ // tag::class[] @SpringBootApplication -@EnableGemFireHttpSession(poolName = "DEFAULT")// <1> @Controller +@EnableGemFireHttpSession(poolName = "DEFAULT")// <1> +@SuppressWarnings("unused") public class Application { static final long DEFAULT_TIMEOUT = TimeUnit.SECONDS.toMillis(60); 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 INDEX_TEMPLATE_VIEW_NAME = "index"; static final String PING_RESPONSE = "PONG"; @@ -94,166 +107,216 @@ public class Application { SpringApplication.run(Application.class, args); } - @Bean - static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() { - return new PropertySourcesPlaceholderConfigurer(); - } + @Configuration + static class GemFireConfiguration { - Properties gemfireProperties() { // <2> - Properties gemfireProperties = new Properties(); + @Bean + static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() { + return new PropertySourcesPlaceholderConfigurer(); + } - gemfireProperties.setProperty("name", applicationName()); - //gemfireProperties.setProperty("log-file", "gemfire-client.log"); - gemfireProperties.setProperty("log-level", logLevel()); + Properties gemfireProperties() { // <2> - return gemfireProperties; - } + Properties gemfireProperties = new Properties(); - String applicationName() { - return "spring-session-data-gemfire-boot-sample.".concat(getClass().getSimpleName()); - } + gemfireProperties.setProperty("name", applicationName()); + //gemfireProperties.setProperty("log-file", "gemfire-client.log"); + gemfireProperties.setProperty("log-level", logLevel()); - String logLevel() { - return System.getProperty("spring-session-data-gemfire.log.level", DEFAULT_GEMFIRE_LOG_LEVEL); - } + return gemfireProperties; + } - @Bean - ClientCacheFactoryBean gemfireCache( - @Value("${spring-session-data-gemfire.cache.server.host:localhost}") String host, - @Value("${spring-session-data-gemfire.cache.server.port:12480}") int port) { // <3> + String applicationName() { + return "spring-session-data-gemfire-boot-sample.".concat(getClass().getSimpleName()); + } - ClientCacheFactoryBean gemfireCache = new ClientCacheFactoryBean(); + String logLevel() { + return System.getProperty("spring-session-data-gemfire.log.level", DEFAULT_GEMFIRE_LOG_LEVEL); + } - gemfireCache.setClose(true); - gemfireCache.setProperties(gemfireProperties()); + @Bean + 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> - 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))); + ClientCacheFactoryBean gemfireCache = new ClientCacheFactoryBean(); - 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) { - return number.intValue(); - } + registerClientMembershipListener(); // <5> - ConnectionEndpoint newConnectionEndpoint(String host, int port) { - return new ConnectionEndpoint(host, port); - } + return gemfireCache; + } - void registerClientMembershipListener() { - ClientMembership.registerClientMembershipListener(new ClientMembershipListenerAdapter() { - @Override - public void memberJoined(ClientMembershipEvent event) { - LATCH.countDown(); - } - }); - } + int intValue(Number number) { + return number.intValue(); + } - @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> + ConnectionEndpoint newConnectionEndpoint(String host, int port) { + return new ConnectionEndpoint(host, port); + } - return new BeanPostProcessor() { + void registerClientMembershipListener() { - private final AtomicBoolean checkGemFireServerIsRunning = new AtomicBoolean(true); - private final AtomicReference defaultPool = new AtomicReference(null); + ClientMembership.registerClientMembershipListener(new ClientMembershipListenerAdapter() { - public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { - if (shouldCheckWhetherGemFireServerIsRunning(bean, beanName)) { - try { - validateCacheClientNotified(); - validateCacheClientSubscriptionQueueConnectionEstablished(); - } - catch (InterruptedException e) { - Thread.currentThread().interrupt(); + @Override + public void memberJoined(ClientMembershipEvent event) { + LATCH.countDown(); + } + }); + } + + @Bean(name = "Example") + ClientRegionFactoryBean exampleRegion(ClientCache gemfireCache) { + + ClientRegionFactoryBean 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 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) - ? checkGemFireServerIsRunning.compareAndSet(true, false) - : 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 (isGemFireRegion(bean, beanName) + ? checkGemFireServerIsRunning.compareAndSet(true, false) + : whenGemFireCache(bean, beanName)); } - return false; - } + private boolean isGemFireRegion(Object bean, String beanName) { - private void validateCacheClientNotified() throws InterruptedException { - boolean didNotTimeout = LATCH.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS); + return (GemFireHttpSessionConfiguration.DEFAULT_SPRING_SESSION_GEMFIRE_REGION_NAME.equals(beanName) + || bean instanceof Region); + } - Assert.state(didNotTimeout, String.format( - "GemFire Cache Server failed to start on host [%s] and port [%d]", host, port)); - } + private boolean whenGemFireCache(Object bean, String beanName) { - @SuppressWarnings("all") - private void validateCacheClientSubscriptionQueueConnectionEstablished() throws InterruptedException { - boolean cacheClientSubscriptionQueueConnectionEstablished = false; + if (bean instanceof ClientCache) { + defaultPool.compareAndSet(null, ((ClientCache) bean).getDefaultPool()); + } - Pool pool = defaultIfNull(this.defaultPool.get(), GemfireConstants.DEFAULT_GEMFIRE_POOL_NAME, - GEMFIRE_DEFAULT_POOL_NAME); + return false; + } - if (pool instanceof PoolImpl) { - long timeout = (System.currentTimeMillis() + DEFAULT_TIMEOUT); + private void validateCacheClientNotified() throws InterruptedException { - while (System.currentTimeMillis() < timeout - && !((PoolImpl) pool).isPrimaryUpdaterAlive()) { + boolean didNotTimeout = LATCH.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS); + + 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 |= - ((PoolImpl) pool).isPrimaryUpdaterAlive(); + Assert.state(cacheClientSubscriptionQueueConnectionEstablished, String.format( + "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( - "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) { - private Pool defaultIfNull(Pool pool, String... poolNames) { - for (String poolName : poolNames) { - pool = (pool != null ? pool : PoolManager.find(poolName)); + AtomicReference poolRef = new AtomicReference<>(null); + + 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("/") - public String index() { // <6> - return INDEX_TEMPLATE_VIEW_NAME; + @Configuration + static class SpringWebMvcConfiguration { + + @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 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") @@ -262,6 +325,19 @@ public class Application { 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") public String session(HttpSession session, ModelMap modelMap, @RequestParam(name = "attributeName", required = false) String name, @@ -276,6 +352,7 @@ public class Application { /* (non-Javadoc) */ @SuppressWarnings("all") HttpSession updateRequestCount(HttpSession session) { + synchronized (session) { Integer currentRequestCount = (Integer) session.getAttribute(REQUEST_COUNT_ATTRIBUTE_NAME); session.setAttribute(REQUEST_COUNT_ATTRIBUTE_NAME, nullSafeIncrement(currentRequestCount)); @@ -290,11 +367,12 @@ public class Application { /* (non-Javadoc) */ int nullSafeIntValue(Number value) { - return (value != null ? value.intValue() : 0); + return Optional.ofNullable(value).map(Number::intValue).orElse(0); } /* (non-Javadoc) */ HttpSession setAttribute(HttpSession session, String attributeName, String attributeValue) { + if (isSet(attributeName, attributeValue)) { session.setAttribute(attributeName, attributeValue); } @@ -304,6 +382,7 @@ public class Application { /* (non-Javadoc) */ boolean isSet(String... values) { + boolean set = true; for (String value : values) { @@ -315,7 +394,8 @@ public class Application { /* (non-Javadoc) */ Map attributes(HttpSession session) { - Map sessionAttributes = new HashMap(); + + Map sessionAttributes = new HashMap<>(); for (String attributeName : toIterable(session.getAttributeNames())) { sessionAttributes.put(attributeName, String.valueOf(session.getAttribute(attributeName))); @@ -325,12 +405,9 @@ public class Application { } /* (non-Javadoc) */ - Iterable toIterable(final Enumeration enumeration) { - return new Iterable() { - public Iterator iterator() { - return (enumeration == null ? Collections.emptyIterator() - : CollectionUtils.toIterator(enumeration)); - } - }; + Iterable toIterable(Enumeration enumeration) { + + return () -> Optional.ofNullable(enumeration).map(CollectionUtils::toIterator) + .orElseGet(Collections::emptyIterator); } } 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 0f65756..9a7e97e 100644 --- a/samples/boot/gemfire/src/main/java/sample/server/GemFireServer.java +++ b/samples/boot/gemfire/src/main/java/sample/server/GemFireServer.java @@ -16,17 +16,26 @@ package sample.server; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.Properties; 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.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; +import org.springframework.boot.WebApplicationType; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.data.gemfire.CacheFactoryBean; +import org.springframework.data.gemfire.ReplicatedRegionFactoryBean; import org.springframework.data.gemfire.server.CacheServerFactoryBean; 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[] @SpringBootApplication @EnableGemFireHttpSession(maxInactiveIntervalInSeconds = 20) // <1> +@SuppressWarnings("unused") 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) { SpringApplication springApplication = new SpringApplication(GemFireServer.class); - springApplication.setWebEnvironment(false); + springApplication.setWebApplicationType(WebApplicationType.NONE); springApplication.run(args); } @@ -60,13 +70,14 @@ public class GemFireServer { } Properties gemfireProperties() { // <2> + Properties gemfireProperties = new Properties(); gemfireProperties.setProperty("name", applicationName()); //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; } @@ -81,6 +92,7 @@ public class GemFireServer { @Bean CacheFactoryBean gemfireCache() { // <3> + CacheFactoryBean gemfireCache = new CacheFactoryBean(); gemfireCache.setClose(true); @@ -93,7 +105,7 @@ public class GemFireServer { CacheServerFactoryBean gemfireCacheServer(Cache gemfireCache, @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> + @Value("${spring-session-data-gemfire.cache.server.port:40404}") int port) { // <4> CacheServerFactoryBean gemfireCacheServer = new CacheServerFactoryBean(); @@ -106,5 +118,30 @@ public class GemFireServer { return gemfireCacheServer; } + + @Bean(name = "Example") + ReplicatedRegionFactoryBean exampleRegion(Cache gemfireCache) { + + ReplicatedRegionFactoryBean exampleRegionFactory = new ReplicatedRegionFactoryBean<>(); + + exampleRegionFactory.setCache(gemfireCache); + exampleRegionFactory.setClose(false); + + return exampleRegionFactory; + } + + @Resource(name = "Example") + private Region 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[] diff --git a/samples/boot/gemfire/src/main/resources/application.properties b/samples/boot/gemfire/src/main/resources/application.properties new file mode 100644 index 0000000..261ef19 --- /dev/null +++ b/samples/boot/gemfire/src/main/resources/application.properties @@ -0,0 +1,2 @@ +#server.error.whitelabel.enabled=false +spring.thymeleaf.prefix=classpath*:/templates/