diff --git a/spring-geode-samples/caching/http-session/spring-geode-samples-caching-httpsession.gradle b/spring-geode-samples/caching/http-session/spring-geode-samples-caching-httpsession.gradle new file mode 100644 index 00000000..c32603dd --- /dev/null +++ b/spring-geode-samples/caching/http-session/spring-geode-samples-caching-httpsession.gradle @@ -0,0 +1,33 @@ +apply plugin: 'io.spring.convention.spring-sample-boot' + +description = "Spring Geode Sample demonstrating Spring Session with Apache Geode for HTTP Session Caching." + +dependencies { + + compile project(":spring-geode-starter") + + compile ("org.springframework.boot:spring-boot-starter-web") { + exclude group: "org.apache.logging.log4j", module: "log4j-to-slf4j" + } + +// runtime project(":spring-geode-starter-session") + + runtime("org.springframework.boot:spring-boot-starter-thymeleaf") { + exclude group: "org.apache.logging.log4j", module: "log4j-to-slf4j" + } + + runtime ("org.springframework.boot:spring-boot-starter-tomcat") { + exclude group: "org.apache.logging.log4j", module: "log4j-to-slf4j" + } + + testCompile("org.springframework.boot:spring-boot-starter-test") { + exclude group: "org.apache.logging.log4j", module: "log4j-to-slf4j" + } + + testCompile "org.springframework.data:spring-data-geode-test" + +} + +bootJar { + mainClassName = 'example.app.caching.lookaside.BootGeodeHttpSessionCachingApplication' +} diff --git a/spring-geode-samples/caching/http-session/src/main/java/example/app/caching/session/http/BootGeodeHttpSessionCachingApplication.java b/spring-geode-samples/caching/http-session/src/main/java/example/app/caching/session/http/BootGeodeHttpSessionCachingApplication.java new file mode 100644 index 00000000..243bacb6 --- /dev/null +++ b/spring-geode-samples/caching/http-session/src/main/java/example/app/caching/session/http/BootGeodeHttpSessionCachingApplication.java @@ -0,0 +1,35 @@ +/* + * Copyright 2019 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 + * + * https://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 example.app.caching.session.http; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * {@link SpringBootApplication} to demo HTTP Session state caching. + * + * @author John Blum + * @see org.springframework.boot.SpringApplication + * @see org.springframework.boot.autoconfigure.SpringBootApplication + * @since 1.1.0 + */ +@SpringBootApplication +public class BootGeodeHttpSessionCachingApplication { + + public static void main(String[] args) { + SpringApplication.run(BootGeodeHttpSessionCachingApplication.class, args); + } +} diff --git a/spring-geode-samples/caching/http-session/src/main/java/example/app/caching/session/http/controller/CounterController.java b/spring-geode-samples/caching/http-session/src/main/java/example/app/caching/session/http/controller/CounterController.java new file mode 100644 index 00000000..9316c285 --- /dev/null +++ b/spring-geode-samples/caching/http-session/src/main/java/example/app/caching/session/http/controller/CounterController.java @@ -0,0 +1,93 @@ +/* + * Copyright 2019 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 + * + * https://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 example.app.caching.session.http.controller; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import javax.servlet.http.HttpSession; + +import org.apache.geode.internal.concurrent.ConcurrentHashSet; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.servlet.ModelAndView; + +/** + * Spring Web MVC {@link Controller} used to count the number of HTTP requests per HTTP Session + * and the number of HTTP Sessions. + * + * @author John Blum + * @see javax.servlet.http.HttpSession + * @see org.springframework.stereotype.Controller + * @see org.springframework.web.bind.annotation.GetMapping + * @see org.springframework.web.bind.annotation.ResponseBody + * @see org.springframework.web.servlet.ModelAndView + * @since 1.1.0 + */ +//tag::class[] +@Controller +public class CounterController { + + public static final String INDEX_TEMPLATE_VIEW_NAME = "index"; + + private final Set sessionIds = new ConcurrentHashSet<>(); + + @GetMapping("/") + @ResponseBody + public String home() { + return format("HTTP Session Caching Example"); + } + + @GetMapping("/ping") + @ResponseBody + public String ping() { + return format("PONG"); + } + + @GetMapping("/session") + public ModelAndView sessionRequestCounts(HttpSession session) { + + this.sessionIds.add(session.getId()); + + Map model = new HashMap<>(); + + model.put("sessionType", session.getClass().getName()); + model.put("sessionCount", this.sessionIds.size()); + model.put("requestCount", getRequestCount(session)); + + return new ModelAndView(INDEX_TEMPLATE_VIEW_NAME, model); + } + + private Object getRequestCount(HttpSession session) { + + Integer requestCount = (Integer) session.getAttribute("requestCount"); + + requestCount = requestCount != null ? requestCount : 0; + requestCount++; + + session.setAttribute("requestCount", requestCount); + + return requestCount; + } + + private String format(String value) { + return String.format("

%s

", value); + } +} +//end::class[] diff --git a/spring-geode-samples/caching/http-session/src/main/resources/application.properties b/spring-geode-samples/caching/http-session/src/main/resources/application.properties new file mode 100644 index 00000000..22a44763 --- /dev/null +++ b/spring-geode-samples/caching/http-session/src/main/resources/application.properties @@ -0,0 +1,8 @@ +# Spring Boot application.properties for the HTTP Session State Caching Example application. + +spring.application.name=HttpSessionCachingApplication +spring.data.gemfire.cache.log-level=error +spring.session.data.gemfire.cache.client.pool.name=DEFAULT +spring.session.data.gemfire.cache.client.region.shortcut=LOCAL +spring.session.data.gemfire.session.region.name=Sessions +server.servlet.session.timeout=15 diff --git a/spring-geode-samples/caching/http-session/src/main/resources/templates/index.html b/spring-geode-samples/caching/http-session/src/main/resources/templates/index.html new file mode 100644 index 00000000..de1028f2 --- /dev/null +++ b/spring-geode-samples/caching/http-session/src/main/resources/templates/index.html @@ -0,0 +1,31 @@ + + + + HTTP Session Details + + + + +
+ + + + + + + + + + + + + + + +
HttpSession TypeHttpSession CountHttpServletRequest Count
valuevaluevalue
+
+