diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e02456b --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +target/ +.idea +*.iml diff --git a/README.md b/README.md new file mode 100644 index 0000000..c2a964c --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +

ABOUT

+This project is prepared to serve as a sample application for Hazelcast, the leading open source in-memory data grid . Here, Hazelcast's use case is HTTP Session Replication. + +

Requirements

+You should have installed Tomcat or Jetty and Apache Maven on your system. There are other requirements already in this repo. + +

Load Balancing With Tomcat or Jetty

+To see how application works, you need to start two different servers at different ports. Also you have to connect these servers to a load balancer. You can use apache mod\_jk module for load balancing. Shortly, you have to enable mod\_jk module apache httpd.conf file and specify workers.properties file. You must enter tomcat server ports and configurations to workers.properties file. +You can find detailed explanations at: +
+http://tomcat.apache.org/connectors-doc/generic_howto/quick.html + +

Build

+* `git clone https://github.com/hazelcast/hazelcast-code-samples.git` - Clone repo into the local +* `cd hazelcast-code-samples/hazelcast-integration/filter-based-session-replication/` +* `mvn install` - Create war file for example + +

Tomcat Deployment

+* `cp target/session-replication.war $CATALINA_HOME/webapps/` - Copy war to Tomcat +* Browse to `http://localhost:8080/session-replication/hazelcast` + +

Jetty Deployment

+* `cp target/session-replication.war $JETTY_HOME/webapps/` - Copy war to Jetty +* Browse to `http://localhost:8080/session-replication/hazelcast` + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..93a3852 --- /dev/null +++ b/pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + war + + com.hazelcast + session-replication + 0.1-SNAPSHOT + Hazelcast Filter Based Session Replication + Hazelcast Filter Based Session Replication Example + + + 3.7.5 + UTF-8 + + + + + + com.hazelcast + hazelcast-all + ${hazelcast.version} + + + javax.servlet + javax.servlet-api + 3.0.1 + provided + + + ch.qos.logback + logback-classic + 1.1.10 + + + + + ${project.artifactId} + + diff --git a/src/main/java/com/hazelcast/HazelcastSessionReplication/HazelcastSessionReplication.java b/src/main/java/com/hazelcast/HazelcastSessionReplication/HazelcastSessionReplication.java new file mode 100755 index 0000000..687eb26 --- /dev/null +++ b/src/main/java/com/hazelcast/HazelcastSessionReplication/HazelcastSessionReplication.java @@ -0,0 +1,53 @@ +package com.hazelcast.HazelcastSessionReplication; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import java.io.IOException; +import java.util.Enumeration; + +public class HazelcastSessionReplication extends HttpServlet { + + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + } + + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + HttpSession session = request.getSession(); + if (session.isNew()) { + request.setAttribute("isNewTest", "Session is created first time."); + } else { + request.setAttribute("isNewTest", "Session already created"); + session.setMaxInactiveInterval(120); + } + + if (request.getParameter("action") != null) { + if (request.getParameter("action").equals("Set Attribute") && request.getParameter("key") != null + && !request.getParameter("value").equals("null")) { + session.setAttribute(request.getParameter("key"), request.getParameter("value")); + } + + if (request.getParameter("action").equals("Get Attribute") && request.getParameter("key") != null) { + request.setAttribute("getKey", session.getAttribute(request.getParameter("key"))); + } + + if (request.getParameter("action").equals("Delete Attribute") && request.getParameter("key") != null) { + session.removeAttribute(request.getParameter("key")); + } + } + + Enumeration names = session.getAttributeNames(); + StringBuilder sb = new StringBuilder(); + sb.append(""); + while (names.hasMoreElements()) { + String name = (String) names.nextElement(); + sb.append(""); + } + sb.append("
KeyValue
").append(name).append("").append(session.getAttribute(name)).append("
"); + String res = sb.toString(); + + request.setAttribute("res", res); + request.getRequestDispatcher("/hazelcast.jsp").forward(request, response); + } +} diff --git a/src/main/resources/hazelcast.xml b/src/main/resources/hazelcast.xml new file mode 100644 index 0000000..de5b9ff --- /dev/null +++ b/src/main/resources/hazelcast.xml @@ -0,0 +1,11 @@ + + + http://localhost:8090/mancenter + + + + + diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml new file mode 100644 index 0000000..a2cad73 --- /dev/null +++ b/src/main/resources/logback.xml @@ -0,0 +1,21 @@ + + + + + + + %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml new file mode 100755 index 0000000..4989f57 --- /dev/null +++ b/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,118 @@ + + + + Archetype Created Web Application + + sessionReplication + com.hazelcast.HazelcastSessionReplication.HazelcastSessionReplication + + + sessionReplication + /hazelcast + + + + 60 + + + + hazelcast-filter + com.hazelcast.web.WebFilter + + + map-name + my-sessions + + + + sticky-session + true + + + + cookie-name + hazelcast.sessionId + + + + cookie-secure + false + + + + cookie-http-only + false + + + + debug + true + + + + + instance-name + default + + + + use-client + false + + + + + shutdown-on-destroy + true + + + + hazelcast-filter + /* + FORWARD + INCLUDE + REQUEST + + + + com.hazelcast.web.SessionListener + + diff --git a/src/main/webapp/hazelcast.jsp b/src/main/webapp/hazelcast.jsp new file mode 100755 index 0000000..7f8d645 --- /dev/null +++ b/src/main/webapp/hazelcast.jsp @@ -0,0 +1,105 @@ +<%-- Created by IntelliJ IDEA. User: bilal Date: 17/06/14 Time: 15:00 +To change this template use File | Settings | File Templates. --%> +<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*" + %> +<% session=request.getSession();%> + + + + + + + + +
+ + + + + + + + + + + + + + + + + +
+ Session Time Out Test + + <% out.println(session.getMaxInactiveInterval());%> +
+ Is New Test + + <% out.print(request.getAttribute( "isNewTest")); %> +
+ Session Creation Time + + <% out.print(new Date(session.getCreationTime())); %> +
+ Session Last Accessed Time + + <% out.println(new Date(session.getLastAccessedTime())); %> +
+
+ + + + + + + + + + + + +
+ key: + + +
+ value: + + +
+ +
+
+
+ + + + + + + + + +
+ key: + + +
+ + + +
+
+

+ result: + <%out.println(request.getAttribute( "getKey"));%> +

+

+ GetAttributeNames - Values: + <% out.print(request.getAttribute( "res")); %> +

+
+ + +