<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-6999217373541258402</id><updated>2025-11-06T05:22:47.761-05:00</updated><category term="Scala"/><category term="Songs"/><category term="Tomcat"/><category term="SimpleMailMessage"/><category term="Spring"/><category term="bean factory-method"/><category term="decomposition"/><category term="pattern matching"/><title type='text'>Âscendant</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://ascendant76.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://ascendant76.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Ramesh</name><uri>http://www.blogger.com/profile/07716715053929751798</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>87</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-6999217373541258402.post-5865824629296831191</id><published>2014-03-30T21:48:00.001-05:00</published><updated>2014-03-30T21:48:27.203-05:00</updated><title type='text'>Scala Pattern Matching &amp; Decomposition</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;style type=&quot;text/css&quot;&gt;ul.lst-kix_6r257rji47d3-6{list-style-type:none}.lst-kix_ibqsv1371314-0&gt;li:before{content:&quot;\0025cf  &quot;}ul.lst-kix_6r257rji47d3-7{list-style-type:none}ul.lst-kix_6r257rji47d3-8{list-style-type:none}.lst-kix_6r257rji47d3-0&gt;li:before{content:&quot;\0025cf  &quot;}ul.lst-kix_ibqsv1371314-0{list-style-type:none}ul.lst-kix_ibqsv1371314-1{list-style-type:none}.lst-kix_ibqsv1371314-1&gt;li:before{content:&quot;\0025cb  &quot;}.lst-kix_6r257rji47d3-6&gt;li:before{content:&quot;\0025cf  &quot;}.lst-kix_6r257rji47d3-5&gt;li:before{content:&quot;\0025a0  &quot;}.lst-kix_6r257rji47d3-2&gt;li:before{content:&quot;\0025a0  &quot;}.lst-kix_6r257rji47d3-3&gt;li:before{content:&quot;\0025cf  &quot;}ul.lst-kix_6r257rji47d3-1{list-style-type:none}ul.lst-kix_6r257rji47d3-0{list-style-type:none}ul.lst-kix_6r257rji47d3-3{list-style-type:none}ul.lst-kix_6r257rji47d3-2{list-style-type:none}ul.lst-kix_6r257rji47d3-5{list-style-type:none}ul.lst-kix_6r257rji47d3-4{list-style-type:none}.lst-kix_ibqsv1371314-4&gt;li:before{content:&quot;\0025cb  &quot;}.lst-kix_ibqsv1371314-6&gt;li:before{content:&quot;\0025cf  &quot;}.lst-kix_6r257rji47d3-7&gt;li:before{content:&quot;\0025cb  &quot;}.lst-kix_6r257rji47d3-8&gt;li:before{content:&quot;\0025a0  &quot;}.lst-kix_ibqsv1371314-5&gt;li:before{content:&quot;\0025a0  &quot;}.lst-kix_ibqsv1371314-7&gt;li:before{content:&quot;\0025cb  &quot;}.lst-kix_ibqsv1371314-3&gt;li:before{content:&quot;\0025cf  &quot;}.lst-kix_ibqsv1371314-8&gt;li:before{content:&quot;\0025a0  &quot;}.lst-kix_6r257rji47d3-4&gt;li:before{content:&quot;\0025cb  &quot;}.lst-kix_ibqsv1371314-2&gt;li:before{content:&quot;\0025a0  &quot;}ul.lst-kix_ibqsv1371314-4{list-style-type:none}ul.lst-kix_ibqsv1371314-5{list-style-type:none}ul.lst-kix_ibqsv1371314-2{list-style-type:none}ul.lst-kix_ibqsv1371314-3{list-style-type:none}ul.lst-kix_ibqsv1371314-8{list-style-type:none}ul.lst-kix_ibqsv1371314-6{list-style-type:none}ul.lst-kix_ibqsv1371314-7{list-style-type:none}.lst-kix_6r257rji47d3-1&gt;li:before{content:&quot;\0025cb  &quot;}ol{margin:0;padding:0}.c5{padding-left:0pt;line-height:1.5;padding-top:23pt;widows:2;orphans:2;direction:ltr;margin-left:36pt;padding-bottom:23pt}.c1{padding-left:0pt;line-height:1.5;padding-top:11pt;widows:2;orphans:2;direction:ltr;margin-left:36pt;padding-bottom:11pt}.c2{line-height:1.5;padding-top:11pt;widows:2;orphans:2;direction:ltr;padding-bottom:11pt}.c7{widows:2;orphans:2;height:11pt;direction:ltr}.c4{line-height:1.8;widows:2;orphans:2;direction:ltr}.c3{color:#444444;font-size:12pt;font-family:&quot;Georgia&quot;}.c9{font-size:10pt;background-color:#f8f8f8;font-family:&quot;Georgia&quot;}.c11{max-width:468pt;background-color:#ffffff;padding:72pt 72pt 72pt 72pt}.c10{background-color:#f8f8f8;font-family:&quot;Georgia&quot;}.c0{font-size:9pt;background-color:#f8f8f8}.c6{margin:0;padding:0}.c8{font-weight:bold}.title{widows:2;padding-top:0pt;line-height:1.15;orphans:2;text-align:left;color:#000000;font-size:21pt;font-family:&quot;Trebuchet MS&quot;;padding-bottom:0pt;page-break-after:avoid}.subtitle{widows:2;padding-top:0pt;line-height:1.15;orphans:2;text-align:left;color:#666666;font-style:italic;font-size:13pt;font-family:&quot;Trebuchet MS&quot;;padding-bottom:10pt;page-break-after:avoid}li{color:#000000;font-size:11pt;font-family:&quot;Arial&quot;}p{color:#000000;font-size:11pt;margin:0;font-family:&quot;Arial&quot;}h1{widows:2;padding-top:10pt;line-height:1.15;orphans:2;text-align:left;color:#000000;font-size:16pt;font-family:&quot;Trebuchet MS&quot;;padding-bottom:0pt;page-break-after:avoid}h2{widows:2;padding-top:10pt;line-height:1.15;orphans:2;text-align:left;color:#000000;font-size:13pt;font-family:&quot;Trebuchet MS&quot;;font-weight:bold;padding-bottom:0pt;page-break-after:avoid}h3{widows:2;padding-top:8pt;line-height:1.15;orphans:2;text-align:left;color:#666666;font-size:12pt;font-family:&quot;Trebuchet MS&quot;;font-weight:bold;padding-bottom:0pt;page-break-after:avoid}h4{widows:2;padding-top:8pt;line-height:1.15;orphans:2;text-align:left;color:#666666;font-size:11pt;text-decoration:underline;font-family:&quot;Trebuchet MS&quot;;padding-bottom:0pt;page-break-after:avoid}h5{widows:2;padding-top:8pt;line-height:1.15;orphans:2;text-align:left;color:#666666;font-size:11pt;font-family:&quot;Trebuchet MS&quot;;padding-bottom:0pt;page-break-after:avoid}h6{widows:2;padding-top:8pt;line-height:1.15;orphans:2;text-align:left;color:#666666;font-style:italic;font-size:11pt;font-family:&quot;Trebuchet MS&quot;;padding-bottom:0pt;page-break-after:avoid}&lt;/style&gt;&lt;br /&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;In this blog post, I will cover various pattern matching variants that I learned in my journey to learn Scala.&lt;/span&gt;&lt;/div&gt;
&lt;ul class=&quot;c6 lst-kix_ibqsv1371314-0 start&quot;&gt;
&lt;li class=&quot;c1&quot;&gt;&lt;span class=&quot;c3&quot;&gt;Matching constant values&lt;/span&gt;&lt;/li&gt;
&lt;li class=&quot;c1&quot;&gt;&lt;span class=&quot;c3&quot;&gt;Wildcard matcher&lt;/span&gt;&lt;/li&gt;
&lt;li class=&quot;c1&quot;&gt;&lt;span class=&quot;c3&quot;&gt;Variable matcher pattern&lt;/span&gt;&lt;/li&gt;
&lt;li class=&quot;c1&quot;&gt;&lt;span class=&quot;c3&quot;&gt;Variables and constants (also know as Stable identifier pattern)&lt;/span&gt;&lt;/li&gt;
&lt;li class=&quot;c1&quot;&gt;&lt;span class=&quot;c3&quot;&gt;Matching types&lt;/span&gt;&lt;/li&gt;
&lt;li class=&quot;c1&quot;&gt;&lt;span class=&quot;c3&quot;&gt;Matching types and restricting the data with guards&lt;/span&gt;&lt;/li&gt;
&lt;li class=&quot;c1&quot;&gt;&lt;span class=&quot;c3&quot;&gt;Matching types(tuples and other objects) &amp;amp; decompostion&lt;/span&gt;&lt;/li&gt;
&lt;li class=&quot;c1&quot;&gt;&lt;span class=&quot;c3&quot;&gt;Matching lists &amp;amp; decomposing lists&lt;/span&gt;&lt;/li&gt;
&lt;li class=&quot;c1&quot;&gt;&lt;span class=&quot;c3&quot;&gt;Matching sequences, ranges and decomposing them&lt;/span&gt;&lt;/li&gt;
&lt;li class=&quot;c1&quot;&gt;&lt;span class=&quot;c3&quot;&gt;Pattern matching in catching exceptions&lt;/span&gt;&lt;/li&gt;
&lt;li class=&quot;c1&quot;&gt;&lt;span class=&quot;c3&quot;&gt;Define multiple vals with patterns&lt;/span&gt;&lt;/li&gt;
&lt;li class=&quot;c1&quot;&gt;&lt;span class=&quot;c3&quot;&gt;Pattern matching in generators&lt;/span&gt;&lt;/li&gt;
&lt;li class=&quot;c1&quot;&gt;&lt;span class=&quot;c3&quot;&gt;Decomposing values based on Regular Expressions&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;I would like introduce val types, tuples, singleton object, case class to help us with the examples for each variant.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c4&quot;&gt;
&lt;span class=&quot;c0&quot;&gt;// Constants&lt;br /&gt;val TheGood = &quot;The Good&quot;&lt;br /&gt;val TheBad = &quot;The Bad&quot;&lt;br /&gt;val theUgly = &quot;The Ugly&quot;&lt;br /&gt;&lt;br /&gt;// A tuple&lt;br /&gt;val tuple = (1, &quot;It is me&quot;)&lt;br /&gt;&lt;br /&gt;// A singleton object&lt;br /&gt;object Singleton&lt;br /&gt;&lt;br /&gt;// A case class&lt;br /&gt;case class Container(x:Any)&lt;br /&gt;&lt;br /&gt;// Singleton object with constants&lt;br /&gt;object Constants {&lt;br /&gt; &amp;nbsp; &amp;nbsp;val pi = &quot;3.14&quot;&lt;br /&gt; &amp;nbsp; &amp;nbsp;val PI = 3.14&lt;br /&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3 c8&quot;&gt;Matching constant values:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;The following function matches values if x is 3.14 or &quot;3.14&quot;. For all other values it raises scala.MatchError exception indicating that, the pattern matching couldn&#39;t fall in any case listed.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c4&quot;&gt;
&lt;span class=&quot;c0&quot;&gt;def matchConstant(x:Any) = x match {&lt;br /&gt; &amp;nbsp; &amp;nbsp;case 3.14 &amp;nbsp; =&amp;gt; &quot;PI&quot;&lt;br /&gt; &amp;nbsp; &amp;nbsp;case &quot;3.14&quot; =&amp;gt; &quot;PI value as String&quot;&lt;br /&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;If you invoke the above function with various values as below:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c4&quot;&gt;
&lt;span class=&quot;c0&quot;&gt;scala&amp;gt; matchConstant(3.14)&lt;br /&gt;res1: String = PI&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchConstant(&quot;3.14&quot;)&lt;br /&gt;res2: String = PI value as String&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchConstant(2)&lt;br /&gt;scala.MatchError: 2 (of class java.lang.Integer)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3 c8&quot;&gt;Wildcard matcher:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;We got scala.MatchError as there is no case, that could match the input integer 2. We can fix this by using wildcard matcher pattern so that we can prevent the MatchError.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;Added wildcard mather to the matchConstant function:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c4&quot;&gt;
&lt;span class=&quot;c0&quot;&gt;def matchConstant(x:Any) = x match {&lt;br /&gt; &amp;nbsp; &amp;nbsp;case 3.14 &amp;nbsp; =&amp;gt; &quot;PI&quot;&lt;br /&gt; &amp;nbsp; &amp;nbsp;case &quot;3.14&quot; =&amp;gt; &quot;PI value as String&quot;&lt;br /&gt;&lt;br /&gt; &amp;nbsp; &amp;nbsp;// Wildcard matcher&lt;br /&gt; &amp;nbsp; &amp;nbsp;case _ =&amp;gt; &quot;Unknown&quot;&lt;br /&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;Now invocation of matchConstant with integer parameter 2 results as &quot;Unknown&quot;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c4&quot;&gt;
&lt;span class=&quot;c0&quot;&gt;scala&amp;gt; matchConstant(2)&lt;br /&gt;res5: String = Unknown&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3 c8&quot;&gt;Variable matcher pattern&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;Certain times you may want to match any value you pass-in. It can be achieved using wildcard. Another alternate way to match is variable pattern. The following example illustrate the variable pattern&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c4&quot;&gt;
&lt;span class=&quot;c0&quot;&gt;def matchAnything(x:Any) = x match {&lt;br /&gt; &amp;nbsp; &amp;nbsp;// Variable pattern&lt;br /&gt; &amp;nbsp; &amp;nbsp;case n =&amp;gt; s&quot;Matched value $n&quot;&lt;br /&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;Invoking the above function:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c4&quot;&gt;
&lt;span class=&quot;c0&quot;&gt;scala&amp;gt; matchAnything(1)&lt;br /&gt;res40: String = Matched value 1&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchAnything(&quot;1&quot;)&lt;br /&gt;res41: String = Matched value 1&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchAnything(Singleton)&lt;br /&gt;res42: String = Matched value Singleton$@1d20b21&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3 c8&quot;&gt;Variables and constants (also know as Stable identifier pattern)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;The val types identifers and singletons fall under this category. Scala language enforces few rules to match stable identifiers.&lt;/span&gt;&lt;/div&gt;
&lt;ul class=&quot;c6 lst-kix_6r257rji47d3-0 start&quot;&gt;
&lt;li class=&quot;c1&quot;&gt;&lt;span class=&quot;c3&quot;&gt;The identifier for constants must start with a capital letter. For example, TheGood and TheBad adhere to this rule.&lt;/span&gt;&lt;/li&gt;
&lt;li class=&quot;c1&quot;&gt;&lt;span class=&quot;c3&quot;&gt;If the constant identfier didn&#39;t start with a capital letter, we need to use backtick(`) . For example &lt;/span&gt;&lt;span class=&quot;c10&quot;&gt;theUgly&lt;/span&gt;&lt;/li&gt;
&lt;li class=&quot;c5&quot;&gt;&lt;span class=&quot;c3&quot;&gt;If the constant identifer didnt&#39; start with a capital letter and it qualified the pattern matching will work. For example, Constants.pi is qualified.&lt;/span&gt;&lt;/li&gt;
&lt;li class=&quot;c5&quot;&gt;&lt;span class=&quot;c3&quot;&gt;import Constants._&lt;/span&gt;&lt;/li&gt;
&lt;li class=&quot;c5&quot;&gt;&lt;span class=&quot;c3&quot;&gt;def matchStableIdentifiers(x:Any) = x match {&lt;/span&gt;&lt;/li&gt;
&lt;li class=&quot;c1&quot;&gt;&lt;span class=&quot;c9&quot;&gt;case TheGood=&amp;gt; &quot;The Good&quot;&lt;br /&gt;case TheBad=&amp;gt; &quot;The Bad&quot;&lt;br /&gt;case `theUgly` =&amp;gt; &quot;The ugly&quot;&lt;br /&gt;&lt;br /&gt;// case `pi` =&amp;gt; &amp;nbsp;s&quot;Matched with backticks pi value $x&quot;&lt;br /&gt;case Constants.pi =&amp;gt; s&quot;Constants.pi has value $x&quot;&lt;br /&gt;case PI =&amp;gt; s&quot;PI value $x&quot;&lt;br /&gt;case Singleton =&amp;gt; &quot;The singleton&quot;&lt;br /&gt;&lt;br /&gt;// Wildcard pattern&lt;br /&gt;case _ =&amp;gt; s&quot;Any value $x&quot;&lt;/span&gt;&lt;/li&gt;
&lt;li class=&quot;c1&quot;&gt;&lt;span class=&quot;c3&quot;&gt;}&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;Let us invoke the matchStableIdentifiers function&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c4&quot;&gt;
&lt;span class=&quot;c0&quot;&gt;scala&amp;gt; matchStableIdentifiers(&quot;The Good&quot;)&lt;br /&gt;res19: String = The Good&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchStableIdentifiers(&quot;The Bad&quot;)&lt;br /&gt;res20: String = The Bad&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchStableIdentifiers(&quot;The Ugly&quot;)&lt;br /&gt;res21: String = The ugly&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchStableIdentifiers(3.14)&lt;br /&gt;res22: String = PI value 3.14&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchStableIdentifiers(&quot;3.14&quot;)&lt;br /&gt;res23: String = Constants.pi has value 3.14&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchStableIdentifiers(Singleton)&lt;br /&gt;res24: String = The singleton&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchStableIdentifiers(&quot;No where to go!&quot;)&lt;br /&gt;res25: String = Any value No where to go!&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3 c8&quot;&gt;Matching types:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;The pattern matching can be applied to based on type of the object. This is similar to instanceof operator in Java.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;In the following example we match x to case b:Boolean if x hold a boolean type value. Similarly if x holds a value of double type then case d:Double =&amp;gt;... will be matched.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c4&quot;&gt;
&lt;span class=&quot;c0&quot;&gt;def matchByType(x: Any) = x match {&lt;br /&gt; &amp;nbsp; &amp;nbsp;case b:Boolean =&amp;gt; s&quot;A boolean value: $b&quot;&lt;br /&gt; &amp;nbsp; &amp;nbsp;case d:Double =&amp;gt; s&quot;An double value: $d&quot;&lt;br /&gt; &amp;nbsp; &amp;nbsp;case _ =&amp;gt; &quot;Unknown type&quot;&lt;br /&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;Let us invoke matchByType function&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c4&quot;&gt;
&lt;span class=&quot;c0&quot;&gt;scala&amp;gt; matchByType(true)&lt;br /&gt;res26: String = A boolean value: true&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchByType(false)&lt;br /&gt;res27: String = A boolean value: false&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchByType(1)&lt;br /&gt;res28: String = Unknown type&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchByType(1.0)&lt;br /&gt;res29: String = An double value: 1.0&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3 c8&quot;&gt;Matching types and restricting the data with guards:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;Let us extends pattern matching to match specific values of a specific type. In the following example we use pattern matching to print a number is even or odd.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c4&quot;&gt;
&lt;span class=&quot;c0&quot;&gt;def matchValueOfTypeWithGuard(x: Any) = x match {&lt;br /&gt; &amp;nbsp; &amp;nbsp;case x:Int if x % 2 == 0 =&amp;gt; s&quot;The input is an even number : $x&quot;&lt;br /&gt; &amp;nbsp; &amp;nbsp;case x:Int if x % 2 != 0 =&amp;gt; s&quot;The input is a odd number : $x&quot;&lt;br /&gt; &amp;nbsp; &amp;nbsp;case _ =&amp;gt; &quot;Unknown type&quot;&lt;br /&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;Let us invoke the above function with various values:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c4&quot;&gt;
&lt;span class=&quot;c0&quot;&gt;scala&amp;gt; matchValueOfTypeWithGuard(2)&lt;br /&gt;res30: String = The input is an even number : 2&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchValueOfTypeWithGuard(3)&lt;br /&gt;res31: String = The input is a odd number : 3&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchValueOfTypeWithGuard(&quot;R&quot;)&lt;br /&gt;res32: String = Unknown value&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3 c8&quot;&gt;Matching types(tuples and other objects) &amp;amp; decompostion:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;Let me show examples of another variant in matching types and also decompose them.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;Following example, matches a tuple and decomposes it to extract objects used in constructing the tuple.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c4&quot;&gt;
&lt;span class=&quot;c0&quot;&gt;def matchByTypeAndDecompostion(x : Any) = x match {&lt;br /&gt;&lt;br /&gt; &amp;nbsp; &amp;nbsp;// Matches a tuple and extracts objects used in contructing the tuple&lt;br /&gt; &amp;nbsp; &amp;nbsp;case (x, y) =&amp;gt; s&quot;A tuple with values $x and $y&quot;&lt;br /&gt;&lt;br /&gt; &amp;nbsp; &amp;nbsp;case (x, y, z) =&amp;gt; s&quot;A tuple with values $x, $y and $z&quot;&lt;br /&gt;&lt;br /&gt; &amp;nbsp; &amp;nbsp;// Contructor decomposition pattern&lt;br /&gt; &amp;nbsp; &amp;nbsp;case Container(x) =&amp;gt; s&quot;Container with value: $x&quot;&lt;br /&gt;&lt;br /&gt; &amp;nbsp; &amp;nbsp;case _ =&amp;gt; &quot;Unknown type&quot;&lt;br /&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;Let us invoke the above function with various values:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c4&quot;&gt;
&lt;span class=&quot;c0&quot;&gt;scala&amp;gt; matchByTypeAndDecompostion((1, (1,2)))&lt;br /&gt;res36: String = A tuple with values 1 and (1,2)&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchByTypeAndDecompostion((1, (1,2), (1,2,3)))&lt;br /&gt;res37: String = A tuple with values 1, (1,2) and (1,2,3)&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchByTypeAndDecompostion(Container(3))&lt;br /&gt;res38: String = Container with value: 3&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchByTypeAndDecompostion(Container(&quot;Hello&quot;))&lt;br /&gt;res39: String = Container with value: Hello&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3 c8&quot;&gt;Matching lists &amp;amp; decomposing lists:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;Following examples demonistrate pattern matching List objects and also decomposing the list objects&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c4&quot;&gt;
&lt;span class=&quot;c0&quot;&gt;def matchList(x:Any) = x match {&lt;br /&gt; &amp;nbsp; &amp;nbsp;// matches a List(1,2,3)&lt;br /&gt; &amp;nbsp; &amp;nbsp;case List(1, 2, 3) =&amp;gt; &quot;Matched List(1, 2, 3)&quot;&lt;br /&gt;&lt;br /&gt; &amp;nbsp; &amp;nbsp;// Matches a list with only one element and extracts the element by decompising the list&lt;br /&gt; &amp;nbsp; &amp;nbsp;case x :: Nil =&amp;gt; s&quot;Only one element in the list $x&quot;&lt;br /&gt;&lt;br /&gt; &amp;nbsp; &amp;nbsp;// &amp;nbsp;Matching a list with single element and extracting the element, can be achieved using constructor decomposition&lt;br /&gt; &amp;nbsp; &amp;nbsp;//case List(x) =&amp;gt; s&quot;Only one element in the list $x&quot;&lt;br /&gt;&lt;br /&gt; &amp;nbsp; &amp;nbsp;// Matches a list with head as integer 2&lt;br /&gt; &amp;nbsp; &amp;nbsp;case 2 :: xs &amp;nbsp;=&amp;gt; s&quot;A list with head 2 and tail $xs&quot;&lt;br /&gt;&lt;br /&gt; &amp;nbsp; &amp;nbsp;// Matches a list that has integer 3 followed by 4 and other elements. &lt;br /&gt; &amp;nbsp; &amp;nbsp;// Decomposes list and extract rest of the list excluding the first two elements&lt;br /&gt; &amp;nbsp; &amp;nbsp;case List(3, 4, xs @ _*) =&amp;gt; s&quot;A list with &amp;nbsp;3, 4 and tail $xs&quot;&lt;br /&gt;&lt;br /&gt; &amp;nbsp; &amp;nbsp;// Decomposing a list as head and tail&lt;br /&gt; &amp;nbsp; &amp;nbsp;case x :: xs =&amp;gt; s&quot;A list with head $x and tail $xs&quot;&lt;br /&gt;&lt;br /&gt; &amp;nbsp; &amp;nbsp;// Matching a empty list or null&lt;br /&gt; &amp;nbsp; &amp;nbsp;case nil =&amp;gt; &quot;Empty List&quot;&lt;br /&gt; &amp;nbsp; &amp;nbsp;// Matching a empty list using contructor decomposition??&lt;br /&gt; &amp;nbsp; &amp;nbsp;//case List() =&amp;gt; &amp;nbsp;&quot;Empty List&quot;&lt;br /&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;Let us invoke the function to match a specific case&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c4&quot;&gt;
&lt;span class=&quot;c0&quot;&gt;scala&amp;gt; matchList(List(1,2,3))&lt;br /&gt;res49: String = Matched List(1, 2, 3)&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchList(List(1))&lt;br /&gt;res50: String = Only one element in the list 1&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchList(List(2,3,4))&lt;br /&gt;res51: String = A list with head 2 and tail List(3, 4)&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchList(List(3,4,5,6,7,8,9))&lt;br /&gt;res52: String = A list with &amp;nbsp;3, 4 and tail List(5, 6, 7, 8, 9)&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchList(List(4,5,6,7,8,9))&lt;br /&gt;res53: String = A list with head 4 and tail List(5, 6, 7, 8, 9)&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchList(List())&lt;br /&gt;res54: String = Empty List&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchList(null)&lt;br /&gt;res55: String = Empty List&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3 c8&quot;&gt;Matching sequences, ranges and decomposing them:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;Following example illustrates matching sequences, ranges and decompose them.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c4&quot;&gt;
&lt;span class=&quot;c0&quot;&gt;def matchSequence[E](x: Seq[E]) = x match {&lt;br /&gt; &amp;nbsp; &amp;nbsp;case x +: Nil =&amp;gt; s&quot;Only element is head: $x&quot;&lt;br /&gt; &amp;nbsp; &amp;nbsp;case _ :+ x =&amp;gt; s&quot;Last element $x&quot;&lt;br /&gt; &amp;nbsp; &amp;nbsp;case nil =&amp;gt; &quot;Empty Sequence&quot;&lt;br /&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;Let us invoke the above function with ranges and sequences&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c4&quot;&gt;
&lt;span class=&quot;c0&quot;&gt;scala&amp;gt; matchSequence( 1 to 3)&lt;br /&gt;res56: String = Last element 3&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchSequence( 1 to 3 take 1)&lt;br /&gt;res57: String = Only element is head: 1&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchSequence( 1 to 3 take 0)&lt;br /&gt;res58: String = Empty Sequence&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchSequence(Seq(1,2,3,4))&lt;br /&gt;res64: String = Last element 4&lt;br /&gt;&lt;br /&gt;scala&amp;gt; matchSequence(Seq(1))&lt;br /&gt;res65: String = Only element is head: 1&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3 c8&quot;&gt;Pattern matching in catching exceptions&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;The following example demonistrates catching various exception using pattern matching.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c4&quot;&gt;
&lt;span class=&quot;c0&quot;&gt;def stringToPositiveIntOnly(s: String) = try {&lt;br /&gt; &amp;nbsp; &amp;nbsp;val i = s.toInt&lt;br /&gt; &amp;nbsp; &amp;nbsp;println(s&quot;Input value $i&quot;)&lt;br /&gt; &amp;nbsp; &amp;nbsp;assert(i &amp;gt; 0)&lt;br /&gt;} catch {&lt;br /&gt; &amp;nbsp; &amp;nbsp;case e: NumberFormatException =&amp;gt; &quot;Not a number&quot; &lt;br /&gt; &amp;nbsp; &amp;nbsp;case e:java.lang.AssertionError =&amp;gt; &quot;Assertion failed&quot;&lt;br /&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;Let us invoke the above function with various values&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c4&quot;&gt;
&lt;span class=&quot;c0&quot;&gt;scala&amp;gt; stringToPositiveIntOnly(null)&lt;br /&gt;res84: Any = Not a number&lt;br /&gt;&lt;br /&gt;scala&amp;gt; stringToPositiveIntOnly(&quot;-1&quot;)&lt;br /&gt;Input value -1&lt;br /&gt;res85: Any = Assertion failed&lt;br /&gt;&lt;br /&gt;scala&amp;gt; stringToPositiveIntOnly(&quot;1&quot;)&lt;br /&gt;Input value 1&lt;br /&gt;res86: Any = ()&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3 c8&quot;&gt;Defining multiple vals with patterns:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;The following examples illustrate pattern matching and decompose them to multiple vals&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c4&quot;&gt;
&lt;span class=&quot;c0&quot;&gt;scala&amp;gt; val (a, b) = (1, (&quot;Ramesh&quot;, &quot;NY&quot;))&lt;br /&gt;a: Int = 1&lt;br /&gt;b: (String, String) = (Ramesh,NY)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3 c8&quot;&gt;Pattern matching in generators:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c4&quot;&gt;
&lt;span class=&quot;c0&quot;&gt;for ( keyAndValue &lt;- -=&quot;&quot; vector=&quot;&quot;&gt; &quot;a&quot;, 2 -&amp;gt; &quot;b&quot;) ) &lt;br /&gt; &amp;nbsp; &amp;nbsp;yield keyAndValue._2&lt;br /&gt;&lt;br /&gt;for ( (k, v) &lt;- -=&quot;&quot; 1=&quot;&quot; vector=&quot;&quot;&gt; &quot;a&quot;, &amp;nbsp;2 -&amp;gt; &quot;b&quot;) ) &lt;br /&gt; &amp;nbsp; &amp;nbsp;yield v&lt;!-----&gt;&lt;!-----&gt;&lt;/-&gt;&lt;/-&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3 c8&quot;&gt;Decomposing values based on Regular Expressions:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c2&quot;&gt;
&lt;span class=&quot;c3&quot;&gt;Regular expression can be used to match certain values and decompse the input to extract interesting parts. Notice that the decomposition will be based on the groups matched by regular expression&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c4&quot;&gt;
&lt;span class=&quot;c0&quot;&gt;// Regular Expressions with only one groups&lt;br /&gt;val Yes = &quot;(y|Y)es&quot;.r&lt;br /&gt;&lt;br /&gt;// Checking regex works&lt;br /&gt;Yes.pattern.matcher(&quot;yes&quot;).matches&lt;br /&gt;Yes.unapplySeq(&quot;yes&quot;) will result: Option[List[String]] = Some(List(y))&lt;br /&gt;&lt;br /&gt;def catchYes(s:String) = s match {&lt;br /&gt; &amp;nbsp; &amp;nbsp;case Yes(x) =&amp;gt; s&lt;br /&gt; &amp;nbsp; &amp;nbsp;case _ =&amp;gt; None&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;catchYes(&quot;yes&quot;)&lt;br /&gt;catchYes(&quot;Yes&quot;)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Regular Expressions with multiple groups&lt;br /&gt;val Yes = &quot;(y|Y)(es)&quot;.r&lt;br /&gt;&lt;br /&gt;// Checking regex works&lt;br /&gt;Yes.pattern.matcher(&quot;yes&quot;).matches&lt;br /&gt;Yes.unapplySeq(&quot;yes&quot;) will result: Option[List[String]] = Some(List(y, es))&lt;br /&gt;&lt;br /&gt;def catchYes(s:String) = s match {&lt;br /&gt; &amp;nbsp; &amp;nbsp;// As regex has two groups we should have enough parameter to extract&lt;br /&gt; &amp;nbsp; &amp;nbsp;case Yes(x,y) =&amp;gt; s&lt;br /&gt; &amp;nbsp; &amp;nbsp;case _ =&amp;gt; None&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;catchYes(&quot;yes&quot;)&lt;br /&gt;catchYes(&quot;Yes&quot;)&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c7&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://ascendant76.blogspot.com/feeds/5865824629296831191/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6999217373541258402/5865824629296831191' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/5865824629296831191'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/5865824629296831191'/><link rel='alternate' type='text/html' href='http://ascendant76.blogspot.com/2014/03/scala-pattern-matching-decomposition.html' title='Scala Pattern Matching &amp; Decomposition'/><author><name>Ramesh</name><uri>http://www.blogger.com/profile/07716715053929751798</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6999217373541258402.post-5875492053930995126</id><published>2014-03-30T21:18:00.004-05:00</published><updated>2014-03-30T21:18:59.935-05:00</updated><title type='text'>Take away from the talk &quot;Simplicity in Scala Design&quot; by Bill Venners</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h2&gt;
Take away from the talk &quot;Simplicity in Scala Design&quot; by Bill Venners&lt;/h2&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;object class=&quot;BLOGGER-youtube-video&quot; classid=&quot;clsid:D27CDB6E-AE6D-11cf-96B8-444553540000&quot; codebase=&quot;http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0&quot; data-thumbnail-src=&quot;https://ytimg.googleusercontent.com/vi/Pqh_QFFzIlI/0.jpg&quot; height=&quot;266&quot; width=&quot;320&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;https://youtube.googleapis.com/v/Pqh_QFFzIlI&amp;source=uds&quot; /&gt;&lt;param name=&quot;bgcolor&quot; value=&quot;#FFFFFF&quot; /&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot; /&gt;&lt;embed width=&quot;320&quot; height=&quot;266&quot;  src=&quot;https://youtube.googleapis.com/v/Pqh_QFFzIlI&amp;source=uds&quot; type=&quot;application/x-shockwave-flash&quot; allowfullscreen=&quot;true&quot;&gt;&lt;/embed&gt;&lt;/object&gt;
&quot;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;b&gt;Simplicity in Scala Design&lt;/b&gt;&quot; by&amp;nbsp;&lt;b&gt;Bill Venners&lt;/b&gt;
&lt;/div&gt;
&lt;br /&gt;
- Design for busy teams.
 Make the user use the library with out reading about it. Just like the car manual  no one will read it when renting the car.&lt;br /&gt;
&lt;br /&gt;
- Make it obvious, guessable, or easy to remember.&lt;br /&gt;
&lt;br /&gt;
- Design for readers and then for writers.&lt;br /&gt;
&amp;nbsp; Make both parties happy. When you cannot favor reader. 
 Make it hard to type when you want to discourage an specific method by choose a longer name&lt;br /&gt;
&lt;br /&gt;
- Make errors impossible or difficult.&lt;br /&gt;
&amp;nbsp; Enforce it via type errors if possible
 Let the user be specific when getting functionality based on contracts&lt;br /&gt;
&lt;br /&gt;
- Document with examples.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;Provide samples examples in documentation. Provide good examples as people tend to copy.&lt;br /&gt;
&lt;br /&gt;
- Minimize redundancy.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;There should be one -- and preferably only one -- obvious way to do it.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;In scalatest should vs must. Only one choice should have been there.&lt;br /&gt;
&lt;br /&gt;
- Maximize consistency.&lt;br /&gt;
&amp;nbsp; API should  be consistent.&lt;br /&gt;
&amp;nbsp; Consider Set, Map and Seq  first two are immutable whereas Seq is not.&lt;br /&gt;
&amp;nbsp; Providing variations won&#39;t leave a choice to readers. Where as writer can pick one.&lt;br /&gt;
&lt;br /&gt;
- Minimize the magic. &lt;br /&gt;
&amp;nbsp; Avoid implicits&lt;br /&gt;
&lt;br /&gt;
&quot;Remember what tools are for. They are for solving problems, not finding problems to solve&quot;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;- Terry Wall&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://ascendant76.blogspot.com/feeds/5875492053930995126/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6999217373541258402/5875492053930995126' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/5875492053930995126'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/5875492053930995126'/><link rel='alternate' type='text/html' href='http://ascendant76.blogspot.com/2014/03/take-away-from-talk-simplicity-in-scala.html' title='Take away from the talk &quot;Simplicity in Scala Design&quot; by Bill Venners'/><author><name>Ramesh</name><uri>http://www.blogger.com/profile/07716715053929751798</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6999217373541258402.post-5457341657832302459</id><published>2012-10-31T20:46:00.004-05:00</published><updated>2012-10-31T20:48:28.355-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="decomposition"/><category scheme="http://www.blogger.com/atom/ns#" term="pattern matching"/><category scheme="http://www.blogger.com/atom/ns#" term="Scala"/><title type='text'>Scala Pattern Matching &amp; Decomposition</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
&lt;i&gt;&lt;b&gt;Scala Pattern Matching &amp;amp; Decomposition&lt;/b&gt;&lt;/i&gt;&lt;/h2&gt;
&lt;i&gt;&lt;/i&gt;&lt;br /&gt;
In this blog post, I will cover various pattern matching variants that I 
learned in my journey to learn Scala.&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Matching constant values 
&lt;/li&gt;
&lt;li&gt;Wildcard matcher 
&lt;/li&gt;
&lt;li&gt;Variable matcher pattern 
&lt;/li&gt;
&lt;li&gt;Variables and constants (also know as Stable identifier pattern) 
&lt;/li&gt;
&lt;li&gt;Matching types 
&lt;/li&gt;
&lt;li&gt;Matching types and restricting the data with guards 
&lt;/li&gt;
&lt;li&gt;Matching types(tuples and other objects) &amp;amp; decompostion 
&lt;/li&gt;
&lt;li&gt;Matching lists &amp;amp; decomposing lists 
&lt;/li&gt;
&lt;li&gt;Matching sequences, ranges and decomposing them 
&lt;/li&gt;
&lt;li&gt;Pattern matching in catching exceptions 
&lt;/li&gt;
&lt;li&gt;Define multiple vals with patterns 
&lt;/li&gt;
&lt;li&gt;Pattern matching in generators 
&lt;/li&gt;
&lt;li&gt;Decomposing values based on Regular Expressions &lt;/li&gt;
&lt;/ul&gt;
I would like introduce val types, tuples, singleton object, case class to 
help us with the examples for each variant.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;// Constants
val TheGood = &quot;The Good&quot;
val TheBad = &quot;The Bad&quot;
val theUgly = &quot;The Ugly&quot;

// A tuple
val tuple = (1, &quot;It is me&quot;)

// A singleton object
object Singleton

// A case class
case class Container(x:Any)

// Singleton object with constants
object Constants {
    val pi = &quot;3.14&quot;
    val PI = 3.14
} 
&lt;/code&gt;&lt;/pre&gt;
&lt;b&gt;Matching constant values:&lt;/b&gt;&lt;br /&gt;
The following function matches values if x is 3.14 or “3.14”. For all other 
values it raises scala.MatchError exception indicating that, the pattern 
matching couldn’t fall in any case listed.&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;def matchConstant(x:Any) = x match {
    case 3.14   =&amp;gt; &quot;PI&quot;
    case &quot;3.14&quot; =&amp;gt; &quot;PI value as String&quot;
}   
&lt;/code&gt;&lt;/pre&gt;
If you invoke the above function with various values as below:&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;scala&amp;gt; matchConstant(3.14)
res1: String = PI

scala&amp;gt; matchConstant(&quot;3.14&quot;)
res2: String = PI value as String

scala&amp;gt; matchConstant(2)
scala.MatchError: 2 (of class java.lang.Integer)&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;&amp;nbsp;&lt;/code&gt;&lt;/pre&gt;
&lt;b&gt;Wildcard matcher:&lt;/b&gt;&lt;br /&gt;
We got scala.MatchError as there is no case, that could match the input 
integer 2. We can fix this by using wildcard matcher pattern so that we can 
prevent the MatchError.&lt;br /&gt;
Added wildcard mather to the matchConstant function:&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;def matchConstant(x:Any) = x match {
    case 3.14   =&amp;gt; &quot;PI&quot;
    case &quot;3.14&quot; =&amp;gt; &quot;PI value as String&quot;

    // Wildcard matcher
    case _ =&amp;gt; &quot;Unknown&quot;
}   
&lt;/code&gt;&lt;/pre&gt;
Now invocation of matchConstant with integer parameter 2 results as 
“Unknown”&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;scala&amp;gt; matchConstant(2)
res5: String = Unknown&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;&amp;nbsp;&lt;/code&gt;&lt;/pre&gt;
&lt;b&gt;Variable matcher pattern&lt;/b&gt;&lt;br /&gt;
Certain times you may want to match any value you pass-in. It can be achieved 
using wildcard. Another alternate way to match is variable pattern. The 
following example illustrate the variable pattern&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;def matchAnything(x:Any) = x match {
    // Variable pattern
    case n =&amp;gt; s&quot;Matched value $n&quot;
}
&lt;/code&gt;&lt;/pre&gt;
Invoking the above function:&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;scala&amp;gt; matchAnything(1)
res40: String = Matched value 1

scala&amp;gt; matchAnything(&quot;1&quot;)
res41: String = Matched value 1

scala&amp;gt; matchAnything(Singleton)
res42: String = Matched value Singleton$@1d20b21
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Variables and constants (also know as Stable identifier pattern) 
&lt;/b&gt;&lt;br /&gt;
The val types identifers and singletons fall under this category. Scala 
language enforces few rules to match stable identifiers.&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;The identifier for constants must start with a capital letter. For example, 
TheGood and TheBad adhere to this rule. 
&lt;/li&gt;
&lt;li&gt;If the constant identfier didn’t start with a capital letter, we need to use 
backtick(&lt;code&gt;) . For example.,&lt;/code&gt;theUgly` 
&lt;/li&gt;
&lt;li&gt;
If the constant identifer didnt&#39; start with a capital letter and it qualified 
the pattern matching will work. For example, Constants.pi is qualified&lt;br /&gt;

import Constants._&lt;br /&gt;

def matchStableIdentifiers(x:Any) = x match {&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;  case TheGood=&amp;gt; &quot;The Good&quot;
  case TheBad=&amp;gt; &quot;The Bad&quot;
  case `theUgly` =&amp;gt; &quot;The ugly&quot;

  // case `pi` =&amp;gt;  s&quot;Matched with backticks pi value $x&quot;
  case Constants.pi =&amp;gt; s&quot;Constants.pi has value $x&quot;
  case PI =&amp;gt; s&quot;PI value $x&quot;
  case Singleton =&amp;gt; &quot;The singleton&quot;

  // Wildcard pattern
  case _ =&amp;gt; s&quot;Any value $x&quot;
&lt;/code&gt;&lt;/pre&gt;
}&lt;/li&gt;
&lt;/ul&gt;
Let us invoke the matchStableIdentifiers function&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;scala&amp;gt; matchStableIdentifiers(&quot;The Good&quot;)
res19: String = The Good

scala&amp;gt; matchStableIdentifiers(&quot;The Bad&quot;)
res20: String = The Bad

scala&amp;gt; matchStableIdentifiers(&quot;The Ugly&quot;)
res21: String = The ugly

scala&amp;gt; matchStableIdentifiers(3.14)
res22: String = PI value 3.14

scala&amp;gt; matchStableIdentifiers(&quot;3.14&quot;)
res23: String = Constants.pi has value 3.14

scala&amp;gt; matchStableIdentifiers(Singleton)
res24: String = The singleton

scala&amp;gt; matchStableIdentifiers(&quot;No where to go!&quot;)
res25: String = Any value No where to go!&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;&amp;nbsp;&lt;/code&gt;&lt;/pre&gt;
&lt;b&gt;Matching types:&lt;/b&gt;&lt;br /&gt;
The pattern matching can be applied to based on type of the object. This is 
similar to instanceof operator in Java.&lt;br /&gt;
In the following example we match x to case b:Boolean if x hold a boolean 
type value. Similarly if x holds a value of double type then case d:Double 
=&amp;gt;… will be matched.&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;def matchByType(x: Any) = x match {
    case b:Boolean =&amp;gt; s&quot;A boolean value: $b&quot;
    case d:Double =&amp;gt; s&quot;An double value: $d&quot;
    case _ =&amp;gt; &quot;Unknown type&quot;
}
&lt;/code&gt;&lt;/pre&gt;
Let us invoke matchByType function&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;scala&amp;gt; matchByType(true)
res26: String = A boolean value: true

scala&amp;gt; matchByType(false)
res27: String = A boolean value: false

scala&amp;gt; matchByType(1)
res28: String = Unknown type

scala&amp;gt; matchByType(1.0)
res29: String = An double value: 1.0
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Matching types and restricting the data with guards:&lt;/b&gt;&lt;br /&gt;
Let us extends pattern matching to match specific values of a specific type. 
In the following example we use pattern matching to print a number is even or 
odd.&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;def matchValueOfTypeWithGuard(x: Any) = x match {
    case x:Int if x % 2 == 0 =&amp;gt; s&quot;The input is an even number : $x&quot;
    case x:Int if x % 2 != 0 =&amp;gt; s&quot;The input is a odd number : $x&quot;
    case _ =&amp;gt; &quot;Unknown type&quot;
}
&lt;/code&gt;&lt;/pre&gt;
Let us invoke the above function with various values:&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;scala&amp;gt; matchValueOfTypeWithGuard(2)
res30: String = The input is an even number : 2

scala&amp;gt; matchValueOfTypeWithGuard(3)
res31: String = The input is a odd number : 3

scala&amp;gt; matchValueOfTypeWithGuard(&quot;R&quot;)
res32: String = Unknown value&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;&amp;nbsp;&lt;/code&gt;&lt;/pre&gt;
&lt;b&gt;Matching types(tuples and other objects) &amp;amp; 
decompostion:&lt;/b&gt;&lt;br /&gt;
Let me show examples of another variant in matching types and also decompose 
them.&lt;br /&gt;
Following example, matches a tuple and decomposes it to extract objects used 
in constructing the tuple.&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;def matchByTypeAndDecompostion(x : Any) = x match {

    // Matches a tuple and extracts objects used in contructing the tuple
    case (x, y) =&amp;gt; s&quot;A tuple with values $x and $y&quot;

    case (x, y, z) =&amp;gt; s&quot;A tuple with values $x, $y and $z&quot;

    // Contructor decomposition pattern
    case Container(x) =&amp;gt; s&quot;Container with value: $x&quot;

    case _ =&amp;gt; &quot;Unknown type&quot;
}
&lt;/code&gt;&lt;/pre&gt;
Let us invoke the above function with various values:&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;scala&amp;gt; matchByTypeAndDecompostion((1, (1,2)))
res36: String = A tuple with values 1 and (1,2)

scala&amp;gt; matchByTypeAndDecompostion((1, (1,2), (1,2,3)))
res37: String = A tuple with values 1, (1,2) and (1,2,3)

scala&amp;gt; matchByTypeAndDecompostion(Container(3))
res38: String = Container with value: 3

scala&amp;gt; matchByTypeAndDecompostion(Container(&quot;Hello&quot;))
res39: String = Container with value: Hello&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;&amp;nbsp;&lt;/code&gt;&lt;/pre&gt;
&lt;b&gt;Matching lists &amp;amp; decomposing lists:&lt;/b&gt;&lt;br /&gt;
Following examples demonistrate pattern matching List objects and also 
decomposing the list objects&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;def matchList(x:Any) = x match {
    // matches a List(1,2,3)
    case List(1, 2, 3) =&amp;gt; &quot;Matched List(1, 2, 3)&quot;

    // Matches a list with only one element and extracts the element by decompising the list
    case x :: Nil =&amp;gt; s&quot;Only one element in the list $x&quot;

    //  Matching a list with single element and extracting the element, can be achieved using constructor decomposition
    //case List(x) =&amp;gt; s&quot;Only one element in the list $x&quot;

    // Matches a list with head as integer 2
    case 2 :: xs  =&amp;gt; s&quot;A list with head 2 and tail $xs&quot;

    // Matches a list that has integer 3 followed by 4 and other elements. 
    // Decomposes list and extract rest of the list excluding the first two elements
    case List(3, 4, xs @ _*) =&amp;gt; s&quot;A list with  3, 4 and tail $xs&quot;

    // Decomposing a list as head and tail
    case x :: xs =&amp;gt; s&quot;A list with head $x and tail $xs&quot;

    // Matching a empty list or null
    case nil =&amp;gt; &quot;Empty List&quot;
    // Matching a empty list using contructor decomposition??
    //case List() =&amp;gt;  &quot;Empty List&quot;
}
&lt;/code&gt;&lt;/pre&gt;
Let us invoke the function to match a specific case&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;scala&amp;gt; matchList(List(1,2,3))
res49: String = Matched List(1, 2, 3)

scala&amp;gt; matchList(List(1))
res50: String = Only one element in the list 1

scala&amp;gt; matchList(List(2,3,4))
res51: String = A list with head 2 and tail List(3, 4)

scala&amp;gt; matchList(List(3,4,5,6,7,8,9))
res52: String = A list with  3, 4 and tail List(5, 6, 7, 8, 9)

scala&amp;gt; matchList(List(4,5,6,7,8,9))
res53: String = A list with head 4 and tail List(5, 6, 7, 8, 9)

scala&amp;gt; matchList(List())
res54: String = Empty List

scala&amp;gt; matchList(null)
res55: String = Empty List&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;&amp;nbsp;&lt;/code&gt;&lt;/pre&gt;
&lt;b&gt;Matching sequences, ranges and decomposing them:&lt;/b&gt;&lt;br /&gt;
Following example illustrates matching sequences, ranges and decompose 
them.&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;def matchSequence[E](x: Seq[E]) = x match {
    case x +: Nil =&amp;gt; s&quot;Only element is head: $x&quot;
    case _ :+ x =&amp;gt; s&quot;Last element $x&quot;
    case nil =&amp;gt; &quot;Empty Sequence&quot;
}
&lt;/code&gt;&lt;/pre&gt;
Let us invoke the above function with ranges and sequences&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;scala&amp;gt; matchSequence( 1 to 3)
res56: String = Last element 3

scala&amp;gt; matchSequence( 1 to 3 take 1)
res57: String = Only element is head: 1

scala&amp;gt; matchSequence( 1 to 3 take 0)
res58: String = Empty Sequence

scala&amp;gt; matchSequence(Seq(1,2,3,4))
res64: String = Last element 4

scala&amp;gt; matchSequence(Seq(1))
res65: String = Only element is head: 1&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;&amp;nbsp;&lt;/code&gt;&lt;/pre&gt;
&lt;b&gt;Pattern matching in catching exceptions&lt;/b&gt;&lt;br /&gt;
The following example demonistrates catching various exception using pattern 
matching.&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;def stringToPositiveIntOnly(s: String) = try {
    val i = s.toInt
    println(s&quot;Input value $i&quot;)
    assert(i &amp;gt; 0)
} catch {
    case e: NumberFormatException =&amp;gt; &quot;Not a number&quot; 
    case e:java.lang.AssertionError =&amp;gt; &quot;Assertion failed&quot;
}
&lt;/code&gt;&lt;/pre&gt;
Let us invoke the above function with various values&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;scala&amp;gt; stringToPositiveIntOnly(null)
res84: Any = Not a number

scala&amp;gt; stringToPositiveIntOnly(&quot;-1&quot;)
Input value -1
res85: Any = Assertion failed

scala&amp;gt; stringToPositiveIntOnly(&quot;1&quot;)
Input value 1
res86: Any = ()&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;&amp;nbsp;&lt;/code&gt;&lt;/pre&gt;
&lt;b&gt;Defining multiple vals with patterns:&lt;/b&gt;&lt;br /&gt;
The following examples illustrate pattern matching and decompose them to 
multiple vals&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;scala&amp;gt; val (a, b) = (1, (&quot;Ramesh&quot;, &quot;NY&quot;))
a: Int = 1
b: (String, String) = (Ramesh,NY)
&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Pattern matching in generators:&lt;/b&gt;&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;for ( keyAndValue &lt;- -=&quot;-&quot; vector=&quot;vector&quot;&gt; &quot;a&quot;, 2 -&amp;gt; &quot;b&quot;) ) 
    yield keyAndValue._2

for ( (k, v) &lt;- -=&quot;-&quot; 1=&quot;1&quot; vector=&quot;vector&quot;&gt; &quot;a&quot;,  2 -&amp;gt; &quot;b&quot;) )  
    yield v
&lt;!-----&gt;&lt;!-----&gt;&lt;/-&gt;&lt;/-&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Decomposing values based on Regular Expressions:&lt;/b&gt;&lt;br /&gt;
Regular expression can be used to match certain values and decompse the input 
to extract interesting parts. Notice that the decomposition will be based on the 
groups matched by regular expression&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;// Regular Expressions with only one groups
val Yes = &quot;(y|Y)es&quot;.r

// Checking regex works
Yes.pattern.matcher(&quot;yes&quot;).matches
Yes.unapplySeq(&quot;yes&quot;) will result: Option[List[String]] = Some(List(y))

def catchYes(s:String) = s match {
    case Yes(x) =&amp;gt; s
    case _ =&amp;gt; None
}

catchYes(&quot;yes&quot;)
catchYes(&quot;Yes&quot;)


// Regular Expressions with multiple groups
val Yes = &quot;(y|Y)(es)&quot;.r

// Checking regex works
Yes.pattern.matcher(&quot;yes&quot;).matches
Yes.unapplySeq(&quot;yes&quot;) will result: Option[List[String]] = Some(List(y, es))

def catchYes(s:String) = s match {
    // As regex has two groups we should have enough parameter to extract
    case Yes(x,y) =&amp;gt; s
    case _ =&amp;gt; None
}

catchYes(&quot;yes&quot;)
catchYes(&quot;Yes&quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://ascendant76.blogspot.com/feeds/5457341657832302459/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6999217373541258402/5457341657832302459' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/5457341657832302459'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/5457341657832302459'/><link rel='alternate' type='text/html' href='http://ascendant76.blogspot.com/2012/10/scala-pattern-matching-decomposition.html' title='Scala Pattern Matching &amp; Decomposition'/><author><name>Ramesh</name><uri>http://www.blogger.com/profile/07716715053929751798</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6999217373541258402.post-3499656372229607124</id><published>2012-08-05T12:31:00.000-05:00</published><updated>2012-08-05T12:31:53.017-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Scala"/><title type='text'>Scala - Concurrency -  I</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div class=&quot;c0 c5&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;c3 c0&quot;&gt;
&lt;span class=&quot;c1&quot;&gt;&lt;a class=&quot;c2&quot; href=&quot;https://docs.google.com/document/pub?id=1UgZy6XiiRS9vMxwzH7IyCtyYCyyxCpilCZ91cQb8NWk#h.vm4966c89jjy&quot;&gt;Creating a Future using the global ExecutionContextExecutor&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c3 c0&quot;&gt;
&lt;span class=&quot;c1&quot;&gt;&lt;a class=&quot;c2&quot; href=&quot;https://docs.google.com/document/pub?id=1UgZy6XiiRS9vMxwzH7IyCtyYCyyxCpilCZ91cQb8NWk#h.uvosm9z01ih4&quot;&gt;Creating a Future using a custom ExecutionContextExecutor&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c0 c3&quot;&gt;
&lt;span class=&quot;c1&quot;&gt;&lt;a class=&quot;c2&quot; href=&quot;https://docs.google.com/document/pub?id=1UgZy6XiiRS9vMxwzH7IyCtyYCyyxCpilCZ91cQb8NWk#h.uwsruonpnvyx&quot;&gt;Successful scenario&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c3 c0&quot;&gt;
&lt;span class=&quot;c1&quot;&gt;&lt;a class=&quot;c2&quot; href=&quot;https://docs.google.com/document/pub?id=1UgZy6XiiRS9vMxwzH7IyCtyYCyyxCpilCZ91cQb8NWk#h.yu4mnh6bk0sn&quot;&gt;Failure Scenario&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c3 c0&quot;&gt;
&lt;span class=&quot;c1&quot;&gt;&lt;a class=&quot;c2&quot; href=&quot;https://docs.google.com/document/pub?id=1UgZy6XiiRS9vMxwzH7IyCtyYCyyxCpilCZ91cQb8NWk#h.q9vokzlcgefw&quot;&gt;On Completing the computation&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;c0 c5&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
There
 are very few examples on Scala Concurrency as most of the people using 
Scala Actors or Akka Actors as they provide more features that cover 
various use cases. &lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
The&lt;a class=&quot;c2&quot; href=&quot;http://docs.scala-lang.org/sips/pending/futures-promises.html&quot;&gt;&amp;nbsp;&lt;/a&gt;&lt;span class=&quot;c1&quot;&gt;&lt;a class=&quot;c2&quot; href=&quot;http://docs.scala-lang.org/sips/pending/futures-promises.html&quot;&gt;SIP-14&lt;/a&gt;&lt;/span&gt;&amp;nbsp;provides
 most of the information related to Scala Concurrency. &amp;nbsp;In this blog 
post, I&#39;ll cover few examples. I will blog on the concurrency as I 
discover more Scala Concurrency features.&lt;/div&gt;
&lt;div class=&quot;c0 c5&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
Scala
 provides a global execution context to manage thread pools across an 
application. If needed we can provide own thread pools(Executors).&lt;/div&gt;
&lt;h4 class=&quot;c0&quot;&gt;
&lt;a href=&quot;http://draft.blogger.com/blogger.g?blogID=6999217373541258402&quot; name=&quot;h.vm4966c89jjy&quot;&gt;&lt;/a&gt;&lt;span class=&quot;c6&quot;&gt;Creating a Future using the global ExecutionContextExecutor&lt;/span&gt;&lt;/h4&gt;
&lt;div class=&quot;c0&quot;&gt;
import scala.concurrent.{future, Future}&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
import scala.concurrent.ExecutionContext.global&lt;/div&gt;
&lt;div class=&quot;c0 c5&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
The following future will raise an exception or, will compute the sum.&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
val f: Future[Int] = future {&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
&amp;nbsp; if ( System.currentTimeMillis % 2 == 0 ) {&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;throw new Exception(&quot;Exception&quot;)&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
&amp;nbsp; (1 to 100000000).sum&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;c0 c5&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
In this case, the required context is implicitly available in the current scope as we imported &lt;span class=&quot;c8&quot;&gt;&quot;scala.concurrent.ExecutionContext.global&quot;&lt;/span&gt;&lt;/div&gt;
&lt;h4 class=&quot;c0&quot;&gt;
&lt;a href=&quot;http://draft.blogger.com/blogger.g?blogID=6999217373541258402&quot; name=&quot;h.uvosm9z01ih4&quot;&gt;&lt;/a&gt;&lt;span class=&quot;c6&quot;&gt;Creating a Future using a custom ExecutionContextExecutor&lt;/span&gt;&lt;/h4&gt;
&lt;div class=&quot;c0&quot;&gt;
import scala.concurrent.{future, Future}&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
import scala.concurrent.ExecutionContext&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
import java.util.concurrent.Executors&lt;/div&gt;
&lt;div class=&quot;c0 c5&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
ExecutorContext
 provides various factory methods to create execution contexts. The 
following will use a scheduled thread pool to create the context and 
makes it implicitly available in the current scope of the execution.&lt;/div&gt;
&lt;div class=&quot;c0 c5&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
&lt;span class=&quot;c9&quot;&gt;implicit&lt;/span&gt;&amp;nbsp;val ctx = ExecutionContext.fromExecutor(Executors.newScheduledThreadPool(10))&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
The following future will raise an exception or, will compute the sum.&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
val f: Future[Int] = future { &lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
&amp;nbsp; if ( System.currentTimeMillis % 2 == 0 ) {&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;throw new Exception(&quot;Exception&quot;)&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
&amp;nbsp; }&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
&amp;nbsp; (1 to 100000000).sum&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
}&lt;/div&gt;
&lt;h4 class=&quot;c0&quot;&gt;
&lt;a href=&quot;http://draft.blogger.com/blogger.g?blogID=6999217373541258402&quot; name=&quot;h.uwsruonpnvyx&quot;&gt;&lt;/a&gt;&lt;span class=&quot;c6&quot;&gt;Successful scenario&lt;/span&gt;&lt;/h4&gt;
&lt;div class=&quot;c0&quot;&gt;
If you are interested only about a success use scenario use &lt;span class=&quot;c4&quot;&gt;onSuccess &lt;/span&gt;callback as shown below:&lt;/div&gt;
&lt;div class=&quot;c0 c5&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
f onSuccess {&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;case a:Int =&amp;gt; a;&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
}&lt;/div&gt;
&lt;h4 class=&quot;c0&quot;&gt;
&lt;a href=&quot;http://draft.blogger.com/blogger.g?blogID=6999217373541258402&quot; name=&quot;h.yu4mnh6bk0sn&quot;&gt;&lt;/a&gt;&lt;span class=&quot;c6&quot;&gt;Failure Scenario&lt;/span&gt;&lt;/h4&gt;
&lt;div class=&quot;c0&quot;&gt;
If you are interested only about a failure use scenario use &lt;span class=&quot;c4&quot;&gt;onFailure &lt;/span&gt;callback as shown below:&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
f onFailure {&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;case e: Exception =&amp;gt; e.printStackTrace&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;case _ =&amp;gt; _&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
As
 a failure can happen for various reasons, it is better to provide 
default case statement. If not a MatchError will be thrown by &lt;span class=&quot;c4&quot;&gt;onFailure &lt;/span&gt;callback.&lt;/div&gt;
&lt;h4 class=&quot;c0&quot;&gt;
&lt;a href=&quot;http://draft.blogger.com/blogger.g?blogID=6999217373541258402&quot; name=&quot;h.q9vokzlcgefw&quot;&gt;&lt;/a&gt;&lt;span class=&quot;c6&quot;&gt;On Completing the computation&lt;/span&gt;&lt;/h4&gt;
&lt;div class=&quot;c0&quot;&gt;
Certain
 times, one may be interested to work with the result and error too. 
&amp;nbsp;The Either[L,R] acts as a holder to the result of the computation or 
the error raised when doing the computation. In this case &lt;span class=&quot;c4&quot;&gt;onComplete &lt;/span&gt;callback will help.&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
f.onComplete{&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;case Right(x) =&amp;gt; &quot;Result: &quot; + x)&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;case Left(x) =&amp;gt; &quot;Error: &quot; + x&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
}&lt;/div&gt;
&lt;div class=&quot;c0 c5&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;c0&quot;&gt;
In a later post, I will cover map, flatMap, filter and foreach combinators. These combinators will enable for comprehensions.&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ascendant76.blogspot.com/feeds/3499656372229607124/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6999217373541258402/3499656372229607124' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/3499656372229607124'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/3499656372229607124'/><link rel='alternate' type='text/html' href='http://ascendant76.blogspot.com/2012/08/scala-concurrency-i.html' title='Scala - Concurrency -  I'/><author><name>Ramesh</name><uri>http://www.blogger.com/profile/07716715053929751798</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6999217373541258402.post-1709439827465635413</id><published>2012-08-05T02:46:00.001-05:00</published><updated>2012-08-06T01:17:20.625-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Scala"/><title type='text'>Scala - No checked exceptions</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
Can the interface implementers choose&amp;nbsp; the exceptions 
(checked exception vs runtime exception) they want to throw with out 
breaking the service contract?&lt;br /&gt;
&lt;br /&gt;
Well, the answer is a yes!. It is possible in Java by leveraging generics. &lt;br /&gt;
&lt;pre&gt; 
interface Processor&lt;i e=&quot;e&quot; exception=&quot;exception&quot; extends=&quot;extends&quot; r=&quot;r&quot;&gt; {
 public R process(I i) throws E;
} 

class StringProcessor implements Processor&lt;string runtimeexception=&quot;runtimeexception&quot; string=&quot;string&quot;&gt; {
 public String process(String str)  {
  return &quot;Hello! &quot; + str ;
 }
}

class StringProcessor2 implements Processor&lt;string exception=&quot;exception&quot; string=&quot;string&quot;&gt; {
 public String process(String str) throws Exception {
  return &quot;Hello! &quot; + str;
 }
}

public class Exceptions {
&lt;i&gt; public static void main(String ... args) throws Exception {
  Processor p = new StringProcessor();
  System.out.println(p.process(&quot;Ramesh&quot;));
  Processor p1 = new StringProcessor2();
  System.out.println(p1.process(&quot;Ramesh&quot;);
 }
}&amp;nbsp;&lt;/i&gt;&lt;/string&gt;&lt;/string&gt;&lt;/i&gt;&lt;/pre&gt;
&lt;br /&gt;
Converting the same interface contract to scala is impossible as scala didn&#39;t have &lt;br /&gt;
the concept of checked exception. When I posed this question, on Scala IRC &amp;lt;dobblego&amp;gt; &lt;br /&gt;
was kind enough to provide me the following trait. &lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;trait Processor[I, R, E &lt;: exception=&quot;exception&quot;&gt;&lt;/:&gt;&lt;/pre&gt;
&lt;pre&gt;   def process(i: I): Either[E, R]&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;}&lt;/pre&gt;
&lt;br /&gt;
This essentially has a benefit to carry the computation till you really need it. &lt;br /&gt;
And it is achieved using the Either construct. Either is place holder to carry error &lt;br /&gt;
on the left or to have the computation on the right.&lt;br /&gt;
&lt;br /&gt;
Recent versions of scala provide a scala.util.Try to work with Either directly using the companion object.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;import scala.util.Try
import scala.util.Try._
val result: Either[Exception, Int] = Try(Integer.parseInt(null)) &lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ascendant76.blogspot.com/feeds/1709439827465635413/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6999217373541258402/1709439827465635413' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/1709439827465635413'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/1709439827465635413'/><link rel='alternate' type='text/html' href='http://ascendant76.blogspot.com/2012/08/scala-no-checked-exceptions.html' title='Scala - No checked exceptions'/><author><name>Ramesh</name><uri>http://www.blogger.com/profile/07716715053929751798</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6999217373541258402.post-7149077055764537399</id><published>2012-08-04T08:08:00.005-05:00</published><updated>2012-08-05T12:31:53.019-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Scala"/><title type='text'>Scala - Reflection - I</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span id=&quot;internal-source-marker_0.6900628865471166&quot; style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;margin-bottom: 0pt; margin-left: 18pt; margin-top: 0pt;&quot;&gt;
&lt;a href=&quot;https://docs.google.com/document/d/1snz2lN5HfFr8enW9QRVjmQ-Ciz-_Cim_7GOZ_ETJuMU/edit#heading=h.mdx4byqak2bc&quot;&gt;&lt;span style=&quot;background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline;&quot;&gt;1. Getting the class information&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;&quot;&gt;
&lt;a href=&quot;https://docs.google.com/document/d/1snz2lN5HfFr8enW9QRVjmQ-Ciz-_Cim_7GOZ_ETJuMU/edit#heading=h.m4bn5xv7e91g&quot;&gt;&lt;span style=&quot;background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline;&quot;&gt;Value type class information&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;&quot;&gt;
&lt;a href=&quot;https://docs.google.com/document/d/1snz2lN5HfFr8enW9QRVjmQ-Ciz-_Cim_7GOZ_ETJuMU/edit#heading=h.pyhmr9vpky7l&quot;&gt;&lt;span style=&quot;background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline;&quot;&gt;Reference type class information&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;&quot;&gt;
&lt;a href=&quot;https://docs.google.com/document/d/1snz2lN5HfFr8enW9QRVjmQ-Ciz-_Cim_7GOZ_ETJuMU/edit#heading=h.p4fldii7segs&quot;&gt;&lt;span style=&quot;background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline;&quot;&gt;Verify an object&#39;s type&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;margin-bottom: 0pt; margin-left: 18pt; margin-top: 0pt;&quot;&gt;
&lt;a href=&quot;https://docs.google.com/document/d/1snz2lN5HfFr8enW9QRVjmQ-Ciz-_Cim_7GOZ_ETJuMU/edit#heading=h.xrkm1xi5hp5r&quot;&gt;&lt;span style=&quot;background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline;&quot;&gt;2. Create an instance of a class&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;margin-bottom: 0pt; margin-left: 18pt; margin-top: 0pt;&quot;&gt;
&lt;a href=&quot;https://docs.google.com/document/d/1snz2lN5HfFr8enW9QRVjmQ-Ciz-_Cim_7GOZ_ETJuMU/edit#heading=h.hxhpx8vdl4a6&quot;&gt;&lt;span style=&quot;background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline;&quot;&gt;3. Create an instance of the companion object&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;&quot;&gt;
&lt;a href=&quot;https://docs.google.com/document/d/1snz2lN5HfFr8enW9QRVjmQ-Ciz-_Cim_7GOZ_ETJuMU/edit#heading=h.fh659bsv651p&quot;&gt;&lt;span style=&quot;background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline;&quot;&gt;Instantiating 1 Arity case class&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;margin-bottom: 0pt; margin-left: 36pt; margin-top: 0pt;&quot;&gt;
&lt;a href=&quot;https://docs.google.com/document/d/1snz2lN5HfFr8enW9QRVjmQ-Ciz-_Cim_7GOZ_ETJuMU/edit#heading=h.e1qmaubo7l9o&quot;&gt;&lt;span style=&quot;background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline;&quot;&gt;Instantiating 2 Arity case class&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;margin-bottom: 0pt; margin-left: 18pt; margin-top: 0pt;&quot;&gt;
&lt;a href=&quot;https://docs.google.com/document/d/1snz2lN5HfFr8enW9QRVjmQ-Ciz-_Cim_7GOZ_ETJuMU/edit#heading=h.hvyphrlnibpk&quot;&gt;&lt;span style=&quot;background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline;&quot;&gt;3. Create Array of objects&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;background-color: transparent; color: #1155cc; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: underline; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;In this blog post, I&#39;ll cover the following using Scala 2.10.0 Milestone 6.&lt;/span&gt;&lt;br /&gt;
&lt;h3 dir=&quot;ltr&quot;&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 19px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;&quot;&gt;1. Getting the class information&lt;/span&gt;&lt;/h3&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;Java
 provides getClass method in java.lang.Object. In scala this 
functionality is available using getClass method declared as abstract in
 &quot;scala.Any&quot;. The implementation for value types is in &quot;&lt;/span&gt;&lt;span style=&quot;background-color: transparent; color: #e428bb; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;&quot;&gt;scala.AnyVal&lt;/span&gt;&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&quot; and for reference types it is in &quot;&lt;/span&gt;&lt;span style=&quot;background-color: transparent; color: #e428bb; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;&quot;&gt;scala.AnyRef&lt;/span&gt;&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&quot; class.&lt;/span&gt;&lt;br /&gt;
&lt;h4 dir=&quot;ltr&quot;&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 16px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;&quot;&gt;Value type class information&lt;/span&gt;&lt;/h4&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;1.getClass&lt;/span&gt;&lt;br /&gt;
&lt;h4 dir=&quot;ltr&quot;&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 16px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;&quot;&gt;Reference type class information&lt;/span&gt;&lt;/h4&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&quot;sb&quot;.getClass&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;Invocation of getClass on null (null.getClass) results in a NullPointerException.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;A reference to &quot;ab&quot; assigned to obj of AnyRef, the supertype of all reference types.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;val obj:AnyRef = &quot;ab&quot;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;A string &quot;ab&quot; assigned to str variable. Here the type is not mentioned and will inferred by the compiler.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;val str = &quot;ab&quot;&lt;/span&gt;&lt;br /&gt;
&lt;h4 dir=&quot;ltr&quot;&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 16px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;&quot;&gt;Verify an object&#39;s type&lt;/span&gt;&lt;/h4&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;Similar to instanceof operator in java, scala provided “&lt;/span&gt;&lt;span style=&quot;background-color: transparent; color: #e428bb; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;&quot;&gt;isInstanceOf” &lt;/span&gt;&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;method in &quot;&lt;/span&gt;&lt;span style=&quot;background-color: transparent; color: #e428bb; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;&quot;&gt;scala.Predef&lt;/span&gt;&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&quot;. We can invoke “&lt;/span&gt;&lt;span style=&quot;background-color: transparent; color: #e428bb; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;&quot;&gt;isInstanceOf&lt;/span&gt;&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;” on null too.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;obj.isInstanceOf[java.lang.String]&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;str.isInstanceOf[java.lang.String]&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;To cast an object to appropriate type, scala provided “&lt;/span&gt;&lt;span style=&quot;background-color: transparent; color: #e428bb; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;&quot;&gt;asInstanceOf&lt;/span&gt;&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;” method in &quot;&lt;/span&gt;&lt;span style=&quot;background-color: transparent; color: #e428bb; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;&quot;&gt;scala.Predef&lt;/span&gt;&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&quot;. We can invoke asInstance of on null too.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;val someStr = obj.asInstanceOf[java.lang.String]&lt;/span&gt;&lt;br /&gt;
&lt;h3 dir=&quot;ltr&quot;&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 19px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;&quot;&gt;2. Create an instance of a class&lt;/span&gt;&lt;/h3&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;Getting the class object is the first step. The behaviour is same as String.class in java defined in “&lt;/span&gt;&lt;span style=&quot;background-color: transparent; color: #e428bb; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;&quot;&gt;scala.Predef&lt;/span&gt;&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;”.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;val clazz: Class[String] = classOf[String]&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;or&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;val clazz = Class.forName(&quot;java.lang.String&quot;)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;We create the instance using available constructors. Following will create a String object with &quot;Ramesh&quot; as value.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;val user = clazz.getConstructor(clazz).newInstance(&quot;Ramesh&quot;).asInstanceOf[String]&lt;/span&gt;&lt;br /&gt;
&lt;h3 dir=&quot;ltr&quot;&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 19px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;&quot;&gt;3. Create an instance of the companion object&lt;/span&gt;&lt;/h3&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;Scala
 case class produces a class and a companion object. As 0 arity case 
classes are deprecated, let us start with greater than 0 arity case 
classes.&lt;/span&gt;&lt;br /&gt;
&lt;h4 dir=&quot;ltr&quot;&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 16px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;&quot;&gt;Instantiating 1 Arity case class&lt;/span&gt;&lt;/h4&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;Let us define 1 arity case class as below:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;case class C1(name:String)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;Get the reference of the companion object.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;val c1Comp = Class.forName(classOf[C1].getName+&quot;$&quot;).newInstance.asInstanceOf[Function1[String,C1]]&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;Invoke the apply method of the companion object with one parameter value &quot;Ramesh&quot;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;val obj = c1Comp(&quot;Ramesh&quot;)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;Let us see if the data is available or not.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;obj._1&lt;/span&gt;&lt;br /&gt;
&lt;h4 dir=&quot;ltr&quot;&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 16px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;&quot;&gt;Instantiating 2 Arity case class&lt;/span&gt;&lt;/h4&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;Let us define 2 artity case class as below:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;case class C2(name:String, location: String)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;Get the reference of the companion object. &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;val c2Comp = Class.forName(classOf[C2].getName+&quot;$&quot;).newInstance.asInstanceOf[Function2[String, String, C2]]&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;Invoke the apply method of the companion object with two parameter values &quot;Ramesh&quot; and &quot;India&quot;.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;val obj2 = c2Comp(&quot;Ramesh&quot;, &quot;India&quot;)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;obj2._1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;obj2._2&lt;/span&gt;&lt;br /&gt;
&lt;h3 dir=&quot;ltr&quot;&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 19px; font-style: normal; font-variant: normal; font-weight: bold; text-decoration: none; vertical-align: baseline;&quot;&gt;3. Create Array of objects&lt;/span&gt;&lt;/h3&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;The following creates a String Array of size 4.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;val s4: Array[String] = classTag[String] newArray(4)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: transparent; color: black; font-family: Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; text-decoration: none; vertical-align: baseline;&quot;&gt;&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ascendant76.blogspot.com/feeds/7149077055764537399/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6999217373541258402/7149077055764537399' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/7149077055764537399'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/7149077055764537399'/><link rel='alternate' type='text/html' href='http://ascendant76.blogspot.com/2012/08/scala-reflection-i.html' title='Scala - Reflection - I'/><author><name>Ramesh</name><uri>http://www.blogger.com/profile/07716715053929751798</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6999217373541258402.post-7311735264780653226</id><published>2012-03-29T23:04:00.000-05:00</published><updated>2012-08-05T12:31:53.021-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Scala"/><title type='text'>Scala - Exception Handling</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h2&gt;



Imperative way&lt;/h2&gt;
&lt;h3&gt;

The try catch finally construct&lt;/h3&gt;
scala&amp;gt; val result: Int = try { &quot;s&quot;.toInt } catch { case e: Exception =&amp;gt; 
0 } finally { println(&quot;converted&quot;) }&lt;br /&gt;
converted&lt;br /&gt;
result: Int = 0&lt;br /&gt;
&lt;h2&gt;

Functional way&lt;/h2&gt;
&lt;h3&gt;

Using Either&lt;/h3&gt;
Gives more controll to the caller. If result is Left, then it is error. If it 
is Right, then the value&lt;br /&gt;
.&lt;br /&gt;
scala&amp;gt; val computedValue : Either[Exception, Int] = try { Right { 
&quot;s&quot;.toInt } } catch { case e: Exception =&amp;gt; Left(e) }&lt;br /&gt;
computedValue: 
Either[Exception,Int] = Left(java.lang.NumberFormatException: For input string: 
&quot;s&quot;)&lt;br /&gt;
&lt;br /&gt;
Caller can use pattern matching to work with the result Either[E,R]&lt;br /&gt;
&lt;br /&gt;
scala&amp;gt; val result = computedValue match { &lt;br /&gt;
case Left(left) =&amp;gt; 
println(left); 0&lt;br /&gt;
case Right(right) =&amp;gt; 
right&lt;br /&gt;
}&lt;br /&gt;
java.lang.NumberFormatException: For input string: &quot;s&quot;&lt;br /&gt;
result: 
Int = 0&lt;br /&gt;
&lt;br /&gt;
Caller can verify if an error is the result is an error &lt;br /&gt;
&lt;br /&gt;
scala&amp;gt; if (computedValue.isLeft) {&lt;br /&gt;
computedValue.left.map { e =&amp;gt; 
e.printStackTrace }&lt;br /&gt;
}&lt;br /&gt;
java.lang.NumberFormatException: For input string: &quot;s&quot;&lt;br /&gt;
at 
java.lang.NumberFormatException.forInputString(Unknown Source)&lt;br /&gt;
at 
java.lang.Integer.parseInt(Unknown Source)&lt;br /&gt;
at 
java.lang.Integer.parseInt(Unknown Source)&lt;br /&gt;
at 
scala.collection.immutable.StringLike$class.toInt(StringLike.scala:231)&lt;br /&gt;
at 
scala.collection.immutable.StringOps.toInt(StringOps.scala:31)&lt;br /&gt;
at 
$line122.$read$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$.liftedTree1$1(:22)&lt;br /&gt;
at 
$line122.$read$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$.(:22)&lt;br /&gt;
at 
$line122.$read$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$$iw$.()&lt;br /&gt;
at 
$line122.$eval$.(:11)&lt;br /&gt;
at 
$line122.$eval$.()&lt;br /&gt;
at $line122.$eval.$print()&lt;br /&gt;
at 
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)&lt;br /&gt;
at 
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)&lt;br /&gt;
at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)&lt;br /&gt;
at 
java.lang.reflect.Method.invoke(Unknown Source)&lt;br /&gt;
at 
scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704)&lt;br /&gt;
at 
scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:914)&lt;br /&gt;
at 
scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:546)&lt;br /&gt;
at 
scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:577)&lt;br /&gt;
at 
scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:543)&lt;br /&gt;
at 
scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:694)&lt;br /&gt;
at 
scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:745)&lt;br /&gt;
at 
scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:651)&lt;br /&gt;
at 
scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:542)&lt;br /&gt;
at 
scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:550)&lt;br /&gt;
at 
scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:822)&lt;br /&gt;
at 
scala.tools.nsc.interpreter.ILoop.main(ILoop.scala:851)&lt;br /&gt;
at 
xsbt.ConsoleInterface.run(ConsoleInterface.scala:57)&lt;br /&gt;
at 
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)&lt;br /&gt;
at 
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)&lt;br /&gt;
at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)&lt;br /&gt;
at 
java.lang.reflect.Method.invoke(Unknown Source)&lt;br /&gt;
at 
sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:57)&lt;br /&gt;
at 
sbt.compiler.AnalyzingCompiler.console(AnalyzingCompiler.scala:48)&lt;br /&gt;
at 
sbt.Console.console0$1(Console.scala:23)&lt;br /&gt;
at 
sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply$mcV$sp(Console.scala:24)&lt;br /&gt;
at 
sbt.TrapExit$.executeMain$1(TrapExit.scala:33)&lt;br /&gt;
at 
sbt.TrapExit$$anon$1.run(TrapExit.scala:42)&lt;br /&gt;
res70: Any = Left(())&lt;br /&gt;
&lt;h2&gt;

Scala built-in contructs to work with exceptions.&lt;/h2&gt;
You need to import the following:&lt;br /&gt;
&lt;br /&gt;
scala&amp;gt; import scala.util.control.Exception._&lt;br /&gt;
&lt;h2&gt;

Ignoring exception&lt;/h2&gt;
Exceptions should not be ignored. May be useful to handle usecases like Close 
a stream silently &lt;br /&gt;
&lt;br /&gt;
scala&amp;gt; val result = ignoring(classOf[java.lang.NumberFormatException]) { 
&quot;x&quot; toInt }&lt;br /&gt;
result: AnyVal = ()&lt;br /&gt;
&lt;br /&gt;
scala&amp;gt; val result = ignoring(classOf[java.lang.NumberFormatException]) { 
&quot;1&quot; toInt }&lt;br /&gt;
result: AnyVal = 1&lt;br /&gt;
&lt;h3&gt;

Ignore the exception and return a default value on error using 
failAsValue&lt;/h3&gt;
Compensates with a value on NumberFormatException&lt;br /&gt;
&lt;br /&gt;
scala&amp;gt; val result = failAsValue(classOf[java.lang.NumberFormatException])( 
0 ) { &quot;&quot;.toInt }&lt;br /&gt;
result: Int = 0&lt;br /&gt;
&lt;h2&gt;

Catch all exceptions using allCatch&lt;/h2&gt;
scala&amp;gt; val result = allCatch opt { &quot;x&quot;.toInt }&lt;br /&gt;
result: Option[Int] = 
None&lt;br /&gt;
&lt;br /&gt;
scala&amp;gt; val result = allCatch opt { &quot;3&quot;.toInt }&lt;br /&gt;
result: Option[Int] = 
Some(3)&lt;br /&gt;
&lt;h2&gt;

Giving more controll to the caller using either&lt;/h2&gt;
Using control structures to handle exceptions instead of 
try...catch...finally constructs&lt;br /&gt;
&lt;br /&gt;
scala&amp;gt; val computedValue = 
catching(classOf[java.lang.NumberFormatException]) either { &quot;s&quot;.toInt 
}&lt;br /&gt;
computedValue: Either[Throwable,Int] = 
Left(java.lang.NumberFormatException: For input string: &quot;s&quot;)&lt;br /&gt;
&lt;h3&gt;

Using either converting the result to Option&lt;/h3&gt;
Some or, None on exception. The identity is defined in scala.Predef&lt;br /&gt;
&lt;br /&gt;
scala&amp;gt; val result = catching(classOf[java.lang.NumberFormatException]) 
either { &quot;1&quot;.toInt } fold( &lt;i&gt;=&amp;gt; None, identity(&lt;/i&gt;) )&lt;br /&gt;
result: Any = 
1&lt;br /&gt;
Preferred when you interested in the value and dont need to know the 
exception if any. The following only capture NumberFormatException exception 
resulting None&lt;br /&gt;
&lt;br /&gt;
scala&amp;gt; val result = catching(classOf[java.lang.NumberFormatException]) opt 
{ &quot;x&quot;.toInt }&lt;br /&gt;
result: Option[Int] = None&lt;br /&gt;
&lt;h3&gt;

For comprehension&lt;/h3&gt;
If exception, skip the execution using for comprehension&lt;br /&gt;
&lt;br /&gt;
scala&amp;gt; val 
result = for ( a &amp;lt;- catching(classOf[java.lang.NumberFormatException]).opt { 
&quot;&quot;.toInt }) {&lt;br /&gt;
if ( a % 2 == 0 ) println(&quot;Entered Even number&quot;)&lt;br /&gt;
else 
println(&quot;Entered Odd number&quot;)&lt;br /&gt;
}&lt;br /&gt;
scala&amp;gt; val result = for ( a &amp;lt;- 
catching(classOf[java.lang.NumberFormatException]).opt { &quot;2&quot;.toInt }) {&lt;br /&gt;
if ( 
a % 2 == 0 ) println(&quot;Entered Even number&quot;)&lt;br /&gt;
else println(&quot;Entered Odd 
number&quot;)&lt;br /&gt;
}&lt;br /&gt;
&lt;h3&gt;

andFinally&lt;/h3&gt;
Similar to try .. finally construct &lt;br /&gt;
&lt;br /&gt;
scala&amp;gt; val result = allCatch.andFinally( println(&quot;cleanup&quot;) ) opt { 
s.toInt }&lt;br /&gt;
The try catch finally&lt;br /&gt;
&lt;br /&gt;
scala&amp;gt; import java.io._&lt;br /&gt;
&lt;br /&gt;
scala&amp;gt; for( a &amp;lt;- allCatch.opt { new 
BufferedWriter( new FileWriter(s) ) } ) {&lt;br /&gt;
allCatch.andFinally { a.close() } 
{&lt;br /&gt;
a.write(&quot;ramesh&quot;)&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;h3&gt;

withApply and apply&lt;/h3&gt;
&lt;br /&gt;
&lt;b&gt;Exception or value&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
scala&amp;gt;val result = allCatch withApply { e =&amp;gt; e } apply { &quot;&quot;.toInt 
}&lt;br /&gt;
result: Any = java.lang.NumberFormatException: For input string: &quot;&quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Option or None&lt;/b&gt;&lt;br /&gt;
scala&amp;gt; val result = allCatch withApply { e =&amp;gt; None } apply { 
Some(&quot;&quot;.toInt) }&lt;br /&gt;
result: Option[Int] = None&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Either&lt;/b&gt;&lt;br /&gt;
scala&amp;gt; val result = allCatch withApply { e =&amp;gt; Left(e) } apply { 
Right(&quot;&quot;.toInt) }&lt;br /&gt;
result: Product with Serializable with 
Either[Throwable,Int] = Left(java.lang.NumberFormatException: For input string: 
&quot;&lt;br /&gt;
&quot;)&lt;br /&gt;
&lt;h3&gt;

Multiple Exceptions&lt;/h3&gt;
scala&amp;gt; val result = catching(classOf[java.lang.ArithmeticException], 
classOf[java.lang.NumberFormatException]) withApply&lt;br /&gt;
{ e =&amp;gt; e } apply { 10 
/ &quot;d&quot;.toInt }&lt;br /&gt;
result: Any = java.lang.NumberFormatException: For input 
string: &quot;d&quot;&lt;br /&gt;
&lt;br /&gt;
scala&amp;gt; val result = catching(classOf[java.lang.ArithmeticException], 
classOf[java.lang.NumberFormatException]) withApply&lt;br /&gt;
{ e =&amp;gt; e } apply { 10 
/ &quot;0&quot;.toInt }&lt;br /&gt;
result: Any = java.lang.ArithmeticException: / by zero&lt;br /&gt;
&lt;h3&gt;

handling&lt;/h3&gt;
Compute block and handle exception using function&lt;br /&gt;
&lt;br /&gt;
scala&amp;gt; val result = handling(classOf[ArithmeticException], 
classOf[NumberFormatException]) by { e =&amp;gt; e } apply { 10 / &quot;d&lt;br /&gt;
&quot;.toInt 
}&lt;br /&gt;
result: Any = java.lang.NumberFormatException: For input string: &quot;d&quot;&lt;br /&gt;
&lt;h3&gt;

&lt;/h3&gt;
&lt;h3&gt;

failing&lt;/h3&gt;
Value of None similar to Option but return value instead of Some&lt;br /&gt;
&lt;br /&gt;
scala&amp;gt; val result = failing(classOf[ArithmeticException], 
classOf[NumberFormatException]) apply { 10 / &quot;d&quot;.toInt }&lt;br /&gt;
result: Any = 
None&lt;br /&gt;
&lt;br /&gt;
scala&amp;gt; val result = failing(classOf[ArithmeticException], 
classOf[NumberFormatException]) apply { 10 / &quot;2&quot;.toInt }&lt;br /&gt;
result: Any = 5&lt;br /&gt;
&lt;h3&gt;

ultimately&lt;/h3&gt;
Similar to try finally construct. Helpful to handle usecases like closing 
resources etc &lt;br /&gt;
&lt;br /&gt;
scala&amp;gt; ultimately { println(&quot;Finally&quot;) } { &quot;&quot; toInt 
}&lt;br /&gt;
Finally&lt;br /&gt;
java.lang.NumberFormatException: For input string: &quot;&quot;&lt;br /&gt;
at 
java.lang.NumberFormatException.forInputString(Unknown Source)&lt;br /&gt;
at 
java.lang.Integer.parseInt(Unknown Source)&lt;br /&gt;
at 
java.lang.Integer.parseInt(Unknown Source)&lt;br /&gt;
at 
scala.collection.immutable.StringLike$class.toInt(StringLike.scala:231)&lt;br /&gt;
at 
scala.collection.immutable.StringOps.toInt(StringOps.scala:31)&lt;br /&gt;
at 
$anonfun$2.apply$mcI$sp(:20)&lt;br /&gt;
at $anonfun$2.apply(:20)&lt;br /&gt;
at 
$anonfun$2.apply(:20)&lt;br /&gt;
at 
scala.util.control.Exception$Catch.apply(Exception.scala:88)&lt;br /&gt;
at 
.(:20)&lt;br /&gt;
at .()&lt;br /&gt;
at 
.(:11)&lt;br /&gt;
at .()&lt;br /&gt;
at $print()&lt;br /&gt;
at 
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)&lt;br /&gt;
at 
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)&lt;br /&gt;
at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)&lt;br /&gt;
at 
java.lang.reflect.Method.invoke(Unknown Source)&lt;br /&gt;
at 
scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:704)&lt;br /&gt;
at 
scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:914)&lt;br /&gt;
at 
scala.tools.nsc.interpreter.IMain.loadAndRunReq$1(IMain.scala:546)&lt;br /&gt;
at 
scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:577)&lt;br /&gt;
at 
scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:543)&lt;br /&gt;
at 
scala.tools.nsc.interpreter.ILoop.reallyInterpret$1(ILoop.scala:694)&lt;br /&gt;
at 
scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:745)&lt;br /&gt;
at 
scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:651)&lt;br /&gt;
at 
scala.tools.nsc.interpreter.ILoop.processLine$1(ILoop.scala:542)&lt;br /&gt;
at 
scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:550)&lt;br /&gt;
at 
scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:822)&lt;br /&gt;
at 
scala.tools.nsc.interpreter.ILoop.main(ILoop.scala:851)&lt;br /&gt;
at 
xsbt.ConsoleInterface.run(ConsoleInterface.scala:57)&lt;br /&gt;
at 
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)&lt;br /&gt;
at 
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)&lt;br /&gt;
at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)&lt;br /&gt;
at 
java.lang.reflect.Method.invoke(Unknown Source)&lt;br /&gt;
at 
sbt.compiler.AnalyzingCompiler.call(AnalyzingCompiler.scala:57)&lt;br /&gt;
at 
sbt.compiler.AnalyzingCompiler.console(AnalyzingCompiler.scala:48)&lt;br /&gt;
at 
sbt.Console.console0$1(Console.scala:23)&lt;br /&gt;
at 
sbt.Console$$anonfun$apply$2$$anonfun$apply$1.apply$mcV$sp(Console.scala:24)&lt;br /&gt;
at 
sbt.TrapExit$.executeMain$1(TrapExit.scala:33)&lt;br /&gt;
at 
sbt.TrapExit$$anon$1.run(TrapExit.scala:42)&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ascendant76.blogspot.com/feeds/7311735264780653226/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6999217373541258402/7311735264780653226' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/7311735264780653226'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/7311735264780653226'/><link rel='alternate' type='text/html' href='http://ascendant76.blogspot.com/2012/03/exception-handling-scala-imperative-way.html' title='Scala - Exception Handling'/><author><name>Ramesh</name><uri>http://www.blogger.com/profile/07716715053929751798</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6999217373541258402.post-1075833197424396195</id><published>2012-03-22T06:16:00.000-05:00</published><updated>2012-08-05T02:47:19.130-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Scala"/><title type='text'>Scala - REPL</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div class=&quot;renderbox&quot;&gt;
&lt;h2&gt;

First step&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Set SCALA_HOME and go ahead...&lt;/li&gt;
&lt;li&gt;Open command prompt&lt;br /&gt;


scala -help&lt;br /&gt;


scala -nc -e &#39;println(util.Properties.versionString)&#39;&lt;br /&gt;


&lt;b&gt;Note&lt;/b&gt;: nc: no compilation daemon: do not use the fsc offline compiler&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;

Scala Read-Evaluate-Print Loop(REPL)&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Start REPL by typing scala at the command prompt...&lt;br /&gt;


&lt;b&gt;Note&lt;/b&gt;: Notice version of scala and jvm&lt;/li&gt;
&lt;li&gt;What is available&lt;br /&gt;


&lt;ul&gt;
&lt;li&gt;Help&lt;br /&gt;


scala&amp;gt;:help&lt;/li&gt;
&lt;li&gt;Power mode&lt;br /&gt;


scala&amp;gt;:power&lt;/li&gt;
&lt;li&gt;Help in power mode&lt;br /&gt;


scala&amp;gt;:help&lt;/li&gt;
&lt;li&gt;You can start scala in power mode by passing &lt;b&gt;-Dscala.repl.power&lt;/b&gt; in the startup script&lt;br /&gt;


cat scala/2.9.1/bin/scala&lt;/li&gt;
&lt;li&gt;Quit :quit&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;

REPL in action&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Basic stuff&lt;br /&gt;


scala&amp;gt;1+2&lt;br /&gt;


scala&amp;gt;val name = &quot;ramesh&quot;&lt;br /&gt;


scala&amp;gt;pritnln(name)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;

Auto Completion&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;packages&lt;br /&gt;


scala&amp;gt;scala.&lt;br /&gt;


scala&amp;gt;scala.sys&lt;br /&gt;


scala&amp;gt;scala.sys.env&lt;/li&gt;
&lt;li&gt;both class methods and instance methods&lt;br /&gt;


scala&amp;gt;val name = &quot;ramesh&quot;&lt;br /&gt;


scala&amp;gt;name.to&lt;/li&gt;
&lt;li&gt;This is possible via reflection&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;

Introspection&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Type of method&lt;br /&gt;


scala&amp;gt;def add(a:Int, b:Int) = a+b&lt;br /&gt;


scala&amp;gt;:type add&lt;/li&gt;
&lt;li&gt;Type of variable&lt;br /&gt;


scala&amp;gt;val name = &quot;Ramesh&quot;&lt;br /&gt;


scala&amp;gt;:type name&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;

Companion objects&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;class and static class&lt;br /&gt;


scala&amp;gt;class Test(name: String)&lt;br /&gt;


defined class Test&lt;br /&gt;


scala&amp;gt;object Test { val t = new Test(&quot;Ramesh&quot;)}&lt;br /&gt;


defined module Test&lt;br /&gt;


warning: previously defined class Test is not a companion to object Test.&lt;br /&gt;


Companions must be defined together; you may wish to use :paste mode for this.&lt;br /&gt;


scala&amp;gt;:paste&lt;br /&gt;


&lt;pre&gt;class Test(name: String)
object Test { val t = new Test(&quot;Ramesh&quot;)}
&lt;/pre&gt;
ctrl+d&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;

Disassemble the code&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;javap&lt;br /&gt;


Welcome to Scala version 2.9.1.final (Java HotSpot(TM) Client VM, Java 1.7.0_02)
.
Type in expressions to have them evaluated.
Type :help for more information.&lt;br /&gt;


scala&amp;gt; case class A(n:Int)
defined class A&lt;br /&gt;


scala&amp;gt; :javap A
:javap unavailable on this platform.&lt;br /&gt;


scala&amp;gt; :javap A$
:javap unavailable on this platform.&lt;/li&gt;
&lt;li&gt;Some issue with JDK 1.7 see https://issues.scala-lang.org/browse/SI-4936&lt;/li&gt;
&lt;li&gt;Try other JDK&lt;br /&gt;


Welcome to Scala version 2.9.1.final (Java HotSpot(TM) Server VM, Java 1.6.0_27).
Type in expressions to have them evaluated.
Type :help for more information.&lt;br /&gt;


scala&amp;gt; :javap  A&lt;br /&gt;


&lt;pre&gt;Compiled from &quot;&quot;
public class A extends java.lang.Object implements scala.ScalaObject,scala.Product,scala.Serializable{
    public scala.collection.Iterator productIterator();
    public scala.collection.Iterator productElements();
    public int n();
    public A copy(int);
    public int copy$default$1();
    public int hashCode();
    public java.lang.String toString();
    public boolean equals(java.lang.Object);
    public java.lang.String productPrefix();
    public int productArity();
    public java.lang.Object productElement(int);
    public boolean canEqual(java.lang.Object);
    public A(int);
}
&lt;/pre&gt;
scala&amp;gt; :javap  A$&lt;br /&gt;


&lt;pre&gt;Compiled from &quot;&quot;
public final class A$ extends scala.runtime.AbstractFunction1 implements scala.ScalaObject,scala.Serializable{
    public static final A$ MODULE$;
    public static {};
    public final java.lang.String toString();
    public scala.Option unapply(A);
    public A apply(int);
    public java.lang.Object readResolve();
    public java.lang.Object apply(java.lang.Object);
    public A$();
}
&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;Other option to bring up console using &quot;&quot;-Xprint:typer&quot;&quot; jvm option&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;

Working with files&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Create a file and loading file&lt;br /&gt;


scala&amp;gt;:sh notepad fib.scala&lt;/li&gt;
&lt;li&gt;Save file with contents&lt;br /&gt;


&lt;pre&gt;def fib(n:Int): Long =  {
    n match {
        case 0 =&amp;gt; 0
        case 1 =&amp;gt; 1
        case n =&amp;gt; fib(n-1) + fib(n-2)
    }
}
&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;Load the file&lt;br /&gt;


scala&amp;gt;:load fib.scala&lt;/li&gt;
&lt;li&gt;execute&lt;br /&gt;


scala&amp;gt;fib(8)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;

Loading file continued....&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;What happens if n is negative. For example -1?&lt;br /&gt;


&lt;pre&gt;&lt;code&gt;&lt;pre&gt;def fib(n:Int): Long =  {
    require(n &amp;gt;=0 , &quot;Input cannot be negative&quot;)
    n match {
        case 0 =&amp;gt; 0
        case 1 =&amp;gt; 1
        case n =&amp;gt; fib(n-1) + fib(n-2)
    }
}
&lt;/pre&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;Reload the file. Quick way to do so is&lt;br /&gt;


scala&amp;gt;:replay&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;

Simple Utility&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Wrappers&lt;br /&gt;


scala&amp;gt;:wrap&lt;/li&gt;
&lt;li&gt;You can use how much time it took to compute something...&lt;br /&gt;


scala&amp;gt;:power&lt;br /&gt;


scala&amp;gt;:paste&lt;br /&gt;


&lt;pre&gt;def timed[T](body: =&amp;gt; T): T = {
    val start = System.nanoTime
    try {
        body
    } finally {
        println(&quot; [3&quot; + scala.util.Random.nextInt(8) + &quot;m &quot; + (System.nanoTime * start) + &quot; nanos elapsed.&quot; + Console.RESET)
    }
}
&lt;/pre&gt;
scala&amp;gt;:wrap timed&lt;br /&gt;


scala&amp;gt;1 to 10000 sum&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;

Scala phases...&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;scalac -Xshow-phases&lt;/li&gt;
&lt;li&gt;Back to REPL&lt;br /&gt;


scala&amp;gt;val x = ?[List[_]]&lt;br /&gt;


scala&amp;gt;x.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;

Imports and implicits&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Import&lt;br /&gt;


scala&amp;gt;:imports&lt;br /&gt;


scala&amp;gt;object MyImplicits { implicit def string2Bigdecimal(s:String)=BigDecimal(s) }&lt;br /&gt;


scala&amp;gt;import MyImplicits._&lt;br /&gt;


scala&amp;gt;:implicits&lt;br /&gt;


scala&amp;gt;:imports&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;

Working with jar files&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Loading a jar file&lt;br /&gt;


scala&amp;gt;:cp c:/repository/lib/commons-lang-2.6.0.jar&lt;br /&gt;


scala&amp;gt;import org.apache.commons.lang.StringUtils&lt;br /&gt;


scala&amp;gt;StringUtils.isBlank(&quot; &quot;)&lt;br /&gt;


Note: module load wont work to load the libraries before starting scala repl&lt;br /&gt;


module load ossjava/commons-lang/2.4.0&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;

Shell commands&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Linux box&lt;br /&gt;


scala&amp;gt;:sh ls -alth&lt;br /&gt;


res7 foreach println&lt;br /&gt;


scala&amp;gt;scala.sys.process.Process(List(&quot;ls&quot;)) !&lt;br /&gt;


&lt;/li&gt;
&lt;li&gt;Windows box.&lt;br /&gt;


scala&amp;gt; :sh cmd /c dir C:\&lt;br /&gt;


res7: scala.tools.nsc.interpreter.ProcessResult = &lt;code&gt;cmd /c dir C:\&lt;/code&gt; (22 lines, ex
it 0)
scala&amp;gt; res7.show&lt;br /&gt;


&lt;/li&gt;
&lt;li&gt;The behaviour of console evolved with new releases of scala&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;

History&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;History&lt;br /&gt;


scala&amp;gt;:history&lt;/li&gt;
&lt;li&gt;Search the history&lt;br /&gt;


scala&amp;gt;:h? wrap&lt;/li&gt;
&lt;li&gt;Then get the history&lt;br /&gt;


scala&amp;gt;:history 100&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;

Scala Interpreter&lt;/h2&gt;
&lt;pre&gt;&lt;code&gt;scala&amp;gt;:power

scala&amp;gt;val x=11

scala&amp;gt;val y = 12

scala&amp;gt;intp.interpret(&quot;x+y&quot;)
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;

Other tools&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;sbt * Simple Build Tool&lt;/li&gt;
&lt;li&gt;If you have sbt setup, to bring up the console:&lt;br /&gt;


sbt console&lt;/li&gt;
&lt;li&gt;Change the JDK&lt;br /&gt;


:cp C:\Java\jdk1.7.0_02\lib   ools.jar&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;

Thank you&lt;/h2&gt;
&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ascendant76.blogspot.com/feeds/1075833197424396195/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6999217373541258402/1075833197424396195' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/1075833197424396195'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/1075833197424396195'/><link rel='alternate' type='text/html' href='http://ascendant76.blogspot.com/2012/03/learning-scala-using-repl.html' title='Scala - REPL'/><author><name>Ramesh</name><uri>http://www.blogger.com/profile/07716715053929751798</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6999217373541258402.post-2276029140825147986</id><published>2010-03-25T15:39:00.001-05:00</published><updated>2012-03-24T09:43:51.778-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Tomcat"/><title type='text'>Production Ready Tomcat</title><content type='html'>&lt;h2&gt;Production Ready Tomcat - Optimizations, Security aspects&lt;/h2&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;To disable directory listing &amp;nbsp;for the entire tomcat server go to $CATALINA_HOME/conf/web.xml and set the listings parameter to false.&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;&lt;blockquote class=&quot;webkit-indent-blockquote&quot; style=&quot;border:none;margin:0 0 0 40px&quot;&gt;&lt;div&gt;&amp;lt;servlet&amp;gt;&lt;/div&gt;&lt;div&gt;&amp;lt;servlet-name&amp;gt;default&amp;lt;/servlet-name&amp;gt;&lt;/div&gt;&lt;div&gt;&amp;lt;servlet-class&amp;gt;org.apache.catalina.servlets.DefaultServlet&amp;lt;/servlet-class&amp;gt;&lt;/div&gt;&lt;div&gt;&amp;lt;init-param&amp;gt;&lt;/div&gt;&lt;div&gt;&amp;lt;param-name&amp;gt;debug&amp;lt;/param-name&amp;gt;&lt;/div&gt;&lt;div&gt;&amp;lt;param-value&amp;gt;0&amp;lt;/param-value&amp;gt;&lt;/div&gt;&lt;div&gt;&amp;lt;/init-param&amp;gt;&lt;/div&gt;&lt;div&gt;&amp;lt;init-param&amp;gt;&lt;/div&gt;&lt;div&gt;&amp;lt;param-name&amp;gt;listings&amp;lt;/param-name&amp;gt;&lt;/div&gt;&lt;div&gt;&amp;lt;param-value&amp;gt;false&amp;lt;/param-value&amp;gt;&lt;/div&gt;&lt;div&gt;&amp;lt;/init-param&amp;gt;&lt;/div&gt;&lt;div&gt;&amp;lt;load-on-startup&amp;gt;1&amp;lt;/load-on-startup&amp;gt;&lt;/div&gt;&lt;div&gt;&amp;lt;/servlet&amp;gt;&lt;/div&gt;&lt;/blockquote&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;Switch off application reloading by setting &amp;quot;reloadable&amp;quot; to &amp;quot;false&amp;quot; gives a small run-time performance increase.&amp;nbsp;&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;Context reloadable=&amp;quot;false&amp;quot; privileged=&amp;quot;true&amp;quot; /&amp;gt;&lt;/div&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;Prevent Locking of Exploded Wars by setting antiResourceLocking=&amp;quot;true&amp;quot;&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;Context antiResourceLocking=&amp;quot;true&amp;quot; privileged=&amp;quot;true&amp;quot; /&amp;gt;&lt;/div&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;By default tomcat uses the MemoryRealm for authentication. Alter the default username and password in tomcat-users.xml with more &amp;nbsp;appropriate ones.&lt;/li&gt;&lt;/ul&gt;&lt;blockquote class=&quot;webkit-indent-blockquote&quot; style=&quot;border:none;margin:0 0 0 40px&quot;&gt;&lt;div&gt;&amp;lt;?xml version=&amp;#39;1.0&amp;#39; encoding=&amp;#39;utf-8&amp;#39;?&amp;gt;&lt;/div&gt;&lt;div&gt;&amp;lt;tomcat-users&amp;gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;lt;role rolename=&amp;quot;tomcat&amp;quot;/&amp;gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;lt;role rolename=&amp;quot;manager&amp;quot;/&amp;gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;lt;role rolename=&amp;quot;admin&amp;quot;/&amp;gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;lt;user username=&amp;quot;tomcat&amp;quot; password=&amp;quot;tomcat&amp;quot; roles=&amp;quot;tomcat&amp;quot;/&amp;gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&amp;lt;user username=&amp;quot;admin&amp;quot; password=&amp;quot;XXXX&amp;quot; roles=&amp;quot;admin,manager&amp;quot;/&amp;gt;&lt;/div&gt;&lt;div&gt;&amp;lt;/tomcat-users&amp;gt;&lt;/div&gt;&lt;/blockquote&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;Make the tomcat-users.xml as readonly by adding readonly=&amp;quot;true&amp;quot; to the &amp;lt;Resource&amp;gt; tag in server.xml&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;Resource name=&amp;quot;UserDatabase&amp;quot; readonly=&amp;quot;true&amp;quot; /&amp;gt;&lt;/div&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;Move to http://svn.apache.org/repos/asf/tomcat/trunk/modules/jdbc-pool/ instead of any other connection pool.&lt;/li&gt;&lt;/ul&gt;&lt;blockquote class=&quot;webkit-indent-blockquote&quot; style=&quot;border:none;margin:0 0 0 40px&quot;&gt;&lt;div&gt;&amp;lt;!--&lt;/div&gt;&lt;div&gt;Download the required jars from http://people.apache.org/~fhanik/jdbc-pool/v1.0.8.5/&lt;/div&gt;&lt;br&gt;&lt;div&gt;mvn install:install-file -DgroupId=org.apache.tomcat \\&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;-DartifactId=jdbc-pool -Dversion=1.0.8.5 -Dpackaging=jar -Dfile=tomcat-jdbc.jar&lt;/div&gt;&lt;div&gt;--&amp;gt;&lt;/div&gt;&lt;/blockquote&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;dependency&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;groupId&amp;gt;org.apache.tomcat&amp;lt;/groupId&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;artifactId&amp;gt;juli&amp;lt;/artifactId&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;version&amp;gt;6.0.24&amp;lt;/version&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;/dependency&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;dependency&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;groupId&amp;gt;org.apache.tomcat&amp;lt;/groupId&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;artifactId&amp;gt;jdbc-pool&amp;lt;/artifactId&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;version&amp;gt;1.0.8.5&amp;lt;/version&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;/dependency&amp;gt;&lt;br&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;Swallow System.out and System.err and direct them to a log file&amp;nbsp;&lt;/li&gt;&lt;/ul&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;Context swallowOutput=&amp;quot;true&amp;quot; /&amp;gt;&amp;nbsp;&lt;/div&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;Rotation of logs&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;b&gt;logging.properties file:&lt;/b&gt;&lt;br&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;.handlers = 1catalina.org.apache.juli.FileHandler&lt;/div&gt;&lt;br&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;1catalina.org.apache.juli.FileHandler.level = FINE&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;1catalina.org.apache.juli.FileHandler.prefix = catalina.&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;1catalina.org.apache.juli.FileHandler.pattern = ${catalina.base}/logs/catalina.%g.log&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;1catalina.org.apache.juli.FileHandler.limit = 20000000 # No more than 5x20mb files&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;1catalina.org.apache.juli.FileHandler.count = 5&lt;/div&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;Tuning Sun JVM settings for Tomcat.Set JAVA_OPTS variable to catalina.sh.&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Try to adopt these settings :&lt;/div&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;-server&amp;nbsp;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;Better for server side applications&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;-Djava.awt.headless=true&lt;/li&gt;&lt;ul&gt;&lt;li&gt;Headless mode is a system configuration in which the display device, keyboard, or mouse is lacking. Sounds unexpected, but actually you can perform different operations in this mode, even with graphic data.&amp;nbsp;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tomcat/dumps&lt;/li&gt;&lt;ul&gt;&lt;li&gt;Prepare thread dumps when a OutOfMemoryError occours. This way you can analyze the thread dumps to figure out the issue.&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;b&gt;Memory related tuning parameters:&lt;/b&gt;&lt;br&gt;&lt;ul&gt;&lt;ul&gt;&lt;li&gt;-Xms&amp;nbsp;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;initial heap size is 1/64th of the machine&amp;#39;s physical memory&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;-Xmx&amp;nbsp;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;maximum heap size is 1/4th of the machine&amp;#39;s physical memory (up to 1GB max).&amp;nbsp;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;-XX:PermSize&amp;nbsp;&lt;/li&gt;&lt;li&gt;-XX:MaxPermSize&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;br&gt;&lt;div&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Garbage Collection Tuning Parameters:&lt;/b&gt;&lt;br&gt;&lt;/div&gt;&lt;br&gt;&lt;ul&gt;&lt;ul&gt;&lt;li&gt;-XX:+UseParallelGC parallel (throughput) garbage collector, or&lt;/li&gt;&lt;li&gt;-XX:+UseConcMarkSweepGC concurrent (low pause time) garbage collector (known as CMS)&lt;/li&gt;&lt;li&gt;-XX:+UseSerialGC serial garbage collector (for smaller applications and systems)&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&amp;nbsp;&amp;nbsp;See http://java.sun.com/performance/reference/whitepapers/tuning.html&lt;/div&gt;&lt;br&gt;</content><link rel='replies' type='application/atom+xml' href='http://ascendant76.blogspot.com/feeds/2276029140825147986/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6999217373541258402/2276029140825147986' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/2276029140825147986'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/2276029140825147986'/><link rel='alternate' type='text/html' href='http://ascendant76.blogspot.com/2010/03/production-ready-tomcat.html' title='Production Ready Tomcat'/><author><name>Ramesh</name><uri>http://www.blogger.com/profile/07716715053929751798</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6999217373541258402.post-4417703113565074034</id><published>2010-03-25T12:17:00.001-05:00</published><updated>2012-03-24T09:43:51.781-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Tomcat"/><title type='text'>Apache Tomcat - Apache Log4j</title><content type='html'>&lt;h2&gt;
Configuring Apache Tomcat Logging using Apache Log4j&lt;/h2&gt;
I will be covering how to use log4j instead of the default logging provided by tomcat based on the reference&amp;nbsp;&lt;a href=&quot;http://tomcat.apache.org/tomcat-6.0-doc/logging.html&quot;&gt;http://tomcat.apache.org/tomcat-6.0-doc/logging.html&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;Objective&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Using log4j to customize the logging in apache-tomcat&lt;/li&gt;
&lt;li&gt;Separate apache-tomcat logs from the application logs&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;Using log4j to customize the logging in apache-tomcat&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &#39;times new roman&#39;;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Create a file called log4j.properties with the following content and save it into $CATALINA_HOME/lib.&lt;/span&gt;&lt;/span&gt;&lt;div align=&quot;left&quot;&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;4&quot; class=&quot;zeroBorder&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td bgcolor=&quot;#ffffff&quot; height=&quot;1&quot;&gt;&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;lt;!DOCTYPE log4j:configuration SYSTEM &quot;log4j.dtd&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;lt;log4j:configuration xmlns:log4j=&quot;http://jakarta.apache.org/log4j/&quot; debug=&quot;false&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;appender name=&quot;CONSOLE&quot; &amp;nbsp;class=&quot;org.apache.log4j.ConsoleAppender&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;layout class=&quot;org.apache.log4j.PatternLayout&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;param name=&quot;ConversionPattern&quot; value=&quot;%d{MMM dd, yyyy HH:mm:ss a} %c %M%n%p: %m%n&quot; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;/layout&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;/appender&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;appender name=&quot;ROLL&quot; class=&quot;org.apache.log4j.RollingFileAppender&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;param name=&quot;file&quot; value=&quot;../logs/catalina.out&quot; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;param name=&quot;append&quot; value=&quot;true&quot; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;param name=&quot;encoding&quot; value=&quot;UTF-8&quot; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;layout class=&quot;org.apache.log4j.PatternLayout&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;param name=&quot;ConversionPattern&quot; value=&quot;%d{MMM dd, yyyy HH:mm:ss a} %c %M%n%p: %m%n&quot; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;/layout&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;/appender&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;logger name=&quot;org.apache.catalina.core.AprLifecycleListener&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;level value=&quot;INFO&quot; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;/logger&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;logger name=&quot;org.apache.coyote.http11.Http11Protocol&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;level value=&quot;INFO&quot; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;/logger&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;logger name=&quot;org.apache.catalina.core.ContainerBase&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;level value=&quot;OFF&quot; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;/logger&amp;gt; &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;root&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;level value=&quot;INFO&quot; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;appender-ref ref=&quot;ROLL&quot; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-family: &#39;courier new&#39;;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;!-- For production comment this appender--&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;appender-ref ref=&quot;CONSOLE&quot; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;/root&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;lt;/log4j:configuration&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &#39;times new roman&#39;;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;a href=&quot;http://logging.apache.org/log4j&quot;&gt;Download Log4J&lt;/a&gt;&amp;nbsp;(v1.2 or later) and place the log4j jar in $CATALINA_HOME/lib.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &#39;times new roman&#39;;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Build or download the additional logging components. See the&amp;nbsp;&lt;a href=&quot;http://tomcat.apache.org/tomcat-6.0-doc/extras.html&quot;&gt;extras components&lt;/a&gt;&amp;nbsp;documentation for details.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &#39;times new roman&#39;;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Replace&amp;nbsp;&lt;span style=&quot;font-family: Courier New;&quot;&gt;$CATALINA_HOME/bin/tomcat-juli.jar&lt;/span&gt;&amp;nbsp;with&amp;nbsp;&lt;span style=&quot;font-family: Courier New;&quot;&gt;output/extras/tomcat-juli.jar&lt;/span&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &#39;times new roman&#39;;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Place&amp;nbsp;&lt;span style=&quot;font-family: Courier New;&quot;&gt;output/extras/tomcat-juli-adapters.jar&lt;/span&gt;&amp;nbsp;in $CATALINA_HOME/lib.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &#39;times new roman&#39;;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Delete&amp;nbsp;&lt;span style=&quot;font-family: Courier New;&quot;&gt;$CATALINA_BASE/conf/logging.properties&lt;/span&gt;&amp;nbsp;to prevent java.util.logging generating zero length log files.&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &#39;times new roman&#39;;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Start Tomcat&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
Separate apache-tomcat logs from the application logs&lt;/h3&gt;
&lt;span style=&quot;font-family: &#39;times new roman&#39;;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;The start with following log4j.properties can be used in your web application to divert all the logs to application specific file (app_demo.out).&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div align=&quot;left&quot;&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;4&quot; class=&quot;zeroBorder&quot;&gt;&lt;tbody&gt;
&lt;tr style=&quot;text-align: left;&quot;&gt;&lt;td bgcolor=&quot;#ffffff&quot; height=&quot;1&quot;&gt;&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;lt;!DOCTYPE log4j:configuration SYSTEM &quot;log4j.dtd&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;lt;log4j:configuration xmlns:log4j=&quot;http://jakarta.apache.org/log4j/&quot; debug=&quot;false&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;appender name=&quot;CONSOLE&quot; &amp;nbsp;class=&quot;org.apache.log4j.ConsoleAppender&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;layout class=&quot;org.apache.log4j.PatternLayout&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;param name=&quot;ConversionPattern&quot; value=&quot;%d{MMM dd, yyyy HH:mm:ss a} %c %M%n%p: %m%n&quot; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;/layout&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;/appender&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;appender name=&quot;ROLL&quot; class=&quot;org.apache.log4j.RollingFileAppender&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;param name=&quot;file&quot; value=&quot;../logs/app_demo.out&quot; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;param name=&quot;append&quot; value=&quot;true&quot; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;param name=&quot;encoding&quot; value=&quot;UTF-8&quot; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;layout class=&quot;org.apache.log4j.PatternLayout&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;param name=&quot;ConversionPattern&quot; value=&quot;%d{MMM dd, yyyy HH:mm:ss a} %c %M%n%p: %m%n&quot; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;/layout&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;/appender&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;logger name=&quot;com.blogger.ascendant76&quot;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;level value=&quot;INFO&quot; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;/logger&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;root&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;level value=&quot;INFO&quot; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;appender-ref ref=&quot;ROLL&quot; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;!-- For production comment this appender--&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;appender-ref ref=&quot;CONSOLE&quot; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;/root&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Courier New;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;lt;/log4j:configuration&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;left&quot;&gt;
@TODO&lt;/div&gt;
&lt;div align=&quot;left&quot;&gt;
Changing the log levels for various loggers when the app is running&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://ascendant76.blogspot.com/feeds/4417703113565074034/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6999217373541258402/4417703113565074034' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/4417703113565074034'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/4417703113565074034'/><link rel='alternate' type='text/html' href='http://ascendant76.blogspot.com/2010/03/apache-tomcat-apache-log4j.html' title='Apache Tomcat - Apache Log4j'/><author><name>Ramesh</name><uri>http://www.blogger.com/profile/07716715053929751798</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6999217373541258402.post-9005311902450128755</id><published>2010-02-25T22:35:00.001-05:00</published><updated>2010-03-14T20:31:15.251-05:00</updated><title type='text'>Recurrsion using parallel processing</title><content type='html'>&lt;h1&gt;Recurrsion using parallel processing&lt;/h1&gt; As I was google around I found the &lt;b&gt;&lt;a href=&quot;https://netfiles.uiuc.edu/dig/RefactoringInfo/tools/Concurrencer/&quot; id=&quot;gjlc&quot; title=&quot;https://netfiles.uiuc.edu/dig/RefactoringInfo/tools/Concurrencer/&quot;&gt;Concurrencer&lt;/a&gt;&lt;/b&gt;: a tool for retrofitting parallelism into sequential Java code. One of the cool thing of this tool is, it can convert the recurrsive program into a parallel program. I learned about &lt;b&gt;&lt;a href=&quot;http://gee.cs.oswego.edu/dl/papers/fj.pdf&quot; id=&quot;mm60&quot; title=&quot;http://gee.cs.oswego.edu/dl/papers/fj.pdf&quot;&gt;ForkJoin framework&lt;/a&gt;&lt;/b&gt; from &lt;b&gt;&lt;a href=&quot;http://cs.oswego.edu/mailman/listinfo/concurrency-interest&quot; id=&quot;jt90&quot; title=&quot;http://cs.oswego.edu/mailman/listinfo/concurrency-interest&quot;&gt;concurrency-interest&lt;/a&gt;&lt;/b&gt; mailinglist. So I quickly wrote a program to see how the performance will be boosted.&lt;br&gt;&lt;br&gt; The ForkJoin framework will be part of JDK7.0. But the following instructions could help to work with ForkJoin framework on J2SE 6.0. First create a maven2 project and under the root create a lib folder. Download the ForkJoin framework jar &lt;b&gt;&amp;quot;&lt;a href=&quot;http://gee.cs.oswego.edu/dl/jsr166/dist/jsr166.jar&quot; id=&quot;mruw&quot; title=&quot;http://gee.cs.oswego.edu/dl/jsr166/dist/jsr166.jar&quot;&gt;jsr166.jar&lt;/a&gt;&amp;quot;&lt;/b&gt; from &lt;b&gt;&lt;a href=&quot;http://gee.cs.oswego.edu/dl/concurrency-interest/index.html&quot; id=&quot;ith9&quot; title=&quot;http://gee.cs.oswego.edu/dl/concurrency-interest/index.html&quot;&gt;Doug Lea&lt;/a&gt;&lt;/b&gt; website. Install jsr166.jar as a maven dependency using the following command:&lt;br&gt;&lt;br style=&quot;color:#cc0000&quot;&gt;&lt;font color=&quot;#cc0000&quot;&gt;mvn install:install-file -DgroupId=jsr166 -DartifactId=jsr166 -Dversion=166 -Dpackaging=jar -Dfile=jsr166.jar&lt;/font&gt;&lt;br&gt;&lt;br&gt; The following program shows a recurrsive program to generate the fibonacci number and the also using parallel processing. ForkJoinPool is a ExecutorPool that orchestrates the threads.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;table bgcolor=&quot;#c0c0c0&quot; cellpadding=&quot;5&quot; cellspacing=&quot;0&quot; class=&quot;zeroBorder&quot; cols=&quot;1&quot; width=&quot;100%&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;center&gt;&lt;font color=&quot;#000000&quot; face=&quot;Arial, Helvetica&quot;&gt; Fibonacci.java&lt;/font&gt;&lt;/center&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;pre&gt;&lt;a id=&quot;l1&quot; name=&quot;l1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;1    &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;package &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;com.blogspot.ascendant.forkjoin; &lt;br /&gt;&lt;a id=&quot;l2&quot; name=&quot;l2&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;2    &lt;/span&gt; &lt;br /&gt;&lt;a id=&quot;l3&quot; name=&quot;l3&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;3    &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;import &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;java.util.concurrent.ForkJoinPool; &lt;br /&gt;&lt;a id=&quot;l4&quot; name=&quot;l4&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;4    &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;import &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;java.util.concurrent.RecursiveAction; &lt;br /&gt;&lt;a id=&quot;l5&quot; name=&quot;l5&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;5    &lt;/span&gt; &lt;br /&gt;&lt;a id=&quot;l6&quot; name=&quot;l6&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;6    &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;public class &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Fibonacci { &lt;br /&gt;&lt;a id=&quot;l7&quot; name=&quot;l7&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;7    &lt;/span&gt; &lt;br /&gt;&lt;a id=&quot;l8&quot; name=&quot;l8&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;8    &lt;/span&gt;    &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;public static void &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;main(String... args) { &lt;br /&gt;&lt;a id=&quot;l9&quot; name=&quot;l9&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;9    &lt;/span&gt; &lt;br /&gt;&lt;a id=&quot;l10&quot; name=&quot;l10&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;10   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;final int &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;input = &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;; &lt;br /&gt;&lt;a id=&quot;l11&quot; name=&quot;l11&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;11   &lt;/span&gt;        System.out.printf(&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;&amp;quot;Input: %d&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;\n\n&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;, input); &lt;br /&gt;&lt;a id=&quot;l12&quot; name=&quot;l12&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;12   &lt;/span&gt; &lt;br /&gt;&lt;a id=&quot;l13&quot; name=&quot;l13&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;13   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;long &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;nowSet1 = System.nanoTime(); &lt;br /&gt;&lt;a id=&quot;l14&quot; name=&quot;l14&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;14   &lt;/span&gt; &lt;br /&gt;&lt;a id=&quot;l15&quot; name=&quot;l15&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;15   &lt;/span&gt;        System.out.printf(&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;&amp;quot;Fibonacci using recursion: %d&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;, fibRecurr(input)); &lt;br /&gt;&lt;a id=&quot;l16&quot; name=&quot;l16&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;16   &lt;/span&gt; &lt;br /&gt;&lt;a id=&quot;l17&quot; name=&quot;l17&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;17   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;long &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;laterSet1 = System.nanoTime(); &lt;br /&gt;&lt;a id=&quot;l18&quot; name=&quot;l18&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;18   &lt;/span&gt;        System.out.printf(&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;&amp;quot;Computing time for recursion: %d nanoseconds&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;\n\n&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;, (laterSet1 - nowSet1)); &lt;br /&gt;&lt;a id=&quot;l19&quot; name=&quot;l19&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;19   &lt;/span&gt; &lt;br /&gt;&lt;a id=&quot;l20&quot; name=&quot;l20&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;20   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;long &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;nowSet2 = System.nanoTime(); &lt;br /&gt;&lt;a id=&quot;l21&quot; name=&quot;l21&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;21   &lt;/span&gt; &lt;br /&gt;&lt;a id=&quot;l22&quot; name=&quot;l22&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;22   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;final int &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;numberOfProcessors = Runtime.getRuntime().availableProcessors(); &lt;br /&gt;&lt;a id=&quot;l23&quot; name=&quot;l23&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;23   &lt;/span&gt;        System.out.printf(&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;&amp;quot;Number of processors %d&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;, numberOfProcessors); &lt;br /&gt;&lt;a id=&quot;l24&quot; name=&quot;l24&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;24   &lt;/span&gt; &lt;br /&gt;&lt;a id=&quot;l25&quot; name=&quot;l25&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;25   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;final &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;FibonacciParallel fp = &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;new &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;FibonacciParallel(input); &lt;br /&gt;&lt;a id=&quot;l26&quot; name=&quot;l26&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;26   &lt;/span&gt; &lt;br /&gt;&lt;a id=&quot;l27&quot; name=&quot;l27&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;27   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;final &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;ForkJoinPool es = &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;new &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;ForkJoinPool(numberOfProcessors); &lt;br /&gt;&lt;a id=&quot;l28&quot; name=&quot;l28&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;28   &lt;/span&gt;        es.invoke(fp); &lt;br /&gt;&lt;a id=&quot;l29&quot; name=&quot;l29&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;29   &lt;/span&gt;        es.shutdown(); &lt;br /&gt;&lt;a id=&quot;l30&quot; name=&quot;l30&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;30   &lt;/span&gt; &lt;br /&gt;&lt;a id=&quot;l31&quot; name=&quot;l31&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;31   &lt;/span&gt;        System.out.printf(&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;&amp;quot;Fibonacci using parallel processing: %d&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;, fp.result); &lt;br /&gt;&lt;a id=&quot;l32&quot; name=&quot;l32&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;32   &lt;/span&gt; &lt;br /&gt;&lt;a id=&quot;l33&quot; name=&quot;l33&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;33   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;long &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;laterSet2 = System.nanoTime(); &lt;br /&gt;&lt;a id=&quot;l34&quot; name=&quot;l34&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;34   &lt;/span&gt;        System.out.printf(&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;&amp;quot;Computing time for parallel processing: %d nanoseconds&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;, (laterSet2 - nowSet2)); &lt;br /&gt;&lt;a id=&quot;l35&quot; name=&quot;l35&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;35   &lt;/span&gt;    } &lt;br /&gt;&lt;a id=&quot;l36&quot; name=&quot;l36&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;36   &lt;/span&gt; &lt;br /&gt;&lt;a id=&quot;l37&quot; name=&quot;l37&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;37   &lt;/span&gt; &lt;br /&gt;&lt;a id=&quot;l38&quot; name=&quot;l38&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;38   &lt;/span&gt;    &lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;/** &lt;br /&gt;&lt;a id=&quot;l39&quot; name=&quot;l39&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;39   &lt;/span&gt;     * Recursive approach &lt;br /&gt;&lt;a id=&quot;l40&quot; name=&quot;l40&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;40   &lt;/span&gt;     */&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; &lt;br /&gt;&lt;a id=&quot;l41&quot; name=&quot;l41&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;41   &lt;/span&gt;    &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;private static long &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;fibRecurr(&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;final int &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;n) { &lt;br /&gt;&lt;a id=&quot;l42&quot; name=&quot;l42&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;42   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;if &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;(n &amp;lt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;) { &lt;br /&gt;&lt;a id=&quot;l43&quot; name=&quot;l43&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;43   &lt;/span&gt;            &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;return &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;n; &lt;br /&gt;&lt;a id=&quot;l44&quot; name=&quot;l44&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;44   &lt;/span&gt;        } &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;else &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;{ &lt;br /&gt;&lt;a id=&quot;l45&quot; name=&quot;l45&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;45   &lt;/span&gt;            &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;return &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;fibRecurr(n - &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;) + fibRecurr(n - &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;); &lt;br /&gt;&lt;a id=&quot;l46&quot; name=&quot;l46&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;46   &lt;/span&gt;        } &lt;br /&gt;&lt;a id=&quot;l47&quot; name=&quot;l47&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;47   &lt;/span&gt;    } &lt;br /&gt;&lt;a id=&quot;l48&quot; name=&quot;l48&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;48   &lt;/span&gt; &lt;br /&gt;&lt;a id=&quot;l49&quot; name=&quot;l49&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;49   &lt;/span&gt; &lt;br /&gt;&lt;a id=&quot;l50&quot; name=&quot;l50&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;50   &lt;/span&gt;    &lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;/** &lt;br /&gt;&lt;a id=&quot;l51&quot; name=&quot;l51&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;51   &lt;/span&gt;     * Recursive Task &lt;br /&gt;&lt;a id=&quot;l52&quot; name=&quot;l52&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;52   &lt;/span&gt;     */&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; &lt;br /&gt;&lt;a id=&quot;l53&quot; name=&quot;l53&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;53   &lt;/span&gt;    &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;public static class &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;FibonacciParallel &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;extends &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;RecursiveAction { &lt;br /&gt;&lt;a id=&quot;l54&quot; name=&quot;l54&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;54   &lt;/span&gt; &lt;br /&gt;&lt;a id=&quot;l55&quot; name=&quot;l55&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;55   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;private static final long &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;serialVersionUID = -&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;6810496708906992778L&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;; &lt;br /&gt;&lt;a id=&quot;l56&quot; name=&quot;l56&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;56   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;final int &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;numberToCompute; &lt;br /&gt;&lt;a id=&quot;l57&quot; name=&quot;l57&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;57   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;volatile long &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;result; &lt;br /&gt;&lt;a id=&quot;l58&quot; name=&quot;l58&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;58   &lt;/span&gt; &lt;br /&gt;&lt;a id=&quot;l59&quot; name=&quot;l59&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;59   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;public &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;FibonacciParallel(&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;int &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;numberToCompute) { &lt;br /&gt;&lt;a id=&quot;l60&quot; name=&quot;l60&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;60   &lt;/span&gt;            &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;.numberToCompute = numberToCompute; &lt;br /&gt;&lt;a id=&quot;l61&quot; name=&quot;l61&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;61   &lt;/span&gt;        } &lt;br /&gt;&lt;a id=&quot;l62&quot; name=&quot;l62&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;62   &lt;/span&gt; &lt;br /&gt;&lt;a id=&quot;l63&quot; name=&quot;l63&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;63   &lt;/span&gt;        @Override &lt;br /&gt;&lt;a id=&quot;l64&quot; name=&quot;l64&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;64   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;protected void &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;compute() { &lt;br /&gt;&lt;a id=&quot;l65&quot; name=&quot;l65&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;65   &lt;/span&gt;            &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;if &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;(numberToCompute &amp;lt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;) { &lt;br /&gt;&lt;a id=&quot;l66&quot; name=&quot;l66&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;66   &lt;/span&gt;                result = numberToCompute; &lt;br /&gt;&lt;a id=&quot;l67&quot; name=&quot;l67&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;67   &lt;/span&gt;            } &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;else &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;{ &lt;br /&gt;&lt;a id=&quot;l68&quot; name=&quot;l68&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;68   &lt;/span&gt;                FibonacciParallel future1 = &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;new &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;FibonacciParallel(numberToCompute - &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;); &lt;br /&gt;&lt;a id=&quot;l69&quot; name=&quot;l69&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;69   &lt;/span&gt;                FibonacciParallel future2 = &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;new &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;FibonacciParallel(numberToCompute - &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;); &lt;br /&gt;&lt;a id=&quot;l70&quot; name=&quot;l70&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;70   &lt;/span&gt; &lt;br /&gt;&lt;a id=&quot;l71&quot; name=&quot;l71&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;71   &lt;/span&gt;                invokeAll(future1, future2); &lt;br /&gt;&lt;a id=&quot;l72&quot; name=&quot;l72&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;72   &lt;/span&gt; &lt;br /&gt;&lt;a id=&quot;l73&quot; name=&quot;l73&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;73   &lt;/span&gt;                result = future1.result + future2.result; &lt;br /&gt;&lt;a id=&quot;l74&quot; name=&quot;l74&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;74   &lt;/span&gt;            } &lt;br /&gt;&lt;a id=&quot;l75&quot; name=&quot;l75&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;75   &lt;/span&gt;        } &lt;br /&gt;&lt;a id=&quot;l76&quot; name=&quot;l76&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;76   &lt;/span&gt;    } &lt;br /&gt;&lt;a id=&quot;l77&quot; name=&quot;l77&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;77   &lt;/span&gt;} &lt;br /&gt;&lt;a id=&quot;l78&quot; name=&quot;l78&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;78   &lt;/span&gt; &lt;br /&gt;&lt;a id=&quot;l79&quot; name=&quot;l79&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;79   &lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br&gt;As we are alternat version of &lt;b&gt;java.util.concurrent &lt;/b&gt;package we need to alter the bootclasspath using -Xbootclasspath/a:/home/ramesh/Workspace/ascendant/forkjoin/lib/jsr166.jar option.&lt;br&gt;&lt;br&gt;&lt;b&gt;java -Xbootclasspath/a:/home/ramesh/Workspace/ascendant/forkjoin/lib/jsr166.jar com.blogspot.ascendant.forkjoin.Fibonacci&lt;/b&gt;&lt;br&gt;&lt;br&gt;&lt;b&gt;ramesh@ramesh-laptop:~$ grep &amp;quot;model name&amp;quot; /proc/cpuinfo&lt;/b&gt;&lt;br&gt;model name&amp;nbsp;&amp;nbsp; &amp;nbsp;: Genuine Intel(R) CPU&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; T2500&amp;nbsp; @ 2.00GHz&lt;br&gt;model name&amp;nbsp;&amp;nbsp; &amp;nbsp;: Genuine Intel(R) CPU&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; T2500&amp;nbsp; @ 2.00GHz&lt;br&gt;&lt;br&gt;&lt;b&gt;Here is the output on my laptop:&lt;/b&gt;&lt;br&gt;Input: 40&lt;br&gt;&lt;br&gt;Fibonacci using recursion: 102334155&lt;br&gt;Computing time for recursion: 1919286038 nanoseconds&lt;br&gt;&lt;br&gt;Number of processors 2&lt;br&gt;Fibonacci using parallel processing: 102334155&lt;br&gt;Computing time for parallel processing: 33836924737 nanoseconds&lt;br&gt;&lt;br&gt;Process finished with exit code 0&lt;br&gt;&lt;br&gt;As you see the results using parallel computing is not attractive. I guess this is due to the overload of the thread infrastructure. As the computation becomes more complex, this parallel processing should result better results.&lt;br&gt;&lt;br&gt;Here is the completed project:&lt;br&gt;&lt;a href=&quot;http://www.box.net/shared/egrj42i8xf&quot; id=&quot;njlq&quot; title=&quot;http://www.box.net/shared/egrj42i8xf&quot;&gt;http://www.box.net/shared/egrj42i8xf&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;div&gt;Update:&amp;nbsp;&lt;/div&gt;&lt;div&gt;Other people also facing the same performance issue. See&amp;nbsp;&lt;a href=&quot;http://cs.oswego.edu/pipermail/concurrency-interest/2010-March/006913.html&quot; id=&quot;tgh5&quot; title=&quot;http://cs.oswego.edu/pipermail/concurrency-interest/2010-March/006913.html&quot;&gt;http://cs.oswego.edu/pipermail/concurrency-interest/2010-March/006913.html&lt;/a&gt;&lt;/div&gt;&lt;br&gt;</content><link rel='replies' type='application/atom+xml' href='http://ascendant76.blogspot.com/feeds/9005311902450128755/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6999217373541258402/9005311902450128755' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/9005311902450128755'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/9005311902450128755'/><link rel='alternate' type='text/html' href='http://ascendant76.blogspot.com/2010/02/recurrsion-using-parallel-processing.html' title='Recurrsion using parallel processing'/><author><name>Ramesh</name><uri>http://www.blogger.com/profile/07716715053929751798</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6999217373541258402.post-5874902143278712746</id><published>2010-02-12T18:30:00.001-05:00</published><updated>2010-02-12T18:34:10.787-05:00</updated><title type='text'>Upcoming JDBC-Pool -</title><content type='html'>&lt;h2&gt;Upcoming Tomcat JDBC-Pool an alternate to Jakarta DBCP Component&lt;/h2&gt;&lt;br&gt;The &lt;a href=&quot;http://commons.apache.org/dbcp/&quot; id=&quot;fpd9&quot; title=&quot;Commons DBCP&quot;&gt;Jakarta Commons DBCP&lt;/a&gt; internally uses &lt;a href=&quot;http://commons.apache.org/pool/&quot; id=&quot;c4j_&quot; title=&quot;Commons Pool&quot;&gt;Commons Pool&lt;/a&gt; along with other dependencies. It worked me pretty well all the time. As I recently encountered Tomcat JDBC-Pool at &lt;a href=&quot;http://www.grails.org/plugin/jdbc-pool&quot; id=&quot;v75l&quot; title=&quot;Grails.org&quot;&gt;Grails.org&lt;/a&gt;. I thought of exploring it to see, if it works as a standalone projects.&amp;nbsp; And certainly works with out tomcat and usage is very straight forward and also an inplace replacement to Commons DBCP with very minimum change i.e., use &lt;b&gt;org.apache.tomcat.jdbc.pool.DataSource&lt;/b&gt; instead of &lt;b&gt;org.apache.commons.dbcp.BasicDataSource&lt;/b&gt;.&lt;br&gt;&lt;br&gt;&lt;br&gt;You can download the jars from &lt;a href=&quot;http://people.apache.org/%7Efhanik/jdbc-pool/v1.0.8.5/&quot; target=&quot;_blank&quot;&gt;http://people.apache.org/~fhanik/jdbc-pool/v1.0.8.5/&lt;/a&gt; or build &lt;br&gt;it from the svn repository located at &lt;a href=&quot;http://svn.apache.org/repos/asf/tomcat/trunk/modules/jdbc-pool&quot; id=&quot;xldv&quot; title=&quot;http://svn.apache.org/repos/asf/tomcat/trunk/modules/jdbc-pool&quot;&gt;http://svn.apache.org/repos/asf/tomcat/trunk/modules/jdbc-pool&lt;/a&gt;. As it is not available in any public standard maven2 repository I manually updated local repository using:&lt;br&gt;&lt;br&gt;mvn install:install-file -DgroupId=org.apache.tomcat -DartifactId=jdbc-pool -Dversion=1.0.8.5 -Dpackaging=jar -Dfile=tomcat-jdbc.jar&lt;br&gt;&lt;br&gt;For apache maven lovers, below are the dependenies required:&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;dependency&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;groupId&amp;gt;org.apache.tomcat&amp;lt;/groupId&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;artifactId&amp;gt;jdbc-pool&amp;lt;/artifactId&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;version&amp;gt;1.0.8.5&amp;lt;/version&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/dependency&amp;gt;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;dependency&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;groupId&amp;gt;org.apache.tomcat&amp;lt;/groupId&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;artifactId&amp;gt;juli&amp;lt;/artifactId&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;version&amp;gt;6.0.20&amp;lt;/version&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/dependency&amp;gt;&lt;br&gt;&lt;br&gt;JDBC properties in app.properties file looks a below:&lt;br&gt;&lt;br&gt;# Oracle configuration&lt;br&gt;jdbc.driverClassName=oracle.jdbc.driver.OracleDriver&lt;br&gt;jdbc.url=jdbc:oracle:thin:@//&lt;a href=&quot;http://webdor01.sonyment.com:1521/RCKCPSD1&quot; target=&quot;_blank&quot;&gt;&amp;lt;&amp;lt;Server&amp;gt;&amp;gt;:&amp;lt;&amp;lt;PORT&amp;gt;/&amp;lt;&amp;lt;Service Name&amp;gt;&amp;gt;&lt;/a&gt;&lt;br&gt;jdbc.username=&amp;lt;&amp;lt;Database User&amp;gt;&amp;gt;&lt;br&gt; jdbc.password=&amp;lt;&amp;lt;Database Password&amp;gt;&amp;gt;&lt;br&gt;&lt;br&gt;Spring applicationContext.xml&lt;br&gt;&lt;br&gt;&amp;lt;context:property-placeholder location=&amp;quot;classpath:app.properties&amp;quot; /&amp;gt;&lt;br&gt;&lt;br&gt;&amp;lt;bean id=&amp;quot;datasource&amp;quot; class=&amp;quot;org.apache.tomcat.jdbc.pool.DataSource&amp;quot;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; destroy-method=&amp;quot;close&amp;quot;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p:driverClassName=&amp;quot;${jdbc.driverClassName}&amp;quot;&lt;br&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p:url=&amp;quot;${jdbc.url}&amp;quot;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p:username=&amp;quot;${jdbc.username}&amp;quot;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p:password=&amp;quot;${jdbc.password}&amp;quot;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p:initialSize=&amp;quot;5&amp;quot;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p:minIdle=&amp;quot;5&amp;quot;&lt;br&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p:maxIdle=&amp;quot;10&amp;quot;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p:maxActive=&amp;quot;20&amp;quot;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p:maxWait=&amp;quot;6000&amp;quot;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p:jmxEnabled=&amp;quot;true&amp;quot;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p:jdbcInterceptors=&amp;quot;org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer;org.apache.tomcat.jdbc.pool.interceptor.SlowQueryReportJmx(threshold=10000)&amp;quot;&lt;br&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p:removeAbandoned=&amp;quot;true&amp;quot;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p:removeAbandonedTimeout=&amp;quot;60&amp;quot;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p:logAbandoned=&amp;quot;true&amp;quot;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p:testOnBorrow=&amp;quot;true&amp;quot;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p:testOnReturn=&amp;quot;false&amp;quot;&lt;br&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p:testWhileIdle=&amp;quot;false&amp;quot;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p:useEquals=&amp;quot;true&amp;quot;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p:fairQueue=&amp;quot;true&amp;quot;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p:timeBetweenEvictionRunsMillis=&amp;quot;30000&amp;quot;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p:minEvictableIdleTimeMillis=&amp;quot;30000&amp;quot;&lt;br&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p:validationInterval=&amp;quot;1800000&amp;quot;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p:validationQuery=&amp;quot;select 1 from dual&amp;quot;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p:accessToUnderlyingConnectionAllowed=&amp;quot;true&amp;quot;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /&amp;gt;&lt;br&gt;&lt;br&gt;I still need to see how it works when dealing with vendor specific JDBC features.&lt;br&gt;</content><link rel='replies' type='application/atom+xml' href='http://ascendant76.blogspot.com/feeds/5874902143278712746/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6999217373541258402/5874902143278712746' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/5874902143278712746'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/5874902143278712746'/><link rel='alternate' type='text/html' href='http://ascendant76.blogspot.com/2010/02/upcoming-jdbc-pool.html' title='Upcoming JDBC-Pool -'/><author><name>Ramesh</name><uri>http://www.blogger.com/profile/07716715053929751798</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6999217373541258402.post-8807925842534298337</id><published>2010-02-12T16:57:00.001-05:00</published><updated>2010-02-16T14:57:44.695-05:00</updated><title type='text'>Concurrent URL checker using Completi...</title><content type='html'>&lt;h2&gt;Concurrent URL checker using Completion Service&lt;/h2&gt; From javadocs:&lt;br&gt;&lt;br&gt; &amp;quot;A service that decouples the production of new asynchronous tasks from the consumption of the results of completed tasks. Producers submit tasks for execution. Consumers take completed tasks and process their results in the order they complete. A CompletionService can for example be used to manage asynchronous IO, in which tasks that perform reads are submitted in one part of a program or system, and then acted upon in a different part of the program when the reads complete, possibly in a different order than they were requested. &lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;Typically, a CompletionService relies on a separate &lt;font face=&quot;Courier New&quot;&gt;&lt;a href=&quot;http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Executor.html&quot; id=&quot;ehaf&quot; title=&quot;Executor&quot;&gt;Executor&lt;/a&gt;&lt;/font&gt; to actually execute the tasks, in which case the CompletionService only manages an internal completion queue. The &lt;font face=&quot;Courier New&quot;&gt;&lt;a href=&quot;http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ExecutorCompletionService.html&quot; id=&quot;hh_l&quot; title=&quot;ExecutorCompletionService&quot;&gt;ExecutorCompletionService&lt;/a&gt;&lt;/font&gt; class provides an implementation of this approach.&amp;quot;&lt;/p&gt;&lt;br&gt;The program:&lt;br&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;Takes set of urls as input. This is typically done by a producer.&lt;/li&gt;&lt;li&gt;The &lt;font face=&quot;Courier New&quot;&gt;&lt;span class=&quot;s1&quot;&gt;CallableUrlChecker class checks one url and produces the Result.&lt;/span&gt;&lt;/font&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;&lt;br&gt;&lt;table bgcolor=&quot;#c0c0c0&quot; cellpadding=&quot;5&quot; cellspacing=&quot;0&quot; class=&quot;zeroBorder&quot; cols=&quot;1&quot; width=&quot;100%&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;center&gt;&lt;font color=&quot;#000000&quot; face=&quot;Arial, Helvetica&quot;&gt; UrlChecker.java&lt;/font&gt;&lt;/center&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;pre&gt;&lt;a id=&quot;l1&quot; name=&quot;l1&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;1    &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;package &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;com.blogspot.ascendant.urlchecker; &lt;br&gt;&lt;a id=&quot;l2&quot; name=&quot;l2&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;2    &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l3&quot; name=&quot;l3&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;3    &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;import &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;org.apache.http.HttpHost; &lt;br&gt;&lt;a id=&quot;l4&quot; name=&quot;l4&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;4    &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;import &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;org.apache.http.HttpResponse; &lt;br&gt;&lt;a id=&quot;l5&quot; name=&quot;l5&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;5    &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;import &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;org.apache.http.StatusLine; &lt;br&gt;&lt;a id=&quot;l6&quot; name=&quot;l6&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;6    &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;import &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;org.apache.http.client.HttpClient; &lt;br&gt;&lt;a id=&quot;l7&quot; name=&quot;l7&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;7    &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;import &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;org.apache.http.client.methods.HttpGet; &lt;br&gt;&lt;a id=&quot;l8&quot; name=&quot;l8&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;8    &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;import &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;org.apache.http.impl.client.DefaultHttpClient; &lt;br&gt;&lt;a id=&quot;l9&quot; name=&quot;l9&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;9    &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;import &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;org.apache.http.protocol.BasicHttpContext; &lt;br&gt;&lt;a id=&quot;l10&quot; name=&quot;l10&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;10   &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;import &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;org.apache.http.protocol.ExecutionContext; &lt;br&gt;&lt;a id=&quot;l11&quot; name=&quot;l11&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;11   &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;import &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;org.apache.http.protocol.HttpContext; &lt;br&gt;&lt;a id=&quot;l12&quot; name=&quot;l12&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;12   &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l13&quot; name=&quot;l13&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;13   &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;import &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;java.util.Set; &lt;br&gt;&lt;a id=&quot;l14&quot; name=&quot;l14&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;14   &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;import &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;java.util.TreeSet; &lt;br&gt;&lt;a id=&quot;l15&quot; name=&quot;l15&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;15   &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;import &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;java.util.concurrent.*; &lt;br&gt;&lt;a id=&quot;l16&quot; name=&quot;l16&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;16   &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l17&quot; name=&quot;l17&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;17   &lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;public class &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;UrlChecker { &lt;br&gt;&lt;a id=&quot;l18&quot; name=&quot;l18&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;18   &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l19&quot; name=&quot;l19&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;19   &lt;/span&gt;    &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;public static void &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;main(String... args) { &lt;br&gt;&lt;a id=&quot;l20&quot; name=&quot;l20&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;20   &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l21&quot; name=&quot;l21&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;21   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;final &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Set&amp;lt;String&amp;gt; urls = getUrlsToCheck(); &lt;br&gt;&lt;a id=&quot;l22&quot; name=&quot;l22&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;22   &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l23&quot; name=&quot;l23&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;23   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;// The executor that acutually executes the Url verification. &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; &lt;br&gt;&lt;a id=&quot;l24&quot; name=&quot;l24&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;24   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;final &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;ExecutorService pool = Executors.newFixedThreadPool(&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;); &lt;br&gt;&lt;a id=&quot;l25&quot; name=&quot;l25&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;25   &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l26&quot; name=&quot;l26&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;26   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;// The completion service that manages completed task queue&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; &lt;br&gt;&lt;a id=&quot;l27&quot; name=&quot;l27&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;27   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;final &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;CompletionService&amp;lt;Result&amp;gt; completionService = &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;new &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;ExecutorCompletionService&amp;lt;Result&amp;gt;(&lt;br&gt;&lt;a id=&quot;l28&quot; name=&quot;l28&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;28   &lt;/span&gt;                pool &lt;br&gt;&lt;a id=&quot;l29&quot; name=&quot;l29&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;29   &lt;/span&gt;        ); &lt;br&gt;&lt;a id=&quot;l30&quot; name=&quot;l30&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;30   &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l31&quot; name=&quot;l31&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;31   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;// Submit the url check tasks to the completion service&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; &lt;br&gt;&lt;a id=&quot;l32&quot; name=&quot;l32&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;32   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;for &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;final &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;String url : urls) { &lt;br&gt;&lt;a id=&quot;l33&quot; name=&quot;l33&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;33   &lt;/span&gt;            completionService.submit(&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;new &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;CallableUrlChecker(url)); &lt;br&gt;&lt;a id=&quot;l34&quot; name=&quot;l34&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;34   &lt;/span&gt;        } &lt;br&gt;&lt;a id=&quot;l35&quot; name=&quot;l35&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;35   &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l36&quot; name=&quot;l36&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;36   &lt;/span&gt;        Future&amp;lt;Result&amp;gt; futureResult; &lt;br&gt;&lt;a id=&quot;l37&quot; name=&quot;l37&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;37   &lt;/span&gt;        Result result; &lt;br&gt;&lt;a id=&quot;l38&quot; name=&quot;l38&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;38   &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l39&quot; name=&quot;l39&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;39   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;// Total urls submitted&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; &lt;br&gt;&lt;a id=&quot;l40&quot; name=&quot;l40&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;40   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;final int &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;totalUrlsToCheck = urls.size(); &lt;br&gt;&lt;a id=&quot;l41&quot; name=&quot;l41&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;41   &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l42&quot; name=&quot;l42&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;42   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;int &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;urlCounter = &lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;; &lt;br&gt;&lt;a id=&quot;l43&quot; name=&quot;l43&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;43   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;try &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;{ &lt;br&gt;&lt;a id=&quot;l44&quot; name=&quot;l44&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;44   &lt;/span&gt;            &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;while &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;(urlCounter != totalUrlsToCheck) { &lt;br&gt;&lt;a id=&quot;l45&quot; name=&quot;l45&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;45   &lt;/span&gt;                &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;try &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;{ &lt;br&gt;&lt;a id=&quot;l46&quot; name=&quot;l46&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;46   &lt;/span&gt;                    &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;// Retrieves and removes the Future representing the next completed task,&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; &lt;br&gt;&lt;a id=&quot;l47&quot; name=&quot;l47&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;47   &lt;/span&gt;                    &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;// waiting if none are yet present.&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; &lt;br&gt;&lt;a id=&quot;l48&quot; name=&quot;l48&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;48   &lt;/span&gt;                    futureResult = completionService.take(); &lt;br&gt;&lt;a id=&quot;l49&quot; name=&quot;l49&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;49   &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l50&quot; name=&quot;l50&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;50   &lt;/span&gt;                    &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;// May result a null...&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; &lt;br&gt;&lt;a id=&quot;l51&quot; name=&quot;l51&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;51   &lt;/span&gt;                    &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;if &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;(futureResult != &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;) { &lt;br&gt;&lt;a id=&quot;l52&quot; name=&quot;l52&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;52   &lt;/span&gt;                        &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;// get the result with a time wait...&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; &lt;br&gt;&lt;a id=&quot;l53&quot; name=&quot;l53&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;53   &lt;/span&gt;                        result = futureResult.get(&lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;, TimeUnit.SECONDS); &lt;br&gt;&lt;a id=&quot;l54&quot; name=&quot;l54&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;54   &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l55&quot; name=&quot;l55&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;55   &lt;/span&gt;                        &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;// further process the result&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; &lt;br&gt;&lt;a id=&quot;l56&quot; name=&quot;l56&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;56   &lt;/span&gt;                        System.out.println(urlCounter + &lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;&amp;quot; : &amp;quot; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;+ result); &lt;br&gt;&lt;a id=&quot;l57&quot; name=&quot;l57&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;57   &lt;/span&gt;                    } &lt;br&gt;&lt;a id=&quot;l58&quot; name=&quot;l58&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;58   &lt;/span&gt;                } &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;catch &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;(ExecutionException e) { &lt;br&gt;&lt;a id=&quot;l59&quot; name=&quot;l59&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;59   &lt;/span&gt;                    System.err.println(&lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;&amp;quot;Failed to check the url: &amp;quot; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;+ e.getMessage()); &lt;br&gt;&lt;a id=&quot;l60&quot; name=&quot;l60&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;60   &lt;/span&gt;                } &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;catch &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;(InterruptedException e) { &lt;br&gt;&lt;a id=&quot;l61&quot; name=&quot;l61&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;61   &lt;/span&gt;                    Thread.currentThread().interrupt(); &lt;br&gt;&lt;a id=&quot;l62&quot; name=&quot;l62&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;62   &lt;/span&gt;                } &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;catch &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;(TimeoutException e) { &lt;br&gt;&lt;a id=&quot;l63&quot; name=&quot;l63&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;63   &lt;/span&gt;                    System.err.println(&lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;&amp;quot;Failed to check the url: &amp;quot; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;+ e.getMessage()); &lt;br&gt;&lt;a id=&quot;l64&quot; name=&quot;l64&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;64   &lt;/span&gt;                } &lt;br&gt;&lt;a id=&quot;l65&quot; name=&quot;l65&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;65   &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l66&quot; name=&quot;l66&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;66   &lt;/span&gt;                urlCounter++; &lt;br&gt;&lt;a id=&quot;l67&quot; name=&quot;l67&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;67   &lt;/span&gt;            } &lt;br&gt;&lt;a id=&quot;l68&quot; name=&quot;l68&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;68   &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l69&quot; name=&quot;l69&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;69   &lt;/span&gt;        } &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;finally &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;{ &lt;br&gt;&lt;a id=&quot;l70&quot; name=&quot;l70&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;70   &lt;/span&gt;            &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;// Shutdown the executor pool if you are done&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; &lt;br&gt;&lt;a id=&quot;l71&quot; name=&quot;l71&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;71   &lt;/span&gt;            pool.shutdown(); &lt;br&gt;&lt;a id=&quot;l72&quot; name=&quot;l72&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;72   &lt;/span&gt;        } &lt;br&gt;&lt;a id=&quot;l73&quot; name=&quot;l73&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;73   &lt;/span&gt;    } &lt;br&gt;&lt;a id=&quot;l74&quot; name=&quot;l74&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;74   &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l75&quot; name=&quot;l75&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;75   &lt;/span&gt;    &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/** &lt;br&gt;&lt;a id=&quot;l76&quot; name=&quot;l76&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;76   &lt;/span&gt;     * Set of urls to check &lt;br&gt;&lt;a id=&quot;l77&quot; name=&quot;l77&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;77   &lt;/span&gt;     * &lt;br&gt;&lt;a id=&quot;l78&quot; name=&quot;l78&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;78   &lt;/span&gt;     * &lt;/span&gt;&lt;span class=&quot;s5&quot;&gt;@return &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;non-null set &lt;br&gt;&lt;a id=&quot;l79&quot; name=&quot;l79&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;79   &lt;/span&gt;     */&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; &lt;br&gt;&lt;a id=&quot;l80&quot; name=&quot;l80&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;80   &lt;/span&gt;    &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;private static &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Set&amp;lt;String&amp;gt; getUrlsToCheck() { &lt;br&gt;&lt;a id=&quot;l81&quot; name=&quot;l81&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;81   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;final &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Set&amp;lt;String&amp;gt; urls = &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;new &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;TreeSet&amp;lt;String&amp;gt;(); &lt;br&gt;&lt;a id=&quot;l82&quot; name=&quot;l82&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;82   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;for &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;int &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;i = &lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;; i &amp;lt;= &lt;/span&gt;&lt;span class=&quot;s3&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;; i++) { &lt;br&gt;&lt;a id=&quot;l83&quot; name=&quot;l83&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;83   &lt;/span&gt;            urls.add(String.format(&lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;&amp;quot;http://test%d.com&amp;quot;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;, i)); &lt;br&gt;&lt;a id=&quot;l84&quot; name=&quot;l84&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;84   &lt;/span&gt;        } &lt;br&gt;&lt;a id=&quot;l85&quot; name=&quot;l85&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;85   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;return &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;urls; &lt;br&gt;&lt;a id=&quot;l86&quot; name=&quot;l86&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;86   &lt;/span&gt;    } &lt;br&gt;&lt;a id=&quot;l87&quot; name=&quot;l87&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;87   &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l88&quot; name=&quot;l88&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;88   &lt;/span&gt;    &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/** &lt;br&gt;&lt;a id=&quot;l89&quot; name=&quot;l89&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;89   &lt;/span&gt;     * The callable task that actually verifies the URL and prepares the result... &lt;br&gt;&lt;a id=&quot;l90&quot; name=&quot;l90&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;90   &lt;/span&gt;     */&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; &lt;br&gt;&lt;a id=&quot;l91&quot; name=&quot;l91&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;91   &lt;/span&gt;    &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;public static class &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;CallableUrlChecker &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;implements &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Callable&amp;lt;Result&amp;gt; { &lt;br&gt;&lt;a id=&quot;l92&quot; name=&quot;l92&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;92   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;private final &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;String urlToCheck; &lt;br&gt;&lt;a id=&quot;l93&quot; name=&quot;l93&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;93   &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l94&quot; name=&quot;l94&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;94   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;public &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;CallableUrlChecker(String urlToCheck) { &lt;br&gt;&lt;a id=&quot;l95&quot; name=&quot;l95&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;95   &lt;/span&gt;            &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;.urlToCheck = urlToCheck; &lt;br&gt;&lt;a id=&quot;l96&quot; name=&quot;l96&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;96   &lt;/span&gt;        } &lt;br&gt;&lt;a id=&quot;l97&quot; name=&quot;l97&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;97   &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l98&quot; name=&quot;l98&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;98   &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;public &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Result call() &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;throws &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Exception { &lt;br&gt;&lt;a id=&quot;l99&quot; name=&quot;l99&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;99   &lt;/span&gt;            HttpClient httpclient = &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;new &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;DefaultHttpClient(); &lt;br&gt;&lt;a id=&quot;l100&quot; name=&quot;l100&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;100  &lt;/span&gt;            HttpGet httpget = &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;new &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;HttpGet(urlToCheck);&lt;br&gt;&lt;a id=&quot;l101&quot; name=&quot;l101&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;101  &lt;/span&gt;            HttpContext hc = &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;new &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;BasicHttpContext(); &lt;br&gt;&lt;a id=&quot;l102&quot; name=&quot;l102&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;102  &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l103&quot; name=&quot;l103&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;103  &lt;/span&gt;            Result result = &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;new &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Result(); &lt;br&gt;&lt;a id=&quot;l104&quot; name=&quot;l104&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;104  &lt;/span&gt;            result.setUrl(urlToCheck); &lt;br&gt;&lt;span class=&quot;ln&quot;&gt;105  &lt;/span&gt;            &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;try &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;{ &lt;br&gt;&lt;a id=&quot;l106&quot; name=&quot;l106&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;106  &lt;/span&gt;                HttpResponse response = httpclient.execute(httpget, hc); &lt;br&gt;&lt;a id=&quot;l107&quot; name=&quot;l107&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;107  &lt;/span&gt;                HttpHost target = (HttpHost) hc.getAttribute( &lt;br&gt;&lt;a id=&quot;l108&quot; name=&quot;l108&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;108  &lt;/span&gt;                        ExecutionContext.HTTP_TARGET_HOST &lt;br&gt;&lt;a id=&quot;l109&quot; name=&quot;l109&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;109  &lt;/span&gt;                ); &lt;br&gt;&lt;a id=&quot;l110&quot; name=&quot;l110&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;110  &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l111&quot; name=&quot;l111&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;111  &lt;/span&gt;                StatusLine status = response.getStatusLine(); &lt;br&gt;&lt;a id=&quot;l112&quot; name=&quot;l112&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;112  &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l113&quot; name=&quot;l113&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;113  &lt;/span&gt;                result.setStatusCode(status.getStatusCode()); &lt;br&gt;&lt;a id=&quot;l114&quot; name=&quot;l114&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;114  &lt;/span&gt;                result.setStatusPhrase(status.getReasonPhrase()); &lt;br&gt;&lt;a id=&quot;l115&quot; name=&quot;l115&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;115  &lt;/span&gt;                result.setRedirectUrl(target.toURI()); &lt;br&gt;&lt;a id=&quot;l116&quot; name=&quot;l116&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;116  &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l117&quot; name=&quot;l117&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;117  &lt;/span&gt;            } &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;catch &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;(Exception e) { &lt;br&gt;&lt;a id=&quot;l118&quot; name=&quot;l118&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;118  &lt;/span&gt;                result.setStatusPhrase(e.getMessage()); &lt;br&gt;&lt;a id=&quot;l119&quot; name=&quot;l119&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;119  &lt;/span&gt;            } &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;finally &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;{ &lt;br&gt;&lt;a id=&quot;l120&quot; name=&quot;l120&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;120  &lt;/span&gt;                httpclient.getConnectionManager().shutdown(); &lt;br&gt;&lt;a id=&quot;l121&quot; name=&quot;l121&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;121  &lt;/span&gt;            } &lt;br&gt;&lt;a id=&quot;l122&quot; name=&quot;l122&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;122  &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l123&quot; name=&quot;l123&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;123  &lt;/span&gt;            &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;return &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;result; &lt;br&gt;&lt;a id=&quot;l124&quot; name=&quot;l124&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;124  &lt;/span&gt;        } &lt;br&gt;&lt;a id=&quot;l125&quot; name=&quot;l125&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;125  &lt;/span&gt;    } &lt;br&gt;&lt;a id=&quot;l126&quot; name=&quot;l126&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;126  &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l127&quot; name=&quot;l127&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;127  &lt;/span&gt;    &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;/** &lt;br&gt;&lt;a id=&quot;l128&quot; name=&quot;l128&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;128  &lt;/span&gt;     * Result encapsulates the data that is returned after completion of the task &lt;br&gt;&lt;a id=&quot;l129&quot; name=&quot;l129&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;129  &lt;/span&gt;     */&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; &lt;br&gt;&lt;a id=&quot;l130&quot; name=&quot;l130&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;130  &lt;/span&gt;    &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;public static class &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Result { &lt;br&gt;&lt;a id=&quot;l131&quot; name=&quot;l131&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;131  &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;private &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;String url; &lt;br&gt;&lt;a id=&quot;l132&quot; name=&quot;l132&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;132  &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;private &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;String redirectUrl; &lt;br&gt;&lt;a id=&quot;l133&quot; name=&quot;l133&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;133  &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;private int &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;statusCode; &lt;br&gt;&lt;a id=&quot;l134&quot; name=&quot;l134&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;134  &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;private &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;String statusPhrase; &lt;br&gt;&lt;a id=&quot;l135&quot; name=&quot;l135&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;135  &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l136&quot; name=&quot;l136&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;136  &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;public &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;String getUrl() { &lt;br&gt;&lt;a id=&quot;l137&quot; name=&quot;l137&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;137  &lt;/span&gt;            &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;return &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;url; &lt;br&gt;&lt;a id=&quot;l138&quot; name=&quot;l138&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;138  &lt;/span&gt;        } &lt;br&gt;&lt;a id=&quot;l139&quot; name=&quot;l139&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;139  &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l140&quot; name=&quot;l140&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;140  &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;public void &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;setUrl(String url) { &lt;br&gt;&lt;a id=&quot;l141&quot; name=&quot;l141&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;141  &lt;/span&gt;            &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;.url = url; &lt;br&gt;&lt;a id=&quot;l142&quot; name=&quot;l142&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;142  &lt;/span&gt;        } &lt;br&gt;&lt;a id=&quot;l143&quot; name=&quot;l143&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;143  &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l144&quot; name=&quot;l144&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;144  &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;public &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;String getRedirectUrl() { &lt;br&gt;&lt;a id=&quot;l145&quot; name=&quot;l145&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;145  &lt;/span&gt;            &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;return &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;redirectUrl; &lt;br&gt;&lt;a id=&quot;l146&quot; name=&quot;l146&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;146  &lt;/span&gt;        } &lt;br&gt;&lt;a id=&quot;l147&quot; name=&quot;l147&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;147  &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l148&quot; name=&quot;l148&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;148  &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;public void &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;setRedirectUrl(String redirectUrl) { &lt;br&gt;&lt;a id=&quot;l149&quot; name=&quot;l149&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;149  &lt;/span&gt;            &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;.redirectUrl = redirectUrl; &lt;br&gt;&lt;a id=&quot;l150&quot; name=&quot;l150&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;150  &lt;/span&gt;        } &lt;br&gt;&lt;span class=&quot;ln&quot;&gt;151  &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l152&quot; name=&quot;l152&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;152  &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;public int &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;getStatusCode() { &lt;br&gt;&lt;a id=&quot;l153&quot; name=&quot;l153&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;153  &lt;/span&gt;            &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;return &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;statusCode; &lt;br&gt;&lt;a id=&quot;l154&quot; name=&quot;l154&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;154  &lt;/span&gt;        } &lt;br&gt;&lt;a id=&quot;l155&quot; name=&quot;l155&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;155  &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l156&quot; name=&quot;l156&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;156  &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;public void &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;setStatusCode(&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;int &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;statusCode) { &lt;br&gt;&lt;a id=&quot;l157&quot; name=&quot;l157&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;157  &lt;/span&gt;            &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;.statusCode = statusCode; &lt;br&gt;&lt;a id=&quot;l158&quot; name=&quot;l158&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;158  &lt;/span&gt;        } &lt;br&gt;&lt;a id=&quot;l159&quot; name=&quot;l159&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;159  &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l160&quot; name=&quot;l160&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;160  &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;public &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;String getStatusPhrase() { &lt;br&gt;&lt;a id=&quot;l161&quot; name=&quot;l161&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;161  &lt;/span&gt;            &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;return &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;statusPhrase; &lt;br&gt;&lt;a id=&quot;l162&quot; name=&quot;l162&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;162  &lt;/span&gt;        } &lt;br&gt;&lt;a id=&quot;l163&quot; name=&quot;l163&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;163  &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l164&quot; name=&quot;l164&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;164  &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;public void &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;setStatusPhrase(String statusPhrase) { &lt;br&gt;&lt;a id=&quot;l165&quot; name=&quot;l165&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;165  &lt;/span&gt;            &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;.statusPhrase = statusPhrase; &lt;br&gt;&lt;a id=&quot;l166&quot; name=&quot;l166&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;166  &lt;/span&gt;        } &lt;br&gt;&lt;span class=&quot;ln&quot;&gt;167  &lt;/span&gt; &lt;br&gt;&lt;a id=&quot;l168&quot; name=&quot;l168&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;168  &lt;/span&gt;        @Override &lt;br&gt;&lt;a id=&quot;l169&quot; name=&quot;l169&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;169  &lt;/span&gt;        &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;public &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;String toString() { &lt;br&gt;&lt;a id=&quot;l170&quot; name=&quot;l170&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;170  &lt;/span&gt;            &lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;return &lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;&amp;quot;Result{&amp;quot; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;+ &lt;br&gt;&lt;a id=&quot;l171&quot; name=&quot;l171&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;171  &lt;/span&gt;                    &lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;&amp;quot;url=&amp;#39;&amp;quot; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;+ url + &lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;\&amp;#39;&lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;&amp;#39; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;+ &lt;br&gt;&lt;span class=&quot;ln&quot;&gt;172  &lt;/span&gt;                    &lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;&amp;quot;, redirectUrl=&amp;#39;&amp;quot; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;+ redirectUrl + &lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;\&amp;#39;&lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;&amp;#39; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;+ &lt;br&gt;&lt;a id=&quot;l173&quot; name=&quot;l173&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;173  &lt;/span&gt;                    &lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;&amp;quot;, statusCode=&amp;quot; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;+ statusCode + &lt;br&gt;&lt;a id=&quot;l174&quot; name=&quot;l174&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;174  &lt;/span&gt;                    &lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;&amp;quot;, statusPhrase=&amp;#39;&amp;quot; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;+ statusPhrase + &lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;&amp;#39;&lt;/span&gt;&lt;span class=&quot;s0&quot;&gt;\&amp;#39;&lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;&amp;#39; &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;+ &lt;br&gt;&lt;a id=&quot;l175&quot; name=&quot;l175&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;175  &lt;/span&gt;                    &lt;/span&gt;&lt;span class=&quot;s4&quot;&gt;&amp;#39;}&amp;#39;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;; &lt;br&gt;&lt;a id=&quot;l176&quot; name=&quot;l176&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;176  &lt;/span&gt;        } &lt;br&gt;&lt;a id=&quot;l177&quot; name=&quot;l177&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;177  &lt;/span&gt;    } &lt;br&gt;&lt;a id=&quot;l178&quot; name=&quot;l178&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;178  &lt;/span&gt;} &lt;br&gt;&lt;a id=&quot;l179&quot; name=&quot;l179&quot;&gt;&lt;/a&gt;&lt;span class=&quot;ln&quot;&gt;179  &lt;/span&gt;   &lt;/span&gt;&lt;/pre&gt;&lt;br&gt;The above solution can be improved with the following changes.&lt;br&gt;&lt;br&gt;1) Producer and Consumer are in only class; and this can be separated. Run the producer in a separate thread and the consumer part in a separate thread.&lt;br&gt;&lt;br&gt;2) Can leverage &lt;a href=&quot;http://gee.cs.oswego.edu/dl/papers/fj.pdf&quot; id=&quot;ywlf&quot; title=&quot;Fork/Join Framework&quot;&gt;Fork/Join Framework&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;h3&gt;Maven2 Dependencies&lt;/h3&gt;&lt;b&gt;&lt;a href=&quot;http://hc.apache.org/&quot; id=&quot;e0_b&quot; title=&quot;HttpComponents&quot;&gt;HttpComponents&lt;/a&gt;&lt;/b&gt;.&lt;br&gt;&lt;br&gt; &amp;lt;dependency&amp;gt;&lt;br&gt;&lt;div style=&quot;margin-left:40px&quot;&gt;&amp;lt;groupId&amp;gt;org.apache.httpcomponents&amp;lt;/groupId&amp;gt;&lt;br&gt; &amp;lt;artifactId&amp;gt;httpclient&amp;lt;/artifactId&amp;gt;&lt;br&gt; &amp;lt;version&amp;gt;4.1-alpha1&amp;lt;/version&amp;gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&amp;lt;/dependency&amp;gt;&lt;br&gt;&lt;br&gt;&lt;a href=&quot;http://www.box.net/shared/lq9raiasn3&quot; id=&quot;l59i&quot; title=&quot;URLChecker - Java Concurrent Completion Service&quot;&gt;Download code for URLChecker - Java Concurrent Completion Service&lt;/a&gt;&lt;br&gt;&lt;/div&gt;&lt;br&gt;&lt;br&gt;</content><link rel='replies' type='application/atom+xml' href='http://ascendant76.blogspot.com/feeds/8807925842534298337/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6999217373541258402/8807925842534298337' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/8807925842534298337'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/8807925842534298337'/><link rel='alternate' type='text/html' href='http://ascendant76.blogspot.com/2010/02/concurrent-url-checker-using-completi.html' title='Concurrent URL checker using Completi...'/><author><name>Ramesh</name><uri>http://www.blogger.com/profile/07716715053929751798</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6999217373541258402.post-6627278364500485640</id><published>2009-09-06T18:51:00.001-05:00</published><updated>2009-09-08T08:49:23.569-05:00</updated><title type='text'>Amazon Product Advertising API</title><content type='html'>&lt;h1&gt;&lt;font size=&quot;4&quot;&gt;&lt;span style=&quot;font-weight: normal;&quot;&gt;Product Advertising API Signed Requests - Java SOAP using Spring Web Services&lt;/span&gt;&lt;/font&gt;&lt;br&gt;&lt;/h1&gt;Product Advertising API - https://affiliate-program.amazon.com/gp/advertising/api/detail/main.html/178-0006257-2456255&lt;br&gt;&lt;br&gt;We are using Amazon Web Services to advertise products on one of my clients websites. We used Springframework as the dependency container and Spring Web Services as web service platform as we Spring-WS favors contract-first web services. With the recent changes in the API all the requests must be signed. And the sample code (&lt;a title=&quot;http://developer.amazonwebservices.com/connect/entry.jspa?externalID=2479&amp;amp;categoryID=14&quot; href=&quot;http://developer.amazonwebservices.com/connect/entry.jspa?externalID=2479&amp;amp;categoryID=14&quot; id=&quot;d:ic&quot;&gt;http://developer.amazonwebservices.com/connect/entry.jspa?externalID=2479&amp;amp;categoryID=14&lt;/a&gt;) helped me as a reference to migrate our code base to support the signed request requirement.&lt;br&gt;&lt;br&gt;In the below sections I will walk through the steps to make signed requests using Spring Web Services. &lt;br&gt;&lt;br&gt;&lt;u&gt;&lt;b&gt;Project Setup:&lt;br&gt;&lt;br&gt;&lt;/b&gt;&lt;/u&gt;We used maven2 as our build &amp;amp; reporting tool we followed the maven project directory structure and is as below:&lt;br&gt;&lt;br&gt;awsprocess&lt;br&gt;└───src&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ├───main&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; │&amp;nbsp;&amp;nbsp; ├───java&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; │&amp;nbsp;&amp;nbsp; │&amp;nbsp;&amp;nbsp; └───com&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; │&amp;nbsp;&amp;nbsp; │&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; └───ascendant76&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; │&amp;nbsp;&amp;nbsp; │&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; └───awsdemo&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; │&amp;nbsp;&amp;nbsp; │&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ├───client&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; │&amp;nbsp;&amp;nbsp; │&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; └───service&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; │&amp;nbsp;&amp;nbsp; ├───resources&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; │&amp;nbsp;&amp;nbsp; └───webapp&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; └───test&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ├───java&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; └───resources&lt;br&gt;&lt;br&gt;The project object model uses few plug-ins:&lt;br&gt;&lt;br&gt;&lt;b&gt;maven-compiler-plugin:&lt;/b&gt; To compile the code that is compatible with J2SE 5.0.&lt;br&gt;&lt;br&gt;&lt;b&gt;maven-surefire-plugin&lt;/b&gt;: To execute the unit test cases. I used &lt;a title=&quot;http://testng.org/doc/index.html&quot; target=&quot;_blank&quot; href=&quot;http://testng.org/doc/index.html&quot; id=&quot;qbqe&quot;&gt;TestNG&lt;/a&gt;(&lt;a title=&quot;http://testng.org/doc/index.html&quot; target=&quot;_blank&quot; href=&quot;http://testng.org/doc/index.html&quot; id=&quot;i15f&quot;&gt;http://testng.org/doc/index.html&lt;/a&gt;) for the test cases and their by the testng dependency is imported.&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;dependency&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;groupId&amp;gt;org.testng&amp;lt;/groupId&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;artifactId&amp;gt;testng&amp;lt;/artifactId&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;classifier&amp;gt;jdk15&amp;lt;/classifier&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;version&amp;gt;5.9&amp;lt;/version&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;/dependency&amp;gt;&lt;br&gt;&lt;br&gt;&lt;b&gt;maven-jaxb2-plugin:&lt;/b&gt; Creates JAXB2 binding from XSD. The plug-in scans the xjb files and generates the bindings in our case it AWSECommerceService.xjb in the resources folder.&lt;br&gt;&lt;br&gt;&lt;u&gt;&lt;b&gt;AWSECommerceService.xjb:&lt;br&gt;&lt;br&gt;&lt;/b&gt;&lt;/u&gt;&amp;lt;?xml version=&#39;1.0&#39; encoding=&#39;utf-8&#39; ?&amp;gt;&lt;br&gt;&amp;lt;jxb:bindings version=&quot;2.0&quot; xmlns:jxb=&quot;http://java.sun.com/xml/ns/jaxb&quot;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;&amp;gt;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;jxb:bindings node=&quot;/xs:schema&quot;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;schemaLocation=&quot;AWSECommerceService.xsd&quot;&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;jxb:globalBindings fixedAttributeAsConstantProperty=&quot;false&quot;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;collectionType=&quot;java.util.ArrayList&quot; typesafeEnumBase=&quot;xs:NCName&quot;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;choiceContentProperty=&quot;false&quot; typesafeEnumMemberName=&quot;generateError&quot;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;enableFailFastCheck=&quot;true&quot; generateIsSetMethod=&quot;true&quot;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;underscoreBinding=&quot;asCharInWord&quot;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;enableJavaNamingConventions=&quot;true&quot;&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;/jxb:globalBindings&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;jxb:schemaBindings&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;jxb:package name=&quot;com.ascendant76.awsdemo.model&quot; /&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;/jxb:schemaBindings&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;/jxb:bindings&amp;gt;&lt;br&gt;&amp;lt;/jxb:bindings&amp;gt;&lt;br&gt;&lt;br&gt;The AWSECommerceService.xsd is extracted manually from the web service contract schema from the WSDL located at http://ecs.amazonaws.com/AWSECommerceService/2009-07-01/AWSECommerceService.wsdl &lt;br&gt;&lt;br&gt;&lt;u&gt;&lt;b&gt;Account Setup:&lt;/b&gt;&lt;/u&gt;&lt;br&gt;&lt;br&gt; Login to amazon at &lt;a title=&quot;http://aws-portal.amazon.com/gp/aws/developer/account/index.html?ie=UTF8&amp;amp;action=activity-summary&quot; target=&quot;_blank&quot; href=&quot;http://aws-portal.amazon.com/gp/aws/developer/account/index.html?ie=UTF8&amp;amp;action=activity-summary&quot; id=&quot;q7e9&quot;&gt;http://aws-portal.amazon.com/gp/aws/developer/account/index.html?ie=UTF8&amp;amp;action=activity-summary&lt;/a&gt;.&amp;nbsp;  Goto Access Credentials section by following this URL &lt;a title=&quot;https://aws-portal.amazon.com/gp/aws/developer/account/index.html?ie=UTF8&amp;amp;action=access-key&quot; target=&quot;_blank&quot; href=&quot;https://aws-portal.amazon.com/gp/aws/developer/account/index.html?ie=UTF8&amp;amp;action=access-key&quot; id=&quot;qctq&quot;&gt;https://aws-portal.amazon.com/gp/aws/developer/account/index.html?ie=UTF8&amp;amp;action=access-key&lt;/a&gt;. This page allows you to create new X.509 certificates. Select the Create option to create your certificates and download them. You will have two files pk-XXXX.pem and cert-XXXX.pem&lt;br&gt;&amp;nbsp; &lt;br&gt;&lt;b&gt;Note: &lt;/b&gt;You can download the private key file (pk-XXXX.pem) at the time of certificate creation only and will not be available later. If you lose it, you have to create a new certificate. So keep it safe.&lt;br&gt;&lt;br&gt; Now we need to convert the certificate into the PCKS12 format. We need &lt;a title=&quot;http://www.openssl.org/&quot; target=&quot;_blank&quot; href=&quot;http://www.openssl.org/&quot; id=&quot;egb2&quot;&gt;openssl&lt;/a&gt; (&lt;a title=&quot;http://www.openssl.org/&quot; target=&quot;_blank&quot; href=&quot;http://www.openssl.org&quot; id=&quot;d6ne&quot;&gt;http://www.openssl.org&lt;/a&gt;) to create the certificate in pcks12 format.&lt;br&gt;&lt;br&gt;  C:\openssl pkcs12 -export  -in cert-XXXX.pem  -inkey pk-XXXX.pem  -name ramesh_cert  -out ramesh_cert.p12&lt;br&gt;&lt;br&gt; The pkcs12 file will be generated. Execute the following command to verify the generated certificate using the user name.&lt;br&gt;&amp;nbsp;&lt;br&gt; C:\jdk1.6.0_13\bin\keytool -v -list -storetype pkcs12 -keystore ramesh_cert.p12&lt;br&gt;&lt;br&gt;Enter keystore password:&amp;nbsp; ramesh&lt;br&gt;&lt;br&gt;The output appears something like below:&lt;br&gt;&lt;br&gt;Keystore type: pkcs12&lt;br&gt;Keystore provider: SunJSSE&lt;br&gt;&lt;br&gt;Your keystore contains 1 entry&lt;br&gt;&lt;br&gt;Alias name: ramesh_cert&lt;br&gt;Creation date: Aug 28, 2009&lt;br&gt;Entry type: keyEntry&lt;br&gt;Certificate chain length: 1&lt;br&gt;Certificate[1]:&lt;br&gt;Owner: CN=zzp69oziqnw6, OU=AWS-Developers, O=Amazon.com, C=US&lt;br&gt;Issuer: CN=AWS Limited-Assurance CA, OU=AWS, O=Amazon.com, C=US&lt;br&gt;Serial number: d61749a157&lt;br&gt;Valid from: Fri Aug 28 16:49:58 EDT 2009 until: Sat Aug 28 16:49:58 EDT 2010&lt;br&gt;Certificate fingerprints:&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MD5:&amp;nbsp; 60:BF:22:AF:51:F8:5A:72:36:8F:E8:69:1D:35:8C:C0&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SHA1: D0:D9:5A:60:38:35:98:45:C3:41:CE:1A:8F:76:DC:39:80:3A:02:1B&lt;br&gt;**************************************************************************************&lt;br&gt;&lt;br&gt;&lt;br&gt;Once you verify the certificate generated is okay with the given user&lt;br /&gt;name, copy the certificate generated above - ramesh_cert.p12 - into the&lt;br /&gt;resources directory.&lt;br&gt;&lt;br&gt;&lt;u&gt;&lt;b&gt;Codebase:&lt;br&gt;&lt;br&gt;&lt;/b&gt;&lt;/u&gt;The dependency diagram of various collaborators is given below.&lt;br&gt;&lt;br&gt;&lt;img src=&quot;http://docs.google.com/File?id=dgwfjptc_107hn6b95cn_b&quot; height=&quot;510&quot; width=&quot;517&quot;&gt;&lt;br&gt;The AwsGateway is the sub-class of &lt;b&gt;&lt;span style=&quot;font-family: Courier New;&quot;&gt;org.springframework.ws.client.core.support.WebServiceGatewaySupport&lt;/span&gt;&lt;/b&gt;&amp;nbsp; is responsible for marshaling the java objects in to SOAP messages and sends to location mentioned in the service binding of the WSDL and in this case https://ecs.amazonaws.com/onca/soap?Service=AWSECommerceService. The WebServiceGatewaySupport delegates the&amp;nbsp; responsibility to the Marshaller to marshal java request objects to SOAP messages and UnMarshaller to marshal the SOAP response&amp;nbsp; back to Java object.&lt;br&gt;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;!-- Working with the request and response xml&#39;s using Jaxb2 --&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;bean id=&quot;marshaller&quot; class=&quot;&lt;b&gt;&lt;span style=&quot;font-family: Courier New;&quot;&gt;org.springframework.oxm.jaxb.Jaxb2Marshaller&lt;/span&gt;&lt;/b&gt;&quot;&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;property name=&quot;schema&quot; value=&quot;classpath:AWSECommerceService.xsd&quot; /&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;property name=&quot;contextPath&quot; value=&quot;com.ascendant76.awsdemo.model&quot; /&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;/bean&amp;gt;&lt;br&gt;&lt;br&gt;Spring-WS supports two types of messages factories to deal with SOAP messages &lt;b&gt;&lt;font face=&quot;Courier New&quot;&gt;org.springframework.ws.soap.axiom.AxiomSoapMessageFactory&lt;/font&gt;&lt;/b&gt; and &lt;font face=&quot;Courier New&quot;&gt;&lt;b&gt;org.springframework.ws.soap.saaj.SaajSoapMessageFactory. &lt;/b&gt;&lt;/font&gt;And I choose to use SAAJ (&lt;a title=&quot;https://saaj.dev.java.net/nonav/spec-1.3/api/&quot; target=&quot;_blank&quot; href=&quot;https://saaj.dev.java.net/nonav/spec-1.3/api/&quot; id=&quot;ta00&quot;&gt;https://saaj.dev.java.net/nonav/spec-1.3/api/&lt;/a&gt;). &lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;!-- SOAP with Attachments API for Java --&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;bean id=&quot;messageFactory&quot; class=&quot;&lt;b&gt;&lt;span style=&quot;font-family: Courier New;&quot;&gt;org.springframework.ws.soap.saaj.SaajSoapMessageFactory&lt;/span&gt;&lt;/b&gt;&quot; /&amp;gt;&lt;br&gt;&lt;br&gt;Now the web service gateway definition looks as below:&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;bean id=&quot;awsGateway&quot; class=&quot;com.ascendant76.awsdemo.service.AwsGateway&quot;&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;constructor-arg value=&quot;${aws.accessKey}&quot; /&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;property name=&quot;defaultUri&quot;  value=&quot;https://ecs.amazonaws.com/onca/soap?Service=AWSECommerceService&quot; /&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;property name=&quot;marshaller&quot; ref=&quot;marshaller&quot; /&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;property name=&quot;unmarshaller&quot; ref=&quot;marshaller&quot; /&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;property name=&quot;messageFactory&quot; ref=&quot;messageFactory&quot; /&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;property name=&quot;interceptors&quot;&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;list&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;ref bean=&quot;signatureSecurityInterceptor&quot; /&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;/list&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;/property&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;/bean&amp;gt;&lt;br&gt;&lt;br&gt;&amp;nbsp;As security is an orthogonal concept, spring-ws handles it using interceptors. We use WSS4J for the authentication to produce signed requests. Amazon API rely on Timestamp and Signature.&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;bean id=&quot;signatureSecurityInterceptor&quot;  class=&quot;org.springframework.ws.soap.security.wss4j.Wss4jSecurityInterceptor&quot;&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;property name=&quot;securementActions&quot; value=&quot;&lt;b&gt;Timestamp Signature&lt;/b&gt;&quot; /&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;property name=&quot;timestampPrecisionInMilliseconds&quot; value=&quot;true&quot; /&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;property name=&quot;securementSignatureKeyIdentifier&quot; value=&quot;DirectReference&quot; /&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;property name=&quot;securementUsername&quot; value=&quot;${aws.keystoreUser}&quot; /&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;property name=&quot;securementPassword&quot; value=&quot;${aws.keystorePassword}&quot; /&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;property name=&quot;securementSignatureCrypto&quot; ref=&quot;cryptoFactoryBean&quot; /&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;property name=&quot;securementCallbackHandler&quot; ref=&quot;passwordCallback&quot; /&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;/bean&amp;gt;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;bean id=&quot;cryptoFactoryBean&quot;  class=&quot;org.springframework.ws.soap.security.wss4j.support.CryptoFactoryBean&quot;&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;property name=&quot;cryptoProvider&quot;  value=&quot;org.apache.ws.security.components.crypto.Merlin&quot; /&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;property name=&quot;keyStoreLocation&quot; value=&quot;classpath:${aws.keystoreUser}.p12&quot; /&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;property name=&quot;keyStorePassword&quot; value=&quot;${aws.keystorePassword}&quot; /&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;property name=&quot;keyStoreType&quot; value=&quot;pkcs12&quot; /&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;property name=&quot;keyStoreProvider&quot; value=&quot;SunJSSE&quot; /&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;/bean&amp;gt;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;bean id=&quot;passwordCallback&quot; class=&quot;com.ascendant76.awsdemo.service.PasswordCallback&quot;&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;constructor-arg index=&quot;0&quot; value=&quot;${aws.keystorePassword}&quot; /&amp;gt;&lt;br&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;lt;/bean&amp;gt;&lt;br&gt;&lt;br&gt;To keep all the properties in one place we can use property place holder. The keystore password is being served from properties file:&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;context:property-placeholder location=&quot;classpath:app.properties&quot; /&amp;gt;&lt;br&gt;&lt;br&gt;Change the properties files to reflect your credentials&lt;br&gt;&lt;br&gt;# Amazon Associate Access Key&lt;br&gt;aws.accessKey=&amp;lt;&amp;lt;AWS ACCESSKEY comes here&amp;gt;&amp;gt;&lt;br&gt;aws.keystoreUser=&amp;lt;&amp;lt;KEY STORE comes here&amp;gt;&amp;gt;&lt;br&gt;aws.keystorePassword=&amp;lt;&amp;lt;KEY STORE password&amp;gt;&amp;gt;&lt;br&gt;&lt;br&gt;Now at this stage we are ready to invoke the web service. Execute mvn clean test to execute the test cases.&lt;br&gt;&lt;br&gt;&lt;u&gt;&lt;b&gt;Download the code&lt;/b&gt;&lt;/u&gt;&lt;br&gt;You can download the code from here: &lt;a title=&quot;http://www.box.net/shared/m2vd4oizkm&quot; target=&quot;_blank&quot; href=&quot;http://www.box.net/shared/m2vd4oizkm&quot; id=&quot;bt8t&quot;&gt;Download the Code&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;u&gt;&lt;b&gt;References:&lt;/b&gt;&lt;/u&gt;&lt;br&gt;http://www.openssl.org/docs/apps/pkcs12.html&lt;br&gt;&lt;br&gt;</content><link rel='replies' type='application/atom+xml' href='http://ascendant76.blogspot.com/feeds/6627278364500485640/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6999217373541258402/6627278364500485640' title='24 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/6627278364500485640'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/6627278364500485640'/><link rel='alternate' type='text/html' href='http://ascendant76.blogspot.com/2009/09/amazon-product-advertising-api.html' title='Amazon Product Advertising API'/><author><name>Ramesh</name><uri>http://www.blogger.com/profile/07716715053929751798</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>24</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6999217373541258402.post-2698628085807316443</id><published>2009-08-25T07:33:00.000-05:00</published><updated>2009-08-25T07:33:37.118-05:00</updated><title type='text'>Nandan Nilekani&#39;s ideas for India&#39;s future</title><content type='html'>Interesting video by Nandan Nilekani and his ideas for India&#39;s future. The problem domain is mapped to the following categories. &lt;br /&gt;
&lt;br /&gt;
* Ideas that have arrived&lt;br /&gt;
* Ideas in progress&lt;br /&gt;
* Ideas in conflict&lt;br /&gt;
* Ideas in anticipation &lt;br /&gt;
&lt;br /&gt;
What I like in this presentation is the approach based on the above four fundamentals.&lt;br /&gt;
&lt;br /&gt;
&lt;object height=&quot;326&quot; width=&quot;446&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://video.ted.com/assets/player/swf/EmbedPlayer.swf&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot; /&gt;&lt;param name=&quot;wmode&quot; value=&quot;transparent&quot;&gt;&lt;/param&gt;&lt;param name=&quot;bgColor&quot; value=&quot;#ffffff&quot;&gt;&lt;/param&gt;&lt;param name=&quot;flashvars&quot; value=&quot;vu=http://video.ted.com/talks/embed/NandanNilekani_2009-embed_high.flv&amp;amp;su=http://images.ted.com/images/ted/tedindex/embed-posters/NandanNilekani-2009.embed_thumbnail.jpg&amp;amp;vw=432&amp;amp;vh=240&amp;amp;ap=0&amp;amp;ti=545&quot; /&gt;&lt;embed src=&quot;http://video.ted.com/assets/player/swf/EmbedPlayer.swf&quot; pluginspace=&quot;http://www.macromedia.com/go/getflashplayer&quot; type=&quot;application/x-shockwave-flash&quot; wmode=&quot;transparent&quot; bgColor=&quot;#ffffff&quot; width=&quot;446&quot; height=&quot;326&quot; allowFullScreen=&quot;true&quot; flashvars=&quot;vu=http://video.ted.com/talks/embed/NandanNilekani_2009-embed_high.flv&amp;amp;su=http://images.ted.com/images/ted/tedindex/embed-posters/NandanNilekani-2009.embed_thumbnail.jpg&amp;amp;vw=432&amp;amp;vh=240&amp;amp;ap=0&amp;amp;ti=545&quot;&gt;&lt;/embed&gt;&lt;/object&gt;</content><link rel='replies' type='application/atom+xml' href='http://ascendant76.blogspot.com/feeds/2698628085807316443/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6999217373541258402/2698628085807316443' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/2698628085807316443'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/2698628085807316443'/><link rel='alternate' type='text/html' href='http://ascendant76.blogspot.com/2009/08/nandan-nilekanis-ideas-for-indias.html' title='Nandan Nilekani&#39;s ideas for India&#39;s future'/><author><name>Ramesh</name><uri>http://www.blogger.com/profile/07716715053929751798</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6999217373541258402.post-5198018443828000022</id><published>2009-08-06T12:44:00.000-05:00</published><updated>2009-08-06T12:44:51.902-05:00</updated><title type='text'>Don&#39;t mess with Java naming convensions - Java Generics</title><content type='html'>&lt;div&gt;&lt;span class=&quot;430342517-06082009&quot;&gt;&lt;span style=&quot;font-family: Arial; font-size: x-small;&quot;&gt;Today I found the following program over the internet. First time when I saw it I thought it won&#39;t compile. &lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;430342517-06082009&quot;&gt;&lt;span style=&quot;font-family: Arial; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;430342517-06082009&quot;&gt;&lt;span style=&quot;font-family: Arial; font-size: x-small;&quot;&gt; &lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;430342517-06082009&quot;&gt;&lt;span style=&quot;font-family: Arial; font-size: x-small;&quot;&gt;import  java.util.ArrayList;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;430342517-06082009&quot;&gt;&lt;/span&gt;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;font-family: Arial; font-size: x-small;&quot;&gt;public class ListProvider {&lt;br /&gt;
&amp;nbsp; public static  &lt;arraylist&gt; ArrayList sayHello(ArrayList ar) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return ar;&lt;br /&gt;
&amp;nbsp;  }&lt;br /&gt;
&amp;nbsp; public static void main(String[] args) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;  System.out.println(sayHello(&quot;I am a list&quot;));&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
}&lt;/arraylist&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;430342517-06082009&quot;&gt;&lt;/span&gt;&amp;nbsp;&lt;/div&gt;&lt;div&gt;Now I rewrote the program following java naming conventions.&amp;nbsp; &lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;430342517-06082009&quot;&gt;&lt;/span&gt;&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;430342517-06082009&quot;&gt;&lt;span style=&quot;font-family: Arial; font-size: x-small;&quot;&gt;public class  ListProvider {&lt;br /&gt;
&amp;nbsp; public static &lt;t&gt; T sayHello(T ar) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; return  ar;&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
&amp;nbsp; public static void main(String[] args) {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;  System.out.println(sayHello(&quot;I am a list&quot;));&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
}&lt;/t&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;430342517-06082009&quot;&gt;&lt;span style=&quot;font-family: Arial; font-size: x-small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;430342517-06082009&quot;&gt;&lt;span style=&quot;font-family: Arial; font-size: x-small;&quot;&gt;&lt;arraylist&gt; is a  Generic Type. It is hiding java.util.ArrayList in the first program. Second  program it is bit clean using T as type. &lt;/arraylist&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ascendant76.blogspot.com/feeds/5198018443828000022/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6999217373541258402/5198018443828000022' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/5198018443828000022'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/5198018443828000022'/><link rel='alternate' type='text/html' href='http://ascendant76.blogspot.com/2009/08/dont-mess-with-java-naming-convensions.html' title='Don&#39;t mess with Java naming convensions - Java Generics'/><author><name>Ramesh</name><uri>http://www.blogger.com/profile/07716715053929751798</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6999217373541258402.post-5725826100863200270</id><published>2009-05-19T09:34:00.002-05:00</published><updated>2009-05-19T09:34:29.818-05:00</updated><title type='text'>Checking the file association in Windows from Command line</title><content type='html'>Checking the file association in Windows:&lt;br /&gt;
&lt;br /&gt;
The &quot;assoc&quot; will let you know the file type associated with the extension&lt;br /&gt;
C:\&amp;gt;assoc .txt&lt;br /&gt;
.txt=txtfile&lt;br /&gt;
&lt;br /&gt;
Now the &quot;ftype&quot; displays or modifies file types used in file extension associations&lt;br /&gt;
C:\&amp;gt;ftype txtfile&lt;br /&gt;
txtfile=%SystemRoot%\system32\NOTEPAD.EXE %1</content><link rel='replies' type='application/atom+xml' href='http://ascendant76.blogspot.com/feeds/5725826100863200270/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6999217373541258402/5725826100863200270' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/5725826100863200270'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/5725826100863200270'/><link rel='alternate' type='text/html' href='http://ascendant76.blogspot.com/2009/05/checking-file-association-in-windows.html' title='Checking the file association in Windows from Command line'/><author><name>Ramesh</name><uri>http://www.blogger.com/profile/07716715053929751798</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6999217373541258402.post-423569175602724929</id><published>2008-12-22T09:22:00.000-05:00</published><updated>2010-05-19T20:29:46.165-05:00</updated><title type='text'>12 days of christmas - Indian Version</title><content type='html'>I was stumbled to see this video from my boss. A very unusual e-mail I can say. Check this one.&lt;br /&gt;&lt;br /&gt;&lt;object height=&quot;344&quot; width=&quot;425&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://www.youtube.com/v/owK5tHjL0aE&amp;hl=en&amp;fs=1&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot;&gt;&lt;/param&gt;&lt;embed src=&quot;http://www.youtube.com/v/owK5tHjL0aE&amp;hl=en&amp;fs=1&quot; type=&quot;application/x-shockwave-flash&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot; width=&quot;425&quot; height=&quot;344&quot;&gt;&lt;/embed&gt;&lt;/object&gt;</content><link rel='replies' type='application/atom+xml' href='http://ascendant76.blogspot.com/feeds/423569175602724929/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6999217373541258402/423569175602724929' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/423569175602724929'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/423569175602724929'/><link rel='alternate' type='text/html' href='http://ascendant76.blogspot.com/2008/12/12-days-of-christmas-indian-version.html' title='12 days of christmas - Indian Version'/><author><name>Ramesh</name><uri>http://www.blogger.com/profile/07716715053929751798</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6999217373541258402.post-8877645172659481978</id><published>2008-10-19T17:24:00.001-05:00</published><updated>2008-10-19T17:31:17.709-05:00</updated><title type='text'>When two in Tango ...</title><content type='html'>&lt;object width=&quot;425&quot; height=&quot;344&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://www.youtube.com/v/sEle_nV7bxs&amp;hl=en&amp;fs=1&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;&gt;&lt;/param&gt;&lt;embed src=&quot;http://www.youtube.com/v/sEle_nV7bxs&amp;hl=en&amp;fs=1&quot; type=&quot;application/x-shockwave-flash&quot; allowfullscreen=&quot;true&quot; width=&quot;425&quot; height=&quot;344&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br /&gt;
&lt;br /&gt;
When you look in to my eyes...&lt;br /&gt;
.. eye lids stop flittering&lt;br /&gt;
.. heart beats fast&lt;br /&gt;
.. minds freezes the frame of yours in my eye lids&lt;br /&gt;
&lt;br /&gt;
Let us tango together..&lt;br /&gt;
.. the way no one did&lt;br /&gt;
.. the way no one will&lt;br /&gt;
.. let the eyes be in to my eyes&lt;br /&gt;
&lt;br /&gt;
Let us not stop this...&lt;br /&gt;
.. as long as the clock ticks&lt;br /&gt;
.. as long as the earth rotates&lt;br /&gt;
.. as long as we can with stand standing&lt;br /&gt;
&lt;br /&gt;
If you feel it is the last step we make ..&lt;br /&gt;
.. let our eyes lock together&lt;br /&gt;
.. let the warm hug seize the air separting us&lt;br /&gt;
.. let the God feel jealous of us&lt;br /&gt;
&lt;br /&gt;
Let the tago continue ...&lt;br /&gt;
Let the eyes lock one another...&lt;br /&gt;
Let the time ends ...</content><link rel='replies' type='application/atom+xml' href='http://ascendant76.blogspot.com/feeds/8877645172659481978/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6999217373541258402/8877645172659481978' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/8877645172659481978'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/8877645172659481978'/><link rel='alternate' type='text/html' href='http://ascendant76.blogspot.com/2008/10/when-two-in-tango.html' title='When two in Tango ...'/><author><name>Ramesh</name><uri>http://www.blogger.com/profile/07716715053929751798</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6999217373541258402.post-7138616533572690918</id><published>2008-10-19T16:33:00.002-05:00</published><updated>2008-10-19T16:43:06.089-05:00</updated><title type='text'>Appropriate English Usage 1</title><content type='html'>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhwsJESfG3ecmxhRL6eu3nSjJmm6k4ENZZ9UwZyEV8ZHWn0mdvA5IPPIOErv81aD29iAuCtnJsuq9ADpsmsgMcjXFUgwi8bLYNOx9K4vLNpHZgTLETp4SMAa0uofbhf37hIW489FTECfw8t/s1600-h/Jim+says,+Thank+you+%21.PNG&quot; imageanchor=&quot;1&quot; linkindex=&quot;13&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJ0WDnJHq0TJGkdZmTr2YAJ_vfArfplbL3PCHS6SLQeutDPlt6RkkD3y314TLXTLENKGnDYAEQOFAmMIgm0pkwCh6ux-M0ocjCenneVC0jKZXZlsgdJ83uSG-CLfzdSHhzAiTy5WrdhzWE/s400-r/Jim+says,+Thank+you+%21.PNG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;When some one acknowledges me by saying &quot;Thank you!&quot;,&amp;nbsp; my immediate reply used to be &quot;No problem!&quot;. Today I learned that it is &lt;span class=&quot;body&quot;&gt;not traditional and sounds less polite.&lt;br /&gt;
&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://ascendant76.blogspot.com/feeds/7138616533572690918/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6999217373541258402/7138616533572690918' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/7138616533572690918'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/7138616533572690918'/><link rel='alternate' type='text/html' href='http://ascendant76.blogspot.com/2008/10/appropriate-english-usage-1.html' title='Appropriate English Usage 1'/><author><name>Ramesh</name><uri>http://www.blogger.com/profile/07716715053929751798</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgJ0WDnJHq0TJGkdZmTr2YAJ_vfArfplbL3PCHS6SLQeutDPlt6RkkD3y314TLXTLENKGnDYAEQOFAmMIgm0pkwCh6ux-M0ocjCenneVC0jKZXZlsgdJ83uSG-CLfzdSHhzAiTy5WrdhzWE/s72-c-r/Jim+says,+Thank+you+%21.PNG" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6999217373541258402.post-3201978354393119961</id><published>2008-10-14T08:38:00.001-05:00</published><updated>2010-05-19T20:29:46.177-05:00</updated><title type='text'>Yahoo Query Language (YQL)</title><content type='html'>You may be interested of this Yahoo Query Language (&lt;a href=&quot;http://developer.yahoo.com/yql/&quot;&gt;YQL&lt;/a&gt;)&amp;nbsp; . This is really intereting as they are not exposing their database tables but enabling their domain model ( structured data) using SQL. Behind the scenes they can map to their tables or what ever it could be.&lt;br /&gt;&lt;br /&gt;Yahoo! makes a lot of structured data available to developers, primarily through its web services. These services require developers to locate the right URLs and documentation to access and query them which can result in a very fragmented experience. The YQL platform provides a single endpoint service that enables developers to query, filter and combine data across Yahoo! and beyond. YQL exposes a SQL-like SELECT syntax that that is both familiar to developers and expressive enough for getting the right data. Through the SHOW and DESC commands we enable developers to discover the available data sources and structure without opening another web browser.</content><link rel='replies' type='application/atom+xml' href='http://ascendant76.blogspot.com/feeds/3201978354393119961/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6999217373541258402/3201978354393119961' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/3201978354393119961'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/3201978354393119961'/><link rel='alternate' type='text/html' href='http://ascendant76.blogspot.com/2008/10/yahoo-query-language-yql_14.html' title='Yahoo Query Language (YQL)'/><author><name>Ramesh</name><uri>http://www.blogger.com/profile/07716715053929751798</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6999217373541258402.post-3729379760295792521</id><published>2008-10-14T08:38:00.000-05:00</published><updated>2008-10-14T08:39:55.864-05:00</updated><title type='text'>Yahoo Query Language (YQL)</title><content type='html'>You may be interested of this Yahoo Query Language (&lt;a href=&quot;http://developer.yahoo.com/yql/&quot;&gt;YQL&lt;/a&gt;)&amp;nbsp; . This is really intereting as they are not exposing their database tables but enabling their domain model ( structured data) using SQL. Behind the scenes they can map to their tables or what ever it could be.&lt;br /&gt;
&lt;br /&gt;
Yahoo! makes a lot of structured data available to developers, primarily through its web services. These services require developers to locate the right URLs and documentation to access and query them which can result in a very fragmented experience. The YQL platform provides a single endpoint service that enables developers to query, filter and combine data across Yahoo! and beyond. YQL exposes a SQL-like SELECT syntax that that is both familiar to developers and expressive enough for getting the right data. Through the SHOW and DESC commands we enable developers to discover the available data sources and structure without opening another web browser.</content><link rel='replies' type='application/atom+xml' href='http://ascendant76.blogspot.com/feeds/3729379760295792521/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6999217373541258402/3729379760295792521' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/3729379760295792521'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/3729379760295792521'/><link rel='alternate' type='text/html' href='http://ascendant76.blogspot.com/2008/10/yahoo-query-language-yql.html' title='Yahoo Query Language (YQL)'/><author><name>Ramesh</name><uri>http://www.blogger.com/profile/07716715053929751798</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6999217373541258402.post-5263426879229260408</id><published>2008-10-08T05:20:00.001-05:00</published><updated>2010-05-19T20:29:46.192-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Songs"/><title type='text'>No One Needs to Know by Shania Twain</title><content type='html'>&lt;object width=&quot;428&quot; height=&quot;380&quot; &gt; &lt;param name=&quot;allowScriptAccess&quot; value=&quot;always&quot; /&gt; &lt;param name=&quot;movie&quot; value=&quot;http://player.clevver.com/flash/clvembed.swf?vid=107945&quot; /&gt; &lt;embed src=&quot;http://player.clevver.com/flash/clvembed.swf?vid=107945&quot; width=&quot;428&quot; height=&quot;380&quot; allowScriptAccess=&quot;always&quot; type=&quot;application/x-shockwave-flash&quot; pluginspage=&quot;http://www.macromedia.com/go/getflashplayer&quot; &gt;&lt;/embed&gt;&lt;/object&gt;</content><link rel='replies' type='application/atom+xml' href='http://ascendant76.blogspot.com/feeds/5263426879229260408/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6999217373541258402/5263426879229260408' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/5263426879229260408'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/5263426879229260408'/><link rel='alternate' type='text/html' href='http://ascendant76.blogspot.com/2008/10/no-one-needs-to-know-by-shania-twain_08.html' title='No One Needs to Know by Shania Twain'/><author><name>Ramesh</name><uri>http://www.blogger.com/profile/07716715053929751798</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6999217373541258402.post-8907944794914638138</id><published>2008-10-08T05:20:00.000-05:00</published><updated>2008-10-18T22:54:35.717-05:00</updated><title type='text'>No One Needs to Know by Shania Twain</title><content type='html'>&lt;object width=&quot;428&quot; height=&quot;380&quot; &gt; &lt;param name=&quot;allowScriptAccess&quot; value=&quot;always&quot; /&gt; &lt;param name=&quot;movie&quot; value=&quot;http://player.clevver.com/flash/clvembed.swf?vid=107945&quot; /&gt; &lt;embed src=&quot;http://player.clevver.com/flash/clvembed.swf?vid=107945&quot; width=&quot;428&quot; height=&quot;380&quot; allowScriptAccess=&quot;always&quot; type=&quot;application/x-shockwave-flash&quot; pluginspage=&quot;http://www.macromedia.com/go/getflashplayer&quot; &gt;&lt;/embed&gt;&lt;/object&gt;</content><link rel='replies' type='application/atom+xml' href='http://ascendant76.blogspot.com/feeds/8907944794914638138/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6999217373541258402/8907944794914638138' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/8907944794914638138'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/8907944794914638138'/><link rel='alternate' type='text/html' href='http://ascendant76.blogspot.com/2008/10/no-one-needs-to-know-by-shania-twain.html' title='No One Needs to Know by Shania Twain'/><author><name>Ramesh</name><uri>http://www.blogger.com/profile/07716715053929751798</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-6999217373541258402.post-2254090169964366335</id><published>2008-10-07T05:50:00.001-05:00</published><updated>2010-05-19T20:29:46.202-05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Songs"/><title type='text'>OMG - I Will Kill Her</title><content type='html'>&lt;object width=&quot;425&quot; height=&quot;344&quot;&gt;&lt;param name=&quot;movie&quot; value=&quot;http://www.youtube.com/v/25AsfkriHQc&amp;hl=en&amp;fs=1&quot;&gt;&lt;/param&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot;&gt;&lt;/param&gt;&lt;embed src=&quot;http://www.youtube.com/v/25AsfkriHQc&amp;hl=en&amp;fs=1&quot; type=&quot;application/x-shockwave-flash&quot; allowfullscreen=&quot;true&quot; width=&quot;425&quot; height=&quot;344&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br /&gt;Is that Agony or Jealousy?</content><link rel='replies' type='application/atom+xml' href='http://ascendant76.blogspot.com/feeds/2254090169964366335/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/6999217373541258402/2254090169964366335' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/2254090169964366335'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/6999217373541258402/posts/default/2254090169964366335'/><link rel='alternate' type='text/html' href='http://ascendant76.blogspot.com/2008/10/omg-i-will-kill-her_07.html' title='OMG - I Will Kill Her'/><author><name>Ramesh</name><uri>http://www.blogger.com/profile/07716715053929751798</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>