Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added Servlet 3.0 continuation support, automatic continuation detect…

…ion, and improved Async REST including an example
  • Loading branch information...
commit a8d4a89e2d17f9fcde59eb52991131a01eef7059 1 parent b7a4d67
@dpp dpp authored
View
7 example/src/main/scala/bootstrap/liftweb/Boot.scala
@@ -15,7 +15,7 @@
*/
package bootstrap.liftweb
-import _root_.net.liftweb._
+import net.liftweb._
import common.{Box, Full, Empty, Failure, Loggable}
import util.{Helpers, Log, NamedPF, Props}
import http._
@@ -29,7 +29,7 @@ import widgets.autocomplete._
import comet._
import model._
import lib._
-import _root_.net.liftweb.mapper.{DB, ConnectionManager, Schemifier, DefaultConnectionIdentifier, ConnectionIdentifier}
+import net.liftweb.mapper.{DB, ConnectionManager, Schemifier, DefaultConnectionIdentifier, ConnectionIdentifier}
import _root_.java.sql.{Connection, DriverManager}
import snippet._
@@ -58,6 +58,8 @@ class Boot {
LiftRules.dispatch.append(WebServices)
+ LiftRules.dispatch.append(AsyncRest)
+
StatelessJson.init()
XmlServer.init()
@@ -212,6 +214,7 @@ object MenuInfo {
Menu("Simple Wiring") / "simple_wiring",
Menu("Wiring Invoice") / "invoice_wiring",
Menu("File Upload") / "file_upload",
+ Menu("Async REST") / "async_rest",
Menu(Loc("login", Link(List("login"), true, "/login/index"),
<xml:group>Requiring Login<strike>SiteMap</strike> </xml:group>)),
Menu("Counting") / "count"),
View
55 example/src/main/scala/net/liftweb/example/lib/AsyncRest.scala
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2011 WorldWide Conferencing, LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package net.liftweb
+package example
+package lib
+
+import net.liftweb._
+import http._
+import common._
+import rest._
+
+/**
+ * An example of Lift's RestHelper and RestContinuation
+ */
+object AsyncRest extends RestHelper {
+
+ // serve the URL /async/:id
+ serve {
+ case "async" :: id :: _ Get _ =>
+
+ // move the calculation to another thread
+ RestContinuation.async(
+ reply => {
+ Thread.sleep(2000) // sleep for 2 seconds
+ val name1 = Thread.currentThread.getName
+ val outerSesStr = S.session.toString // this should be Empty
+
+ // the code block for reply will be executed in the
+ // scope of the original request and that may mean
+ // that JDBC connections are consumed, etc.
+ reply{
+ val name2 = Thread.currentThread.getName
+ val innerSesStr = S.session.toString // this should be Full()
+ <i id={id}>name1: {name1} outer: {outerSesStr} name2: {name2}
+ inner: {innerSesStr}</i>
+ }
+ })
+ }
+}
+
+
View
75 example/src/main/webapp/async_rest.html
@@ -0,0 +1,75 @@
+<lift:surround with="default" at="content">
+ <div>
+ This example demonstrates how to use the container's
+ Asynchronous (continuation) response capabilities such
+ that no I/O threads are consumed during the calculation
+ of the REST response value.
+ <a href="/async/yep_it_works">Click</a> to see the response
+ after a 2 second delay.
+ </div>
+
+ <div>
+ <style type="text/css">
+ body {
+ color: #000000;
+ background-color: #ffffff;
+ }
+ .comment {
+ /* font-lock-comment-face */
+ color: #b22222;
+ }
+ .comment-delimiter {
+ /* font-lock-comment-delimiter-face */
+ color: #b22222;
+ }
+ .keyword {
+ /* font-lock-keyword-face */
+ color: #a020f0;
+ }
+ .string {
+ /* font-lock-string-face */
+ color: #bc8f8f;
+ }
+ .type {
+ /* font-lock-type-face */
+ color: #228b22;
+ }
+ .variable-name {
+ /* font-lock-variable-name-face */
+ color: #b8860b;
+ }
+ </style>
+
+ <pre>
+<span class="comment-delimiter">/**</span><span class="comment">
+ * An example of Lift's RestHelper and RestContinuation
+ */</span>
+<span class="keyword">object</span> <span class="variable-name">AsyncRest</span> <span class="keyword">extends</span> <span class="type">RestHelper</span> {
+
+ <span class="comment-delimiter">// </span><span class="comment">serve the URL /async/:id
+</span> serve {
+ <span class="keyword">case</span> <span class="string">"async"</span> <span class="variable-name">::</span> <span class="variable-name">id</span> <span class="variable-name">::</span> <span class="variable-name">_</span> <span class="type">Get</span> <span class="variable-name">_</span> =&gt;
+
+ <span class="comment-delimiter">// </span><span class="comment">move the calculation to another thread
+</span> RestContinuation.async(
+ reply =&gt; {
+ Thread.sleep(2000) <span class="comment-delimiter">// </span><span class="comment">sleep for 2 seconds
+</span> <span class="keyword">val</span> <span class="variable-name">name1</span> = Thread.currentThread.getName
+ <span class="keyword">val</span> <span class="variable-name">outerSesStr</span> = S.session.toString <span class="comment-delimiter">// </span><span class="comment">this should be Empty
+</span>
+ <span class="comment-delimiter">// </span><span class="comment">the code block for reply will be executed in the
+</span> <span class="comment-delimiter">// </span><span class="comment">scope of the original request and that may mean
+</span> <span class="comment-delimiter">// </span><span class="comment">that JDBC connections are consumed, etc.
+</span> reply{
+ <span class="keyword">val</span> <span class="variable-name">name2</span> = Thread.currentThread.getName
+ <span class="keyword">val</span> <span class="variable-name">innerSesStr</span> = S.session.toString <span class="comment-delimiter">// </span><span class="comment">this should be Full()
+</span> &lt;i id={id}&gt;name1: {name1} outer: {outerSesStr} name2: {name2}
+ inner: {innerSesStr}&lt;/i&gt;
+ }
+ })
+ }
+}
+</pre>
+
+ </div>
+</lift:surround>

0 comments on commit a8d4a89

Please sign in to comment.
Something went wrong with that request. Please try again.