Add Sample Code for HTTP Session Caching.
This commit is contained in:
@@ -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'
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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<String> 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<String, Object> 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("<h1>%s</h1>", value);
|
||||
}
|
||||
}
|
||||
//end::class[]
|
||||
@@ -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
|
||||
@@ -0,0 +1,31 @@
|
||||
<!DOCTYPE html SYSTEM "https://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-3.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
|
||||
<head>
|
||||
<title>HTTP Session Details</title>
|
||||
<link rel="stylesheet" th:href="@{/webjars/bootstrap/css/bootstrap.min.css}" href="/webjars/bootstrap/css/bootstrap.min.css"/>
|
||||
<style type="text/css">
|
||||
body {
|
||||
padding: 1em;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<table class="table table-striped" border="1">
|
||||
<thead align="left">
|
||||
<tr>
|
||||
<th>HttpSession Type</th>
|
||||
<th>HttpSession Count</th>
|
||||
<th>HttpServletRequest Count</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td th:text="${sessionType}">value</td>
|
||||
<td th:text="${sessionCount}">value</td>
|
||||
<td th:text="${requestCount}">value</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</body>
|
||||
Reference in New Issue
Block a user