Sunday, January 8, 2012

Memcached Session Manager Installation

Environments: One lighttpd web server (192.168.56.1, installation), one memcaced server (192.168.56.2, installation), and two tomcat servers (192.168.152.140 & 192.168.152.143, here) with memcached session manager packages.

Tomcat server with memcached session manager package:
1. put the following eight jar files into your tomcat library folder (e.g., /usr/share/apache-tomcat-7.0.**/lib)
  • http://memcached-session-manager.googlecode.com/files/memcached-session-manager-tc7-1.4.1.jar
  • http://spymemcached.googlecode.com/files/spymemcached-2.7.jar
  • http://memcached-session-manager.googlecode.com/files/kryo-1.03.jar
  • http://memcached-session-manager.googlecode.com/files/minlog-1.2.jar
  • http://memcached-session-manager.googlecode.com/files/reflectasm-0.9.jar
  • http://mirrors.ibiblio.org/pub/mirrors/maven2/asm/asm/3.2/asm-3.2.jar
  • http://github.com/downloads/magro/kryo-serializers/kryo-serializers-0.8.jar
  • http://memcached-session-manager.googlecode.com/files/msm-kryo-serializer-1.4.0.jar
2. add the following paragraph into context.xml (e.g., /usr/share/apache-tomcat-7.0.**/context.xml)
        <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
         memcachedNodes="n1: 192.168.56.2:11211"
         transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" />
* sometimes if you meet "com.esotericsoftware.kryo.SerializationException: Buffer limit exceeded reading object of type: java.util.concurrent.ConcurrentHashMap" problem, my solution is to change transcoderFactory from "de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" to "de.javakaffee.web.msm.JavaSerializationTranscoderFactory"
3. before restart your tomcat servers, the memcached server and web server have to been started.

Test jsp file:

<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%><%@ page import="java.net.InetAddress;" %>
<%
    String server_ip = "";
    InetAddress inetAddress = InetAddress.getLocalHost();
    server_ip = inetAddress.getHostAddress();
    String abc = (String) session.getAttribute("abc");
    String in_session = "false";
    if (abc == null)  {
        session.setAttribute("abc", "value  in session");
        abc  = "value not in session";
        in_session = "false";
    } else {
        in_session = "true";
    }
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
hello world! This is <%=server_ip%><br>
session id : <%=session.getId()%> <br>
abc value in session ? <%=in_session%><br>
abc value = <%=abc%>
</body>
</html>


Ref:
http://napmas.blogspot.com/2011/06/tomcat-and-memcached-and-session.html
http://code.google.com/p/memcached-session-manager/wiki/SetupAndConfiguration



1 comment:

  1. Thank you so much for posting these step-by-step details. I was about to give up on getting this to work and you provided the pieces I was missing.

    ReplyDelete