Configure sessions in the Jetty cluster to be stored in MySQL, MongoDB, and jettymongodb.

Source: Internet
Author: User
Tags mongodb driver

Configure sessions in the Jetty cluster to be stored in MySQL, MongoDB, and jettymongodb.

In Web development, Session indicates the "Session" between the HTTP server and the client (such as a browser). Each client has its corresponding Session stored on the server, it is usually used to save information associated with the client, such as whether to log on to the shopping cart or not.

The Session is generally stored in the server memory. If the server is restarted, the Session will be lost. In addition, in a cluster environment, Web applications are deployed on multiple servers, and sessions are not shared if they are stored on their respective servers.

To solve this problem, the Jetty server provides a Session Implementation Method for the cluster environment, that is, Session sharing is achieved by connecting multiple Jetty servers to the same Session database.

1. Configure the Session to be stored in a relational database (MySQL is used as an example ):

Configure jetty. xml:

 

Open etc/Jetty. xml in the jetty directory and add the XML fragment to the Configure element:

<Set name="sessionIdManager">    <New id="jdbcidmgr" class="org.eclipse.jetty.server.session.JDBCSessionIdManager">        <Arg>            <Ref id="Server" />        </Arg>        <Set name="workerName">fred</Set>        <Call name="setDriverInfo">            <Arg>com.mysql.jdbc.Driver</Arg>            <Arg>jdbc:mysql://192.168.20.1:3306/jetty_session?user=root&amp;password=123</Arg>        </Call>        <Set name="scavengeInterval">60</Set>    </New></Set><Call name="setAttribute">    <Arg>jdbcIdMgr</Arg>    <Arg>        <Ref id="jdbcidmgr" />    </Arg></Call>

Modify the URL of the database connection in the preceding XML fragment. If other relational databases are used, modify the database driver. Note that the & Symbol in XML must be converted to & amp ;.

Configure context xml:

In the webapps directory of Jetty, create an XML file, such as test. xml, which is used to configure a web application:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"><Configure class="org.eclipse.jetty.webapp.WebAppContext">    <Set name="contextPath">/test</Set>    <Set name="war">D:\test.war</Set>    <Ref name="Server" id="Server">        <Call id="jdbcIdMgr" name="getAttribute">            <Arg>jdbcIdMgr</Arg>        </Call>    </Ref>    <Set name="sessionHandler">        <New class="org.eclipse.jetty.server.session.SessionHandler">            <Arg>                <New id="jdbcmgr" class="org.eclipse.jetty.server.session.JDBCSessionManager">                    <Set name="sessionIdManager">                        <Ref id="jdbcIdMgr" />                    </Set>                </New>            </Arg>        </New>    </Set></Configure>

Where, <Set name = "war"> D: \ test. war </Set> Configure the war package of the web application and <Set name = "contextPath">/test </Set> Configure the contextPath of the web application, for example, http: // localhost: 8080/test.

Start the Jetty test:

Before starting Jetty, you also need to put the MySQL driver jar package under the lib/ext directory of Jetty. In addition, you must create a database and prepare the war package.

After everything is ready, you can start the Jetty server to test whether the Session has been saved in the database.

Run the java-jar start. jar command to start the Jetty server and open the browser to access the page. We can see that two tables are generated in the database: jettysessionids and jettysessions, which are used to store the session id and session information respectively. If you restart the Jetty server, the Session will not be lost because the Session has been persistently stored in the database.

It should be noted that because the Session stores Java objects, it will be written to the database through Java serialization, that is, the objects in the Session must support serialization and deserialization, that is, the Serializable interface is implemented.

Configuration File description:

If you do not understand the above two XML configurations, here is a simple description.

The above XML is actually the IOC configuration file of Jetty. When it comes to IOC, we will first think of the Spring framework. In fact, Jetty's IOC and Spring's IOC solve similar problems, but there are some differences in XML format.

Jetty's IOC configuration is also well understood. For example, <Set name = "contextPath">/test </Set> calls setContextPath ("/test "), <Call name = "getAttribute"> <Arg> jdbcIdMgr </Arg> </Call> calls getAttribute ("jdbcIdMgr"), <New class = "org. eclipse. jetty. server. session. sessionHandler "> </New> is the Java instantiated object new SessionHandler ().

Translate the above two sections of XML into Java code:

