<?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>JavaScriptr &#187; Java</title>
	<atom:link href="http://www.javascriptr.com/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javascriptr.com</link>
	<description>A blog about JavaScript and other web development technologies</description>
	<lastBuildDate>Sun, 20 Sep 2009 19:36:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>GWT + Grizzly + Comet</title>
		<link>http://www.javascriptr.com/2008/05/28/gwt-grizzly-comet/</link>
		<comments>http://www.javascriptr.com/2008/05/28/gwt-grizzly-comet/#comments</comments>
		<pubDate>Thu, 29 May 2008 01:51:57 +0000</pubDate>
		<dc:creator>mo rock</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[comet]]></category>
		<category><![CDATA[grizzly]]></category>
		<category><![CDATA[gwt]]></category>

		<guid isPermaLink="false">http://www.javascriptr.com/2008/05/28/gwt-grizzly-comet/</guid>
		<description><![CDATA[Ok, so you&#8217;re probably looking for a simple example of GWT + Grizzly + Comet. Jean Francois and his buddies at JavaOne screwed us with their vague PDF. Bastards! I gotta say this, though, Jean was right, its not too hard to figure out. This implementation uses XHR long polling, glassfishv2.ur2 and grizzly comet 1.0.9. [...]]]></description>
			<content:encoded><![CDATA[<p>Ok, so you&#8217;re probably looking for a simple example of GWT + Grizzly + Comet. Jean Francois and his buddies at JavaOne screwed us with their vague PDF. Bastards! I gotta say this, though, Jean was right, its not too hard to figure out.</p>
<p>This implementation uses XHR long polling, glassfishv2.ur2 and grizzly comet 1.0.9.</p>
<p><strong>The problem: RemoteServiceServlet</strong></p>
<p>GWT loves POST. All RPC calls made back to your subclass of RemoteServiceServlet will hit the doPost method. However, this method is final and cannot be overridden &#8211; meaning we cannot create a comet handler for the request in our subclass of RemoteServiceServlet. We want to continue using RPC but we don&#8217;t want to use RemoteServiceServlet. WTF?</p>
<p><strong>The solution:  SimpleServiceServlet</strong></p>
<p>Pretty much the RemoteServiceServlet is too secretive. So I <span id="more-13"></span>pulled out its code and gutted it- creating SimpleServiceServlet. It&#8217;s new doPost method calls doConnect which you can override in your subclass &#8211; allowing you to create a comet handler. Yay.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CometServiceImpl <span style="color: #000000; font-weight: bold;">extends</span> SimpleServiceServlet <span style="color: #000000; font-weight: bold;">implements</span> CometService <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> CometContext cc<span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> CometServiceImpl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">class</span> GWTCometHandler <span style="color: #000000; font-weight: bold;">implements</span> CometHandler<span style="color: #339933;">&lt;</span>HttpServletResponse<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
      ....
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doConnect<span style="color: #009900;">&#40;</span>HttpServletRequest req, HttpServletResponse res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
         res.<span style="color: #006633;">setContentType</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;text/html;charset=ISO-8859-1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         GWTCometHandler ch <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GWTCometHandler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         ch.<span style="color: #006633;">attach</span><span style="color: #009900;">&#40;</span>res<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         ch.<span style="color: #006633;">attachRequest</span><span style="color: #009900;">&#40;</span>req<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         cc.<span style="color: #006633;">addCometHandler</span><span style="color: #009900;">&#40;</span>ch<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;connect: e=&quot;</span> <span style="color: #339933;">+</span> e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Other issues: The RPC Thing<br />
</strong></p>
<p>GWT expects a return value from RPC methods. But what about in the case of comet? There technically isn&#8217;t an RPC method &#8211; a connection is established and then the client waits. Well, lets not re-write GWT, lets just make it happy. In traditional GWT RPC apps we extend the &#8220;RemoteService&#8221; interface that&#8217;s used by the server and the client. Let&#8217;s go ahead and do that:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> CometService <span style="color: #000000; font-weight: bold;">extends</span> RemoteService <span style="color: #009900;">&#123;</span>
   Message comet<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And lets also create the object we are going to return via comet and RPC:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Message <span style="color: #000000; font-weight: bold;">implements</span> IsSerializable<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> text<span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setText<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> text<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">text</span><span style="color: #339933;">=</span>text<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getText<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> text<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>So we basically follow the pattern perscribed to us by GWT &#8211; but heres the catch. In our implementation of CometService we do the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> Message comet<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Yes, we return null! This way we make GWT happy &#8211; it can continue to do all of its fancy RPC&#8217;ing. And we can do comet without any hassles. But, how do we return serialized objects to the client?</p>
<p><strong>Return serialized objects?</strong></p>
<p>We do it in our subclass of grizzly&#8217;s CometHandler -</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> GWTCometHandler <span style="color: #000000; font-weight: bold;">implements</span> CometHandler<span style="color: #339933;">&lt;</span>HttpServletResponse<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
   HttpServletResponse res<span style="color: #339933;">;</span>
   HttpServletRequest req<span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onEvent<span style="color: #009900;">&#40;</span>CometEvent ce<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;onEvent&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">// GWTEvent event = (GWTEvent) ce.attachment();</span>
      Message msg <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Message<span style="color: #009900;">&#41;</span> ce.<span style="color: #006633;">attachment</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #666666; font-style: italic;">//LETS SERIALIZE!!!</span>
         <span style="color: #003399;">String</span> encoded <span style="color: #339933;">=</span> RPC.<span style="color: #006633;">encodeResponseForSuccess</span><span style="color: #009900;">&#40;</span>CometServiceImpl.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getMethod</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;comet&quot;</span><span style="color: #009900;">&#41;</span>, msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #666666; font-style: italic;">//LETS WRITE THE RESPONSE</span>
         writeResponse<span style="color: #009900;">&#40;</span>req, res, encoded<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         removeThisFromContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">SecurityException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>SerializationException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">NoSuchMethodException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
   ......
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>The Code:</strong></p>
<p>A good sample app is worth a thousand words &#8211; <a href="http://www.glicky.com/downloads/MyProject.jar" title="MyProject.jar">download it here</a>.  This is a very simple and rough attempt at getting GWT working with Grizzly Comet. I&#8217;ll be posting an improved version at some point. It does not work in the GWT shell. You will have to deploy it to glassfish.<br />
<</p>
]]></content:encoded>
			<wfw:commentRss>http://www.javascriptr.com/2008/05/28/gwt-grizzly-comet/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
