<?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>Anand Nalya &#187; programming</title>
	<atom:link href="http://anandnalya.com/tag/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://anandnalya.com</link>
	<description>blog</description>
	<lastBuildDate>Tue, 01 May 2012 10:25:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Role based views using css and javascript</title>
		<link>http://anandnalya.com/2011/11/16/role-based-views-using-css-and-javascript/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=role-based-views-using-css-and-javascript</link>
		<comments>http://anandnalya.com/2011/11/16/role-based-views-using-css-and-javascript/#comments</comments>
		<pubDate>Wed, 16 Nov 2011 07:40:24 +0000</pubDate>
		<dc:creator>anand</dc:creator>
				<category><![CDATA[Codeprix]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[roles]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://anandnalya.com/?p=157</guid>
		<description><![CDATA[In web applications, role based views are normally done using server side if..else tags etc. When developing pure javascript clients, one way of doing role based views is again using javascript conditionals. This approach suffers from the fact that role-related &#8230; <a href="http://anandnalya.com/2011/11/16/role-based-views-using-css-and-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In web applications, role based views are normally done using server side <code>if..else</code> <a href="http://static.springsource.org/spring-security/site/docs/3.0.x/reference/taglibs.html#d0e6296">tags</a> etc. When developing pure javascript clients, one way of doing role based views is again using javascript conditionals. This approach suffers from the fact that role-related code is scattered throughout the document. Another simple way to achieve the same is using css classes.</p>
<p>Let us assume that we have three roles, <code>ADMIN</code> and <code>USER</code> and <code>GUEST</code>. Then we will define three css classes as follows:</p>
<div id="gist-1369451" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nc">.role_admin</span><span class="o">,</span> <span class="nc">.role_user</span><span class="o">,</span> <span class="nc">.role_guest</span><span class="p">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">display</span><span class="o">:</span><span class="k">none</span><span class="p">;</span></div><div class='line' id='LC3'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1369451/f8e3f87ba993b3f17cb7cac9404c8b2566263ed3/role.css" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1369451#file_role.css" style="float:right;margin-right:10px;color:#666">role.css</a>
            <a href="https://gist.github.com/1369451">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Now consider the following html doc, that has css classes attached to elements according to the current user&#8217;s role.</p>
<div id="gist-1369451" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nt">&lt;html&gt;</span></div><div class='line' id='LC2'><span class="nt">&lt;head&gt;</span></div><div class='line' id='LC3'><span class="nt">&lt;link</span> <span class="na">href=</span><span class="s">&quot;./role.css&quot;</span> <span class="na">rel=</span><span class="s">&quot;stylesheet&quot;</span> <span class="na">type=</span><span class="s">&quot;text/css&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC4'><span class="nt">&lt;/head&gt;</span></div><div class='line' id='LC5'><span class="nt">&lt;body&gt;</span></div><div class='line' id='LC6'><span class="nt">&lt;h1</span> <span class="na">class=</span><span class="s">&quot;role_admin&quot;</span><span class="nt">&gt;</span>This should be visible to admin only.<span class="nt">&lt;h1&gt;</span></div><div class='line' id='LC7'><span class="nt">&lt;h2</span> <span class="na">class=</span><span class="s">&quot;role_user&quot;</span><span class="nt">&gt;</span>This is for the normal logged in user.<span class="nt">&lt;/h1&gt;</span></div><div class='line' id='LC8'><span class="nt">&lt;h3</span> <span class="na">class=</span><span class="s">&quot;role_guest&quot;</span><span class="nt">&gt;</span>And this is for the guest.<span class="nt">&lt;/h1&gt;</span></div><div class='line' id='LC9'><span class="nt">&lt;/body&gt;</span></div><div class='line' id='LC10'><span class="nt">&lt;/html&gt;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1369451/a30ef0aa9a9852da64669667cfd8b3fd922dc6eb/roleSample.html" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1369451#file_role_sample.html" style="float:right;margin-right:10px;color:#666">roleSample.html</a>
            <a href="https://gist.github.com/1369451">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>At this stage if you load the document in a browser, you will not see anything as nothing all the roles are invisible. So lets spruce this up with a bit of javascript to set proper css class properties.</p>
<div id="gist-1369451" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">$</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nx">ready</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// We are assuming that jQuery and jQuery Cookie plugin are included in the doc</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">role</span> <span class="o">=</span> <span class="nx">$</span><span class="p">.</span><span class="nx">cookie</span><span class="p">(</span><span class="s1">&#39;user_role&#39;</span><span class="p">);</span> <span class="c1">// Should return one of &#39;ADMIN&#39;, &#39;USER&#39; OR &#39;GUEST&#39;</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// set property for relevant css class</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">cssClass</span> <span class="o">=</span> <span class="s1">&#39;.role_&#39;</span><span class="o">+</span> <span class="nx">role</span><span class="p">.</span><span class="nx">toLowerCase</span><span class="p">();</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">(</span><span class="s2">&quot;&lt;style type=&#39;text/css&#39;&gt; &quot;</span><span class="o">+</span> <span class="nx">cssClass</span> <span class="o">+</span> <span class="s2">&quot;{ display:block } &lt;/style&gt;&quot;</span><span class="p">).</span><span class="nx">appendTo</span><span class="p">(</span><span class="s2">&quot;head&quot;</span><span class="p">);</span></div><div class='line' id='LC9'><span class="p">});</span></div><div class='line' id='LC10'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1369451/d27ccc00e3b0607785bb11cec4d118a9ca62695b/role.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1369451#file_role.js" style="float:right;margin-right:10px;color:#666">role.js</a>
            <a href="https://gist.github.com/1369451">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>If a cookie <code>role</code> is set that represents the current user&#8217;s role then adding the above snippet of code will enable all the elements tagged with css class <code>.role_{roleCookie}</code> to be visible.</p>
<p>Even though this is a very simple implementation, we can modify it easily to take into account more complex scenarios. For example, if you have completely ordered role based visibility (access level), i.e. given role R1 and R2, we can always tell which role has more visibility, then we can extend as follows:
<div id="gist-1369451" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">$</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nx">ready</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// We are assuming that jQuery and jQuery Cookie plugin are included in the doc</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">role</span> <span class="o">=</span> <span class="nx">$</span><span class="p">.</span><span class="nx">cookie</span><span class="p">(</span><span class="s1">&#39;user_role&#39;</span><span class="p">);</span> <span class="c1">// Should return one of &#39;ADMIN&#39;, &#39;USER&#39; OR &#39;GUEST&#39;</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// set property for relevant css class</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span><span class="p">(</span><span class="nx">role</span> <span class="o">==</span> <span class="s2">&quot;ADMIN&quot;</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// make everything visible</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">(</span><span class="s2">&quot;&lt;style type=&#39;text/css&#39;&gt;.role_admin, .role_user, .role_guest {display:block} &lt;/style&gt;&quot;</span><span class="p">).</span><span class="nx">appendTo</span><span class="p">(</span><span class="s2">&quot;head&quot;</span><span class="p">);</span></div><div class='line' id='LC10'><br/></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span> <span class="k">else</span> <span class="k">if</span><span class="p">(</span><span class="nx">app</span><span class="p">.</span><span class="nx">role</span> <span class="o">==</span> <span class="s2">&quot;USER&quot;</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// make user and guest part visible, admin part will remain invisible</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">(</span><span class="s2">&quot;&lt;style type=&#39;text/css&#39;&gt;.role_user, .role_guest {display:block} &lt;/style&gt;&quot;</span><span class="p">).</span><span class="nx">appendTo</span><span class="p">(</span><span class="s2">&quot;head&quot;</span><span class="p">);</span></div><div class='line' id='LC14'><br/></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span> <span class="k">else</span> <span class="k">if</span><span class="p">(</span><span class="nx">app</span><span class="p">.</span><span class="nx">role</span> <span class="o">==</span> <span class="s2">&quot;GUEST&quot;</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// only show the guest part</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">(</span><span class="s2">&quot;&lt;style type=&#39;text/css&#39;&gt;.role_guest {display:block} &lt;/style&gt;&quot;</span><span class="p">).</span><span class="nx">appendTo</span><span class="p">(</span><span class="s2">&quot;head&quot;</span><span class="p">);</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC19'><span class="p">});</span></div><div class='line' id='LC20'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1369451/ca1c750910a67bc77f119a2551e42561337a85f6/roleOrdered.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1369451#file_role_ordered.js" style="float:right;margin-right:10px;color:#666">roleOrdered.js</a>
            <a href="https://gist.github.com/1369451">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p><em>Caution:</em> One thing to note here is that all the parts of the document are sent to all the users, only what is visible on the browser is role based. So, server side authorisation checks will always be there as nothing stops a suspecting user from looking into the source and firing that dreaded request.</p>
]]></content:encoded>
			<wfw:commentRss>http://anandnalya.com/2011/11/16/role-based-views-using-css-and-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building a simple [Yahoo] S4 application</title>
		<link>http://anandnalya.com/2011/09/05/building-a-simple-yahoo-s4-application/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=building-a-simple-yahoo-s4-application</link>
		<comments>http://anandnalya.com/2011/09/05/building-a-simple-yahoo-s4-application/#comments</comments>
		<pubDate>Mon, 05 Sep 2011 13:22:04 +0000</pubDate>
		<dc:creator>anand</dc:creator>
				<category><![CDATA[Codeprix]]></category>
		<category><![CDATA[event processing]]></category>
		<category><![CDATA[hadoop]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[s4]]></category>
		<category><![CDATA[stream processing]]></category>
		<category><![CDATA[yahoo s4]]></category>

		<guid isPermaLink="false">http://anandnalya.com/?p=139</guid>
		<description><![CDATA[<p><a href="http://s4.io/">S4</a> is a distributed stream processing platform from Yahoo. It is often seen as the real-time counterpart of <a href="http://hadoop.apache.org/">Hadoop</a>. S4 being fault tolerant and horizontally scalable helps you in building very large stream processing application that can do anything from detecting earthquakes to finding that perfect bit of advertising that the visitor on your website is most likely to click.</p>
<p>At its core, an S4 application consists of a number of Processing Elements <em>(PEs)</em> that are wired together with the help of a <a href="http://static.springsource.org/spring/docs/3.1.0.M2/spring-framework-reference/html/beans.html#beans-definition">spring</a> configuration file that defines the <em>PEs</em> and the flow of events in the system. Also, events are produced by event producers that listen that sends these events to the client adapter for S4, from where, the S4 platform takes over and dispatch it to appropriate processing elements. After processing these events, <em>PEs</em> can choose to dispatch them to other <em>PEs</em> for further processing or they can choose to produce output events. Thus, arbitrarily complex behavior can be derived together by wiring a simple set of <em>PEs</em>.</p>

<p>S4 comes with a few example applications, but here is a much simpler <a href="https://github.com/anandnalya/S4WordCount">S4WordCount</a> application that shows how to:
<ol><li>Keep state in a <em>PE</em>.</li>
<li>Dispatch events from a <em>PE</em>.</li>
<li>Process multiple events from a single <em>PE</em>.</li>
<li>Write a simple java client for sending events to S4.</li></ol></p> <a href="http://anandnalya.com/2011/09/05/building-a-simple-yahoo-s4-application/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://s4.io/">S4</a> is a distributed stream processing platform from Yahoo. It is often seen as the real-time counterpart of <a href="http://hadoop.apache.org/">Hadoop</a>. S4 being fault tolerant and horizontally scalable helps you in building very large stream processing application that can do anything from detecting earthquakes to finding that perfect bit of advertising that the visitor on your website is most likely to click.</p>
<p>At its core, an S4 application consists of a number of Processing Elements <em>(PEs)</em> that are wired together with the help of a <a href="http://static.springsource.org/spring/docs/3.1.0.M2/spring-framework-reference/html/beans.html#beans-definition">spring</a> configuration file that defines the <em>PEs</em> and the flow of events in the system. Also, events are produced by event producers that listen that sends these events to the client adapter for S4, from where, the S4 platform takes over and dispatch it to appropriate processing elements. After processing these events, <em>PEs</em> can choose to dispatch them to other <em>PEs</em> for further processing or they can choose to produce output events. Thus, arbitrarily complex behavior can be derived together by wiring a simple set of <em>PEs</em>.</p>
<p>S4 comes with a few example applications, but here is a much simpler <a href="https://github.com/anandnalya/S4WordCount">S4WordCount</a> application that shows how to:</p>
<ol>
<li>Keep state in a <em>PE</em>.</li>
<li>Dispatch events from a <em>PE</em>.</li>
<li>Process multiple events from a single <em>PE</em>.</li>
<li>Write a simple java client for sending events to S4.</li>
</ol>
<p>In S4WordCount, we will build a simple <code>WordReceiverPE</code>, that will receive events in the form of word and will simply print these words on <code>stdout</code>. It will also identify sentences in the word stream and then forward these sentences for further processing to <code>SenteceReceiverPE</code>. <code>WordReceiverPE</code> will also produce receive sentence events and print them to <code>stdout</code>.</p>
<p>First lets have a look at <code>Word</code> and <code>Sentence</code>, the event object used in our example:</p>
<div id="gist-1194444" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kn">package</span> <span class="n">test</span><span class="o">.</span><span class="na">s4</span><span class="o">;</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Word</span> <span class="o">{</span></div><div class='line' id='LC4'>	<span class="kd">private</span> <span class="n">String</span> <span class="n">string</span><span class="o">;</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'>	<span class="kd">public</span> <span class="n">String</span> <span class="nf">getString</span><span class="o">()</span> <span class="o">{</span></div><div class='line' id='LC7'>		<span class="k">return</span> <span class="n">string</span><span class="o">;</span></div><div class='line' id='LC8'>	<span class="o">}</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'>	<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setString</span><span class="o">(</span><span class="n">String</span> <span class="n">message</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC11'>		<span class="k">this</span><span class="o">.</span><span class="na">string</span> <span class="o">=</span> <span class="n">message</span><span class="o">;</span></div><div class='line' id='LC12'>	<span class="o">}</span></div><div class='line' id='LC13'><br/></div><div class='line' id='LC14'>	<span class="nd">@Override</span></div><div class='line' id='LC15'>	<span class="kd">public</span> <span class="n">String</span> <span class="nf">toString</span><span class="o">()</span> <span class="o">{</span></div><div class='line' id='LC16'>		<span class="k">return</span> <span class="s">&quot;Word [string=&quot;</span> <span class="o">+</span> <span class="n">string</span> <span class="o">+</span> <span class="s">&quot;]&quot;</span><span class="o">;</span></div><div class='line' id='LC17'>	<span class="o">}</span></div><div class='line' id='LC18'><span class="o">}</span></div><div class='line' id='LC19'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1194444/84b7b2b714cefe17d525904424cd5854a0d32aae/Word.java" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1194444#file_word.java" style="float:right;margin-right:10px;color:#666">Word.java</a>
            <a href="https://gist.github.com/1194444">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
<br />
<div id="gist-1194444" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kn">package</span> <span class="n">test</span><span class="o">.</span><span class="na">s4</span><span class="o">;</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Sentence</span> <span class="o">{</span></div><div class='line' id='LC4'>	<span class="kd">private</span> <span class="n">String</span> <span class="n">string</span><span class="o">;</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'>	<span class="kd">public</span> <span class="nf">Sentence</span><span class="o">(){</span></div><div class='line' id='LC7'>		<span class="c1">// default constructor</span></div><div class='line' id='LC8'>	<span class="o">}</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'>	<span class="kd">public</span> <span class="nf">Sentence</span><span class="o">(</span><span class="n">String</span> <span class="n">string</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC11'>		<span class="k">this</span><span class="o">.</span><span class="na">string</span> <span class="o">=</span> <span class="n">string</span><span class="o">;</span></div><div class='line' id='LC12'>	<span class="o">}</span></div><div class='line' id='LC13'><br/></div><div class='line' id='LC14'>	<span class="kd">public</span> <span class="n">String</span> <span class="nf">getString</span><span class="o">()</span> <span class="o">{</span></div><div class='line' id='LC15'>		<span class="k">return</span> <span class="n">string</span><span class="o">;</span></div><div class='line' id='LC16'>	<span class="o">}</span></div><div class='line' id='LC17'><br/></div><div class='line' id='LC18'>	<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setString</span><span class="o">(</span><span class="n">String</span> <span class="n">message</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC19'>		<span class="k">this</span><span class="o">.</span><span class="na">string</span> <span class="o">=</span> <span class="n">message</span><span class="o">;</span></div><div class='line' id='LC20'>	<span class="o">}</span></div><div class='line' id='LC21'><br/></div><div class='line' id='LC22'>	<span class="kd">public</span> <span class="n">String</span> <span class="nf">getSentenceId</span><span class="o">(){</span></div><div class='line' id='LC23'>		<span class="c1">// all sentences have the same key</span></div><div class='line' id='LC24'>		<span class="k">return</span> <span class="s">&quot;1&quot;</span><span class="o">;</span></div><div class='line' id='LC25'>	<span class="o">}</span></div><div class='line' id='LC26'>	<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setSentenceId</span><span class="o">(</span><span class="n">String</span> <span class="n">id</span><span class="o">){</span></div><div class='line' id='LC27'>		<span class="c1">// do nothing</span></div><div class='line' id='LC28'>	<span class="o">}</span></div><div class='line' id='LC29'><br/></div><div class='line' id='LC30'>	<span class="nd">@Override</span></div><div class='line' id='LC31'>	<span class="kd">public</span> <span class="n">String</span> <span class="nf">toString</span><span class="o">()</span> <span class="o">{</span></div><div class='line' id='LC32'>		<span class="k">return</span> <span class="s">&quot;Sentence [string=&quot;</span> <span class="o">+</span> <span class="n">string</span> <span class="o">+</span> <span class="s">&quot;]&quot;</span><span class="o">;</span></div><div class='line' id='LC33'>	<span class="o">}</span></div><div class='line' id='LC34'><br/></div><div class='line' id='LC35'><span class="o">}</span></div><div class='line' id='LC36'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1194444/0eccaefede65714d3eafaaf0b563749819280b42/Sentence.java" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1194444#file_sentence.java" style="float:right;margin-right:10px;color:#666">Sentence.java</a>
            <a href="https://gist.github.com/1194444">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>S4 uses keys, which is a set of some properties of the event object, for routing/dispatching events. In this case since <code>Word</code> is the key-less entry point into the system, and we don&#8217;t have any key for it, but for <code>Sentence</code> which will be processed further, we have a <code>Sentence.sentenceId</code> as the key. (For simplicity, all <code>Sentence</code> have the same <code>sentenceId, 1</code>)</p>
<p>Now let&#8217;s have a look at our first <em>PE</em>, i.e. <code>WordReceiverPE</code>:</p>
<div id="gist-1194444" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kn">package</span> <span class="n">test</span><span class="o">.</span><span class="na">s4</span><span class="o">;</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="kn">import</span> <span class="nn">io.s4.dispatcher.Dispatcher</span><span class="o">;</span></div><div class='line' id='LC4'><span class="kn">import</span> <span class="nn">io.s4.processor.AbstractPE</span><span class="o">;</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'><span class="cm">/**</span></div><div class='line' id='LC7'><span class="cm"> * This class receive word and sentence events and print them to stdout</span></div><div class='line' id='LC8'><span class="cm"> */</span></div><div class='line' id='LC9'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">WordReceiverPE</span> <span class="kd">extends</span> <span class="n">AbstractPE</span> <span class="o">{</span></div><div class='line' id='LC10'>	<span class="kd">private</span> <span class="n">StringBuilder</span> <span class="n">builder</span> <span class="o">=</span> <span class="k">new</span> <span class="n">StringBuilder</span><span class="o">();</span></div><div class='line' id='LC11'><br/></div><div class='line' id='LC12'>	<span class="cm">/**</span></div><div class='line' id='LC13'><span class="cm">	 * Dispatcher that will dispatch events on &lt;code&gt;Sentence *&lt;/code&gt; stream.</span></div><div class='line' id='LC14'><span class="cm">	 */</span></div><div class='line' id='LC15'>	<span class="kd">private</span> <span class="n">Dispatcher</span> <span class="n">dispatcher</span><span class="o">;</span></div><div class='line' id='LC16'><br/></div><div class='line' id='LC17'>	<span class="kd">public</span> <span class="n">Dispatcher</span> <span class="nf">getDispatcher</span><span class="o">()</span> <span class="o">{</span></div><div class='line' id='LC18'>		<span class="k">return</span> <span class="n">dispatcher</span><span class="o">;</span></div><div class='line' id='LC19'>	<span class="o">}</span></div><div class='line' id='LC20'><br/></div><div class='line' id='LC21'>	<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setDispatcher</span><span class="o">(</span><span class="n">Dispatcher</span> <span class="n">dispatcher</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC22'>		<span class="k">this</span><span class="o">.</span><span class="na">dispatcher</span> <span class="o">=</span> <span class="n">dispatcher</span><span class="o">;</span></div><div class='line' id='LC23'>	<span class="o">}</span></div><div class='line' id='LC24'><br/></div><div class='line' id='LC25'>	<span class="cm">/**</span></div><div class='line' id='LC26'><span class="cm">	 * Process word events. This prints out the received word and also builds</span></div><div class='line' id='LC27'><span class="cm">	 * the sentence that will be dispatched once we found the end of the</span></div><div class='line' id='LC28'><span class="cm">	 * sentence identified by &lt;code&gt;.&lt;/code&gt;</span></div><div class='line' id='LC29'><span class="cm">	 * </span></div><div class='line' id='LC30'><span class="cm">	 * @param word</span></div><div class='line' id='LC31'><span class="cm">	 *            Word received on &lt;code&gt;RawWords *&lt;/code&gt; stream.</span></div><div class='line' id='LC32'><span class="cm">	 */</span></div><div class='line' id='LC33'>	<span class="kd">public</span> <span class="kt">void</span> <span class="nf">processEvent</span><span class="o">(</span><span class="n">Word</span> <span class="n">word</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC34'>		<span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;Received: &quot;</span> <span class="o">+</span> <span class="n">word</span><span class="o">);</span></div><div class='line' id='LC35'><br/></div><div class='line' id='LC36'>		<span class="c1">// keep building the sentence</span></div><div class='line' id='LC37'>		<span class="n">builder</span><span class="o">.</span><span class="na">append</span><span class="o">(</span><span class="sc">&#39; &#39;</span><span class="o">).</span><span class="na">append</span><span class="o">(</span><span class="n">word</span><span class="o">.</span><span class="na">getString</span><span class="o">().</span><span class="na">trim</span><span class="o">());</span></div><div class='line' id='LC38'><br/></div><div class='line' id='LC39'>		<span class="k">if</span> <span class="o">(</span><span class="n">builder</span><span class="o">.</span><span class="na">toString</span><span class="o">().</span><span class="na">endsWith</span><span class="o">(</span><span class="s">&quot;.&quot;</span><span class="o">))</span> <span class="o">{</span></div><div class='line' id='LC40'>			<span class="n">System</span><span class="o">.</span><span class="na">err</span><span class="o">.</span><span class="na">print</span><span class="o">(</span><span class="s">&quot;End of sentence found&quot;</span><span class="o">);</span></div><div class='line' id='LC41'><br/></div><div class='line' id='LC42'>			<span class="c1">// dispatch a Sentence event</span></div><div class='line' id='LC43'>			<span class="n">dispatcher</span><span class="o">.</span><span class="na">dispatchEvent</span><span class="o">(</span><span class="s">&quot;Sentence&quot;</span><span class="o">,</span> <span class="k">new</span> <span class="n">Sentence</span><span class="o">(</span><span class="n">builder</span><span class="o">.</span><span class="na">toString</span><span class="o">()));</span></div><div class='line' id='LC44'><br/></div><div class='line' id='LC45'>			<span class="c1">// reset buffer.</span></div><div class='line' id='LC46'>			<span class="n">builder</span><span class="o">.</span><span class="na">setLength</span><span class="o">(</span><span class="mi">0</span><span class="o">);</span></div><div class='line' id='LC47'>		<span class="o">}</span></div><div class='line' id='LC48'>	<span class="o">}</span></div><div class='line' id='LC49'><br/></div><div class='line' id='LC50'>	<span class="cm">/**</span></div><div class='line' id='LC51'><span class="cm">	 * Process sentence events. This method demonstrated that one class can</span></div><div class='line' id='LC52'><span class="cm">	 * recieve multiple type of events on different Streams.</span></div><div class='line' id='LC53'><span class="cm">	 * </span></div><div class='line' id='LC54'><span class="cm">	 * @param sentence</span></div><div class='line' id='LC55'><span class="cm">	 *            Sentence recieved on &lt;code&gt;Sentence *&lt;code&gt; stream.</span></div><div class='line' id='LC56'><span class="cm">	 */</span></div><div class='line' id='LC57'>	<span class="kd">public</span> <span class="kt">void</span> <span class="nf">processEvent</span><span class="o">(</span><span class="n">Sentence</span> <span class="n">sentence</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC58'>		<span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;Received Sentence(WordReceiverPE) : &quot;</span> <span class="o">+</span> <span class="n">sentence</span><span class="o">);</span></div><div class='line' id='LC59'>	<span class="o">}</span></div><div class='line' id='LC60'><br/></div><div class='line' id='LC61'>	<span class="nd">@Override</span></div><div class='line' id='LC62'>	<span class="kd">public</span> <span class="kt">void</span> <span class="nf">output</span><span class="o">()</span> <span class="o">{</span></div><div class='line' id='LC63'>		<span class="c1">// TODO Auto-generated method stub</span></div><div class='line' id='LC64'>	<span class="o">}</span></div><div class='line' id='LC65'><br/></div><div class='line' id='LC66'>	<span class="nd">@Override</span></div><div class='line' id='LC67'>	<span class="kd">public</span> <span class="n">String</span> <span class="nf">getId</span><span class="o">()</span> <span class="o">{</span></div><div class='line' id='LC68'>		<span class="k">return</span> <span class="k">this</span><span class="o">.</span><span class="na">getClass</span><span class="o">().</span><span class="na">getName</span><span class="o">();</span></div><div class='line' id='LC69'>	<span class="o">}</span></div><div class='line' id='LC70'><span class="o">}</span></div><div class='line' id='LC71'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1194444/5c95abd7494b8a851364a4a42945c3ddc82db1bc/WordReceiverPE.java" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1194444#file_word_receiver_pe.java" style="float:right;margin-right:10px;color:#666">WordReceiverPE.java</a>
            <a href="https://gist.github.com/1194444">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>We define a <code>StringBuilder</code> that will be used to accumulate words to form a <code>Sentence</code>. The <code>processEvent(Word)</code> method simply prints the received word on <code>stdout</code> and appends the word to the builder. It then checks if the sentence is complete by checking for <code>.</code> at the end of the <code>builder</code>  and if so, it creates a <code>Sentence</code> event(object) and dispatches it to the <code>Sentence</code> stream. Once dispatched the <code>processEvent(Sentence)</code> will receive that event and will again print the sentence to <code>stdout</code>.</p>
<p>Now let&#8217;s have a look at <code>SenteceReceiverPE</code>, our second <em>PE</em> which does nothing but print the received <code>Sentence</code> on <code>stdout</code>.</p>
<div id="gist-1194444" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kn">package</span> <span class="n">test</span><span class="o">.</span><span class="na">s4</span><span class="o">;</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="kn">import</span> <span class="nn">io.s4.processor.AbstractPE</span><span class="o">;</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'><span class="cm">/**</span></div><div class='line' id='LC6'><span class="cm"> * This class receives Sentence events and print them on stdout.</span></div><div class='line' id='LC7'><span class="cm"> */</span></div><div class='line' id='LC8'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">SentenceReceiverPE</span> <span class="kd">extends</span> <span class="n">AbstractPE</span> <span class="o">{</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'>	<span class="cm">/**</span></div><div class='line' id='LC11'><span class="cm">	 * Print received sentence event.</span></div><div class='line' id='LC12'><span class="cm">	 * @param sentence</span></div><div class='line' id='LC13'><span class="cm">	 */</span></div><div class='line' id='LC14'>	<span class="kd">public</span> <span class="kt">void</span> <span class="nf">processEvent</span><span class="o">(</span><span class="n">Sentence</span> <span class="n">sentence</span><span class="o">){</span></div><div class='line' id='LC15'>		<span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;Received Sentence: &quot;</span> <span class="o">+</span> <span class="n">sentence</span><span class="o">);</span></div><div class='line' id='LC16'>	<span class="o">}</span></div><div class='line' id='LC17'><br/></div><div class='line' id='LC18'>	<span class="nd">@Override</span></div><div class='line' id='LC19'>	<span class="kd">public</span> <span class="kt">void</span> <span class="nf">output</span><span class="o">()</span> <span class="o">{</span></div><div class='line' id='LC20'>		<span class="c1">// TODO Auto-generated method stub</span></div><div class='line' id='LC21'>	<span class="o">}</span></div><div class='line' id='LC22'><br/></div><div class='line' id='LC23'>	<span class="nd">@Override</span></div><div class='line' id='LC24'>	<span class="kd">public</span> <span class="n">String</span> <span class="nf">getId</span><span class="o">()</span> <span class="o">{</span></div><div class='line' id='LC25'>		<span class="k">return</span> <span class="k">this</span><span class="o">.</span><span class="na">getClass</span><span class="o">().</span><span class="na">getName</span><span class="o">();</span></div><div class='line' id='LC26'>	<span class="o">}</span></div><div class='line' id='LC27'><br/></div><div class='line' id='LC28'><span class="o">}</span></div><div class='line' id='LC29'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1194444/b86ab76dadb1f09c84ebbad2a2ac232343df975a/SentenceReceiverPE.java" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1194444#file_sentence_receiver_pe.java" style="float:right;margin-right:10px;color:#666">SentenceReceiverPE.java</a>
            <a href="https://gist.github.com/1194444">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Finally, lets see the content of the application config file that wires all this together and forms a valid S4 application. The name of the config file should follow the naming convention of <code>&lt;AppName&gt;-config.xml</code>. In this case we will call the config file <code>S4WordCount-conf.xml</code></p>
<div id="gist-1194444" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="cp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></div><div class='line' id='LC2'><span class="nt">&lt;beans</span> <span class="na">xmlns=</span><span class="s">&quot;http://www.springframework.org/schema/beans&quot;</span></div><div class='line' id='LC3'>	<span class="na">xmlns:xsi=</span><span class="s">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></div><div class='line' id='LC4'>	<span class="na">xsi:schemaLocation=</span><span class="s">&quot;http://www.springframework.org/schema/beans</span></div><div class='line' id='LC5'><span class="s">       http://www.springframework.org/schema/beans/spring-beans-2.0.xsd&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'>	<span class="nt">&lt;bean</span> <span class="na">id=</span><span class="s">&quot;wordCatcher&quot;</span> <span class="na">class=</span><span class="s">&quot;test.s4.WordReceiverPE&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC8'>	   <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;dispatcher&quot;</span> <span class="na">ref=</span><span class="s">&quot;dispatcher&quot;</span><span class="nt">/&gt;</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'>		<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;keys&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC11'>		  <span class="c">&lt;!--  Listen for both words and sentences --&gt;</span></div><div class='line' id='LC12'>			<span class="nt">&lt;list&gt;</span></div><div class='line' id='LC13'>				<span class="nt">&lt;value&gt;</span>RawWords *<span class="nt">&lt;/value&gt;</span></div><div class='line' id='LC14'>				<span class="nt">&lt;value&gt;</span>Sentence *<span class="nt">&lt;/value&gt;</span></div><div class='line' id='LC15'>			<span class="nt">&lt;/list&gt;</span></div><div class='line' id='LC16'>		<span class="nt">&lt;/property&gt;</span></div><div class='line' id='LC17'>	<span class="nt">&lt;/bean&gt;</span></div><div class='line' id='LC18'><br/></div><div class='line' id='LC19'>	   <span class="nt">&lt;bean</span> <span class="na">id=</span><span class="s">&quot;sentenceCatcher&quot;</span> <span class="na">class=</span><span class="s">&quot;test.s4.SentenceReceiverPE&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;keys&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;list&gt;</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;value&gt;</span>Sentence *<span class="nt">&lt;/value&gt;</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/list&gt;</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/property&gt;</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/bean&gt;</span></div><div class='line' id='LC26'><br/></div><div class='line' id='LC27'>	<span class="nt">&lt;bean</span> <span class="na">id=</span><span class="s">&quot;dispatcher&quot;</span> <span class="na">class=</span><span class="s">&quot;io.s4.dispatcher.Dispatcher&quot;</span></div><div class='line' id='LC28'>		<span class="na">init-method=</span><span class="s">&quot;init&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC29'>		<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;partitioners&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC30'>			<span class="nt">&lt;list&gt;</span></div><div class='line' id='LC31'>			<span class="c">&lt;!-- Partition on senteceId which is &quot;1&quot; for all sentences. --&gt;</span></div><div class='line' id='LC32'>				<span class="nt">&lt;ref</span> <span class="na">bean=</span><span class="s">&quot;sentenceIdPartitioner&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC33'>			<span class="nt">&lt;/list&gt;</span></div><div class='line' id='LC34'>		<span class="nt">&lt;/property&gt;</span></div><div class='line' id='LC35'>		<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;eventEmitter&quot;</span> <span class="na">ref=</span><span class="s">&quot;commLayerEmitter&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC36'>		<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;loggerName&quot;</span> <span class="na">value=</span><span class="s">&quot;s4&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC37'>	<span class="nt">&lt;/bean&gt;</span></div><div class='line' id='LC38'><br/></div><div class='line' id='LC39'>	<span class="nt">&lt;bean</span> <span class="na">id=</span><span class="s">&quot;sentenceIdPartitioner&quot;</span> <span class="na">class=</span><span class="s">&quot;io.s4.dispatcher.partitioner.DefaultPartitioner&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC40'>		<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;streamNames&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC41'>			<span class="nt">&lt;list&gt;</span></div><div class='line' id='LC42'>				<span class="nt">&lt;value&gt;</span>Sentence<span class="nt">&lt;/value&gt;</span></div><div class='line' id='LC43'>			<span class="nt">&lt;/list&gt;</span></div><div class='line' id='LC44'>		<span class="nt">&lt;/property&gt;</span></div><div class='line' id='LC45'>		<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;hashKey&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC46'>			<span class="nt">&lt;list&gt;</span></div><div class='line' id='LC47'>				<span class="nt">&lt;value&gt;</span>sentenceId<span class="nt">&lt;/value&gt;</span></div><div class='line' id='LC48'>			<span class="nt">&lt;/list&gt;</span></div><div class='line' id='LC49'>		<span class="nt">&lt;/property&gt;</span></div><div class='line' id='LC50'>		<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;hasher&quot;</span> <span class="na">ref=</span><span class="s">&quot;hasher&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC51'>		<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;debug&quot;</span> <span class="na">value=</span><span class="s">&quot;true&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC52'>	<span class="nt">&lt;/bean&gt;</span></div><div class='line' id='LC53'><br/></div><div class='line' id='LC54'><span class="nt">&lt;/beans&gt;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1194444/1b9943f836fcedc94a783707181c57a6c1c4b22b/S4WordCount-conf.xml" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1194444#file_s4_word_count_conf.xml" style="float:right;margin-right:10px;color:#666">S4WordCount-conf.xml</a>
            <a href="https://gist.github.com/1194444">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>This is a spring bean definition file. The first bean, <code>wordCatcher</code> is an object of class <code>test.s4.WordReceiverPE</code> and it injects a  dispatcher called <code>dispatcher</code> into <code>WordRecieverPE</code>. We will look at properties of dispatcher later. <code>keys</code> define the stream on which our PE will be listening for event. <code>RawWords *</code> means that it will be receiving all events on the stream named <code>RawWords</code> irrespective of their keys. Similar is the intention for <code>Sentence *</code>.</p>
<p>Our second bean is <code>sentenceCatcher</code> which is an object of class <code>test.s4.SentenceReceiverPE</code> and will accept all events on stream called <code>Sentence</code>.</p>
<p>Third is the definition for the <code>dispatcher</code> which we injected into <code>wordCatcher</code>. The dispatcher needs a <code>partitioner</code> that partitions the events based on some key and then dispatches them to appropriate <em>PEs</em>. In this case we are using the <code>DefaultPartitioner</code> whose properties are defined later by <code>sentenceIdPartitoner</code> bean which says that partition the event objects on <code>Sentence</code> stream by <code>sentenceId</code> property. <code>dispatcher</code> uses the S4 provided <code>commLayerEmitter</code> to emit the events.</p>
<h2>Running the application</h2>
<p>To run this application on the S4:</p>
<ol>
<li>Setup S4 as documented <a href="http://docs.s4.io/tutorials/getting_started.html#set-up-s4">here</a>.</li>
<li>Create a <code>S4WordCount.jar</code> from above classes.</li>
<li>Deploy the application on S4, by creating the following directory structure:
<pre><code>	/$S4_IMAGE
		/s4-apps
			/S4WordCount
				S4WordCount-conf.xml
				/lib
					S4Wordcount.jar</code></pre>
</li>
<li>Start S4: <code>$S4_IMAGE/scripts/start-s4.sh -r client-adapter &amp;</code></li>
<li>Start client adapter: <code>$S4_IMAGE/scripts/run-client-adapter.sh -s client-adapter -g s4 -d $S4_IMAGE/s4-core/conf/default/client-stub-conf.xml &amp;</code></li>
</ol>
<p>Now S4 is ready to receive events. Following is an event sender that uses the <a href="http://docs.s4.io/manual/client_usage.html">java client library</a> to send events to S4. It reads one word at a time from <code>stdin</code> and sends it to S4.</p>
<div id="gist-1194444" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kn">package</span> <span class="n">test</span><span class="o">.</span><span class="na">s4</span><span class="o">.</span><span class="na">generator</span><span class="o">;</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="kn">import</span> <span class="nn">io.s4.client.Driver</span><span class="o">;</span></div><div class='line' id='LC4'><span class="kn">import</span> <span class="nn">io.s4.client.Message</span><span class="o">;</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'><span class="kn">import</span> <span class="nn">java.io.BufferedReader</span><span class="o">;</span></div><div class='line' id='LC7'><span class="kn">import</span> <span class="nn">java.io.IOException</span><span class="o">;</span></div><div class='line' id='LC8'><span class="kn">import</span> <span class="nn">java.io.InputStreamReader</span><span class="o">;</span></div><div class='line' id='LC9'><span class="kn">import</span> <span class="nn">java.io.Reader</span><span class="o">;</span></div><div class='line' id='LC10'><br/></div><div class='line' id='LC11'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">TestMessageSender</span> <span class="o">{</span></div><div class='line' id='LC12'>	<span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC13'>		<span class="k">if</span> <span class="o">(</span><span class="n">args</span><span class="o">.</span><span class="na">length</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">err</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;No host name specified&quot;</span><span class="o">);</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">exit</span><span class="o">(</span><span class="mi">1</span><span class="o">);</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">String</span> <span class="n">hostName</span> <span class="o">=</span> <span class="n">args</span><span class="o">[</span><span class="mi">0</span><span class="o">];</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="o">(</span><span class="n">args</span><span class="o">.</span><span class="na">length</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">err</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;No port specified&quot;</span><span class="o">);</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">exit</span><span class="o">(</span><span class="mi">1</span><span class="o">);</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">int</span> <span class="n">port</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="o">;</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">try</span> <span class="o">{</span></div><div class='line' id='LC26'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">port</span> <span class="o">=</span> <span class="n">Integer</span><span class="o">.</span><span class="na">parseInt</span><span class="o">(</span><span class="n">args</span><span class="o">[</span><span class="mi">1</span><span class="o">]);</span></div><div class='line' id='LC27'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">NumberFormatException</span> <span class="n">nfe</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC28'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">err</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;Bad port number specified: &quot;</span> <span class="o">+</span> <span class="n">args</span><span class="o">[</span><span class="mi">1</span><span class="o">]);</span></div><div class='line' id='LC29'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">exit</span><span class="o">(</span><span class="mi">1</span><span class="o">);</span></div><div class='line' id='LC30'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC31'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC32'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="o">(</span><span class="n">args</span><span class="o">.</span><span class="na">length</span> <span class="o">&lt;</span> <span class="mi">3</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC33'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">err</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;No stream name specified&quot;</span><span class="o">);</span></div><div class='line' id='LC34'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">exit</span><span class="o">(</span><span class="mi">1</span><span class="o">);</span></div><div class='line' id='LC35'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC36'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">String</span> <span class="n">streamName</span> <span class="o">=</span> <span class="n">args</span><span class="o">[</span><span class="mi">2</span><span class="o">];</span></div><div class='line' id='LC37'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC38'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="o">(</span><span class="n">args</span><span class="o">.</span><span class="na">length</span> <span class="o">&lt;</span> <span class="mi">4</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC39'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">err</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;No class name specified&quot;</span><span class="o">);</span></div><div class='line' id='LC40'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">exit</span><span class="o">(</span><span class="mi">1</span><span class="o">);</span></div><div class='line' id='LC41'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC42'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">String</span> <span class="n">clazz</span> <span class="o">=</span> <span class="n">args</span><span class="o">[</span><span class="mi">3</span><span class="o">];</span>       </div><div class='line' id='LC43'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC44'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">Driver</span> <span class="n">d</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Driver</span><span class="o">(</span><span class="n">hostName</span><span class="o">,</span> <span class="n">port</span><span class="o">);</span></div><div class='line' id='LC45'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">Reader</span> <span class="n">inputReader</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span></div><div class='line' id='LC46'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">BufferedReader</span> <span class="n">br</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span></div><div class='line' id='LC47'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">try</span> <span class="o">{</span></div><div class='line' id='LC48'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="o">(!</span><span class="n">d</span><span class="o">.</span><span class="na">init</span><span class="o">())</span> <span class="o">{</span></div><div class='line' id='LC49'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">err</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;Driver initialization failed&quot;</span><span class="o">);</span></div><div class='line' id='LC50'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">exit</span><span class="o">(</span><span class="mi">1</span><span class="o">);</span></div><div class='line' id='LC51'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC52'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC53'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="o">(!</span><span class="n">d</span><span class="o">.</span><span class="na">connect</span><span class="o">())</span> <span class="o">{</span></div><div class='line' id='LC54'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">err</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;Driver initialization failed&quot;</span><span class="o">);</span></div><div class='line' id='LC55'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">exit</span><span class="o">(</span><span class="mi">1</span><span class="o">);</span>           </div><div class='line' id='LC56'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC57'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC58'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">inputReader</span> <span class="o">=</span> <span class="k">new</span> <span class="n">InputStreamReader</span><span class="o">(</span><span class="n">System</span><span class="o">.</span><span class="na">in</span><span class="o">);</span></div><div class='line' id='LC59'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">br</span> <span class="o">=</span> <span class="k">new</span> <span class="n">BufferedReader</span><span class="o">(</span><span class="n">inputReader</span><span class="o">);</span></div><div class='line' id='LC60'><br/></div><div class='line' id='LC61'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">for</span>  <span class="o">(</span><span class="n">String</span> <span class="n">inputLine</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span> <span class="o">(</span><span class="n">inputLine</span> <span class="o">=</span> <span class="n">br</span><span class="o">.</span><span class="na">readLine</span><span class="o">())</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">;)</span> <span class="o">{</span></div><div class='line' id='LC62'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">String</span> <span class="n">string</span> <span class="o">=</span> <span class="s">&quot;{\&quot;string\&quot;:\&quot;&quot;</span><span class="o">+</span><span class="n">inputLine</span><span class="o">+</span><span class="s">&quot;\&quot;}&quot;</span><span class="o">;</span></div><div class='line' id='LC63'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;sending &quot;</span> <span class="o">+</span> <span class="n">string</span><span class="o">);</span></div><div class='line' id='LC64'>				<span class="n">Message</span> <span class="n">m</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Message</span><span class="o">(</span><span class="n">streamName</span><span class="o">,</span> <span class="n">clazz</span><span class="o">,</span> <span class="n">string</span><span class="o">);</span></div><div class='line' id='LC65'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">d</span><span class="o">.</span><span class="na">send</span><span class="o">(</span><span class="n">m</span><span class="o">);</span></div><div class='line' id='LC66'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC67'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">IOException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC68'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span></div><div class='line' id='LC69'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC70'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">finally</span> <span class="o">{</span></div><div class='line' id='LC71'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">try</span> <span class="o">{</span> <span class="n">d</span><span class="o">.</span><span class="na">disconnect</span><span class="o">();</span> <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{}</span></div><div class='line' id='LC72'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">try</span> <span class="o">{</span> <span class="n">br</span><span class="o">.</span><span class="na">close</span><span class="o">();</span> <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{}</span></div><div class='line' id='LC73'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">try</span> <span class="o">{</span> <span class="n">inputReader</span><span class="o">.</span><span class="na">close</span><span class="o">();</span> <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{}</span></div><div class='line' id='LC74'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC75'>	<span class="o">}</span></div><div class='line' id='LC76'><span class="o">}</span></div><div class='line' id='LC77'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1194444/4f762ba33a7344d3ed94ec7f6a92cccddc18b826/TestMessageSender.java" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1194444#file_test_message_sender.java" style="float:right;margin-right:10px;color:#666">TestMessageSender.java</a>
            <a href="https://gist.github.com/1194444">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Run client: <code>java TestMessageSender localhost 2334 RawWords test.s4.Word</code></p>
<p>Here is the sample output on the S4 console:</p>
<pre><code>
Received: Word [string=this]
Received: Word [string=is]
Received: Word [string=a]
Received: Word [string=sentence.]

Using fast path!
Value 1, partition id 0
wrapper is stream:Sentence keys:[{sentenceId = 1}:0] keyNames:null event:Sentence [string= this is a sentence.] => 0
Received Sentence(WordReceiverPE) : Sentence [string= this is a sentence.]
Received Sentence: Sentence [string= this is a sentence.]
</code></pre></p>
]]></content:encoded>
			<wfw:commentRss>http://anandnalya.com/2011/09/05/building-a-simple-yahoo-s4-application/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Java Code Generators &#8211; A short rant</title>
		<link>http://anandnalya.com/2011/07/27/java-code-generators-a-short-rant/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=java-code-generators-a-short-rant</link>
		<comments>http://anandnalya.com/2011/07/27/java-code-generators-a-short-rant/#comments</comments>
		<pubDate>Wed, 27 Jul 2011 06:02:51 +0000</pubDate>
		<dc:creator>anand</dc:creator>
				<category><![CDATA[Codeprix]]></category>
		<category><![CDATA[Thinking]]></category>
		<category><![CDATA[code generators]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[play]]></category>
		<category><![CDATA[productivity]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ror]]></category>

		<guid isPermaLink="false">http://anandnalya.com/?p=124</guid>
		<description><![CDATA[Java is known to be a verbose language and the situation worsens when you step into bloated enterprise java world. You need to write tons of code and configure a lot of JXXX to make your simple webapp work. Though &#8230; <a href="http://anandnalya.com/2011/07/27/java-code-generators-a-short-rant/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Java is known to be a verbose language and the situation worsens when you step into bloated enterprise java world. You need to write tons of code and configure a lot of <em>JXXX</em> to make your simple webapp work. Though the situation is improving in the recent years with the introduction of convention over configuration approach and also of annotation based configurations but still, if you compare the amount of code required for a functional webapp in Java then it would be at least 2X to 4X more than that of similar webapps written in other frameworks like <a href="https://www.djangoproject.com/">Django</a> or <a href="http://rubyonrails.org/">RoR</a>.</p>
<p>A lot of java frameworks and IDEs tries to hide this complexity by generating code &#8211; from simple getters and setters to entire DAO layers and what not &#8211; that might give small productivity gains in the beginning but eventually every additional line of code in your project, either hand-written or generated by a state of art code generator, someone will need to maintain and evolve it, which according to <a href="http://users.jyu.fi/~koskinen/smcosts.htm">some</a> is almost 90% of the total software costs.</p>
<p>Thats why framework like <a href="http://www.playframework.org/">Play</a> feels like fresh air and it seems to be making inroads in the bloated enterprise java world.</p>
]]></content:encoded>
			<wfw:commentRss>http://anandnalya.com/2011/07/27/java-code-generators-a-short-rant/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My first Scheme program</title>
		<link>http://anandnalya.com/2010/08/23/my-first-scheme-program/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=my-first-scheme-program</link>
		<comments>http://anandnalya.com/2010/08/23/my-first-scheme-program/#comments</comments>
		<pubDate>Mon, 23 Aug 2010 11:45:58 +0000</pubDate>
		<dc:creator>anand</dc:creator>
				<category><![CDATA[Codeprix]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[scheme]]></category>
		<category><![CDATA[sicp]]></category>

		<guid isPermaLink="false">http://anandnalya.com/?p=116</guid>
		<description><![CDATA[I&#8217;ve just started experimenting with Scheme, specifically to follow Structure and Interpretation of Computer Programs. Here is a Scheme procedure that returns the sum of squares of two larger numbers out of the given three.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just started experimenting with Scheme, specifically to follow <a href="http://mitpress.mit.edu/sicp/full-text/book/book.html">Structure and Interpretation of Computer Programs</a>. Here is a Scheme procedure that returns the sum of squares of two larger numbers out of the given three.<br />
<div id="gist-1124413" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="p">(</span><span class="k">define </span><span class="p">(</span><span class="nf">sumLargeSquare</span> <span class="nv">a</span> <span class="nv">b</span> <span class="nv">c</span><span class="p">)</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="p">(</span><span class="k">cond </span><span class="p">((</span><span class="k">and </span><span class="p">(</span><span class="nf">&amp;lt</span><span class="c1">; a b) (&amp;lt; a c)) (+ (* b b) (* c c)))</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">((</span><span class="k">and </span><span class="p">(</span><span class="nf">&amp;lt</span><span class="c1">; b a) (&amp;lt; b c)) (+ (* a a) (* c c)))</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">(</span><span class="k">else </span><span class="p">(</span><span class="nb">+ </span><span class="p">(</span><span class="nb">* </span><span class="nv">a</span> <span class="nv">a</span><span class="p">)</span> <span class="p">(</span><span class="nb">* </span><span class="nv">b</span> <span class="nv">b</span><span class="p">))))</span></div><div class='line' id='LC5'><span class="p">)</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1124413/eb93d491f382fe38c9b71f1aa7c4f6e59479421e/gistfile1.scm" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1124413#file_gistfile1.scm" style="float:right;margin-right:10px;color:#666">gistfile1.scm</a>
            <a href="https://gist.github.com/1124413">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
 </p>
]]></content:encoded>
			<wfw:commentRss>http://anandnalya.com/2010/08/23/my-first-scheme-program/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Minifying Javascript/css without changing file references in your source</title>
		<link>http://anandnalya.com/2009/06/05/minifying-javascriptcss-without-changing-file-references-in-your-source/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=minifying-javascriptcss-without-changing-file-references-in-your-source</link>
		<comments>http://anandnalya.com/2009/06/05/minifying-javascriptcss-without-changing-file-references-in-your-source/#comments</comments>
		<pubDate>Fri, 05 Jun 2009 13:21:02 +0000</pubDate>
		<dc:creator>anand</dc:creator>
				<category><![CDATA[Codeprix]]></category>
		<category><![CDATA[compression]]></category>
		<category><![CDATA[compressor]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://anandnalya.com/?p=79</guid>
		<description><![CDATA[Rule 10 of Steve Souders High Performance Web Sites: Minify Javascript The most common problem faced while implemnting this is how you handle the full and minified version and how to change there reference in referencing documents. One of the &#8230; <a href="http://anandnalya.com/2009/06/05/minifying-javascriptcss-without-changing-file-references-in-your-source/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Rule 10 of Steve Souders High Performance Web Sites: <a href="http://developer.yahoo.net/blog/archives/2007/07/high_performanc_8.html">Minify Javascript</a></p>
<p>The most common problem faced while implemnting this is how you handle the full and minified version and how to change there reference in referencing documents. One of the easier ways to do this is to make it part of the deployment process. </p>
<p>Here are the relevent steps involved.</p>
<p>I&#8217;m using <a href="http://developer.yahoo.com/yui/compressor/">YUI Compressor</a>.<br />
<div id="gist-1124427" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c">#!/bin/bash</span></div><div class='line' id='LC2'>&nbsp;</div><div class='line' id='LC3'><span class="c">#Execute this script after checking out the latest source from repository.</span></div><div class='line' id='LC4'>&nbsp;</div><div class='line' id='LC5'><span class="c">#Minify all javascript files</span></div><div class='line' id='LC6'><span class="nb">cd</span> /path/to/javascript</div><div class='line' id='LC7'><span class="k">for </span>x in <span class="sb">`</span>ls *.js<span class="sb">`</span></div><div class='line' id='LC8'><span class="k">do</span></div><div class='line' id='LC9'><span class="k">        </span>java -jar /path/to/compressor/yuicompressor-2.4.2.jar -o <span class="k">${</span><span class="nv">x</span><span class="p">%%.*</span><span class="k">}</span>-min.js --preserve-semi  <span class="nv">$x</span></div><div class='line' id='LC10'><span class="k">done</span></div><div class='line' id='LC11'>&nbsp;</div><div class='line' id='LC12'><span class="c">#Minfiy all css files</span></div><div class='line' id='LC13'><span class="nb">cd</span> /path/to/css</div><div class='line' id='LC14'><span class="k">for </span>x in <span class="sb">`</span>ls *.css<span class="sb">`</span></div><div class='line' id='LC15'><span class="k">do</span></div><div class='line' id='LC16'><span class="k">        </span>java -jar /path/to/compressor/yuicompressor-2.4.2.jar -o <span class="k">${</span><span class="nv">x</span><span class="p">%%.*</span><span class="k">}</span>-min.css  <span class="nv">$x</span></div><div class='line' id='LC17'><span class="k">done</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1124427/ad846053c02470535698614eed39c9401f5caa88/minify.sh" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1124427#file_minify.sh" style="float:right;margin-right:10px;color:#666">minify.sh</a>
            <a href="https://gist.github.com/1124427">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
<br />
Now you don&#8217;t want to replace all references to <em>x.css</em> or <em>x.js</em> in your development code with references to <em>x-min.css</em> and <em>x-min.js</em> respectively. So what you can do is rewrite all those filenames at the web server level.</p>
<p>For apache the following rewrite rules work fine:<br />
<div id="gist-1124427" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'>#enable rewriting</div><div class='line' id='LC2'>RewriteEngine on</div><div class='line' id='LC3'>RewriteRule /(.*)\.js /$1-min.js</div><div class='line' id='LC4'>RewriteRule /(.*)\.css /$1-min.css</div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1124427/0a7c12d3f31f779d173b821962361c0d2f176f65/httpd.conf" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1124427#file_httpd.conf" style="float:right;margin-right:10px;color:#666">httpd.conf</a>
            <a href="https://gist.github.com/1124427">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
<br />
<em>Caution:</em> Remember to delete existing minified css/js file before running the minifying script or you will end up with file names like x-min-min-min.js and so on. One way to do this is to clear the js/css folder before checking out files from your source repository.</p>
]]></content:encoded>
			<wfw:commentRss>http://anandnalya.com/2009/06/05/minifying-javascriptcss-without-changing-file-references-in-your-source/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