Import org. eclipse. jetty. server. server; import org. eclipse. jetty. server. session. JDBCSessionIdManager; import org. eclipse. jetty. server. session. JDBCSessionManager; import org. eclipse. jetty. server. session. sessionHandler; import org. eclipse. jetty. webapp. webAppContext; public class Main {public static void main (String [] args) throws Exception {Server server Server = new Server (8080); // jetty corresponding to the following. xml configuration JDBCSessi OnIdManager sessionIdManager = new JDBCSessionIdManager (server); sessionIdManager. setWorkerName ("fred"); sessionIdManager. setDriverInfo ("com. mysql. jdbc. driver "," jdbc: mysql: // 192.168.20.1: 3306/jetty_session? User = root & password = 123 "); sessionIdManager. setScavengeInterval (60); server. setAttribute ("jdbcIdMgr", sessionIdManager); // configure WebAppContext webapp = new WebAppContext () in the context xml file. setContextPath ("/test"); webapp. setWar ("D: \ test. war "); JDBCSessionIdManager jdbcIdMgr = (JDBCSessionIdManager) server. getAttribute ("jdbcIdMgr"); JDBCSessionManager sessionManager = new JDBCSessionManager (); sessionManager. setSessionIdManager (jdbcIdMgr); SessionHandler sessionHandler = new SessionHandler (sessionManager); webapp. setSessionHandler (sessionHandler); // start the server. setHandler (webapp); server. start (); server. join ();}}

Running Java code can also start the server and store sessions in the database to achieve the same effect.

2. Configure Session storage to MongoDB

Configure jetty. xml:

Open etc/jetty. xml and add the XML fragment to the Configure element:

<New id="mongodb" class="com.mongodb.MongoClient">    <Arg type="java.lang.String">192.168.20.1</Arg>    <Arg type="int">27017</Arg>    <Call name="getDB">        <Arg>jetty_session</Arg>        <Call id="sessionDocument" name="getCollection">            <Arg>jetty_session_collection</Arg>        </Call>    </Call></New><Set name="sessionIdManager">    <New id="mongoIdMgr" class="org.eclipse.jetty.nosql.mongodb.MongoSessionIdManager">        <Arg>            <Ref id="Server" />        </Arg>        <Arg>            <Ref id="sessionDocument" />        </Arg>        <Set name="workerName">fred</Set>        <Set name="scavengePeriod">60</Set>    </New></Set><Call name="setAttribute">    <Arg>mongoIdMgr</Arg>    <Arg>        <Ref id="mongoIdMgr" />    </Arg></Call>

Modify the IP address, port number, database name, and Collection name of MongoDB in the preceding XML segment.

Configure context xml:

In the webapps directory of Jetty, create an XML file, such as test. xml, which is used to configure a web application:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"> <Configure class="org.eclipse.jetty.webapp.WebAppContext">  <Set name="contextPath">/test</Set>  <Set name="war">D:\test.war</Set>  <Ref name="Server" id="Server">    <Call id="mongoIdMgr" name="getSessionIdManager"/>  </Ref>   <Set name="sessionHandler">    <New class="org.eclipse.jetty.server.session.SessionHandler">      <Arg>        <New id="mongoMgr" class="org.eclipse.jetty.nosql.mongodb.MongoSessionManager">          <Set name="sessionIdManager">            <Ref id="mongoIdMgr"/>          </Set>        </New>      </Arg>    </New>  </Set></Configure>

Start the Jetty test:

Before testing, put two jar packages in the lib/ext directory of Jetty. One is the MongoDB driver package, and the other is the jetty-nosql jar package. In addition, the corresponding database is created in MongoDB.

Run the java-jar start. jar command to start the Jetty server and open the page in the browser. You can see that the configured Collection is created in MongoDB and data is inserted:

"Translate" into Java code:

Convert the above two Jetty IOC configuration files into Java code and run them directly to implement the same function:

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. DBCollection; import com. mongodb. expose client; public class Main {public static void main (String [] args) throws Exception {Server server Server = new Server (8080); // jetty corresponding to the following. xml configuration expose client = new expose client ("192.168.20.1", 27017); DB db DB = expose client. getDB ("jetty_session"); DBCollection collection = db. getCollection ("jetty_session_collection"); sessionIdManager = new sessionIdManager (server, collection); sessionIdManager. setWorkerName ("fred"); sessionIdManager. setScavengePeriod (60); server. setAttribute ("mongoIdMgr", sessionIdManager); // configure WebAppContext webapp = new WebAppContext () in the context xml file. setContextPath ("/test"); webapp. setWar ("D: \ test. war "); your sessionidmanager your idmgr = (your sessionidmanager) server. getAttribute ("mongoIdMgr"); sessionManager = new MongoSessionManager (); sessionManager. setSessionIdManager (mongoIdMgr); SessionHandler sessionHandler = new SessionHandler (sessionManager); webapp. setSessionHandler (sessionHandler); // start the server. setHandler (webapp); server. start (); server. join ();}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.