Using MongoDB for a Java Web App’s HttpSession(转)

处理tomcat的session

NoSqlSessionswithJetty7andJetty8

转http://www.jamesward.com/2011/11/30/using-mongodb-for-a-java-web-apps-httpsession

Sincetheweb’sinceptionwe’vebeenusingitasaglorifiedgreenscreen.Inthismodelallwebapplicationinteractionsandthestateassociatedwiththoseinteractions,ishandledbytheserver.Thismodelisarealpaintoscale.LuckilythemodelisshiftingtomoreofaClient/ServerapproachwheretheUIstatemovestotheclient(whereitshouldbe).Butformanyoftoday’sapplicationswestillhavetodealwithserver-sidestate.Typicallythatstateisjuststoredinmemory.It’sfastbutifweneedmorethanoneserver(forfailoverorload-balancing)thenweusuallyneedtoreplicatethatstateacrossourservers.Tokeepwebclientstalkingtothesameserver(usuallyforperformanceandconsistency)ourload-balancershaveimplementedstickysessions.Sessionreplicationandstickysessionsarereallyjustaby-productofputtingclientstateinformationinmemory.Untilweallmovetostatelesswebarchitecturesweneedtofindmorescalableandmaintainablewaystohandlesessionstate.

Jettyhasrecentlyaddedsupportforapluggablesessionstatemanager.Thisallowsustomoveawayfromstickysessionsandsessionreplicationandinsteaduseexternalsystemstostoresessionstate.JettyprovidesaMongoDBimplementationout-of-the-boxbutpresumablyitwouldn’tbeveryhardtoaddotherimplementationslikeMemcached.JettyhassomegooddocumentationonhowtoconfigurethiswithXML.LetswalkthroughasampleapplicationusingJettyandMongoDBforsessionstateandthendeploythatapplicationonthecloudwithHeroku.

FirstletscoversomeHerokubasics.Herokurunsapplicationson“dynos”.Youcanthinkofdynosasisolatedexecutionenvironmentsforyourapplication.AnapplicationonHerokucanhavewebdynosandnon-webdynos.WebdynoswillbeusedforhandlingHTTPrequestsforyourapplication.Non-webdynoscanbeusedforbackgroundprocessing,one-offprocesses,scheduledjobs,etc.HTTP(orHTTPS)requeststoyourapplicationareautomaticallyloadbalancedacrossyourwebdynos.Herokudoesnotusestickysessionssoitisuptoyoutoinsurethatifyouhavemorethanonewebdynoorifadynoisrestarted,thatyourusers’sessionswillnotbelost.

Herokudoesnothaveanyspecial/customAPIsanddoesnotdictatewhichappserveryouuse.Thismeansyouhavetobringyourappserverwithyou.Thereareavarietyofwaystodothatbutthepreferredapproachistospecifyyourappserverasadependencyinyourapplicationbuilddescriptor(Maven,sbt,etc).

YoumusttellHerokuwhatprocessneedstoberunwhenanewdynoisstarted.Thisisdefinedinafilecalled“Procfile”thatmustbeintherootdirectoryofyourproject.

Herokuprovidesareallyniftyandsimplewaytoprovisionnewexternalsystemsthatyoucanuseinyourapplication.Thesearecalled“add-ons”.TherearetonsofHerokuadd-onsbutforthisexamplewewillbeusingtheMongoHQadd-onthatprovidesaMongoDBinstance.

WiththatinmindletswalkthroughasimpleapplicationthatusesJetty’sMongoDB-backedsessions.Youcangetallofthiscodefromgithuborjustclonethegithubrepo:

git clone git://github.com/jamesward/jetty-mongo-session-test.git

FirstletssetupaMavenbuildthatwillincludetheJettyandMongoDBdriverdependencies.Wewilluse“appassembler-maven-plugin”togenerateascriptthatstartstheJettyserver.Hereisthepom.xmlMavenbuilddescriptor:

<?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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.heroku.test</groupId>
    <version>1.0-SNAPSHOT</version>
    <name>jettySessionTest</name>
    <artifactId>jettySessionTest</artifactId>
    <packaging>jar</packaging>
 
    <dependencies>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-webapp</artifactId>
            <version>8.0.3.v20111011</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-nosql</artifactId>
            <version>8.0.3.v20111011</version>
        </dependency>
        <dependency>
            <groupId>org.mongodb</groupId>
            <artifactId>mongo-java-driver</artifactId>
            <version>2.6.5</version>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>appassembler-maven-plugin</artifactId>
                <version>1.1.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>assemble</goal>
                        </goals>
                        <configuration>
                            <assembleDirectory>target</assembleDirectory>
                            <programs>
                                <program>
                                    <mainClass>com.heroku.test.Main</mainClass>
                                    <name>webapp</name>
                                </program>
                            </programs>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
 
