Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
aktr committed Feb 7, 2017
1 parent 5156308 commit c84b678
Show file tree
Hide file tree
Showing 8 changed files with 378 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target/
.idea
*.iml
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<h2>ABOUT</h2>
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.

<h2>Requirements</h2>
You should have installed Tomcat or Jetty and Apache Maven on your system. There are other requirements already in this repo.

<h3>Load Balancing With Tomcat or Jetty</h3>
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:
</br>
http://tomcat.apache.org/connectors-doc/generic_howto/quick.html

<h1>Build</h1>
* `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

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

<h1>Jetty Deployment</h1>
* `cp target/session-replication.war $JETTY_HOME/webapps/` - Copy war to Jetty
* Browse to `http://localhost:8080/session-replication/hazelcast`

42 changes: 42 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>

<groupId>com.hazelcast</groupId>
<artifactId>session-replication</artifactId>
<version>0.1-SNAPSHOT</version>
<name>Hazelcast Filter Based Session Replication</name>
<description>Hazelcast Filter Based Session Replication Example</description>

<properties>
<hazelcast.version>3.7.5</hazelcast.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>


<dependencies>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-all</artifactId>
<version>${hazelcast.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.10</version>
</dependency>
</dependencies>

<build>
<finalName>${project.artifactId}</finalName>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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("<table border=\"1\"><th>Key</th><th>Value</th>");
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
sb.append("<tr><td>").append(name).append("</td><td>").append(session.getAttribute(name)).append("</td></tr>");
}
sb.append("</table>");
String res = sb.toString();

request.setAttribute("res", res);
request.getRequestDispatcher("/hazelcast.jsp").forward(request, response);
}
}
11 changes: 11 additions & 0 deletions src/main/resources/hazelcast.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<hazelcast xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.hazelcast.com/schema/config
http://www.hazelcast.com/schema/config/hazelcast-config-3.8.xsd"
xmlns="http://www.hazelcast.com/schema/config">

<management-center enabled="true">http://localhost:8090/mancenter</management-center>
<executor-service name="hazelcast-executor">
<!-- SOME INVALID XML TAG -->
<import resource="hazelcast-network.xml"/>
</executor-service>
</hazelcast>
21 changes: 21 additions & 0 deletions src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>

<!--<logger name="com.hazelcast.web.ClusteredSessionService" level="trace"-->
<!--additivity="false">-->
<!--<appender-ref ref="STDOUT" />-->
<!--</logger>-->

<root level="info">
<appender-ref ref="STDOUT" />
</root>

</configuration>
118 changes: 118 additions & 0 deletions src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>sessionReplication</servlet-name>
<servlet-class>com.hazelcast.HazelcastSessionReplication.HazelcastSessionReplication</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sessionReplication</servlet-name>
<url-pattern>/hazelcast</url-pattern>
</servlet-mapping>

<session-config>
<session-timeout>60</session-timeout>
</session-config>

<filter>
<filter-name>hazelcast-filter</filter-name>
<filter-class>com.hazelcast.web.WebFilter</filter-class>
<!--
Name of the distributed map storing
your web session objects
-->
<init-param>
<param-name>map-name</param-name>
<param-value>my-sessions</param-value>
</init-param>
<!--
How is your load-balancer configured?
stick-session means all requests of a session
is routed to the node where the session is first created.
This is excellent for performance.
If sticky-session is set to false, when a session is updated
on a node, entry for this session on all other nodes is invalidated.
You have to know how your load-balancer is configured before
setting this parameter. Default is true.
-->
<init-param>
<param-name>sticky-session</param-name>
<param-value>true</param-value>
</init-param>
<!--
Name of session id cookie
-->
<init-param>
<param-name>cookie-name</param-name>
<param-value>hazelcast.sessionId</param-value>
</init-param>
<!--
Should cookie only be sent using a secure protocol? Default is false.
-->
<init-param>
<param-name>cookie-secure</param-name>
<param-value>false</param-value>
</init-param>
<!--
Should HttpOnly attribute be set on cookie ? Default is false.
-->
<init-param>
<param-name>cookie-http-only</param-name>
<param-value>false</param-value>
</init-param>
<!--
Are you debugging? Default is false.
-->
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>

<!--
Do you want to use an existing HazelcastInstance?
Default is null.
-->
<init-param>
<param-name>instance-name</param-name>
<param-value>default</param-value>
</init-param>
<!--
Do you want to connect as a client to an existing cluster?
Default is false.
-->
<init-param>
<param-name>use-client</param-name>
<param-value>false</param-value>
</init-param>
<!--
Client configuration location;
* as servlet resource OR
* as classpath resource OR
* as URL
Default is null.
-->
<!--
Do you want to shutdown HazelcastInstance during
web application undeploy process?
Default is true.
-->
<init-param>
<param-name>shutdown-on-destroy</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hazelcast-filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>

<listener>
<listener-class>com.hazelcast.web.SessionListener</listener-class>
</listener>
</web-app>
105 changes: 105 additions & 0 deletions src/main/webapp/hazelcast.jsp
Original file line number Diff line number Diff line change
@@ -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();%>
<html>

<head>
<title>
</title>
</head>

<body>
<div align="center">
<table border="1">
<tr>
<td>
Session Time Out Test
</td>
<td>
<% out.println(session.getMaxInactiveInterval());%>
</td>
</tr>
<tr>
<td>
Is New Test
</td>
<td>
<% out.print(request.getAttribute( "isNewTest")); %>
</td>
</tr>
<tr>
<td>
Session Creation Time
</td>
<td>
<% out.print(new Date(session.getCreationTime())); %>
</td>
</tr>
<tr>
<td>
Session Last Accessed Time
</td>
<td>
<% out.println(new Date(session.getLastAccessedTime())); %>
</td>
</tr>
</table>
<form action="" method="get">
<table>
<tr>
<td>
key:
</td>
<td>
<input type="text" name="key">
</td>
</tr>
<tr>
<td>
value:
</td>
<td>
<input type="text" name="value">
</td>
</tr>
<tr>
<td>
<input type="submit" name="action" value="Set Attribute">
</td>
</tr>
</table>
</form>
<form action="" method="get" style="">
<table>
<tr>
<td>
key:
</td>
<td>
<input type="text" name="key">
</td>
</tr>
<tr>
<td>
<input type="submit" name="action" value="Get Attribute" />
</td>
<td>
<input type="submit" name="action" value="Delete Attribute" />
</td>
</tr>
</table>
</form>
<p>
result:
<%out.println(request.getAttribute( "getKey"));%>
</p>
<p>
GetAttributeNames - Values:
<% out.print(request.getAttribute( "res")); %>
</p>
</div>
</body>

</html>

0 comments on commit c84b678

Please sign in to comment.