<?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>c0nnexx10n : C0nnect1ng L1fe w1th Techn010gy</title>
	<atom:link href="http://vikashazrati.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://vikashazrati.wordpress.com</link>
	<description>Byte by Byte Impressions on Technology, People and Process !</description>
	<lastBuildDate>Thu, 08 Dec 2011 07:31:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='vikashazrati.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>c0nnexx10n : C0nnect1ng L1fe w1th Techn010gy</title>
		<link>http://vikashazrati.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://vikashazrati.wordpress.com/osd.xml" title="c0nnexx10n : C0nnect1ng L1fe w1th Techn010gy" />
	<atom:link rel='hub' href='http://vikashazrati.wordpress.com/?pushpress=hub'/>
		<item>
		<title>QuckTip: Tagging your code on git</title>
		<link>http://vikashazrati.wordpress.com/2011/12/08/qucktip-tagging-your-code-on-git/</link>
		<comments>http://vikashazrati.wordpress.com/2011/12/08/qucktip-tagging-your-code-on-git/#comments</comments>
		<pubDate>Thu, 08 Dec 2011 07:27:16 +0000</pubDate>
		<dc:creator>Vikas Hazrati</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[git]]></category>

		<guid isPermaLink="false">http://vikashazrati.wordpress.com/2011/12/08/qucktip-tagging-your-code-on-git/</guid>
		<description><![CDATA[  For Tagging &#62;git tag -a v1.4 -m &#8216;version 1.4&#8242; For viewing the tag &#62;git tag For pushing it for everyone &#62;git push --tags For tagging an existing commit &#62; git tag -a v1.2 9fceb02 &#60;start of the changeset code&#62; For fetching tags from repository &#62; git fetch --tags Filed under: Java Tagged: git<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vikashazrati.wordpress.com&amp;blog=999392&amp;post=2430&amp;subd=vikashazrati&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p> </p>
<pre><code>For Tagging</code></pre>
<div><span class="Apple-style-span" style="font-family:Monaco, Consolas, 'Andale Mono', 'DejaVu Sans Mono', monospace;line-height:normal;white-space:pre;background-color:#f4f4f4;">&gt;git tag -a v1.4 -m &#8216;version 1.4&#8242;</span></div>
<pre><code>For viewing the tag</code></pre>
<pre><code>&gt;git tag</code></pre>
<pre><code>For pushing it for everyone</code></pre>
<pre><code>&gt;git push --tags</code></pre>
<pre><code>For tagging an existing commit</code></pre>
<pre><code>&gt; </code>git tag -a v1.2 9fceb02 &lt;start of the changeset code&gt;</pre>
<pre>For fetching tags from repository</pre>
<pre>&gt; git fetch --tags</pre>
<br />Filed under: <a href='http://vikashazrati.wordpress.com/category/java/'>Java</a> Tagged: <a href='http://vikashazrati.wordpress.com/tag/git/'>git</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vikashazrati.wordpress.com/2430/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vikashazrati.wordpress.com/2430/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vikashazrati.wordpress.com/2430/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vikashazrati.wordpress.com/2430/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vikashazrati.wordpress.com/2430/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vikashazrati.wordpress.com/2430/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vikashazrati.wordpress.com/2430/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vikashazrati.wordpress.com/2430/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vikashazrati.wordpress.com/2430/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vikashazrati.wordpress.com/2430/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vikashazrati.wordpress.com/2430/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vikashazrati.wordpress.com/2430/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vikashazrati.wordpress.com/2430/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vikashazrati.wordpress.com/2430/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vikashazrati.wordpress.com&amp;blog=999392&amp;post=2430&amp;subd=vikashazrati&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vikashazrati.wordpress.com/2011/12/08/qucktip-tagging-your-code-on-git/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c5837310ca52838cc93da501d1dd4d48?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vikashazrati</media:title>
		</media:content>
	</item>
		<item>
		<title>QuickTip: Limiting the number of rows returned by MySQL. Pagination</title>
		<link>http://vikashazrati.wordpress.com/2011/11/15/quicktip-limiting-the-number-of-rows-returned-by-mysql-pagination/</link>
		<comments>http://vikashazrati.wordpress.com/2011/11/15/quicktip-limiting-the-number-of-rows-returned-by-mysql-pagination/#comments</comments>
		<pubDate>Tue, 15 Nov 2011 05:47:38 +0000</pubDate>
		<dc:creator>Vikas Hazrati</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://vikashazrati.wordpress.com/2011/11/15/quicktip-limiting-the-number-of-rows-returned-by-mysql-pagination/</guid>
		<description><![CDATA[If you would like to get a row num, you would have to do something like this for getting it in the right order or simply For limiting the number of rows however, there is the LIMIT keyword [bash] mysql&#62; select * from CODE_PANEL_DETAIL limit 1,4; +&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+ &#124; CODE_PANEL_DETAIL_ID &#124; CODE_PANEL_ID &#124; CODE &#124; CODE_TYPE [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vikashazrati.wordpress.com&amp;blog=999392&amp;post=2406&amp;subd=vikashazrati&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you would like to get a row num, you would have to do something like this</p>
<p><pre class="brush: plain;">
SELECT @rn:=@rn+1 AS rank, itemID, ordercount
FROM (
  SELECT itemID, COUNT(*) AS ordercount
  FROM orders
  GROUP BY itemID
  ORDER BY ordercount DESC
) t1, (SELECT @rn:=0) t2;

</pre></p>
<p>for getting it in the right order </p>
<p>or simply</p>
<p><pre class="brush: plain;">
SET @rank=0;
SELECT @rank:=@rank+1 AS rank, itemID, COUNT(*) as ordercount
  FROM orders
  GROUP BY itemID
  ORDER BY ordercount DESC;
</pre></p>
<p>For limiting the number of rows however, there is the LIMIT keyword</p>
<p>[bash]</p>
<p>mysql&gt; select * from CODE_PANEL_DETAIL limit 1,4;<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
| CODE_PANEL_DETAIL_ID | CODE_PANEL_ID | CODE | CODE_TYPE                   | CODE_SEARCH_STRING |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
|                    2 |             3 | 010  | Age appropriateness of Toys | +toy               |<br />
|                    3 |             3 | 021  | Employee Helpfulness        | age                |<br />
|                    6 |             3 | 020  | Aisles                      | +toy               |<br />
|                    7 |             3 | 030  | Large Variety               | +toy               |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
4 rows in set (0.00 sec)</p>
<p>mysql&gt; select * from CODE_PANEL_DETAIL limit 2;<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
| CODE_PANEL_DETAIL_ID | CODE_PANEL_ID | CODE | CODE_TYPE                   | CODE_SEARCH_STRING |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
|                    1 |             3 | 001  | Oranization                 | +toy               |<br />
|                    2 |             3 | 010  | Age appropriateness of Toys | +toy               |<br />
+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;+&#8212;&#8212;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
2 rows in set (0.00 sec)<br />
[/bash]</p>
<br />Filed under: <a href='http://vikashazrati.wordpress.com/category/java/'>Java</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vikashazrati.wordpress.com/2406/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vikashazrati.wordpress.com/2406/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vikashazrati.wordpress.com/2406/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vikashazrati.wordpress.com/2406/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vikashazrati.wordpress.com/2406/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vikashazrati.wordpress.com/2406/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vikashazrati.wordpress.com/2406/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vikashazrati.wordpress.com/2406/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vikashazrati.wordpress.com/2406/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vikashazrati.wordpress.com/2406/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vikashazrati.wordpress.com/2406/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vikashazrati.wordpress.com/2406/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vikashazrati.wordpress.com/2406/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vikashazrati.wordpress.com/2406/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vikashazrati.wordpress.com&amp;blog=999392&amp;post=2406&amp;subd=vikashazrati&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vikashazrati.wordpress.com/2011/11/15/quicktip-limiting-the-number-of-rows-returned-by-mysql-pagination/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c5837310ca52838cc93da501d1dd4d48?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vikashazrati</media:title>
		</media:content>
	</item>
		<item>
		<title>QuickTip: VerifyError with jdk 7</title>
		<link>http://vikashazrati.wordpress.com/2011/10/09/quicktip-verifyerror-with-jdk-7/</link>
		<comments>http://vikashazrati.wordpress.com/2011/10/09/quicktip-verifyerror-with-jdk-7/#comments</comments>
		<pubDate>Sat, 08 Oct 2011 19:40:10 +0000</pubDate>
		<dc:creator>Vikas Hazrati</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[jdk 7]]></category>

		<guid isPermaLink="false">http://vikashazrati.wordpress.com/?p=2067</guid>
		<description><![CDATA[If you are struggling with the following kind of error java.lang.VerifyError: Expecting a stackmap frame at branch target 6 bla bla bla &#8230;. then a simple solution is to add the following default JVM argument -XX:-UseSplitVerifier If you are doing it in eclipse, like i was, then goto Window -&#62; Preferences -&#62; Installed JREs -&#62; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vikashazrati.wordpress.com&amp;blog=999392&amp;post=2067&amp;subd=vikashazrati&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you are struggling with the following kind of error </p>
<p>java.lang.VerifyError: Expecting a stackmap frame at branch target 6 bla bla bla &#8230;.</p>
<p>then a simple solution is to add the following default JVM argument <strong>-XX:-UseSplitVerifier</strong></p>
<p>If you are doing it in eclipse, like i was, then goto Window -&gt; Preferences -&gt; Installed JREs -&gt;  and add the default parameter.</p>
<p>What does this setting do? Oracle <a href="http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html" target="_blank">just mentions</a> that it a new type checker with StackMapTable attributes. Huh! Enjoy! It is Oracle now.</p>
<br />Filed under: <a href='http://vikashazrati.wordpress.com/category/java/'>Java</a> Tagged: <a href='http://vikashazrati.wordpress.com/tag/jdk-7/'>jdk 7</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vikashazrati.wordpress.com/2067/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vikashazrati.wordpress.com/2067/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vikashazrati.wordpress.com/2067/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vikashazrati.wordpress.com/2067/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vikashazrati.wordpress.com/2067/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vikashazrati.wordpress.com/2067/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vikashazrati.wordpress.com/2067/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vikashazrati.wordpress.com/2067/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vikashazrati.wordpress.com/2067/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vikashazrati.wordpress.com/2067/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vikashazrati.wordpress.com/2067/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vikashazrati.wordpress.com/2067/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vikashazrati.wordpress.com/2067/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vikashazrati.wordpress.com/2067/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vikashazrati.wordpress.com&amp;blog=999392&amp;post=2067&amp;subd=vikashazrati&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vikashazrati.wordpress.com/2011/10/09/quicktip-verifyerror-with-jdk-7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c5837310ca52838cc93da501d1dd4d48?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vikashazrati</media:title>
		</media:content>
	</item>
		<item>
		<title>Power of Parallel Processing in Akka</title>
		<link>http://vikashazrati.wordpress.com/2011/09/19/power-of-parallel-processing-in-akka/</link>
		<comments>http://vikashazrati.wordpress.com/2011/09/19/power-of-parallel-processing-in-akka/#comments</comments>
		<pubDate>Mon, 19 Sep 2011 07:30:10 +0000</pubDate>
		<dc:creator>Vikas Hazrati</dc:creator>
				<category><![CDATA[Scala]]></category>
		<category><![CDATA[Akka]]></category>
		<category><![CDATA[Akka actor]]></category>
		<category><![CDATA[Master Worker]]></category>
		<category><![CDATA[Parallel Processing]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://thoughts.inphina.com/?p=2402</guid>
		<description><![CDATA[If you have been following the Inphina blog and our work with Scala related technologies, you would have noticed a spurt in our blog feeds. The reason being that we have recently come out of a very successful massively scalable framework created with Akka. Inphina holds an active stake in the product and the details [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vikashazrati.wordpress.com&amp;blog=999392&amp;post=2402&amp;subd=vikashazrati&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you have been following the <a href="http://thoughts.inphina.com" target="_blank">Inphina blog</a> and our work with <a href="http://scala.inphina.com" target="_blank">Scala related technologies</a>, you would have noticed a spurt in our blog feeds. The reason being that we have recently come out of a very successful massively scalable framework created with Akka. Inphina holds an active stake in the product and the details of the framework would follow once the embargo is over. Meanwhile, in this post I would like to share some wins that we achieved with Akka.</p>
<p>For the matter of benchmarks look at the graph below. As of now we are able to process<strong> over 2 million messages</strong> coming on the RabbitMQ server in a matter of less than 100 seconds. You might argue that this number does not tell you anything unless we know what we are processing. Fair point, but in this post we are not debating that. What we are trying to show is that once you have a lot of Akka actors running in parallel under a master worker kind of relationship, you can do wonders on the number of cores that you have.</p>
<p><a href="http://vikashazrati.files.wordpress.com/2011/09/benchmarks-1.png"><img class="aligncenter size-full wp-image-2404" title="benchmarks-1" src="http://vikashazrati.files.wordpress.com/2011/09/benchmarks-1.png?w=594" alt=""   /></a></p>
<p>The bisecting line that you see in the graph is the one that we got when we had our Akka actors listening to the queue with the help of AMQP client. <a href="http://thoughts.inphina.com/2011/09/15/amqp-and-akka/" target="_blank">See the earlier post</a>. On the flick of a switch you can go parallel in the framework or hybrid as the case may be depending on your messages. When we switched to full parallel, we had actors consuming messages from the queue which would in-turn hand off the message to a Master Actor which would spawn Child Actors. This is based on the standard master-worker relationship. We are launching the number of workers either based on a configuration depending on the number of cores on which the framework is deployed or on the basis of the number of plugins which need to be launched in parallel.</p>
<p><a href="http://vikashazrati.files.wordpress.com/2011/09/master-worker.png"><img class="aligncenter size-full wp-image-2405" title="master-worker" src="http://vikashazrati.files.wordpress.com/2011/09/master-worker.png?w=594" alt=""   /></a></p>
<p>Let us look at some code now, following is the process message of the Master Actor</p>
<p><pre class="brush: scala;">
class ParallelProcessor extends Actor {
…
...
 def receive = {
 case MessageFromQueue(message) =&gt; process(message)

 case ChildMessage(plugin, message) =&gt;
 val transformedMessage = processMessage(message, plugin)
      self reply Result(message, plugin.target)

    case Result(message, target) =&gt;
 parallelCounter -= 1
 messageList ++= List(new Result(message, target))

 if (parallelCounter == 0) {
        logger.info(&quot;**** REDUCER CALLED ---- &quot;)
 val reducedMessage = reduceMessages(messageList)
 postPluginProcessing(reducedMessage)
 self ! PoisonPill
      }

  }
…
...

def process(message: VajraPMMessage) = {
 val numberOfWorkers = VajraPM.numberOfWorkers
 parallelCounter = currentStep.pluginsInStep.length
 val workers = Vector.fill(fetchWorkerCount(numberOfWorkers, parallelCounter))(actorOf[ParallelProcessor].start)
 val router = Routing.loadBalancerActor(CyclicIterator(workers)).start
    currentStep.pluginsInStep foreach { plugin =&gt;
 router ! new ChildMessage(plugin, transformer.createVajraMessage(message.toXml().toString))
    }
 router ! Broadcast(PoisonPill)
 router ! PoisonPill
  }

...
</pre></p>
<p>As you would notice, the master actor (ParallelProcessor) receives a message. The <strong>case MessageFromQueue gets called and we call the process(message)</strong>. As you would notice in the process(message),<span style="text-decoration:underline;"> we spawn a number of child workers</span> which is the same actor ParallelProcessor again.<br />
<span id="more-2402"></span></p>
<p>Let us zoom into the process method, we create a router to handle the even delivery of ChildMessage to the workers. Right now, the router uses a CyclicIterator to distribute messages.</p>
<p><pre class="brush: scala;">
val workers = Vector.fill(fetchWorkerCount(numberOfWorkers))(actorOf[ParallelProcessor].start)
 val router = Routing.loadBalancerActor(CyclicIterator(workers)).start
    currentStep.pluginsInStep foreach { plugin =&gt;
 router ! new ChildMessage(plugin, transformer.createVajraMessage(message.toXml().toString))
</pre></p>
<p>An interesting thing that we do here is that once the router has passed all the messages to the child workers, we issue a <strong>poisonpill</strong> to the child workers so that they can die after processing. We also pass a poison pill to the router.<br />
<em>Caution: If you are using a work stealing dispatcher, please ensure that your poison pills are not being eaten by actors which are yet to receive messages thus causing unintended behavior.</em></p>
<p>Let us see how the child receives the message, again we have the same receive method</p>
<p><pre class="brush: scala;">
 def receive = {
 case MessageFromQueue(message) =&gt; process(message)

 case ChildMessage(plugin, message) =&gt;
 val transformedMessage = processMessage(message, plugin)
      self reply Result(message, plugin.target)

    case Result(message, target) =&gt;
 parallelCounter -= 1
 messageList ++= List(new Result(message, target))

 if (parallelCounter == 0) {
        logger.info(&quot;**** REDUCER CALLED ---- &quot;)
 val reducedMessage = reduceMessages(messageList)
 postPluginProcessing(reducedMessage)
 self ! PoisonPill
      }

  }
</pre></p>
<p><strong>This time, we fall into the block of case ChildMessage(plugin, message) =&gt;</strong><br />
Here, every child does some processing and passes back the results to the master with the <strong>self reply Result(message, plugin.target)</strong></p>
<p>Finally, the results come back to the master, where <span style="text-decoration:underline;">it keeps a count of the number of results coming back</span>. If you recall, the counter (parallelCounter) was set in the process method where we spawned child workers either on the basis of the number of parallel steps or depending on the number of cores that we have on the machine on which the framework is running.</p>
<p><pre class="brush: scala;">
 def receive = {
 case MessageFromQueue(message) =&gt; process(message)

 case ChildMessage(plugin, message) =&gt;
 val transformedMessage = processMessage(message, plugin)
      self reply Result(message, plugin.target)

    case Result(message, target) =&gt;
 parallelCounter -= 1
 messageList ++= List(new Result(message, target))

 if (parallelCounter == 0) {
        logger.info(&quot;**** REDUCER CALLED ---- &quot;)
 val reducedMessage = reduceMessages(messageList)
 postPluginProcessing(reducedMessage)
 self ! PoisonPill
      }
  }
</pre></p>
<p>The <strong>case Result(message, target) =&gt;</strong> does a reduction logic on all the results coming back from the child workers.</p>
<p>Thus, with the master worker logic we were able to reduce the processing time considerably.</p>
<p>One of the things that we fell into was that we ended up creating a lot of new native threads on our OS as a result of which we were getting limited by the number of native threads which could be used. For this, we used a thread-pool instead of spawning a new thread. <a href="http://akka.io/docs/akka/1.1.3/scala/dispatchers.html" target="_blank">See Akka Dispatchers</a>&nbsp;and our blog related on dispatchers. I would cover the threading issue in detail in another post.</p>
<br />Filed under: <a href='http://vikashazrati.wordpress.com/category/scala-3-8/'>Scala</a> Tagged: <a href='http://vikashazrati.wordpress.com/tag/akka/'>Akka</a>, <a href='http://vikashazrati.wordpress.com/tag/akka-actor/'>Akka actor</a>, <a href='http://vikashazrati.wordpress.com/tag/master-worker/'>Master Worker</a>, <a href='http://vikashazrati.wordpress.com/tag/parallel-processing/'>Parallel Processing</a>, <a href='http://vikashazrati.wordpress.com/tag/scala-2/'>scala</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vikashazrati.wordpress.com/2402/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vikashazrati.wordpress.com/2402/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vikashazrati.wordpress.com/2402/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vikashazrati.wordpress.com/2402/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vikashazrati.wordpress.com/2402/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vikashazrati.wordpress.com/2402/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vikashazrati.wordpress.com/2402/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vikashazrati.wordpress.com/2402/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vikashazrati.wordpress.com/2402/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vikashazrati.wordpress.com/2402/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vikashazrati.wordpress.com/2402/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vikashazrati.wordpress.com/2402/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vikashazrati.wordpress.com/2402/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vikashazrati.wordpress.com/2402/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vikashazrati.wordpress.com&amp;blog=999392&amp;post=2402&amp;subd=vikashazrati&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vikashazrati.wordpress.com/2011/09/19/power-of-parallel-processing-in-akka/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<georss:point>28.632778 77.219722</georss:point>
		<geo:lat>28.632778</geo:lat>
		<geo:long>77.219722</geo:long>
		<media:content url="http://0.gravatar.com/avatar/c5837310ca52838cc93da501d1dd4d48?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vikashazrati</media:title>
		</media:content>

		<media:content url="http://vikashazrati.files.wordpress.com/2011/09/benchmarks-1.png" medium="image">
			<media:title type="html">benchmarks-1</media:title>
		</media:content>

		<media:content url="http://vikashazrati.files.wordpress.com/2011/09/master-worker.png" medium="image">
			<media:title type="html">master-worker</media:title>
		</media:content>
	</item>
		<item>
		<title>AMQP and AKKA</title>
		<link>http://vikashazrati.wordpress.com/2011/09/15/amqp-and-akka/</link>
		<comments>http://vikashazrati.wordpress.com/2011/09/15/amqp-and-akka/#comments</comments>
		<pubDate>Thu, 15 Sep 2011 11:56:49 +0000</pubDate>
		<dc:creator>Vikas Hazrati</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Akka]]></category>
		<category><![CDATA[AMQP]]></category>
		<category><![CDATA[RabbitMQ]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://thoughts.inphina.com/?p=2361</guid>
		<description><![CDATA[AMQP is a message protocol that deals with publishers and consumers. It would look a lot like JMS but it is not. The main entities are Exchanges, Queues and Bindings. Look at the following diagram So a producer would send a message to the exchange and it is the job of the message broker (RabbitMQ [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vikashazrati.wordpress.com&amp;blog=999392&amp;post=2361&amp;subd=vikashazrati&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>AMQP is a message protocol that deals with publishers and consumers. It would look a lot like JMS but it is not. The main entities are Exchanges, Queues and Bindings. Look at the following diagram</p>
<p><a href="http://vikashazrati.files.wordpress.com/2011/09/amqp-akka.png"><img class="aligncenter size-full wp-image-2363" title="amqp-akka" src="http://vikashazrati.files.wordpress.com/2011/09/amqp-akka.png?w=594" alt=""   /></a></p>
<p>So a producer would send a message to the exchange and it is the job of the message broker (RabbitMQ in our case) to ensure that the messages are delivered to the right queue.<br />
But first the connections have to be built so that publishers can publish on the exchange and consumers can listen from it. Let us see how this is done in Akka in our project. Our framework gets initialized by the following method</p>
<p><pre class="brush: scala;">
def initializeFramework(xml: Elem) {
    connectionInfo = getAMQPChannelInformation(xml)
    val connection = getAMQPChannel(connectionInfo)
    val exchangeParameters = getExchangeParameters(connectionInfo)
    instantiateProducer(connection, exchangeParameters)

    logger.info(&quot;VajraPM is now initialized and ready to process messages...&quot;)
  }
</pre></p>
<p>I have taken out the business logic and left out the methods which would form the core of this post. Let us go method by method, first, we get all the connection information which would be relevant for us to make the connection</p>
<p><pre class="brush: scala;">
def getAMQPChannelInformation(xml: Elem): ConnectionInfo = {
    val momServer = (xml  &quot;VAJRA&quot;  &quot;RABBITSERVER&quot;).text
    val momPort = (xml  &quot;VAJRA&quot;  &quot;RABBITPORT&quot;).text
    val userName = (xml  &quot;VAJRA&quot;  &quot;USERNAME&quot;).text
    val password = (xml  &quot;VAJRA&quot;  &quot;PASSWORD&quot;).text
    val queueName = (xml  &quot;VAJRA&quot;  &quot;QUEUENAME&quot;).text
    val exchangeName = (xml  &quot;VAJRA&quot;  &quot;EXCHANGENAME&quot;).text
    val durable = (xml  &quot;VAJRA&quot;  &quot;DURABLE&quot;).text
    val connectionInfo = new ConnectionInfo(momServer, momPort, userName, password, queueName, exchangeName, durable)
    connectionInfo
  }
</pre></p>
<p>Here, use the Scala XML magic and get done soon. Our sample XML through which</p>
<p><pre class="brush: xml;">
&lt;VAJRA&gt;
		&lt;RABBITSERVER&gt;localhost&lt;/RABBITSERVER&gt;
		&lt;RABBITPORT&gt;5672&lt;/RABBITPORT&gt;
		&lt;USERNAME&gt;guest&lt;/USERNAME&gt;
		&lt;PASSWORD&gt;guest&lt;/PASSWORD&gt;
		&lt;QUEUENAME&gt;myqueue&lt;/QUEUENAME&gt;
		&lt;EXCHANGENAME&gt;myexchange&lt;/EXCHANGENAME&gt;
	&lt;/VAJRA&gt;
</pre></p>
<p>Now let us see how we get a connection</p>
<p><pre class="brush: scala;">
def getAMQPChannel(connectionInfo: ConnectionInfo): ActorRef = {
    val myAddresses = Array(new Address(connectionInfo.momServer, connectionInfo.momPort.toInt))
    val connectionParameters = ConnectionParameters(myAddresses, connectionInfo.userName, connectionInfo.password)
    val connection = AMQP.newConnection(connectionParameters)
    connection
  }
</pre></p>
<p>As you would notice, all the connection parameters are passed to the akka.amqp.AMQP.ConnectionParameters and a connection object is received.</p>
<p>Once we have the connection, let us instantiate the producer. The method instantiateProducer(connection, exchangeParameters) looks like this</p>
<p><span id="more-2361"></span></p>
<p><pre class="brush: scala;">
 def instantiateProducer(connection: ActorRef, exchangeParameters: ExchangeParameters) {
    producer = AMQP.newProducer(connection, ProducerParameters(Some(exchangeParameters)))
  }
</pre></p>
<p>As you would notice, we pass exchangeParameters to the producer. You would see in the diagram above that AMQP producer requires a reference of the exchange to which it can send messages. We get the exchange information as follows</p>
<p><pre class="brush: scala;">
  def getExchangeParameters(connectionInfo: ConnectionInfo): ExchangeParameters = {
    var exchangeParameters: ExchangeParameters = ExchangeParameters(connectionInfo.exchangeName, Topic)
    connectionInfo.durable match {
      case &quot;DURABLE&quot; =&gt; exchangeParameters = ExchangeParameters(connectionInfo.exchangeName, Topic, ActiveDeclaration(durable = true, autoDelete = false))
      case _ =&gt; exchangeParameters
    }
    exchangeParameters
  }

</pre></p>
<p>If in our configuration we define the exchange to be durable, then we set the durable and autodelete properties of the exchange.</p>
<p>This would set up our producer actor and we would be able to send messages to the producer like this with the bang operator</p>
<p><strong>producer ! Message(message)</strong></p>
<p>As soon as the producer actor would receive the message, it would put it on the exchange that we have specified. Now, let us look at the consumer</p>
<p><pre class="brush: scala;">
 def createConsumer(exchangeParameters: ExchangeParameters, connection: ActorRef, queueName: String, actor: ActorRef, connectionInfo: ConnectionInfo): ActorRef = {
    connectionInfo.durable match {
      case &quot;DURABLE&quot; =&gt; {
        val queueDeclaration = ActiveDeclaration(durable = true, autoDelete = false)
        AMQP.newConsumer(connection, ConsumerParameters(&quot;@vajrafeed&quot;, actor, Some(queueName), Some(exchangeParameters), queueDeclaration))
      }
      case _ =&gt; AMQP.newConsumer(connection, ConsumerParameters(&quot;@vajrafeed&quot;, actor, Some(queueName), Some(exchangeParameters)))
    }
  }
</pre></p>
<p>Hence, now as shown in the figure above, we have a consumer listening on the queue called myqueue which is listening to the exchange myexchange. You would notice that we have a &#8220;@vajrafeed&#8221; This is the binding key which lets the messgage broker decide, which messages it should pass on onto the consumer. If there was another consumer attached to the queue with a binding key say @nyse then the messages for @vajrafeed would not be delivered to it. You can get more information <a href="http://blog.springsource.com/2010/06/14/understanding-amqp-the-protocol-used-by-rabbitmq/" target="_blank">about AMQP here</a>.</p>
<br />Filed under: <a href='http://vikashazrati.wordpress.com/category/architecture/'>Architecture</a>, <a href='http://vikashazrati.wordpress.com/category/scala-3-7/'>Scala</a> Tagged: <a href='http://vikashazrati.wordpress.com/tag/akka/'>Akka</a>, <a href='http://vikashazrati.wordpress.com/tag/amqp/'>AMQP</a>, <a href='http://vikashazrati.wordpress.com/tag/rabbitmq/'>RabbitMQ</a>, <a href='http://vikashazrati.wordpress.com/tag/scala-2/'>scala</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vikashazrati.wordpress.com/2361/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vikashazrati.wordpress.com/2361/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vikashazrati.wordpress.com/2361/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vikashazrati.wordpress.com/2361/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vikashazrati.wordpress.com/2361/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vikashazrati.wordpress.com/2361/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vikashazrati.wordpress.com/2361/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vikashazrati.wordpress.com/2361/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vikashazrati.wordpress.com/2361/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vikashazrati.wordpress.com/2361/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vikashazrati.wordpress.com/2361/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vikashazrati.wordpress.com/2361/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vikashazrati.wordpress.com/2361/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vikashazrati.wordpress.com/2361/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vikashazrati.wordpress.com&amp;blog=999392&amp;post=2361&amp;subd=vikashazrati&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vikashazrati.wordpress.com/2011/09/15/amqp-and-akka/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<georss:point>28.632778 77.219722</georss:point>
		<geo:lat>28.632778</geo:lat>
		<geo:long>77.219722</geo:long>
		<media:content url="http://0.gravatar.com/avatar/c5837310ca52838cc93da501d1dd4d48?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vikashazrati</media:title>
		</media:content>

		<media:content url="http://vikashazrati.files.wordpress.com/2011/09/amqp-akka.png" medium="image">
			<media:title type="html">amqp-akka</media:title>
		</media:content>
	</item>
		<item>
		<title>Building a Plugin Based Architecture in Scala</title>
		<link>http://vikashazrati.wordpress.com/2011/09/15/building-a-plugin-based-architecture-in-scala/</link>
		<comments>http://vikashazrati.wordpress.com/2011/09/15/building-a-plugin-based-architecture-in-scala/#comments</comments>
		<pubDate>Thu, 15 Sep 2011 09:59:31 +0000</pubDate>
		<dc:creator>Vikas Hazrati</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Plugin framework]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://thoughts.inphina.com/?p=2336</guid>
		<description><![CDATA[A plugin based architecture has many advantages. Some of the common ones include Extending an application&#8217;s functionality without compiling it again Adding functionality without requiring access to the original source code. Replacing or adding new functionality becomes easy Help in organizing large projects Help in extending the functionality of the system to unimagined areas. I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vikashazrati.wordpress.com&amp;blog=999392&amp;post=2336&amp;subd=vikashazrati&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A plugin based architecture has many advantages. Some of the common ones include</p>
<ul>
<li>Extending an application&#8217;s functionality without compiling it again</li>
<li>Adding functionality without requiring access to the original source code.</li>
<li>Replacing or adding new functionality becomes easy</li>
<li>Help in organizing large projects</li>
<li>Help in extending the functionality of the system to unimagined areas. I know this one sounds extreme but believe me it is true. For our framework, plugins were written by third-party library providers whose domain ranged from health-care to natural language processing.</li>
</ul>
<p>The major components of our plugin system are the following</p>
<ol>
<li><strong>Plugin trait</strong> – Which all the plugins would extend</li>
<li><strong>PluginManager Objec</strong>t – Responsible for loading the plugin at the time of instantiation of the framework</li>
<li><strong>MessageContext trai</strong>t – All messages would extend this trait. This makes easy to pass a variety of messages to the plugin without restricting it to a few</li>
</ol>
<p>Let us look at each of these one by one. Our <strong>Plugin trait</strong> looks like this</p>
<p><pre class="brush: scala;">
trait VajraPlugin {

  def name:String
  def performAction(context:MessageContext):Boolean
  def result:String

}
</pre></p>
<p>As you would notice, the implementation class would be defining a name, it would have an implementation for a performAction method which takes input as a MessageContext. We expect all our plugins to finally return the result with the result method.</p>
<p>A sample implementation of the plugin would look like this</p>
<p><pre class="brush: scala;">
class ZeroToHalfPlugin extends VajraPlugin {

  def name(): String = { &quot;ZeroToHalf&quot; }

  def performAction(context:MessageContext):Boolean = {
    val sleepTime = java.lang.Math.random() * 501
    val payload = context.getPayload()
    println(&quot;&quot;)
    println(&quot;I am busy for &quot; + sleepTime/1000 + &quot; seconds&quot;)
    Thread.sleep(sleepTime.toLong)
    println(&quot;&quot;)
    return true
  }

  def result(): String = { //return some result here }
}
</pre></p>
<p>Now, let us look at the <strong>Plugin manager</strong></p>
<p><span id="more-2336"></span></p>
<p><pre class="brush: scala;">
object PluginManager {

  var pluginMap = Map[String, String]()
  val logger = Logger(PluginManager.getClass())

  def init() {
    val classpath = List(&quot;.&quot;).map(new File(_))
    val finder = ClassFinder(classpath)
    val classes = finder.getClasses
    val classMap = ClassFinder.classInfoMap(classes)
    val plugins = ClassFinder.concreteSubclasses(&quot;com.vajra.plugin.VajraPlugin&quot;, classMap)

    plugins.foreach {
      pluginString =&gt;
        val plugin = Class.forName(pluginString.name).newInstance().asInstanceOf[VajraPMPlugin]
        pluginMap += (plugin.name -&gt; pluginString.name)
    }
  }

  def getPlugin(name: String): VajraPlugin = {
    if (pluginMap.isEmpty) init
    logger.debug(&quot;Fetching plugin from non-persistent pool.&quot;)
    Class.forName(pluginMap(name)).newInstance().asInstanceOf[VajraPMPlugin]

  }
</pre></p>
<p>Now, the consumer who needs to use a plugin,  would come to the PluginManager and ask for a plugin via the getPlugin() method. If you remember, each plugin defines its name.</p>
<p>When this method is called, the managers checks if the plugin map is empty. If it is then the <strong>init</strong> is called where we load the plugins into the framework. Loading the plugins is done on the basis of all classes which are extending the<br />
com.vajra.plugin.VajraPlugin trait. For this, we use are using a utility (ClassFinder) provided by Clapper. <a href="http://thoughts.inphina.com/2011/08/16/scala-nuggets-finding-all-classes-in-a-package/" target="_blank">More details can be found here</a>.</p>
<p>The idea is simple. As soon as we are starting the framework, we should not have to specify the plugins that the framework would be using anywhere in any configuration. The framework should be able to pick up the plugins automatically. In our case, we pick up all the plugins which are implementing our plugin trait i.e. VajraPlugin</p>
<p>The last piece in our puzzle is the message which is passed to the plugin. Since we want to keep this also extensible, instead of passing the message directly, we use a trait <strong>MessageContext</strong>.</p>
<p><pre class="brush: scala;">
trait MessageContext {

  def toXml(): Elem
  def setPayload(payload:Payload): Unit
  def getPayload(): Payload
  def addOrAppendElement(elementName: String, data: String)
  def addOrReplaceElement(elementName: String, elementData: String)
  def getElementData(elementName: String): String

}
</pre></p>
<p>As you would notice we have a number of utility methods here which all the messages would be implementing. We have a large number of methods here because they are relevant to our system. You could have the relevant methods for your system. A typical message which extends the MessageContext would look like this</p>
<p><pre class="brush: scala;">
class MessageContextAdapter extends MessageContext {

  def toXml(): Elem = {
    null
  }
  def setPayload(payload: Payload): Unit = {}
  def getPayload(): Payload = { null }
  def addOrAppendElement(elementName: String, data: String) = {}
  def addOrReplaceElement(elementName: String, elementData: String) = {}
  def getElementData(elementName: String): String = { &quot;&quot; }
  def getPlugin(): Plugin = {
    return null
  }
  def removeStep {}
}
</pre></p>
<p>The main takeaways are that you should definitely have all the plugins extend a trait. The plugin manager is responsible for creation, destruction and maintenance of plugins. The object passed to the plugin should (could) be a trait so that the framework can be extensible.</p>
<br />Filed under: <a href='http://vikashazrati.wordpress.com/category/architecture/'>Architecture</a>, <a href='http://vikashazrati.wordpress.com/category/scala-3-6/'>Scala</a> Tagged: <a href='http://vikashazrati.wordpress.com/tag/plugin-framework/'>Plugin framework</a>, <a href='http://vikashazrati.wordpress.com/tag/scala-2/'>scala</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vikashazrati.wordpress.com/2336/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vikashazrati.wordpress.com/2336/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vikashazrati.wordpress.com/2336/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vikashazrati.wordpress.com/2336/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vikashazrati.wordpress.com/2336/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vikashazrati.wordpress.com/2336/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vikashazrati.wordpress.com/2336/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vikashazrati.wordpress.com/2336/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vikashazrati.wordpress.com/2336/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vikashazrati.wordpress.com/2336/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vikashazrati.wordpress.com/2336/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vikashazrati.wordpress.com/2336/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vikashazrati.wordpress.com/2336/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vikashazrati.wordpress.com/2336/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vikashazrati.wordpress.com&amp;blog=999392&amp;post=2336&amp;subd=vikashazrati&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vikashazrati.wordpress.com/2011/09/15/building-a-plugin-based-architecture-in-scala/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<georss:point>28.632778 77.219722</georss:point>
		<geo:lat>28.632778</geo:lat>
		<geo:long>77.219722</geo:long>
		<media:content url="http://0.gravatar.com/avatar/c5837310ca52838cc93da501d1dd4d48?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vikashazrati</media:title>
		</media:content>
	</item>
		<item>
		<title>Searching gmail inbox for unread mails only</title>
		<link>http://vikashazrati.wordpress.com/2011/09/08/searching-gmail-inbox-for-unread-mails-only/</link>
		<comments>http://vikashazrati.wordpress.com/2011/09/08/searching-gmail-inbox-for-unread-mails-only/#comments</comments>
		<pubDate>Thu, 08 Sep 2011 11:45:15 +0000</pubDate>
		<dc:creator>Vikas Hazrati</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://vikashazrati.wordpress.com/2011/09/08/searching-gmail-inbox-for-unread-mails-only/</guid>
		<description><![CDATA[in:inbox is:unread Filed under: Java<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vikashazrati.wordpress.com&amp;blog=999392&amp;post=2065&amp;subd=vikashazrati&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>in:inbox is:unread</p>
<br />Filed under: <a href='http://vikashazrati.wordpress.com/category/java/'>Java</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vikashazrati.wordpress.com/2065/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vikashazrati.wordpress.com/2065/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vikashazrati.wordpress.com/2065/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vikashazrati.wordpress.com/2065/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vikashazrati.wordpress.com/2065/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vikashazrati.wordpress.com/2065/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vikashazrati.wordpress.com/2065/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vikashazrati.wordpress.com/2065/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vikashazrati.wordpress.com/2065/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vikashazrati.wordpress.com/2065/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vikashazrati.wordpress.com/2065/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vikashazrati.wordpress.com/2065/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vikashazrati.wordpress.com/2065/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vikashazrati.wordpress.com/2065/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vikashazrati.wordpress.com&amp;blog=999392&amp;post=2065&amp;subd=vikashazrati&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vikashazrati.wordpress.com/2011/09/08/searching-gmail-inbox-for-unread-mails-only/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c5837310ca52838cc93da501d1dd4d48?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vikashazrati</media:title>
		</media:content>
	</item>
		<item>
		<title>QuickTip: Ignoring some jars from getting assembled in SBT 0.10x</title>
		<link>http://vikashazrati.wordpress.com/2011/09/01/quicktip-ignoring-some-jars-from-getting-assembled-in-sbt-0-10x/</link>
		<comments>http://vikashazrati.wordpress.com/2011/09/01/quicktip-ignoring-some-jars-from-getting-assembled-in-sbt-0-10x/#comments</comments>
		<pubDate>Thu, 01 Sep 2011 12:24:47 +0000</pubDate>
		<dc:creator>Vikas Hazrati</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[jars]]></category>
		<category><![CDATA[sbt]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://vikashazrati.wordpress.com/2011/09/01/quicktip-ignoring-some-jars-from-getting-assembled-in-sbt-0-10x/</guid>
		<description><![CDATA[We are using sbt-onejar and as expected but not intended all the jars along with thier transitive dependencies were getting included in the jar. This was giving us several warnings like JarClassLoader: Warning: org/hamcrest/BaseDescription.class in lib/mockito-all-1.8.5.jar is hidden by lib/junit-4.7.jar (with different bytecode) JarClassLoader: Warning: org/hamcrest/BaseMatcher.class in lib/mockito-all-1.8.5.jar is hidden by lib/junit-4.7.jar (with different bytecode) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vikashazrati.wordpress.com&amp;blog=999392&amp;post=2064&amp;subd=vikashazrati&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>We are using sbt-onejar and as expected but not intended all the jars along with thier transitive dependencies were getting included in the jar. This was giving us several warnings like </p>
<p>JarClassLoader: Warning: org/hamcrest/BaseDescription.class in lib/mockito-all-1.8.5.jar is hidden by lib/junit-4.7.jar (with different bytecode)<br />
JarClassLoader: Warning: org/hamcrest/BaseMatcher.class in lib/mockito-all-1.8.5.jar is hidden by lib/junit-4.7.jar (with different bytecode)</p>
<p>when we we executing code.</p>
<p>To remove this, you need to have the intransitive() suffixed to the dependency like this</p>
<p>libraryDependencies += &#8220;org.scalatest&#8221; % &#8220;scalatest_2.9.0&#8243; % &#8220;1.6.1&#8243; intransitive()</p>
<p>libraryDependencies += &#8220;org.specs2&#8243; % &#8220;specs2_2.9.0-1&#8243; % &#8220;1.5&#8243; </p>
<p>libraryDependencies += &#8220;org.clapper&#8221; % &#8220;classutil_2.9.0&#8243; % &#8220;0.3.6&#8243;</p>
<p>libraryDependencies += &#8220;org.clapper&#8221; % &#8220;avsl_2.9.0&#8243; % &#8220;0.3.2&#8243;</p>
<p>libraryDependencies += &#8220;org.jsoup&#8221; % &#8220;jsoup&#8221; % &#8220;1.6.1&#8243; intransitive()</p>
<p>You would do it for all the dependencies where you have some jars which could be stepping on the toes of others because of transitive dependency.</p>
<br />Filed under: <a href='http://vikashazrati.wordpress.com/category/java/'>Java</a> Tagged: <a href='http://vikashazrati.wordpress.com/tag/jars/'>jars</a>, <a href='http://vikashazrati.wordpress.com/tag/sbt/'>sbt</a>, <a href='http://vikashazrati.wordpress.com/tag/scala-2/'>scala</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vikashazrati.wordpress.com/2064/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vikashazrati.wordpress.com/2064/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vikashazrati.wordpress.com/2064/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vikashazrati.wordpress.com/2064/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vikashazrati.wordpress.com/2064/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vikashazrati.wordpress.com/2064/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vikashazrati.wordpress.com/2064/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vikashazrati.wordpress.com/2064/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vikashazrati.wordpress.com/2064/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vikashazrati.wordpress.com/2064/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vikashazrati.wordpress.com/2064/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vikashazrati.wordpress.com/2064/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vikashazrati.wordpress.com/2064/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vikashazrati.wordpress.com/2064/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vikashazrati.wordpress.com&amp;blog=999392&amp;post=2064&amp;subd=vikashazrati&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vikashazrati.wordpress.com/2011/09/01/quicktip-ignoring-some-jars-from-getting-assembled-in-sbt-0-10x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/c5837310ca52838cc93da501d1dd4d48?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vikashazrati</media:title>
		</media:content>
	</item>
		<item>
		<title>Akka Actors on Mac beating the S#!t Out of my Dell</title>
		<link>http://vikashazrati.wordpress.com/2011/08/26/akka-actors-on-mac-beating-the-st-out-of-my-dell/</link>
		<comments>http://vikashazrati.wordpress.com/2011/08/26/akka-actors-on-mac-beating-the-st-out-of-my-dell/#comments</comments>
		<pubDate>Fri, 26 Aug 2011 11:08:33 +0000</pubDate>
		<dc:creator>Vikas Hazrati</dc:creator>
				<category><![CDATA[Scala]]></category>
		<category><![CDATA[Akka]]></category>
		<category><![CDATA[Akka actor]]></category>
		<category><![CDATA[Benchmark]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://thoughts.inphina.com/?p=2262</guid>
		<description><![CDATA[Let me explain the scenario. We have n (tens, hundreds or thousands) of Akka actors listening to a queue on RabbitMQ server. So the scenario looks something like this As you would notice, each of the actors gets a message from the Q and invokes a plugin to do the processing and returns the results [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vikashazrati.wordpress.com&amp;blog=999392&amp;post=2262&amp;subd=vikashazrati&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Let me explain the scenario. We have n (tens, hundreds or thousands) of Akka actors listening to a queue on RabbitMQ server. So the scenario looks something like this</p>
<p><pre class="brush: scala;">

AMQP.newConsumer(connection, ConsumerParameters(&quot;*&quot;, actor, Some(queueName), Some(exchangeParameters)))

</pre></p>
<p><a href="http://vikashazrati.files.wordpress.com/2011/08/q-consumer.png"><img class="size-full wp-image-2265 alignleft" title="q-consumer" src="http://vikashazrati.files.wordpress.com/2011/08/q-consumer.png?w=594" alt=""   /></a></p>
<p>As you would notice, each of the actors gets a message from the Q and invokes a plugin to do the processing and returns the results back to the result Q.<br />
Now the fun happens when I execute the same code on my machine and<a href="http://in.linkedin.com/in/mmaltiar" target="_blank"> my competitors (Meetu&#8217;s)</a> machine.</p>
<p><strong>Meetu has a Macbook pro – 2 cores – 2.3 GHz Intel core i5 – RAM 4GB – 64 bit<br />
Mine is Dell Vostro – 4 cores – 2.4 GHz Intel core i5 – RAM 3GB – 32 bit<br />
</strong></p>
<p>And here are the results</p>
<p style="text-align:center;"><a href="http://vikashazrati.files.wordpress.com/2011/08/results.png"><img class="size-full wp-image-2263 aligncenter" title="results" src="http://vikashazrati.files.wordpress.com/2011/08/results.png?w=594" alt=""   /></a></p>
<p>We start with 10 actors getting 100 messages and go all the way to 1,000 actors getting 10,000 messages. Everything in the code is the same except the hardware on which the tests are executed. I start of badly and see that on my machine 10 actors are processing messages in 5.298 s as compared to 3.039s on Meetu&#8217;s mac. This prompts me to skip to the other portion of the spectrum where I feel I can show him the power of my 4 cores. I am mistaken that the initial load of fewer messages and actors is actually having overhead for 4 cores and they would perform well on the other end.</p>
<p>Sadly with 1000 actors and 10000 messages the tiny mac with 2 cores outperforms the hulk dell with 4 cores by a margin of more than 10s.</p>
<p>What could be going wrong? There is no other heavy processing happening, on either machine, when we are running the tests. I doubt 64 bit vs 32 bit would cause such an upset. What else?<br />
<a href="http://vikashazrati.files.wordpress.com/2011/08/meetu-vikas.png"><img class="aligncenter size-full wp-image-2264" title="meetu-vikas" src="http://vikashazrati.files.wordpress.com/2011/08/meetu-vikas.png?w=594" alt=""   /></a></p>
<br />Filed under: <a href='http://vikashazrati.wordpress.com/category/scala-3-5/'>Scala</a> Tagged: <a href='http://vikashazrati.wordpress.com/tag/akka/'>Akka</a>, <a href='http://vikashazrati.wordpress.com/tag/akka-actor/'>Akka actor</a>, <a href='http://vikashazrati.wordpress.com/tag/benchmark/'>Benchmark</a>, <a href='http://vikashazrati.wordpress.com/tag/scala-2/'>scala</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vikashazrati.wordpress.com/2262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vikashazrati.wordpress.com/2262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vikashazrati.wordpress.com/2262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vikashazrati.wordpress.com/2262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vikashazrati.wordpress.com/2262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vikashazrati.wordpress.com/2262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vikashazrati.wordpress.com/2262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vikashazrati.wordpress.com/2262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vikashazrati.wordpress.com/2262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vikashazrati.wordpress.com/2262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vikashazrati.wordpress.com/2262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vikashazrati.wordpress.com/2262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vikashazrati.wordpress.com/2262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vikashazrati.wordpress.com/2262/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vikashazrati.wordpress.com&amp;blog=999392&amp;post=2262&amp;subd=vikashazrati&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vikashazrati.wordpress.com/2011/08/26/akka-actors-on-mac-beating-the-st-out-of-my-dell/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<georss:point>28.632778 77.219722</georss:point>
		<geo:lat>28.632778</geo:lat>
		<geo:long>77.219722</geo:long>
		<media:content url="http://0.gravatar.com/avatar/c5837310ca52838cc93da501d1dd4d48?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vikashazrati</media:title>
		</media:content>

		<media:content url="http://vikashazrati.files.wordpress.com/2011/08/q-consumer.png" medium="image">
			<media:title type="html">q-consumer</media:title>
		</media:content>

		<media:content url="http://vikashazrati.files.wordpress.com/2011/08/results.png" medium="image">
			<media:title type="html">results</media:title>
		</media:content>

		<media:content url="http://vikashazrati.files.wordpress.com/2011/08/meetu-vikas.png" medium="image">
			<media:title type="html">meetu-vikas</media:title>
		</media:content>
	</item>
		<item>
		<title>Creating Object Pool(s) in Scala</title>
		<link>http://vikashazrati.wordpress.com/2011/08/26/creating-object-pools-in-scala/</link>
		<comments>http://vikashazrati.wordpress.com/2011/08/26/creating-object-pools-in-scala/#comments</comments>
		<pubDate>Fri, 26 Aug 2011 06:57:31 +0000</pubDate>
		<dc:creator>Vikas Hazrati</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Object Pool]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://thoughts.inphina.com/?p=2255</guid>
		<description><![CDATA[We are currently working on a very exciting web scale project. The framework is built using Scala and Akka actors and till date we are quite pleased with the performance. The architecture is plugin based where we can dynamically add plugins to our framework for processing incoming messages. Now, this is where it gets interesting [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vikashazrati.wordpress.com&amp;blog=999392&amp;post=2255&amp;subd=vikashazrati&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>We are currently working on a very exciting web scale project. The framework is built using <a href="http://www.scala-lang.org/" target="_blank">Scala</a> and <a href="http://www.typesafe.com/" target="_blank">Akka actors</a> and till date we are quite pleased with the performance. The architecture is plugin based where we can dynamically add plugins to our framework for processing incoming messages. Now, this is where it gets interesting and is the reason for this post.</p>
<p>Some of the plugins are expensive to create. They would rather be created when the framework is started and pooled in memory. That brings us to the way we created our object pool(s). For our scenario, the we could actually end up having multiple object pools because each plugin would have a different # of pooled plugins. So we end up with something like this in our configuration file</p>
<p><pre class="brush: xml;">
…
…
  &lt;pluginpool&gt;
     &lt;plugin&gt;
         &lt;name&gt;foo&lt;/name&gt;
         &lt;size&gt;10&lt;/size&gt;
     &lt;/plugin&gt;
  &lt;pluginpool&gt;
  &lt;pluginpool&gt;
     &lt;plugin&gt;
         &lt;name&gt;bar&lt;/name&gt;
         &lt;size&gt;5&lt;/size&gt;
     &lt;/plugin&gt;
  &lt;pluginpool&gt;
…
…
</pre></p>
<p>As you would notice, we have a pool of 10 for foo and 5 for bar. Now, when our Akka actors need to pick up a plugin, they go to the PoolManager and ask for the plugin.</p>
<p style="text-align:center;"><a href="http://vikashazrati.files.wordpress.com/2011/08/scala-object-pooling.png"><img class="size-full wp-image-2256 aligncenter" title="scala-object-pooling" src="http://vikashazrati.files.wordpress.com/2011/08/scala-object-pooling.png?w=594" alt=""   /></a></p>
<p>The PoolManager (is a Scala object) keeps track of the PluginPool(s) which are populated when the framework is initialized. It maintains a map of all the PluginPool(s) available. Hence, at the time of framework initialization the following function of PoolManager is called,</p>
<p><pre class="brush: scala;">
def createPluginPool(pluginName: String, pluginClassName: String, size: Int) {
    val pluginPool = new PluginPool(pluginClassName, size)
    for (i  pluginPool)
  }
</pre></p>
<p>The PluginPool class looks like this<br />
<span id="more-2255"></span></p>
<p><pre class="brush: scala;">
import java.util.concurrent.ArrayBlockingQueue
import java.util.concurrent.atomic.AtomicInteger
import java.util.concurrent.TimeUnit

class PluginPool(pluginClassName: String, number: Int) extends Pool {

  private val size = new AtomicInteger(0)
  private val pool = new ArrayBlockingQueue[DPMPlugin](number)

  def fetchPlugin(): DPMPlugin = {
    pool.poll() match {
      case plugin: DPMPlugin =&gt; return plugin
      case null =&gt; createOrBlock
    }
  }

  def releasePlugin(plugin: DPMPlugin): Unit = {
    pool.offer(plugin)
  }

  def addPlugin(plugin: DPMPlugin):Unit ={
    pool.add(plugin)
  }

  private def createOrBlock: DPMPlugin = {
    size.get match {
      case e: Int if e == number =&gt; block
      case _ =&gt; create
    }
  }

  private def create: DPMPlugin = {
    size.incrementAndGet match {
      case e: Int if e &gt; number =&gt; size.decrementAndGet; fetchPlugin()
      case e: Int =&gt; Class.forName(pluginClassName).newInstance().asInstanceOf[DPMPlugin]
    }
  }

  private def block: DPMPlugin = {
    val timeout = 5000
    pool.poll(timeout, TimeUnit.MILLISECONDS) match {
      case plugin: DPMPlugin =&gt; plugin
      case _ =&gt; throw new Exception(&quot;Couldn't acquire a connection in %d milliseconds.&quot;.format(timeout))
    }
  }
}
</pre></p>
<p>This has been inspired by the <a href="https://github.com/jamesgolick/scala-connection-pool" target="_blank">connection pool written for scala</a>.</p>
<p>And now, let us look at how the Akka actor gets the plugin. It makes a call to getPlugin() with the name of the plugin</p>
<p><pre class="brush: scala;">
 def getPlugin(name: String): Option[DPMPlugin] = {
    val pluginPool = fetchPluginPool(name)
    pluginPool match {
      case null =&gt; None
      case _ =&gt; {
        logger.debug(&quot;Got the plugin_ _ _&quot;)
        Some(pluginPool.fetchPlugin())
      }
    }
  }

private def fetchPluginPool(name: String): PluginPool = {
    val pluginPool = pluginsPoolMap.get(name)
    pluginPool match {
      case Some(pool) =&gt; {
        logger.debug(&quot;Fetching plugin from persistent pool.&quot;)
        pool.asInstanceOf[PluginPool]
      }
      case None =&gt; {
        null
      }
    }
  }
</pre></p>
<p>The PoolManager picks up the right pool and calls fetchPlugin on the pool.</p>
<p>Once the actor is done with its processing, it releases the plugin back to the pool so that it is available for a different actor.</p>
<p><pre class="brush: scala;">
 def releasePlugin(name: String, plugin: DPMPlugin) {
    val pluginPool = fetchPluginPool(name)
    if (pluginPool != null) pluginPool.releasePlugin(plugin)

  }
</pre> </p>
<br />Filed under: <a href='http://vikashazrati.wordpress.com/category/architecture/'>Architecture</a>, <a href='http://vikashazrati.wordpress.com/category/scala-3-4/'>Scala</a> Tagged: <a href='http://vikashazrati.wordpress.com/tag/object-pool/'>Object Pool</a>, <a href='http://vikashazrati.wordpress.com/tag/scala-2/'>scala</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vikashazrati.wordpress.com/2255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vikashazrati.wordpress.com/2255/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vikashazrati.wordpress.com/2255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vikashazrati.wordpress.com/2255/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vikashazrati.wordpress.com/2255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vikashazrati.wordpress.com/2255/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vikashazrati.wordpress.com/2255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vikashazrati.wordpress.com/2255/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vikashazrati.wordpress.com/2255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vikashazrati.wordpress.com/2255/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vikashazrati.wordpress.com/2255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vikashazrati.wordpress.com/2255/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vikashazrati.wordpress.com/2255/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vikashazrati.wordpress.com/2255/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vikashazrati.wordpress.com&amp;blog=999392&amp;post=2255&amp;subd=vikashazrati&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vikashazrati.wordpress.com/2011/08/26/creating-object-pools-in-scala/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<georss:point>28.632778 77.219722</georss:point>
		<geo:lat>28.632778</geo:lat>
		<geo:long>77.219722</geo:long>
		<media:content url="http://0.gravatar.com/avatar/c5837310ca52838cc93da501d1dd4d48?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vikashazrati</media:title>
		</media:content>

		<media:content url="http://vikashazrati.files.wordpress.com/2011/08/scala-object-pooling.png" medium="image">
			<media:title type="html">scala-object-pooling</media:title>
		</media:content>
	</item>
	</channel>
</rss>