</project>

The“appassembler-maven-plugin”referencesaclass“com.heroku.test.Main”thathasn’tbeencreatedyet.Wewillgettothatinaminute.Firstletscreateasimpleservletthatwillstoreanobjectinthesession.Hereistheservletfromthe“src/main/java/com/heroku/test/servlet/TestServlet.java”file:

package com.heroku.test.servlet;
 
 
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
public class TestServlet extends HttpServlet {
 
    private class CountHolder implements Serializable {
 
        private static final long serialVersionUID = 1L;
        private Integer count;
        private Date time;
 
        public CountHolder() {
            count = 0;
            time = new Date();
        }
 
        public Integer getCount() {
            return count;
        }
 
        public void plusPlus() {
            count++;
        }
 
        public void setTime(Date time) {
            this.time = time;
        }
    }
 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
 
        HttpSession session = req.getSession();
 
        CountHolder count;
 
        if(session.getAttribute("count") != null) {            
            count = (CountHolder) session.getAttribute("count");
        } else {
            count = new CountHolder();
        }
 
        count.setTime(new Date());
        count.plusPlus();
 
        System.out.println("Count: " + count.getCount());
 
        session.setAttribute("count", count);
 
        resp.getWriter().print("count = " + count.getCount());
    }
 
}

Asyoucanseethereisnothingspecialhere.WeareusingtheregularHttpSessionnormally,storingandretrievingaSerializableobjectnamedCountHolder.Theapplicationsimplydisplaysthenumberortimestheservlethasbeenaccessedbyauser(where“user”reallymeansarequestthatpassesthesameJSESSIONIDcookieasapreviousrequest).

Nowletsmapthatservlettothe“/”URLpatterninthewebappdescriptor(src/main/webapp/WEB-INF/web.xml):

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
 
    <servlet>
        <servlet-name>Test Servlet</servlet-name>
        <servlet-class>com.heroku.test.servlet.TestServlet</servlet-class>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.ico</url-pattern>
    </servlet-mapping>
 
    <servlet-mapping>
        <servlet-name>Test Servlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
 
</web-app>

Iputaservletmappinginfor“.ico”becausesomebrowsersautomaticallyrequest“favicon.ico”,andthoserequestsifnotmappedtosomethingwillmaptoourservletandmakethecountappeartojump.

Nowletscreatethat“com.heroku.test.Main”classthatwillconfigureJettyandstartit.OnereasonweareusingaJavaclasstostartJettyisbecausewearetryingtoavoidputtingtheMongoDBconnectioninformationinatextfile.Herokuadd-onsplacetheirconnectioninformationfortheexternalsystemsinenvironmentvariables.WecouldcopythatinformationintoaplainXMLJettyconfigfilebutthatisananti-patternbecauseiftheadd-onproviderneedstochangetheconnectioninformation(perhapsforfailoverpurposes)thenourapplicationwouldstopworkinguntilwemanuallyupdatedtheconfigfile.SooursimpleMainclasswilljustreadtheconnectioninformationfromanenvironmentvariableandconfigureJettyatruntime.Hereisthesourceforthe“src/main/java/com/heroku/test/Main.java”file:

package com.heroku.test;
 
import java.util.Date;
import java.util.Random;
 
import org.eclipse.jetty.nosql.mongodb.MongoSessionIdManager;
import org.eclipse.jetty.nosql.mongodb.MongoSessionManager;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.session.SessionHandler;
import org.eclipse.jetty.webapp.WebAppContext;
 
import com.mongodb.DB;
import com.mongodb.MongoURI;
 
public class Main {
 
