<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Coders Like Us &#187; gravity</title>
	<atom:link href="http://coderslike.us/tag/gravity/feed/" rel="self" type="application/rss+xml" />
	<link>http://coderslike.us</link>
	<description>Things I've learned and stuff I'm thinking about.</description>
	<lastBuildDate>Sat, 04 Feb 2012 20:12:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='coderslike.us' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Coders Like Us &#187; gravity</title>
		<link>http://coderslike.us</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://coderslike.us/osd.xml" title="Coders Like Us" />
	<atom:link rel='hub' href='http://coderslike.us/?pushpress=hub'/>
		<item>
		<title>Data Push with GDS Gravity and Tomcat</title>
		<link>http://coderslike.us/2009/01/28/data-push-with-gds-gravity-and-tomcat/</link>
		<comments>http://coderslike.us/2009/01/28/data-push-with-gds-gravity-and-tomcat/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 15:23:44 +0000</pubDate>
		<dc:creator>dkavanagh</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[data push]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[gravity]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[tomcat]]></category>

		<guid isPermaLink="false">http://coderslikeus.wordpress.com/?p=15</guid>
		<description><![CDATA[I&#8217;ve been working on a project that uses the very nice application stack of mysql-hibernate-graniteds-flex and recently decided I needed to push data from the server to the client. Since I was already using GraniteDS for AMF remoting, I thought I&#8217;d take advantage of the Gravity package which does a Comet-like data push. The GDS [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coderslike.us&amp;blog=6290704&amp;post=15&amp;subd=coderslikeus&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div id="_mcePaste">I&#8217;ve been working on a project that uses the very nice application stack of mysql-hibernate-graniteds-flex and recently decided I needed to push data from the server to the client. Since I was already using GraniteDS for AMF remoting, I thought I&#8217;d take advantage of the Gravity package which does a <a href="http://en.wikipedia.org/wiki/Comet_(programming)">Comet</a>-like data push. The GDS <a href="http://www.graniteds.org/confluence/display/DOC/3.+Data+Push+(Gravity)">documentation</a> has some of the information I needed. Since I was running on OS X, I also needed <a href="http://ogoglio.wiki.sourceforge.net/OS+X+and+Native+Tomcat">these instructions</a> for getting APR installed with tomcat (which gravity requires for native I/O). BTW, from what I read, APR is a must for any production use of tomcat since it increases I/O performance through native calls to the OS.</div>
<p>After getting this all set up, I was able to get my channel configured and subscribe to it from the flex client. In my tomcat log, I could see how the comet request timeouts happened at regular intervals (as you&#8217;d expect). I wasn&#8217;t, however, getting messages sent to the client. Since that is the whole purpose of this exercise, getting this working was very important!</p>
<p>After digging through the gravity code, it seems that the thing that was missing is that the sub_topic header needs to be set on the message. Once I set that to be the same as the client was expecting, it worked great! Here is the code for a servlet I extend for some REST web services which need to send a message to my flex app.</p>
<pre>
<pre>import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import flex.messaging.messages.AsyncMessage;
import flex.messaging.messages.Message;
import org.granite.gravity.AbstractChannel;
import org.granite.gravity.Gravity;
import org.granite.gravity.tomcat.TomcatChannelFactory;

public class GravityServlet extends HttpServlet {
	private Gravity gravity;
	private AbstractChannel pubChannel;

	public void init(ServletConfig config) throws ServletException {
		gravity = (Gravity)config.getServletContext().getAttribute("org.granite.gravity.Gravity");
		pubChannel = new AbstractChannel(gravity) {
				@Override protected void clearQueue() { }
				@Override public void deliver(
						AbstractChannel from,
						Message message,
						String subscriptionId
					) { }
   			 };
		gravity.registerChannel(pubChannel);
	}

	protected void sendMessage(String msg) {
		AsyncMessage message = new AsyncMessage();
		message.setBody(msg);
		message.setHeader(AsyncMessage.SUBTOPIC_HEADER, "discussion");
		message.setDestination("etlprocess");
		gravity.publishMessage(pubChannel, message);
	}
}</pre>
<p>Here is the code that registers to receive the messages;</p>
<pre>	// this code sets up listener for server push of EDL update status
	consumer = new Consumer();
	consumer.destination = "etlprocess";
	consumer.topic = "discussion";
	consumer.subscribe();
	consumer.addEventListener(MessageEvent.MESSAGE, handleETL);</pre>
</pre>
<p>The web.xml looks like this;</p>
<pre>
<pre>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com
/dtd/web-app_2_3.dtd"&gt;
&lt;web-app&gt;
    &lt;!-- read services-config.xml file at web application startup --&gt;
    &lt;listener&gt;
        &lt;listener-class&gt;org.granite.config.GraniteConfigListener&lt;/listener-class&gt;
    &lt;/listener&gt;

    &lt;!-- handle AMF requests ([de]serialization) --&gt;
    &lt;filter&gt;
        &lt;filter-name&gt;AMFMessageFilter&lt;/filter-name&gt;
        &lt;filter-class&gt;org.granite.messaging.webapp.AMFMessageFilter&lt;/filter-class&gt;
    &lt;/filter&gt;
    &lt;filter-mapping&gt;
        &lt;filter-name&gt;AMFMessageFilter&lt;/filter-name&gt;
        &lt;url-pattern&gt;/graniteamf/*&lt;/url-pattern&gt;
    &lt;/filter-mapping&gt;

    &lt;!-- handle AMF requests (execution) --&gt;
    &lt;servlet&gt;
        &lt;servlet-name&gt;AMFMessageServlet&lt;/servlet-name&gt;
        &lt;servlet-class&gt;org.granite.messaging.webapp.AMFMessageServlet&lt;/servlet-class&gt;
        &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;
    &lt;servlet&gt;
        &lt;servlet-name&gt;GravityServlet&lt;/servlet-name&gt;
        &lt;servlet-class&gt;org.granite.gravity.tomcat.GravityTomcatServlet&lt;/servlet-class&gt;
        &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;AMFMessageServlet&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/graniteamf/*&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
    &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;GravityServlet&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/gravity/*&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
&lt;/web-app&gt;</pre>
</pre>
<p>One more piece that I seem to have left out. In the services-config.xml which configures graniteDS, I had to add a service def that defined my destination (in this case, &#8220;etlprocess&#8221;).</p>
<pre>        &lt;service id="messaging-service"
            class="flex.messaging.services.MessagingService"
            messageTypes="flex.messaging.messages.AsyncMessage"&gt;
            &lt;adapters&gt;
                &lt;adapter-definition
                    id="default"
                    class="org.granite.gravity.adapters.SimpleServiceAdapter"
                    default="true"/&gt;
            &lt;/adapters&gt;

            &lt;destination id="etlprocess"&gt;
                &lt;channels&gt;
                    &lt;channel ref="my-gravityamf"/&gt;
                &lt;/channels&gt;
            &lt;/destination&gt;
        &lt;/service&gt;</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/coderslikeus.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/coderslikeus.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/coderslikeus.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/coderslikeus.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/coderslikeus.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/coderslikeus.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/coderslikeus.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/coderslikeus.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/coderslikeus.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/coderslikeus.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/coderslikeus.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/coderslikeus.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/coderslikeus.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/coderslikeus.wordpress.com/15/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=coderslike.us&amp;blog=6290704&amp;post=15&amp;subd=coderslikeus&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://coderslike.us/2009/01/28/data-push-with-gds-gravity-and-tomcat/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/bd0b0382966354c72daa660969bca2cd?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">dkavanagh</media:title>
		</media:content>
	</item>
	</channel>
</rss>
