<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Caffeine Lab &#187; play framework</title>
	<atom:link href="http://erwan.jp/tag/play-framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://erwan.jp</link>
	<description>Really tasty technologies</description>
	<lastBuildDate>Wed, 07 Jul 2010 10:58:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<cloud domain='erwan.jp' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Asynchronous HTTP Client in Play Framework</title>
		<link>http://erwan.jp/2010/06/29/asynchronous-http-client-in-play-framework/</link>
		<comments>http://erwan.jp/2010/06/29/asynchronous-http-client-in-play-framework/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 13:45:52 +0000</pubDate>
		<dc:creator>erwan</dc:creator>
				<category><![CDATA[hacking]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[play framework]]></category>

		<guid isPermaLink="false">http://erwan.jp/?p=490</guid>
		<description><![CDATA[I just finished migrating Play&#8217;s http client to Ning&#8217;s own asynchronous library. What does it mean? A lot. When you want to retrieve data from a different server, your web application behaves like a web client. Rather than serving a resource to a client, it requests one from a different server. For example, that&#8217;s what [...]]]></description>
			<content:encoded><![CDATA[<p>I just finished migrating Play&#8217;s http client to <a href="http://github.com/ning/async-http-client">Ning&#8217;s own asynchronous library</a>. What does it mean? A lot.</p>
<p>When you want to retrieve data from a different server, your web application behaves like a web client. Rather than serving a resource to a client, it requests one from a different server. For example, that&#8217;s what you would do if you want to interact with the Twitter API or if you want to build a web based feed reader.</p>
<h3>HTTP Client Calls in Play 1.0</h3>
<p>Play has a pretty cool http client library &#8211; pretty cool in the sense that it&#8217;s very simple to use compared to what you usually get in Java. I can get the content of a resource by calling:</p>
<p>[java]<br />
String body = WS.url(&#8220;http://erwan.jp/&#8221;).get().getString();<br />
[/java]</p>
<p>In the same fashion, I can do a post and retrieve the JSON result:</p>
<p>[java]<br />
JsonElement response = WS.url(&#8220;http://api.server.tld/new&#8221;).body(&#8220;content&#8221;).post().getJson();<br />
[/java]</p>
<p>As a comparison, <a href="http://hc.apache.org/httpclient-3.x/tutorial.html">see how it works</a> with Apache HttpClient.</p>
<p>However there&#8217;s a big flaw in this API: it can only be used synchronously. That means that your Java thread will be blocked until the response from the server is received. Depending on the server it could take several seconds, so it is a real issue. If you need to do 5 calls then work on the result, you will have to wait for the previous call to be done before you can launch the second. In Play you can use jobs to have your calls executed in a separate thread, but it&#8217;s a burder to have to create jobs for that and kind of defeats the purpose of using play.libs.WS.</p>
<h3>Play 1.1: Introducing Asynchronous Calls</h3>
<p>Since a commit I did last week, additional methods are available on the request object. Now you can do:</p>
<p>[java]<br />
Future&lt;HttpResponse&gt; response = WS.url(&#8220;http://erwan.jp/&#8221;).getAsync();<br />
[/java]</p>
<p>Now your call is done in a thread &#8211; you can manipulate your <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/Future.html#method_summary">Future</a> object to test if the result is available, to cancel the call or to block the thread until the result is ready. Let&#8217;s say you need to do 3 API calls, and then use the results together. With the synchronous library you have to serialize the calls, but now you can do the calls in parallel:</p>
<p>[java]<br />
// Launch all three calls in parallel<br />
Future&lt;HttpResponse&gt; future1 = WS.url(&#8220;http://server/api/one&#8221;).getAsync();<br />
Future&lt;HttpResponse&gt; future2 = WS.url(&#8220;http://server/api/two&#8221;).getAsync();<br />
Future&lt;HttpResponse&gt; future3 = WS.url(&#8220;http://server/api/three&#8221;).getAsync();<br />
// Now that the calls are launched in separate threads, wait for the results<br />
Document xml1 = future1.get().getXml();<br />
Document xml2 = future2.get().getXml();<br />
Document xml3 = future3.get().getXml();<br />
[/java]</p>
<p>Launch the calls in parallel means a much quicker response, so that&#8217;s an improvement in the case we have several calls to do. But we are still blocking one of Play&#8217;s threads until the three calls are done.</p>
<h3>Avoid blocking Play&#8217;s threads</h3>
<p>Before we go on, it&#8217;s important to understand Play&#8217;s threads pool. It&#8217;s configurable but usually there is one IO thread and 2 execution threads. When the IO thread gets a request it passes it to one of the execution thread, the execution thread calculates it and passes it back to the IO thread that will queue it to serve it to the client who originated the request.</p>
<p>That means that the execution of the action on the controller will block one thread. So if you do a synchronous web service call from your action, you&#8217;ll block a precious thread for all the time you wait for the remote server to respond to your server. When all threads are blocked waiting for a result, other requests get queued. Your site&#8217;s response is slow, and your users are unhappy.</p>
<p>So here is how you can free the thread while you wait for the remote server to respond. The following code is an action, within a controller:</p>
<p>[java]<br />
﻿private static Future<HttpResponse> response;<br />
public static void mirrorFeed() throws Exception {<br />
    if (request.isNew) {<br />
        response = WS.url(&#8220;http://planet.playframework.org/feed&#8221;).getAsync();<br />
        waitFor(response);<br />
    } else {<br />
        renderXml(response.get().getXml());<br />
    }<br />
}<br />
[/java]</p>
<p>This can be tricky to understand at a first glance, so here is the process.</p>
<ul>
<li>Your action gets called a first time: request.isNew is true. An asynchronous HTTP call is made to the playframework.org server.</li>
<li>The &#8220;waitFor(Future<?>)&#8221; (static method on the Controller class) tells Play to wait until the response is received.
<li>When the answer is ready, Play will call the action again. This time, request.isNew is false. We know that the Future is ready so we can do a get() to retrieve the HttpResponse instance. Here we&#8217;re just serving it to the user, but you see how we could parse the feed to use just some information in our response.</li>
</ul>
<p>So here you go: the use of an asynchronous call prevents to block an execution thread, and the performance of your application will not be affected by the response time of the remote server.</p>
<h3>When you should still use a job</h3>
<p>While the waitFor trick prevents you from blocking a Play thread, your user still has to wait for the web service call to be back before he gets his response. In other words, your application may feel slow to the user.</p>
<p>When the information you need is general enough, for example when it comes from a public feed, it can still be a better approach to keep the information up-to-date using a job. At the time the user will fetch the information it will not be the most recent, but the page will be served to the user much quicker.</p>
<p>This is what I am doing for the Twitter box on <a href="http://planet.playframework.org">Planet Play</a>: I don&#8217;t pull it for each visitor at request time (that would be insane), but I have a Job refreshing the information every 5 minutes. Any page rendered will never get anything older than 5 minutes.</p>
<p>[java]<br />
@Every(&#8220;5mn&#8221;)<br />
public class TwitterJob extends Job {<br />
	public void doJob() {<br />
		try {<br />
			TwitterSearch.refresh(&#8220;playframework&#8221;);<br />
		} catch (UnsupportedEncodingException e) {<br />
			Logger.error(&#8220;Error refreshing Twitter search&#8221;);<br />
		}<br />
	}<br />
}<br />
[/java]</p>
<p>The information is then stored in cache and retrieved to render the front page.</p>
]]></content:encoded>
			<wfw:commentRss>http://erwan.jp/2010/06/29/asynchronous-http-client-in-play-framework/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Testing in Play&#8217;s Eclipse Plugin</title>
		<link>http://erwan.jp/2010/03/11/testing-in-plays-eclipse-plugin/</link>
		<comments>http://erwan.jp/2010/03/11/testing-in-plays-eclipse-plugin/#comments</comments>
		<pubDate>Thu, 11 Mar 2010 10:09:35 +0000</pubDate>
		<dc:creator>erwan</dc:creator>
				<category><![CDATA[Misc]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[play framework]]></category>

		<guid isPermaLink="false">http://erwan.jp/?p=484</guid>
		<description><![CDATA[In Play&#8216;s Eclipse plugin, I just added a simple integration with the test framework. Currently, all it does is opening the test page directly within Eclipse. The obvious next step would be that, in case of a failure typically, links from the page brings you directly to your code at the correct line. That would [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.playframework.org">Play</a>&#8216;s <a href="http://www.github.com/erwan/playclipse">Eclipse plugin</a>, I just added a simple integration with the test framework. Currently, all it does is opening the test page directly within Eclipse.</p>
<div id="attachment_485" class="wp-caption alignnone" style="width: 310px"><a href="http://erwan.jp/wp-content/uploads/2010/03/Screenshot.png"><img class="size-medium wp-image-485" title="Play Tests in Eclipse" src="http://erwan.jp/wp-content/uploads/2010/03/Screenshot-300x217.png" alt="" width="300" height="217" /></a><p class="wp-caption-text">Play Tests in Eclipse</p></div>
<p>The obvious next step would be that, in case of a failure typically, links from the page brings you directly to your code at the correct line. That would be also very useful for error pages, just like <a href="http://guillaume.bort.fr">Guillaume</a> did in his <a href="http://bazaar.launchpad.net/~play-developers/play/1.0/annotate/head:/support/textmate.zip">Textmate integration</a>. So I did some research, what is it possible to do with Eclipse?</p>
<p>I found a <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=4922">very old bug, opened in 2001</a>, to support opening files directly from the command line. After nearly 10 years the bug is finally fixed: it&#8217;s in the not-yet-released 3.6 version of Eclipse. It&#8217;s such a basic feature it&#8217;s surprising they couldn&#8217;t do it earlier, but at least they eventually did it. So with a nightly build of Eclipse, you can open files like this:</p>
<pre id="comment_text_72">eclipse -name Eclipse --launcher.openFile &lt;qualified file name&gt;</pre>
<p>That&#8217;s the most important piece already in place, so it&#8217;s good news. But that&#8217;s not enough. To jump right to the error, you need to be able to open the file at a given line number. You don&#8217;t want to have to search in your code for the line responsible for the error. So I opened <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=305336">bug 305336</a>. Looking at the patch for bug 4922, adding the line number seems trivial to do. I&#8217;ll probably submit a patch and cross my fingers to have it in 3.6 before it&#8217;s released.</p>
<p>The last piece we would need is Eclipse registering itself as a protocol handler. That would allow us to insert URLs such as:</p>
<pre>eclipse://open?url=file://&lt;qualified file name&gt;</pre>
<p>Again, I opened a bug (<a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=305345">#305345</a>). It&#8217;s not as critical as the other items because this one can be created manually. For Windows there&#8217;s a key to add to the registry, for Gnome it&#8217;s a key in gconf and for OSX I&#8217;m sure there is something similar. Of course, it would be better if it Eclipse registered itself so the user won&#8217;t have to mess with his registry.</p>
]]></content:encoded>
			<wfw:commentRss>http://erwan.jp/2010/03/11/testing-in-plays-eclipse-plugin/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Deploy a Play Application on Gandi Cloud</title>
		<link>http://erwan.jp/2010/02/23/deploy-a-play-application-on-gandi-cloud/</link>
		<comments>http://erwan.jp/2010/02/23/deploy-a-play-application-on-gandi-cloud/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 21:14:23 +0000</pubDate>
		<dc:creator>erwan</dc:creator>
				<category><![CDATA[tech]]></category>
		<category><![CDATA[cloud]]></category>
		<category><![CDATA[gandi]]></category>
		<category><![CDATA[play framework]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://erwan.jp/?p=478</guid>
		<description><![CDATA[Gandi Cloud is a cloud hosting service, similar to Amazon&#8217;s EC2. Since you get full access to a Linux installation, deployment is similar to using a dedicated server. They have prepackaged solutions for various frameworks but not for Play. Fortunately deploying Play in general is pretty straight-forward. Note that, just like EC2, it&#8217;s not free [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;"><a href="https://www.gandi.net/hebergement/">Gandi Cloud</a> is a cloud hosting service, similar to Amazon&#8217;s EC2. Since you get full access to a Linux installation, deployment is similar to using a dedicated server. They have prepackaged solutions for various frameworks but not for <a href="http://www.playframework.org">Play</a>. Fortunately deploying Play in general is pretty straight-forward.</p>
<p style="text-align: justify;">Note that, just like EC2, it&#8217;s not free hosting &#8211; you have to pay for it. But they have fairly reasonable prices.</p>
<h3>1. Create a server on Gandi</h3>
<p style="text-align: justify;">Make sure you choose a solution that gives you root access to the machine, &#8220;expert mode&#8221;. In the rest I&#8217;ll assume you choose Ubuntu, but any distribution would work.</p>
<h3>2. Install the Requirements</h3>
<p style="text-align: justify;">ssh to your server, and su to get root access. Now:</p>
<pre style="text-align: justify;">apt-get install openjdk-6-jre unzip</pre>
<p style="text-align: justify;">See how fast is the download, I bet they have a local proxy!</p>
<h3>3. Download Play</h3>
<p style="text-align: justify;">exit to get back to regular user mode, then:</p>
<p style="text-align: justify;">
<pre>wget http://download.playframework.org/releases/play-1.0.1.zip
unzip play-1.0.1</pre>
<h3>4. Run It!</h3>
<p style="text-align: justify;">Now you&#8217;re ready to run your app! Copy your app to your server by using <a href="http://en.wikipedia.org/wiki/Secure_copy">scp</a> for example, switch to to prod mode and you can run:</p>
<pre>play start yourAppFolder</pre>
<p style="text-align: justify;">That&#8217;s it, your application is running on your Gandi cloud server.</p>
<h3>5. HTTP Port, database</h3>
<p style="text-align: justify;">Since it&#8217;s a Cloud environment, you can easily create one server for each app. In this case, just configure Play to run on port 80 and you&#8217;re done. But if one Gandi server part is too big for one application and you can&#8217;t afford buying too many parts, you can run a web front-end to host several Play apps on the same server.</p>
<p style="text-align: justify;">See the Play documentation about <a href="http://www.playframework.org/documentation/1.0.1/production">running in production</a> for instructions on how to do it.</p>
<p style="text-align: justify;">For the database, you could configure one manually on the same server, but it&#8217;s much easier to create a separate MySQL server. You can create a server in &#8220;Gandi AI&#8221; mode, where the server is managed by Gandi and you don&#8217;t have direct access to it. That way, you can create a MySQL servers in a few clicks and point your Play app(s) to that separate server.</p>
<p style="text-align: justify;">
]]></content:encoded>
			<wfw:commentRss>http://erwan.jp/2010/02/23/deploy-a-play-application-on-gandi-cloud/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Going to FOSDEM, talking about Play</title>
		<link>http://erwan.jp/2010/01/23/going-to-fosdem-talking-about-play/</link>
		<comments>http://erwan.jp/2010/01/23/going-to-fosdem-talking-about-play/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 20:21:20 +0000</pubDate>
		<dc:creator>erwan</dc:creator>
				<category><![CDATA[Misc]]></category>
		<category><![CDATA[conference]]></category>
		<category><![CDATA[fosdem]]></category>
		<category><![CDATA[play framework]]></category>

		<guid isPermaLink="false">http://erwan.jp/?p=475</guid>
		<description><![CDATA[I will be at FOSDEM in Bruxelles on Saturday 6 February. I will make a talk about the Play framework in the Free Java session, at 17:15.]]></description>
			<content:encoded><![CDATA[<p>I will be at <a href="http://www.fosdem.org/2010/">FOSDEM</a> in Bruxelles on Saturday 6 February. I will make a talk about the Play framework in the <a href="http://www.fosdem.org/2010/schedule/devrooms/freejava">Free Java session</a>, at 17:15.</p>
<p><a href="http://erwan.jp/wp-content/uploads/2010/01/fosdem-2010.png"><img class="aligncenter size-full wp-image-476" title="FOSDEM 2010" src="http://erwan.jp/wp-content/uploads/2010/01/fosdem-2010.png" alt="I" width="150" height="89" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://erwan.jp/2010/01/23/going-to-fosdem-talking-about-play/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What I&#8217;ve been up to: Zenexity, Play</title>
		<link>http://erwan.jp/2009/11/30/what-ive-been-up-to-zenexity-play/</link>
		<comments>http://erwan.jp/2009/11/30/what-ive-been-up-to-zenexity-play/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 11:01:20 +0000</pubDate>
		<dc:creator>erwan</dc:creator>
				<category><![CDATA[hacking]]></category>
		<category><![CDATA[life]]></category>
		<category><![CDATA[tech]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[play framework]]></category>

		<guid isPermaLink="false">http://erwan.jp/?p=469</guid>
		<description><![CDATA[Busy as I was, I realized I didn&#8217;t blog about my recent employment change. I left Yoono 2 months ago to join a company called Zenexity (site in French). It&#8217;s really cool because after Flock and Yoono that were very similar (consumer oriented/social mashup/Mozilla technologies), I get to work on really different stuff: more server-side, [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Busy as I was, I realized I didn&#8217;t blog about my recent employment change. I left <a href="http://yoono.com/">Yoono</a> 2 months ago to join a company called <a href="http://www.zenexity.fr">Zenexity</a> (site in French). It&#8217;s really cool because after <a href="http://www.flock.com">Flock</a> and Yoono that were very similar (consumer oriented/social mashup/Mozilla technologies), I get to work on really different stuff: more server-side, and more business oriented. But still with a strong R&amp;D component, and it&#8217;s something that really motivated me to get on board with Zenexity: they&#8217;re independent because they earn their own money (e.g. don&#8217;t live on VC money) but still spend a lot of effort in R&amp;D projects. Projects for customers also are really state-of-the-art of the web.</p>
<p style="text-align: justify;">Specifically, they (I mean &#8220;we&#8221;) have an Open Source project called the <a href="http://www.playframework.org">Play! Framework</a>. It&#8217;s an MVC framework similar to <a href="http://www.djangoproject.com/">Django</a> or <a href="http://rubyonrails.org/">Ruby on Rails</a>, in Java. Within the Java world, I think it&#8217;s pretty disruptive. It contrasts from bloated stacks, and manages to provide simplicity and productivity to Java web development. Also, it <em>speaks the language of the web</em> by making it easy to create RESTful web apps, pretty URLs and web services.</p>
<p style="text-align: justify;">Here is a screencast I did last month for the 1.0 release.</p>
<p style="text-align: justify;"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="400" height="300" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://vimeo.com/moogaloop.swf?clip_id=7087610&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=BCE569&amp;fullscreen=1" /><embed type="application/x-shockwave-flash" width="400" height="300" src="http://vimeo.com/moogaloop.swf?clip_id=7087610&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=0&amp;show_portrait=0&amp;color=BCE569&amp;fullscreen=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p><a href="http://vimeo.com/7087610">A web app  in 10 minutes using Play!</a> from <a href="http://vimeo.com/user2463720">zenexity</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://erwan.jp/2009/11/30/what-ive-been-up-to-zenexity-play/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