    public static void main(String[] args) throws Exception{
        String webappDirLocation = "src/main/webapp/";
 
        String webPort = System.getenv("PORT");
        if(webPort == null || webPort.isEmpty()) {
            webPort = "8080";
        }
 
        Server server = new Server(Integer.valueOf(webPort));
        WebAppContext root = new WebAppContext();
 
        MongoURI mongoURI = new MongoURI(System.getenv("MONGOHQ_URL"));
        DB connectedDB = mongoURI.connectDB();
 
        if (mongoURI.getUsername() != null) {
            connectedDB.authenticate(mongoURI.getUsername(), mongoURI.getPassword());
        }
 
        MongoSessionIdManager idMgr = new MongoSessionIdManager(server, connectedDB.getCollection("sessions"));
 
        Random rand = new Random((new Date()).getTime());
        int workerNum = 1000 + rand.nextInt(8999);
 
        idMgr.setWorkerName(String.valueOf(workerNum));
        server.setSessionIdManager(idMgr);
 
        SessionHandler sessionHandler = new SessionHandler();
        MongoSessionManager mongoMgr = new MongoSessionManager();
        mongoMgr.setSessionIdManager(server.getSessionIdManager());
        sessionHandler.setSessionManager(mongoMgr);
 
        root.setSessionHandler(sessionHandler);
        root.setContextPath("/");
        root.setDescriptor(webappDirLocation+"/WEB-INF/web.xml");
        root.setResourceBase(webappDirLocation);
        root.setParentLoaderPriority(true);
 
        server.setHandler(root);
 
        server.start();
        server.join();
    }
 
}

AsyoucanseetheMongoSessionManagerisbeingconfiguredbasedontheMONGOHQ_URLenvironmentvariable,theJettyserverisbeingconfiguredtousetheMongoSessionManagerandpointedtothewebapplocation,andthenJettyisstarted.

Nowletsgiveitatry!IfyouwanttoruneverythinglocallythenyouwillneedtohaveMaven3andMongoDBinstalledandstarted.ThenruntheMavenbuild

mvn package

Thiswillusetheappassembler-maven-plugintogeneratethestartscriptwhichsetsuptheCLASSPATHandthenrunsthecom.heroku.test.Mainclass.BeforewerunweneedtosettheenvironmentvariabletopointtoourlocalMongoDB:

OnWindows:

set MONGOHQ_URL=mongodb://127.0.0.1:27017/test

OnLinux/Mac:

export MONGOHQ_URL=mongodb://127.0.0.1:27017/test

Nowrunthegeneratedstartscript:

OnWindows:

target\bin\webapp.bat

OnLinux/Mac:

export MONGOHQ_URL=mongodb://127.0.0.1:27017/test
		export PORT=9090
		sh target/bin/webapp

Nowinyourbrowsermakeafewrequeststo:

http://localhost:9090/

Verifythatthesessionisconsistentbetweenthetwoservers.

NowletsdeploythisapponthecloudwithHeroku.Asmentionedearlierweneedafilenamed“Procfile”intherootdirectorythatwilltellHerokuwhatprocesstorunwhenadynoisstarted.HereistheProcfileforthisapplication:

web: sh target/bin/webapp

Tocreateanddeploytheapplicationyouwillneedtoinstallgit&theHerokuToolbelt,createanHeroku,andsincewewillbeusingadd-onsyouwillneedtoverifyyourHerokuaccount.EachapplicationyourcreateonHerokugets750freedynohourspermonth.Soaslongasyoudon’tgoabovethatandyoustickwiththefreetieroftheMongoHQadd-on,thenyouwon’tbebilledforanything.

LogintoHerokuusingtheherokucommandlineinterface:

heroku login

Ifyouhaven’talreadysetupanSSHkeyforHerokuthentheloginprocesswillwalkyouthroughthat.

IntherootdirectoryofthisprojectcreatetheapponHerokuwiththe“cedar”stackandthefreeMongoHQadd-on:

heroku create --stack cedar --addons mongohq:free

UploadyourapplicationtoHerokuusinggit:

git push heroku master

Opentheapplicationinyourbrowser:

heroku open

Ifyouwanttoaddmoredynoshandlingwebrequeststhenrun:

heroku scale web=2

ToseewhatisrunningonHerokurun:

heroku ps

Ifyouwanttoturnoffallofthedynosforyourapplicationjustscaleto0:

heroku scale web=0

Toseethelogginginformationforyourapplicationrun:

heroku logs

Toseealiveversionofthisdemovisit:

http://young-wind-7462.herokuapp.com/

Well,thereyougo.You’velearnedhowtoavoidstickysessionsandsessionreplicationbymovingsessionstatetoanexternalMongoDBsystemthatcanbescaledindependentlyofthewebtier.You’vealsolearnedhowtorunthisonthecloudwithHeroku.Letmeknowifyouhaveanyquestions.

BTW:I’dliketothankJohnSimonefromHerokuforwritingmostofthecodeforthisdemo.