Terracotta
From Resin 3.0
(Difference between revisions)
(Checkpoint: Roots) |
|||
(6 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
− | + | __FORCETOC__ | |
+ | (The following has been tested with Resin 3.1.9 and Terracotta 3.0.1) | ||
− | + | == Terracotta Distributed Shared Objects (DSOs) and Resin == | |
− | + | ||
− | + | ||
<ol> | <ol> | ||
Line 24: | Line 23: | ||
</pre> | </pre> | ||
</li> | </li> | ||
− | <li>Create the HelloWorld Servlet <code> | + | <li>Create the HelloWorld Servlet <code>$RESIN/webapps/terracotta/WEB-INF/classes/example/HelloWorldServlet.java</code> |
<pre>package example; | <pre>package example; | ||
Line 135: | Line 134: | ||
</li> | </li> | ||
<li> | <li> | ||
− | Browse to | + | Browse to http://localhost:8080/terracotta and hit refresh a few times. You should see a date added to the page on each refresh. |
+ | </li> | ||
+ | <li> | ||
+ | Open another browser window to http://localhost:8081/terracotta. You should see the same dates from the previous page, but with one now added. | ||
+ | </li> | ||
+ | <li> | ||
+ | You can run <pre>$TERRACOTTA/bin/admin.sh</pre> to see the details of the shared objects that Terracotta is managing. | ||
+ | </li> | ||
+ | </ol> | ||
+ | |||
+ | == Terracotta Sessions and Resin == | ||
+ | '''NOTE: The following only works with versions of Terracotta before 3.0''' | ||
+ | |||
+ | |||
+ | Terracotta sessions are built on their DSOs, so much of the instructions will be the same. We will use Quercus here to show how to use Java sessions from PHP and how Terracotta is able to share Java sessions even through PHP and Quercus. | ||
+ | |||
+ | <ol> | ||
+ | <li>Download Resin from http://www.caucho.com/download</li> | ||
+ | <li>Unzip Resin. From now on, we'll refer to the location of the Resin root directory as $RESIN</li> | ||
+ | <li>Download Terracotta from http://www.terracotta.org</li> | ||
+ | <li>Unzip Terracotta. From now on, we'll refer to the location of the Terracotta root directory as $TERRACOTTA</li> | ||
+ | <li>Create the example webapp directory structure: | ||
+ | <pre>mkdir -p $RESIN/webapps/terracotta/WEB-INF/classes/example/</pre> | ||
+ | </li> | ||
+ | <li>Create the example PHP file <code>$RESIN/webapps/terracotta/index.php</code> | ||
+ | <pre><?php | ||
+ | |||
+ | import com.caucho.server.resin.Resin; | ||
+ | |||
+ | $resin = Resin::getLocal(); | ||
+ | |||
+ | echo "This server is $resin<br/>"; | ||
+ | |||
+ | $session = $request->getSession(true); | ||
+ | |||
+ | $count = $session->getAttribute("count"); | ||
+ | |||
+ | |||
+ | if (!isset($count)) { | ||
+ | $session->setAttribute("count", 0); | ||
+ | } else { | ||
+ | $session->setAttribute("count", $count+1); | ||
+ | } | ||
+ | |||
+ | echo "Count: " . $session->getAttribute("count"); | ||
+ | ?> | ||
+ | </pre> | ||
+ | </li> | ||
+ | <li>Create the Terracotta configuration file <code>$RESIN/tc-config.xml</code> | ||
+ | <pre> | ||
+ | <?xml version="1.0" encoding="UTF-8"?> | ||
+ | <tc:tc-config xsi:schemaLocation="http://www.terracotta.org/schema/terracotta-4.xsd" xmlns:tc="http://www.terracotta.org/config" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
+ | <!--Tell DSO where the Terracotta server can be found; | ||
+ | See the Terracotta DSO Guide for additional information.--> | ||
+ | <servers> | ||
+ | <server host="%i" name="sample"> | ||
+ | <data>data/server-data</data> | ||
+ | <logs>logs/server-logs</logs> | ||
+ | </server> | ||
+ | <update-check> | ||
+ | <enabled>true</enabled> | ||
+ | </update-check> | ||
+ | </servers> | ||
+ | <!--Tell DSO where to put the generated client logs | ||
+ | See the Terracotta DSO Guide for additional information.--> | ||
+ | <clients> | ||
+ | <logs>logs/client-logs/%(webserver.log.name)</logs> | ||
+ | </clients> | ||
+ | <application> | ||
+ | <dso> | ||
+ | <instrumented-classes> | ||
+ | <!--Include all classes for DSO instrumentation--> | ||
+ | <include> | ||
+ | <class-expression>*..*</class-expression> | ||
+ | </include> | ||
+ | </instrumented-classes> | ||
+ | |||
+ | <!--Tell DSO which applications in your web container is using DSO--> | ||
+ | <web-applications> | ||
+ | <web-application>terracotta</web-application> | ||
+ | </web-applications> | ||
+ | </dso> | ||
+ | </application> | ||
+ | </tc:tc-config> | ||
+ | </pre></li> | ||
+ | <li>Create the Terracotta boot jar: | ||
+ | <pre>$TERRACOTTA/bin/make-boot-jar.sh -f $RESIN/tc-config.xml</pre> | ||
+ | Note the path of the boot jar created by this command. You will use it in the next step. | ||
+ | </li> | ||
+ | <li>Edit resin.conf, making the following changes: | ||
+ | <ol> | ||
+ | <li> | ||
+ | <pre> | ||
+ | <jvm-arg>-Dcom.sun.management.jmxremote</jvm-arg> | ||
+ | <jvm-arg>-Xbootclasspath/p:<path-to-boot-jar></jvm-arg> | ||
+ | <jvm-arg>-Dtc.install-root=<path-to-terracotta></jvm-arg> | ||
+ | <jvm-arg>-Dtc.config=localhost:9510</jvm-arg> | ||
+ | </pre> | ||
+ | Make sure to replace the <path-to-boot-jar> and <path-to-terracotta>. | ||
+ | </li> | ||
+ | <li> | ||
+ | Remove the line <pre><http address="*" port="8080"/></pre> from <server-default> section. | ||
+ | </li> | ||
+ | <li> | ||
+ | Remove the default server line <pre><server id="" address="127.0.0.1" port="6800"/></pre> | ||
+ | </li> | ||
+ | <li> | ||
+ | Add two servers: | ||
+ | <pre> | ||
+ | <server id="a" address="127.0.0.1" port="6800"> | ||
+ | <http address="*" port="8080"/> | ||
+ | </server> | ||
+ | |||
+ | <server id="b" address="127.0.0.1" port="6801"> | ||
+ | <http address="*" port="8081"/> | ||
+ | </server> | ||
+ | </pre> | ||
+ | </li> | ||
+ | <li> | ||
+ | Add Terracotta's session filter under the <web-app-default> section: | ||
+ | <pre> | ||
+ | <filter filter-name='terracotta' | ||
+ | filter-class='com.terracotta.session.SessionFilter'/> | ||
+ | <filter-mapping url-pattern='/*' filter-name='terracotta'/> | ||
+ | </pre> | ||
+ | </ol> | ||
+ | </li> | ||
+ | <li>Copy the Terracotta session jar to Resin's lib directory: | ||
+ | <pre> | ||
+ | cp $TERRACOTTA/lib/session/tc-session.jar $RESIN/lib | ||
+ | </pre> | ||
+ | <li>Start the Terracotta server: <pre>$TERRACOTTA/bin/start-tc-server.sh -f $RESIN/tc-config.xml</pre></li> | ||
+ | <li>Start both Resin servers: | ||
+ | <pre> | ||
+ | cd $RESIN | ||
+ | java -jar lib/resin.jar -server a start | ||
+ | java -jar lib/resin.jar -server b start | ||
+ | </pre> | ||
+ | </li> | ||
+ | <li> | ||
+ | Browse to http://localhost:8080/terracotta and hit refresh a few times. You should see an updated count on each refresh. | ||
</li> | </li> | ||
<li> | <li> | ||
− | Open another browser window to | + | Open another browser window to http://localhost:8081/terracotta. You should see the count from the previous page, incremented by one. |
</li> | </li> | ||
<li> | <li> |
Latest revision as of 19:12, 15 June 2009
(The following has been tested with Resin 3.1.9 and Terracotta 3.0.1)
Contents |
[edit]
- Download Resin from http://www.caucho.com/download
- Unzip Resin. From now on, we'll refer to the location of the Resin root directory as $RESIN
- Download Terracotta from http://www.terracotta.org
- Unzip Terracotta. From now on, we'll refer to the location of the Terracotta root directory as $TERRACOTTA
- Create the example webapp directory structure:
mkdir -p $RESIN/webapps/terracotta/WEB-INF/classes/example/
- Create the configuration file
$RESIN/webapps/terracotta/WEB-INF/resin-web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://caucho.com/ns/resin"> <servlet servlet-name="hello-servlet" servlet-class="example.HelloWorldServlet"/> <servlet-mapping url-pattern="/*" servlet-name="hello-servlet"/> </web-app>
- Create the HelloWorld Servlet
$RESIN/webapps/terracotta/WEB-INF/classes/example/HelloWorldServlet.java
package example; import java.io.*; import java.text.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorldServlet extends HttpServlet { // this is the variable that Terracotta will distribute private ArrayList<Calendar> _accessTimes = new ArrayList<Calendar>(); @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Calendar now = Calendar.getInstance(); synchronized(_accessTimes) { _accessTimes.add((Calendar) now.clone()); DateFormat format = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z"); resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); out.println("<html>"); out.println("<body>"); for (Calendar calendar : _accessTimes) { out.println("<br/>" + format.format(calendar.getTime())); } out.println("</body>"); out.println("</html>"); } } }
- Create the Terracotta configuration file
$RESIN/tc-config.xml
<?xml version="1.0" encoding="UTF-8"?> <tc:tc-config xmlns:tc="http://www.terracotta.org/config"> <application> <dso> <roots> <root> <field-name>example.HelloWorldServlet._accessTimes</field-name> </root> </roots> <locks> <autolock> <method-expression>* example.HelloWorldServlet*.*(..)</method-expression> <lock-level>write</lock-level> </autolock> </locks> <instrumented-classes> <include><class-expression>example..*</class-expression></include> </instrumented-classes> <additional-boot-jar-classes> <include>java.util.TimeZone</include> <include>sun.util.calendar.ZoneInfo</include> </additional-boot-jar-classes> </dso> </application> </tc:tc-config>
- Create the Terracotta boot jar:
$TERRACOTTA/bin/make-boot-jar.sh -f $RESIN/tc-config.xml
Note the path of the boot jar created by this command. You will use it in the next step.
- Edit resin.conf, making the following changes:
-
<jvm-arg>-Dcom.sun.management.jmxremote</jvm-arg> <jvm-arg>-Xbootclasspath/p:<path-to-boot-jar></jvm-arg> <jvm-arg>-Dtc.install-root=<path-to-terracotta></jvm-arg> <jvm-arg>-Dtc.config=localhost:9510</jvm-arg>
Make sure to replace the <path-to-boot-jar> and <path-to-terracotta>.
-
Remove the line
<http address="*" port="8080"/>
from <server-default> section. -
Remove the default server line
<server id="" address="127.0.0.1" port="6800"/>
-
Add two servers:
<server id="a" address="127.0.0.1" port="6800"> <http address="*" port="8080"/> </server> <server id="b" address="127.0.0.1" port="6801"> <http address="*" port="8081"/> </server>
-
- Start the Terracotta server:
$TERRACOTTA/bin/start-tc-server.sh -f $RESIN/tc-config.xml
- Start both Resin servers:
cd $RESIN java -jar lib/resin.jar -server a start java -jar lib/resin.jar -server b start
- Browse to http://localhost:8080/terracotta and hit refresh a few times. You should see a date added to the page on each refresh.
- Open another browser window to http://localhost:8081/terracotta. You should see the same dates from the previous page, but with one now added.
-
You can run
$TERRACOTTA/bin/admin.sh
to see the details of the shared objects that Terracotta is managing.
[edit] Terracotta Sessions and Resin
NOTE: The following only works with versions of Terracotta before 3.0
Terracotta sessions are built on their DSOs, so much of the instructions will be the same. We will use Quercus here to show how to use Java sessions from PHP and how Terracotta is able to share Java sessions even through PHP and Quercus.
- Download Resin from http://www.caucho.com/download
- Unzip Resin. From now on, we'll refer to the location of the Resin root directory as $RESIN
- Download Terracotta from http://www.terracotta.org
- Unzip Terracotta. From now on, we'll refer to the location of the Terracotta root directory as $TERRACOTTA
- Create the example webapp directory structure:
mkdir -p $RESIN/webapps/terracotta/WEB-INF/classes/example/
- Create the example PHP file
$RESIN/webapps/terracotta/index.php
<?php import com.caucho.server.resin.Resin; $resin = Resin::getLocal(); echo "This server is $resin<br/>"; $session = $request->getSession(true); $count = $session->getAttribute("count"); if (!isset($count)) { $session->setAttribute("count", 0); } else { $session->setAttribute("count", $count+1); } echo "Count: " . $session->getAttribute("count"); ?>
- Create the Terracotta configuration file
$RESIN/tc-config.xml
<?xml version="1.0" encoding="UTF-8"?> <tc:tc-config xsi:schemaLocation="http://www.terracotta.org/schema/terracotta-4.xsd" xmlns:tc="http://www.terracotta.org/config" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <!--Tell DSO where the Terracotta server can be found; See the Terracotta DSO Guide for additional information.--> <servers> <server host="%i" name="sample"> <data>data/server-data</data> <logs>logs/server-logs</logs> </server> <update-check> <enabled>true</enabled> </update-check> </servers> <!--Tell DSO where to put the generated client logs See the Terracotta DSO Guide for additional information.--> <clients> <logs>logs/client-logs/%(webserver.log.name)</logs> </clients> <application> <dso> <instrumented-classes> <!--Include all classes for DSO instrumentation--> <include> <class-expression>*..*</class-expression> </include> </instrumented-classes> <!--Tell DSO which applications in your web container is using DSO--> <web-applications> <web-application>terracotta</web-application> </web-applications> </dso> </application> </tc:tc-config>
- Create the Terracotta boot jar:
$TERRACOTTA/bin/make-boot-jar.sh -f $RESIN/tc-config.xml
Note the path of the boot jar created by this command. You will use it in the next step.
- Edit resin.conf, making the following changes:
-
<jvm-arg>-Dcom.sun.management.jmxremote</jvm-arg> <jvm-arg>-Xbootclasspath/p:<path-to-boot-jar></jvm-arg> <jvm-arg>-Dtc.install-root=<path-to-terracotta></jvm-arg> <jvm-arg>-Dtc.config=localhost:9510</jvm-arg>
Make sure to replace the <path-to-boot-jar> and <path-to-terracotta>.
-
Remove the line
<http address="*" port="8080"/>
from <server-default> section. -
Remove the default server line
<server id="" address="127.0.0.1" port="6800"/>
-
Add two servers:
<server id="a" address="127.0.0.1" port="6800"> <http address="*" port="8080"/> </server> <server id="b" address="127.0.0.1" port="6801"> <http address="*" port="8081"/> </server>
-
Add Terracotta's session filter under the <web-app-default> section:
<filter filter-name='terracotta' filter-class='com.terracotta.session.SessionFilter'/> <filter-mapping url-pattern='/*' filter-name='terracotta'/>
-
- Copy the Terracotta session jar to Resin's lib directory:
cp $TERRACOTTA/lib/session/tc-session.jar $RESIN/lib
- Start the Terracotta server:
$TERRACOTTA/bin/start-tc-server.sh -f $RESIN/tc-config.xml
- Start both Resin servers:
cd $RESIN java -jar lib/resin.jar -server a start java -jar lib/resin.jar -server b start
- Browse to http://localhost:8080/terracotta and hit refresh a few times. You should see an updated count on each refresh.
- Open another browser window to http://localhost:8081/terracotta. You should see the count from the previous page, incremented by one.
-
You can run
$TERRACOTTA/bin/admin.sh
to see the details of the shared objects that Terracotta is managing.