<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Jaksa&#039;s Blog</title>
	<atom:link href="http://jaksa.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://jaksa.wordpress.com</link>
	<description>Programming, Software Engineering, Computer Science</description>
	<lastBuildDate>Thu, 03 Nov 2011 08:39:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='jaksa.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Jaksa&#039;s Blog</title>
		<link>http://jaksa.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://jaksa.wordpress.com/osd.xml" title="Jaksa&#039;s Blog" />
	<atom:link rel='hub' href='http://jaksa.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Google AppEngine Disappointment</title>
		<link>http://jaksa.wordpress.com/2009/06/22/google-appengine-disappointment/</link>
		<comments>http://jaksa.wordpress.com/2009/06/22/google-appengine-disappointment/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 12:58:29 +0000</pubDate>
		<dc:creator>jaksa</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AppEngine]]></category>
		<category><![CDATA[cloud computing]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[google app engine]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://jaksa.wordpress.com/?p=127</guid>
		<description><![CDATA[I was eager to try out Google&#8217;s AppEngine for Java, but I was soon disappointed to find out that the AppEngine is just a partial implementation of the Java APIs. The biggest problem with the missing APIs is that a lot of 3rd party software and libraries simply won&#8217;t work, which means that you loose [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jaksa.wordpress.com&amp;blog=2662408&amp;post=127&amp;subd=jaksa&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft" src="http://weblogs.java.net/blog/felipegaucho/archive/ae_gwt_java.png" alt="" width="300" height="200" />I was eager to try out Google&#8217;s <a href="http://code.google.com/appengine/docs/java/overview.html">AppEngine for Java</a>, but I was soon disappointed to find out that the AppEngine is just a <a href="http://code.google.com/appengine/docs/java/jrewhitelist.html">partial implementation</a> of the Java APIs. The biggest problem with the missing APIs is that a lot of 3rd party software and libraries simply won&#8217;t work, which means that you loose all the benefits of the mature Java ecosystem. Porting an existing Java application to AppEngine is simply out of question.</p>
<p>Another major problem is persistence. You cannot deploy your favourite DBMS on AppEngine, you have to use Google&#8217;s Datastore, which is not a common relational DB. Google makes available JDO and JPA interfaces to access the Datastore. However, these are also partial implementations which provide just a familiar syntax for a substantially different persistence mechanism and create a false sense of familiarity. This is particularly true when you try to refer to the JDO documentation for troubleshooting, but you discover that the semantics of Google&#8217;s JDO implementation are quite different. It would have been much better to stick with a proprietary persisntece API and not pretend to fill the gap to reach JDO or JPA semantics.</p>
<p>Another important missing feature are Threads, which makes AppEngine a poor choice for massive computations since paralellization of subtasks is not possible, at least until Google implement the AppEngine <a href="http://labs.google.com/papers/mapreduce-osdi04.pdf">Map/Reduce</a> service.</p>
<p>Lastly the overall sense of a finished product is not there. Things that one would expect to work, fail for some obscure reason. Error messages are often cryptic. Apart from an introductionary tutorial, documentation is quite scarce. I guess this is justifiable since AppEngine is still a beta release, but I would rather consider it a pre-alpha. I was expecting much more considerate choices from the smart Google engineers, who have amazed us many times so far.</p>
<p>There were a few things I did like about the app engine. For example, the Eclipse plugin  simplifies configuration, testing and deployment. Integration with other Google services are a promissing factor. All in all, Google AppEngine is an interesting toy, but compared to other solutions it&#8217;s remains just a toy. If you want to play with it, I suggest you should read <a href="http://www.pardontheinformation.com/2009/06/google-app-engine-for-java-3-tips-for.html">this</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jaksa.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jaksa.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jaksa.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jaksa.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jaksa.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jaksa.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jaksa.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jaksa.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jaksa.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jaksa.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jaksa.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jaksa.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jaksa.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jaksa.wordpress.com/127/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jaksa.wordpress.com&amp;blog=2662408&amp;post=127&amp;subd=jaksa&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jaksa.wordpress.com/2009/06/22/google-appengine-disappointment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">jaksa</media:title>
		</media:content>

		<media:content url="http://weblogs.java.net/blog/felipegaucho/archive/ae_gwt_java.png" medium="image" />
	</item>
		<item>
		<title>Parallel For Loop in C#: Part 2</title>
		<link>http://jaksa.wordpress.com/2009/05/08/parallel-for-loop-2/</link>
		<comments>http://jaksa.wordpress.com/2009/05/08/parallel-for-loop-2/#comments</comments>
		<pubDate>Fri, 08 May 2009 14:05:55 +0000</pubDate>
		<dc:creator>jaksa</dc:creator>
				<category><![CDATA[concurrency]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[currying]]></category>
		<category><![CDATA[parallel for]]></category>

		<guid isPermaLink="false">http://jaksa.wordpress.com/?p=119</guid>
		<description><![CDATA[In the previous part we have implemented a ThreadPoolExecutor that accepts tasks and executes them in parallel. In this part we will implement a parallel for loop. The parallel for needs to iterate over a collection of items and invoke the specified closure on every item. It has to perform these invocations in parallel and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jaksa.wordpress.com&amp;blog=2662408&amp;post=119&amp;subd=jaksa&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://jaksa.wordpress.com/2009/05/08/parallel-for-loop-1/">previous part </a> we have implemented a ThreadPoolExecutor that accepts tasks and executes them in parallel. In this part we will implement a parallel for loop.<br />
The parallel for needs to iterate over a collection of items and invoke the specified closure on every item. It has to perform these invocations in parallel and the next instruction should be executed only after all the elements have been processed. When used, it should look something like this:</p>
<pre>ForEach(distributionList, (destination) =&gt; {
   Message msg = new Message(destination.Address);
   msg.Content = Encrypt(Content, destination.PublicKey);
   msg.Send();
   Console.WriteLine("sent message to" + destination.Name);
}</pre>
<p>So the signature of the method should be:</p>
<pre>public static void ForEach(ICollection&lt;T&gt; collection, Action&lt;T&gt; action)</pre>
<p>We know that we need to use the ThreadPoolExecutor, enqueue all the actions and execute them and then wait for the executor to finish. But we have a mismatch. The ForEach() method takes a parameterized action, while the thread executor takes an action without any arguments. What we need to do is construct a set of parameterless actions by adding each element of the collection as the parameter of the parameterized action.</p>
<p>action(x) -&gt; action1, action2, action3&#8230; actionN</p>
<p>We can do it with one simple line of code:</p>
<p><code>Action parameterlessAction = (() =&gt; parameterizedAction.Invoke(element));</code></p>
<p>This was first invented in lambda calculus, and it was called a <strong>closure</strong>, because the function gets closed over the free parameter. Hence the (slightly misnamed) term closure that the rest of us programmers use. Most of the programmers would be more familiar with the term <strong>currying</strong> to define this operation.</p>
<p>Our code would take the following form:</p>
<pre>public static void ForEach(ICollection&lt;T&gt; collection, Action&lt;T&gt; action) {
            ThreadPoolExecutor executor = new ThreadPoolExecutor();

            foreach (T element in collection) {
                Action parameterlessAction = (() =&gt; parameterizedAction.Invoke(element));
                executor.AddTask(parameterlessAction);
            }

            executor.Finish();
   }

   executor.Finish();
}</pre>
<p>However, this code has a well hidden and nasty bug. The bug is caused by a certain property of closures, which makes them dangerous in the wrong hands. This is also one of the major reasons why many Java developers don&#8217;t want closures. Don&#8217;t you see it? Hmmm&#8230; Are you ready for using closures? <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
Closures require that their free variables have an extent at least as long as the lifetime of the closure itself.<br />
In our scenario we are referring to <code>element</code> inside the <code>parameterlessAction</code>, but the value of <code>element</code> changes with each iteration. So by the time we invoke the closure unexpected results can happen. Fortunately enough, in our case, Visual Studio was able to detect this situation through static analysis and shyly display a warning in the form of a blue squiggly underline. The solution is to assign element to a temporary variable, and the compiler will be smart enough to allocate that variable on the heap instead of the stack, so that the variable has a longer lifetime than the method invocation.<br />
So here&#8217;s the final version of the code:</p>
<pre>public static void ForEach(ICollection&lt;T&gt; collection, Action&lt;T&gt; action) {
            ThreadPoolExecutor executor = new ThreadPoolExecutor();

            foreach (T element in collection) {
                T parameter = element;
                Action parameterlessAction = (() =&gt; parameterizedAction.Invoke(parameter));
                executor.AddTask(parameterlessAction);
            }

            executor.Finish();
   }

   executor.Finish();
}</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jaksa.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jaksa.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jaksa.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jaksa.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jaksa.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jaksa.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jaksa.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jaksa.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jaksa.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jaksa.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jaksa.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jaksa.wordpress.com/119/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jaksa.wordpress.com/119/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jaksa.wordpress.com/119/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jaksa.wordpress.com&amp;blog=2662408&amp;post=119&amp;subd=jaksa&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jaksa.wordpress.com/2009/05/08/parallel-for-loop-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">jaksa</media:title>
		</media:content>
	</item>
		<item>
		<title>Parallel For Loop in C#: Part 1</title>
		<link>http://jaksa.wordpress.com/2009/05/08/parallel-for-loop-1/</link>
		<comments>http://jaksa.wordpress.com/2009/05/08/parallel-for-loop-1/#comments</comments>
		<pubDate>Fri, 08 May 2009 10:32:05 +0000</pubDate>
		<dc:creator>jaksa</dc:creator>
				<category><![CDATA[concurrency]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[parallel for]]></category>
		<category><![CDATA[ThreadPoolExecutor]]></category>

		<guid isPermaLink="false">http://jaksa.wordpress.com/?p=111</guid>
		<description><![CDATA[This is the first of the two posts that shows you how to build a parallel for loop in C#. In this part we will start building a rudimentary ThreadPoolExecutor that will be the core of the parallel execution. It will have has 2 methods: AddTask(Action a) &#8211; it queues up a task to be [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jaksa.wordpress.com&amp;blog=2662408&amp;post=111&amp;subd=jaksa&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is the first of the two posts that shows you how to build a parallel for loop in C#.</p>
<p>In this part we will start building a rudimentary ThreadPoolExecutor that will be the core of the parallel execution. It will have has 2 methods:</p>
<ul>
<li><code>AddTask(Action a)</code> &#8211; it queues up a task to be executed one of the threads in the pool.</li>
<li><code>Finish()</code> &#8211; signals the executor to stop accepting any other tasks and wait till all tasks are completed.</li>
</ul>
<p>The implementation is based on the BlockingQueue implemented in the <a href="http://jaksa.wordpress.com/2009/05/07/blocking-queue-in-c/">previous post</a>. It uses 10 worker threads that constanty get tasks from the queue and execute them. Since it is a blocking queue, the workers will block untill someone puts a task in the queue. The queue is thread safe, so every task will be processed only by one worker.</p>
<p>We will start by implementing the logic that queues up the tasks and the worker threads that execute the tasks, later we will thing about termination.</p>
<pre>    public class ThreadPoolExecutor {
        private readonly BlockingQueue tasks = new BlockingQueue();
        private readonly Thread[] workers;

        public ThreadPoolExecutor() {
            workers = new Thread[10];
            for (int i = 0; i &lt; workers.Length; i++) {
                workers[i] = new Thread(run);
                workers[i].Start();
            }
        }

        public void AddTask(Action task) {
            tasks.Enqueue(task);
        }

        private void run() {
             while (true) {
                 tasks.Get().Invoke();
             }
        }
    }
</pre>
<p>Now we need to add the termination condition. Let&#8217;s start by just not accepting new threads, and later we will think about waiting for the tasks to be processed. We will use a boolean that indicates whether the ThreadPoolExecutor is open or closed. If it is open, it can accept new tasks. Once the Finish() method gets called, the ThreadPoolExecutor becomes closed and doesn&#8217;t accept any new task.</p>
<pre>
public class ThreadPoolExecutor {
        private bool open = true; // open to accept new tasks
        private readonly BlockingQueue tasks = new BlockingQueue();
        private readonly Thread[] workers;

        ...

        public void AddTask(Action task) {
             if (open) {
                 tasks.Enqueue(task);
             } else {
                 throw new Exception("Queue is closed");
             }
        }

        public void Finish() {
             open = false;
        }

        private void run() {
             while (true) {
                 tasks.Get().Invoke();
             }
        }
    }
</pre>
<p>The problem with this implementation is that the Finish() method doesn&#8217;t wait for all tasks to be processed, but returns immediately. The tasks to be processed include the ones in the queue along with the ones being currently processed by the workers. We will introduce a counter to keep track of this number. The Finish() method will block until that counter reaches 0. For blocking we will use <a href="http://msdn.microsoft.com/en-us/library/system.threading.monitor.aspx">System.Threading.Monitor</a> and we will synchronize on &#8220;this&#8221;. Moreover, once all the tasks have been processed, we&#8217;ll interrupt all the worker threads that are blocked on the queue.</p>
<pre>using System;
using System.Collections.Generic;
using System.Threading;

namespace Concurrent {
    public class ThreadPoolExecutor {
        private bool open = true; // open to accept new tasks
        private readonly BlockingQueue tasks = new BlockingQueue();
        private readonly Thread[] workers;
        private int pendingTasks; // the number of tasks in the queue

        public ThreadPoolExecutor() {
            workers = new Thread[10];
            for (int i = 0; i &lt; workers.Length; i++) {
                workers[i] = new Thread(run);
                workers[i].Start();
            }
        }

        public void AddTask(Action task) {
            lock (this) {
                if (open) {
                    tasks.Enqueue(task);
                    pendingTasks++;
                } else {
                    throw new Exception("Queue is closed");
                }
            }
        }

        public void Finish() {
            lock(this) {
                open = false;

                // wait till all tasks are finished
                while (pendingTasks &gt; 0) {
                    Monitor.Wait(this);
                }

                // kill the workers waiting for more tasks
                foreach (Thread worker in workers) {
                    worker.Interrupt();
                }
            }
        }

        private void run() {
            try {
                while (true) {
                    lock (this) {
                        if (!open &amp;&amp; pendingTasks == 0)
                            break; // if we're not open anymore and there are no tasks in queue, break the loop
                    }

                    // don't block other threads while processing the task
                    tasks.Get().Invoke();

                    lock (this) {
                        pendingTasks--;
                        Monitor.PulseAll(this); // notify waiting threads that the tasks are all processed
                    }
                }
            } catch(ThreadInterruptedException) {}
        }
    }
}</pre>
<p>In order to parallelize your program, divide it in concurrently executable tasks and wrap every task in an Action object. For convenience you can also use closures. Add all the tasks to the ThreadPoolExecutor and wait for them to finish by calling the Finish() method.</p>
<p>In the next post we will be adding some syntactic sugar to implement a parallel for construct.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jaksa.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jaksa.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jaksa.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jaksa.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jaksa.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jaksa.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jaksa.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jaksa.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jaksa.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jaksa.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jaksa.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jaksa.wordpress.com/111/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jaksa.wordpress.com/111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jaksa.wordpress.com/111/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jaksa.wordpress.com&amp;blog=2662408&amp;post=111&amp;subd=jaksa&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jaksa.wordpress.com/2009/05/08/parallel-for-loop-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">jaksa</media:title>
		</media:content>
	</item>
		<item>
		<title>Time Tracking Tools</title>
		<link>http://jaksa.wordpress.com/2009/05/01/time-tracking-tools/</link>
		<comments>http://jaksa.wordpress.com/2009/05/01/time-tracking-tools/#comments</comments>
		<pubDate>Fri, 01 May 2009 10:45:07 +0000</pubDate>
		<dc:creator>jaksa</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[productivity]]></category>
		<category><![CDATA[time tracking]]></category>

		<guid isPermaLink="false">http://jaksa.wordpress.com/?p=91</guid>
		<description><![CDATA[Have you ever asked yourself how much time during the day in the office do you spend looking at funny youtube videos, reading emails, taking coffee or doing some boring administrative task that keeps you from getting your work finished? I&#8217;ve actually been asking myself that for what it feels my entire life. I always [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jaksa.wordpress.com&amp;blog=2662408&amp;post=91&amp;subd=jaksa&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Have you ever asked yourself how much time during the day in the office do you spend looking at funny youtube videos, reading emails, taking coffee or doing some boring administrative task that keeps you from getting your work finished? I&#8217;ve actually been asking myself that for what it feels my entire life. I always tought that a well organized and productive 3 hours could be more valuable than a whole day of distractions. I always wanted to measure that productivity in detail and see what things I&#8217;m spending my time on and how much. Now I finally can.</p>
<p>A few months ago I found out on <a href="http://lifehacker.com/">Lifehacker</a> about this software called <a href="http://www.rescuetime.com/">RescueTime</a> that tracks how you spend your time on a computer. It&#8217;s free and you run it in the background. It categrorizes your activities in Communication, Development Tools, Reference/Search etc. If you are using a browser it is smart enough to distinguish which sites are you accessing, so reading The Server Side and Failblog.org are considered two different activities.</p>
<p> </p>
<p><img class="aligncenter size-medium wp-image-93" title="rescuetime-screenshot" src="http://jaksa.files.wordpress.com/2009/05/rescuetime-screenshot.png?w=300&#038;h=261" alt="rescuetime-screenshot" width="300" height="261" /></p>
<p>One downside of RescueTime is that it posts your usage statistics to their servers, so if you don&#8217;t want anyone to see how much time you spent&#8230; watching porn in the office, you might choose to disable RescueTime for a couple of hours. On the other side, your usage statistics are accessible only by you, so your boss can&#8217;t detract from your salary every 5 mins you spent checking your private mail.</p>
<p>Another downside is that it doesn&#8217;t show you when exactly did you start working with a particular program and when were you AFK. So you can&#8217;t distinguish between a meeting and a lunch break.</p>
<div>
<div>Both of these problems are solved by <a href="http://www.manictime.com/">ManicTime</a>. Manic time works only locally and gives you information on what did you do at an exact moment of the day: </div>
<div><img class="aligncenter size-medium wp-image-94" title="manictime-screenshot" src="http://jaksa.files.wordpress.com/2009/05/manictime-screenshot.png?w=300&#038;h=219" alt="manictime-screenshot" width="300" height="219" /></div>
<div>
<p> </p>
<p> </p>
<p>The only downside of ManicTime is that it is too fine grained so you don&#8217;t get the quick overview that you can get in RescueTime. Currently I&#8217;m using both of them until I decide which one is better or one of them implements the functionalities of the other.</p></div>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jaksa.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jaksa.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jaksa.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jaksa.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jaksa.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jaksa.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jaksa.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jaksa.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jaksa.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jaksa.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jaksa.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jaksa.wordpress.com/91/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jaksa.wordpress.com/91/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jaksa.wordpress.com/91/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jaksa.wordpress.com&amp;blog=2662408&amp;post=91&amp;subd=jaksa&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jaksa.wordpress.com/2009/05/01/time-tracking-tools/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">jaksa</media:title>
		</media:content>

		<media:content url="http://jaksa.files.wordpress.com/2009/05/rescuetime-screenshot.png?w=300" medium="image">
			<media:title type="html">rescuetime-screenshot</media:title>
		</media:content>

		<media:content url="http://jaksa.files.wordpress.com/2009/05/manictime-screenshot.png?w=300" medium="image">
			<media:title type="html">manictime-screenshot</media:title>
		</media:content>
	</item>
		<item>
		<title>Active and Passive Replication in Distributed Systems</title>
		<link>http://jaksa.wordpress.com/2009/05/01/active-and-passive-replication-in-distributed-systems/</link>
		<comments>http://jaksa.wordpress.com/2009/05/01/active-and-passive-replication-in-distributed-systems/#comments</comments>
		<pubDate>Fri, 01 May 2009 10:35:47 +0000</pubDate>
		<dc:creator>jaksa</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[distributed systems]]></category>
		<category><![CDATA[fault tolerance]]></category>
		<category><![CDATA[primary/backup]]></category>
		<category><![CDATA[replication]]></category>
		<category><![CDATA[state machine]]></category>

		<guid isPermaLink="false">http://jaksa.wordpress.com/?p=84</guid>
		<description><![CDATA[In the distributed systems research area replication is mainly used to provide fault tolerance. The entity being replicated is a process. Two replication strategies have been used in distributed systems: Active and Passive replication. In active replication each client request is processed by all the servers. Active Replication was first introduced by Leslie Lamport under [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jaksa.wordpress.com&amp;blog=2662408&amp;post=84&amp;subd=jaksa&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In the distributed systems research area replication is mainly used to provide fault tolerance. The entity being replicated is a process. Two replication strategies have been used in distributed systems: <strong>Active </strong>and <strong>Passive </strong>replication.</p>
<p><img class="aligncenter size-full wp-image-85" title="active-passive-replication" src="http://jaksa.files.wordpress.com/2009/02/active-passive-replication.gif?w=557&#038;h=253" alt="active-passive-replication" width="557" height="253" /></p>
<p>In <strong>active replication</strong> each client request is processed by all the servers. Active Replication was first introduced by <a href="http://research.microsoft.com/en-us/um/people/lamport/">Leslie Lamport</a> under the name <strong>state machine replication</strong>. This requires that the process hosted by the servers is <a href="http://en.wikipedia.org/wiki/Deterministic_algorithm">deterministic</a>. <strong>Deterministic </strong>means that, given the same initial state and a request sequence, all processes will produce the same response sequence and end up in the same final state. In order to make all the servers receive the same sequence of operations, an <a href="http://doi.acm.org/10.1145/1041680.1041682">atomic broadcast</a> protocol must be used. An atomic broadcast protocol guarantees that either all the servers receive a message or none, plus that they all receive messages in the same order. The big disadvantage for active replication is that in practice most of the real world servers are non‐deterministic. Still active replication is the preferable choice when dealing with real time systems that require quick response even under the presence of faults or with systems that must handle <a href="http://research.microsoft.com/en-us/um/people/lamport/pubs/byz.pdf">byzantine faults</a>.  </p>
<p>In <strong>passive replication </strong>there is only one server (called primary) that processes client requests. After processing a request, the primary server updates the state on the other (backup) servers and sends back the response to the client. If the primary server fails, one of the backup servers takes its place. Passive replication may be used even for non‐deterministic processes. The disadvantage of passive replication compared to active is that in case of failure the response is delayed.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jaksa.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jaksa.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jaksa.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jaksa.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jaksa.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jaksa.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jaksa.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jaksa.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jaksa.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jaksa.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jaksa.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jaksa.wordpress.com/84/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jaksa.wordpress.com/84/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jaksa.wordpress.com/84/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jaksa.wordpress.com&amp;blog=2662408&amp;post=84&amp;subd=jaksa&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jaksa.wordpress.com/2009/05/01/active-and-passive-replication-in-distributed-systems/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">jaksa</media:title>
		</media:content>

		<media:content url="http://jaksa.files.wordpress.com/2009/02/active-passive-replication.gif" medium="image">
			<media:title type="html">active-passive-replication</media:title>
		</media:content>
	</item>
		<item>
		<title>Webapp stacks comparison update 1</title>
		<link>http://jaksa.wordpress.com/2008/12/01/webapp-stacks-comparison-update-1/</link>
		<comments>http://jaksa.wordpress.com/2008/12/01/webapp-stacks-comparison-update-1/#comments</comments>
		<pubDate>Mon, 01 Dec 2008 11:26:03 +0000</pubDate>
		<dc:creator>jaksa</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jaksa.wordpress.com/?p=73</guid>
		<description><![CDATA[We have received measurements from one of the remaining level 3 applications. It&#8217;s Immo&#8216;s implementation based on Ruby + Ramaze + MySQL. Here are the updated results: As soon as we confirm the measurements and get the numbers for the ASP implementation we will publish the updated point chart.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jaksa.wordpress.com&amp;blog=2662408&amp;post=73&amp;subd=jaksa&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>We have received measurements from one of the remaining level 3 applications. It&#8217;s <a href="http://aspsp.blogspot.com/">Immo</a>&#8216;s implementation based on <span lang="EN-GB"><a href="http://www.ruby-lang.org/en/">Ruby</a><span> </span>+<span> </span><a href="http://ramaze.net/">Ramaze</a><span> </span>+<span> </span><a href="http://www.mysql.com/">MySQL</a></span>. Here are the updated results:</p>
<p><a href="http://jaksa.files.wordpress.com/2008/12/performance1.png"><img class="alignnone size-full wp-image-74" title="performance1" src="http://jaksa.files.wordpress.com/2008/12/performance1.png?w=580&#038;h=334" alt="performance1" width="580" height="334" /></a></p>
<p>As soon as we confirm the measurements and get the numbers for the ASP implementation we will publish the updated point chart.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jaksa.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jaksa.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jaksa.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jaksa.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jaksa.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jaksa.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jaksa.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jaksa.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jaksa.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jaksa.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jaksa.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jaksa.wordpress.com/73/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jaksa.wordpress.com/73/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jaksa.wordpress.com/73/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jaksa.wordpress.com&amp;blog=2662408&amp;post=73&amp;subd=jaksa&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jaksa.wordpress.com/2008/12/01/webapp-stacks-comparison-update-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">jaksa</media:title>
		</media:content>

		<media:content url="http://jaksa.files.wordpress.com/2008/12/performance1.png" medium="image">
			<media:title type="html">performance1</media:title>
		</media:content>
	</item>
		<item>
		<title>Webapp stacks comparison</title>
		<link>http://jaksa.wordpress.com/2008/11/30/webapp-stacks/</link>
		<comments>http://jaksa.wordpress.com/2008/11/30/webapp-stacks/#comments</comments>
		<pubDate>Sun, 30 Nov 2008 14:34:27 +0000</pubDate>
		<dc:creator>jaksa</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[asp]]></category>
		<category><![CDATA[benchmark]]></category>
		<category><![CDATA[couchDB]]></category>
		<category><![CDATA[ejb]]></category>
		<category><![CDATA[ejb3]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[hybernate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jboss]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[ramaze]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[vb]]></category>
		<category><![CDATA[web application]]></category>

		<guid isPermaLink="false">http://jaksa.wordpress.com/?p=46</guid>
		<description><![CDATA[Ever wondered what is the best technology stack for building web applications? I&#8217;m sure you have. Last week my company went on a workshop, during which we tried to answer that question. I wrote a requirements specification for a simple web application. Everyone had to choose the technology stack they&#8217;re most familiar with and implement [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jaksa.wordpress.com&amp;blog=2662408&amp;post=46&amp;subd=jaksa&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Ever wondered what is the best technology stack for building web applications? I&#8217;m sure you have.</p>
<p>Last week my company went on a workshop, during which we tried to answer that question. I wrote a requirements specification for a simple web application. Everyone had to choose the technology stack they&#8217;re most familiar with and implement the application. Everyone received a benchmark script for the application and at the end of the day we compared the performance results. The application was just enough complex to require at least a day to implement. In the end we multiplied the performance by the level of functionality implemented.</p>
<p><strong>The Application</strong></p>
<p>The webapp I chose is the same one for the <a href="http://code.google.com/p/cloudspeed/">couldspeed project</a>. It is based on the model of social networking applicatins. <span>Once registered and logged in, users can add other users as </span><span>friends and submit posts. <span>The homepage will display the 20 most recent posts from all </span><span>user’s friends including the user himself.</span></span></p>
<p><span><span>There are 2 entities each with 2 attributes:</span></span></p>
<ul>
<li>users(email, password)</li>
<li>post(date, content)</li>
</ul>
<p><span><span>and there are 2 relationships</span></span></p>
<ul>
<li>users are friends with other users (n to n)</li>
<li>each post is written by a user (1 to n)</li>
</ul>
<p>there are 2 web pages: login and home.</p>
<p>From the login page, users can register themselves or log in.</p>
<p>The home page displays the number of friends, the 20 most recent posts (for each post, the date, author and content) and allows users to add posts, friends or logout.</p>
<p>Information has to be persisted somehow.</p>
<p><strong>The Benchmark</strong></p>
<p>Each developer got a copy of <a href="http://mirror.public-internet.co.uk/ftp/apache/jakarta/jmeter/binaries/jakarta-jmeter-2.3.2.zip">JMeter</a> and 3 scripts. The 3 scripts excercized 3 different levels of functionality:</p>
<ol>
<li>Register Users &#8211; registered a number of users (100)</li>
<li>Make Friends &#8211; created around 10 friends per user</li>
<li>Post Stuff &#8211; posted messages</li>
</ol>
<p>For the first level of functionality, the developer had to implement the functionality behind the Register button. Registered users had to be persisted. This level had a multiplier of 1.</p>
<p>For the second level of functionality, developers had to implement logging in, keeping session information, and the friendship relationship. The homepage should have shown the number of friends. This level had a multiplier of 3.</p>
<p>For the third level of functionality, developers had to implement everything: posting messages and showing the messages from all friends. Since this functionality included a complex query that ate a lot of processing power, the multiplier was 50.</p>
<p><strong>The Contenders</strong></p>
<p><a href="http://21ccw.blogspot.com/">Ben</a>: <a href="http://www.erlang.org/">Erlang</a> + <a href="http://incubator.apache.org/couchdb/">CouchDB</a></p>
<p>Team 2: Java + <a href="http://www.springframework.org/">Spring</a> + <a href="http://www.hibernate.org/">Hibernate</a> + <a href="http://www.postgresql.org/">Postgres</a></p>
<p>Team 3: Java + <a href="http://www.springframework.org/">Spring</a> + <a href="http://www.hibernate.org/">Hibernate</a></p>
<p>Team 4: <a href="http://www.rubyonrails.org/">Ruby On Rails</a> + <a href="http://www.mysql.com/">MySQL</a></p>
<p>Team 5: <a href="http://www.asp.net/">ASP</a> with <a href="http://msdn.microsoft.com/en-us/vbasic/default.aspx">Visual Basic</a> + <a href="http://www.microsoft.com/sqlserver/2008/en/us/default.aspx">SQLServer</a></p>
<p><a href="http://aspsp.blogspot.com/">Immo</a>: <a href="http://www.ruby-lang.org/en/">Ruby</a> + <a href="http://ramaze.net/">Ramaze</a> + <a href="http://www.mysql.com/">MySQL</a></p>
<p><strong>The Results</strong></p>
<p><img class="alignnone size-full wp-image-54" title="performance" src="http://jaksa.files.wordpress.com/2008/11/performance.png?w=588&#038;h=342" alt="performance" width="588" height="342" /></p>
<p>Only three of the contenders implemented level 3 of functionality, but we were able to measure the performance only for the erlang implementation. The other 2 that made it to the 3rd level were ASP+SQLServer and Ramaze. We will soon publish the results of the two missing level 3 implementations. </p>
<p><img class="alignnone size-full wp-image-55" title="total-points" src="http://jaksa.files.wordpress.com/2008/11/total-points.png?w=588&#038;h=342" alt="total-points" width="588" height="342" /></p>
<p>The winner was Ben with 1111.1 points. Erlang + CouchDB proved to be quite fast and productive in the right hands.</p>
<p><strong>Conclusions</strong></p>
<p>Allthough just one day of development is not nearly enough to measure productivity, we noticed that choosing a particular language that promised a high level of productivity wasn&#8217;t as important as choosing the language you&#8217;re most familiar with.</p>
<p>Alltough Level 1 of functionality does not provide a meaningful scenario to measure performance, we can see that most of the implementations had the same performance, including Ruby, which has been measured to be <a href="http://shootout.alioth.debian.org/u32q/benchmark.php?test=all&amp;lang=all">100 times slower</a> than C++, but in our scenario it actually showed the best performance.</p>
<p>Allthough we must point out that both ruby implementations and the ASP implementation showed instability. Errors ranging from 15% to 30% were reported during the benchmarks.</p>
<p>As a comparison, I have done some java implementations before the competition. They all provide level 3 functionality. They all use Servlets in the web tier, but the DAO tier changes: we have JDBC with MySQL, EJB3 with MySQL and pure in memory.</p>
<p>Compared to the Erlang implementation wich serves 15 req/s I have similar results for the JDBC (15 req/s). A &#8220;heavyweigth&#8221; stack such as EJBs actually manages to produce 38 req/s, but by far the fastest solution is keeping everything in memory that brings us to an impressive 2000 req/s. Obviously the in memory solution cannot be compared to the others because it doesn&#8217;t satisfy the persistence requirement. But can still be used as a comparison to see how much performance do we loose on the persistence layer.</p>
<p><strong>Future Work</strong></p>
<p>We have had requests from other people that were not taking part of the competition to submit their implementations. Since we have no way to measure productivity, all submissions now require level 3 of functionality. All implementations will be published on the <a href="http://code.google.com/p/cloudspeed/">cloudspeed website</a>. If you wish to contribute by improving an implementation or submitting your own just send me an email or leave a comment here.</p>
<p>Currently the benchmark is adapted to quickly give a very aproximative measure. 100 users is not a realistic number even for a small website. Our next goal is to create a benchmark that will register up to millions of users, have an average of 100 friends per user and post several million messages.</p>
<p>The final goal is to port these implementations to the cloud and measure their scalability. We don&#8217;t only want to see the performance on a single machine, but also how will the performance be when we reach the limits of a single server. Ideally we want to try to run these applications on clusters as big as we can get.</p>
<p>Stay tuned for updates&#8230;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jaksa.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jaksa.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jaksa.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jaksa.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jaksa.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jaksa.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jaksa.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jaksa.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jaksa.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jaksa.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jaksa.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jaksa.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jaksa.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jaksa.wordpress.com/46/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jaksa.wordpress.com&amp;blog=2662408&amp;post=46&amp;subd=jaksa&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jaksa.wordpress.com/2008/11/30/webapp-stacks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">jaksa</media:title>
		</media:content>

		<media:content url="http://jaksa.files.wordpress.com/2008/11/performance.png" medium="image">
			<media:title type="html">performance</media:title>
		</media:content>

		<media:content url="http://jaksa.files.wordpress.com/2008/11/total-points.png" medium="image">
			<media:title type="html">total-points</media:title>
		</media:content>
	</item>
		<item>
		<title>Tuning the EJB3 Implementation</title>
		<link>http://jaksa.wordpress.com/2008/10/09/tuning-the-ejb3-implementation/</link>
		<comments>http://jaksa.wordpress.com/2008/10/09/tuning-the-ejb3-implementation/#comments</comments>
		<pubDate>Thu, 09 Oct 2008 15:53:45 +0000</pubDate>
		<dc:creator>jaksa</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ejb3]]></category>
		<category><![CDATA[jpa]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[tuning]]></category>
		<category><![CDATA[eager fetching]]></category>
		<category><![CDATA[lazy fetching]]></category>
		<category><![CDATA[transactions]]></category>
		<category><![CDATA[hybernate]]></category>
		<category><![CDATA[problems]]></category>

		<guid isPermaLink="false">http://jaksa.wordpress.com/?p=38</guid>
		<description><![CDATA[During my evaluation of highly scalable technologies I incurred in some performance problems with the EJB3 implementation. It seems to me that it is a quite common problem Yesterday I was getting very dissappointing results from the benchmarks of the EJB3 implementation of the sample application. I have discovered some nice hints for optimizing Hybernate and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jaksa.wordpress.com&amp;blog=2662408&amp;post=38&amp;subd=jaksa&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>During my <a href="http://code.google.com/p/cloudspeed/">evaluation</a> of highly scalable technologies I incurred in some performance problems with the EJB3 implementation. It seems to me that it is a quite common problem</p>
<p><span id="more-38"></span></p>
<p>Yesterday I was getting <a href="http://jaksa.wordpress.com/2008/10/08/measuring-the-speed-of-clouds/">very dissappointing results </a>from the benchmarks of the EJB3 implementation of the <a href="http://code.google.com/p/cloudspeed/wiki/ApplicationSpecification">sample application</a>. I have discovered some nice <a href="http://blogs.jboss.com/blog/acoliver/2006/01/23/Hibernate_EJB3_Tuning.txt">hints</a> for optimizing Hybernate and EJB3 applications and a very good <a href="http://www.infoq.com/presentations/ejb-3">video on EJB3</a>. This hepled me understand why my EJBs were not updated properly, but most of all why I was getting such poor performance.</p>
<p>The performance problem was due to the use of eager fetching on a many to many relationship. The relationship in question was friendship between users. This is a many to many relationship with itself. This means eager fetching is propagated transitively, so each time I loaded a user, all the friends were loaded as well, and also the friends of the friends and the friends of&#8230; basically the whole table.</p>
<p>By default many to many relationships use lazy fetching, but I changed this to eager when I wrote the application. The reason I had eager fetching on this relationship is that when I tried to get the number of friends a user had, my appserver was complaining that it couldn&#8217;t be done with lazy fetching. My error was in performing this through user.getFriends().size(). What I&#8217;m doing now instead is asking the database to give me the number of friends by using a query like &#8220;SELECT count(f) FROM user u JOIN u.friends f WHERE&#8230;&#8221;.</p>
<p>After that I changed some transaction attributes so that read only operations do not require transactions. Given the semantic of the methods I don&#8217;t require a strong level on consistency, so it&#8217;s relatively safe to do this. This minimizes delays due to contention and increases the paralellizability (try pronouncing this aloud) of the applications.</p>
<p>The result was that now my application runs 10 times faster. Now I&#8217;m getting 79 requests per second if I run the server on an EC2 instance and the client on my machine. This is just a little slower than the 100 requests per second obtained by the pojo in memoty solution, which means that EJB3 is pretty fast. I still have to test this without the communication bottleneck to be sure of the actual performance.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jaksa.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jaksa.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jaksa.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jaksa.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jaksa.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jaksa.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jaksa.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jaksa.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jaksa.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jaksa.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jaksa.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jaksa.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jaksa.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jaksa.wordpress.com/38/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jaksa.wordpress.com&amp;blog=2662408&amp;post=38&amp;subd=jaksa&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jaksa.wordpress.com/2008/10/09/tuning-the-ejb3-implementation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">jaksa</media:title>
		</media:content>
	</item>
		<item>
		<title>Measuring the Speed of Clouds</title>
		<link>http://jaksa.wordpress.com/2008/10/08/measuring-the-speed-of-clouds/</link>
		<comments>http://jaksa.wordpress.com/2008/10/08/measuring-the-speed-of-clouds/#comments</comments>
		<pubDate>Wed, 08 Oct 2008 16:39:24 +0000</pubDate>
		<dc:creator>jaksa</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[cloud computing]]></category>
		<category><![CDATA[benchmark]]></category>
		<category><![CDATA[ec2]]></category>
		<category><![CDATA[ejb3]]></category>
		<category><![CDATA[jee]]></category>
		<category><![CDATA[servlets]]></category>
		<category><![CDATA[j2ee]]></category>
		<category><![CDATA[jboss]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[amazon]]></category>
		<category><![CDATA[jpa]]></category>
		<category><![CDATA[scalability]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://jaksa.wordpress.com/?p=36</guid>
		<description><![CDATA[I have started a project for benchmarking highly scalable technologies. My plan is to use cloud computing platforms and implement the same application using different stacks of technologies. The aspect I want to evaluate is performance when the number of nodes grows a lot. Application specification The application I&#8217;m using for benchmarking is a simplified [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jaksa.wordpress.com&amp;blog=2662408&amp;post=36&amp;subd=jaksa&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have started a project for benchmarking highly scalable technologies. My plan is to use cloud computing platforms and implement the same application using different stacks of technologies. The aspect I want to evaluate is performance when the number of nodes grows a lot.<span id="more-36"></span></p>
<p><strong>Application specification</strong><br />
The application I&#8217;m using for benchmarking is a simplified model of Facebook.</p>
<p><strong>Implementations</strong><br />
Currently I have 2 implementations:<br />
Servlets + EJB3<br />
Servlets + in memory Java</p>
<p><strong>Benchmark</strong><br />
As benchmarking client I&#8217;m using JMeter.<br />
The benchmark looks like this: a number of threads will start iterating over the following sequence</p>
<ol>
<li>load first page (redirection)</li>
<li>load login page</li>
<li>login</li>
<li>load main page</li>
<li>add post</li>
<li>add friend</li>
<li>logout</li>
</ol>
<p>There are no pauses so in theory, this should test the capacity of the server.</p></div>
<p>Ideally there would be a very large number of threads (users) performing requests occasionally.</p></div>
<p><strong>Configurations tested</strong><br />
I have tested everything <strong>on my machine </strong>before deploying on EC2. I have used JBoss and MySql for the EJB solution.<br />
The performance I obtained was around 10 requests per second, which is dissapointing. Probably I have a bottleneck somewhere and need to configure things properly.<br />
The In Memory Java implementation achieves more than 1000 requests per second, which is 100 times faster than using EJBs. However it occasionally throws ConcurrentModificationExceptions because I&#8217;m not synchronozing properly (fixing this will lower performance).</p>
<p>Then I deployed everything on <strong>one EC2 instance</strong>.<br />
When I run the client on the outside I get around 100 requests per second, which may be due to my internet connection speed. But when I execute the client on another EC2 instance, I expect no communication bottlenecks. Instead I get 3 requests per second. Either there is a huge traffic jam on the EC2 internal network or I have misconfigured the benchmark client.<br />
Running the benchmark on the EJB3 implementation shows a performance of 7 requests per second.</p>
<p>Next thing I need to do is some EJB3 tuning. I have the feeling I have not set up transactions correctly.<br />
If you have any suggestion or wish your technology stack to be evaluated or perhaps want to contribute, please leave a comment.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jaksa.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jaksa.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jaksa.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jaksa.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jaksa.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jaksa.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jaksa.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jaksa.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jaksa.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jaksa.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jaksa.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jaksa.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jaksa.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jaksa.wordpress.com/36/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jaksa.wordpress.com&amp;blog=2662408&amp;post=36&amp;subd=jaksa&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jaksa.wordpress.com/2008/10/08/measuring-the-speed-of-clouds/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">jaksa</media:title>
		</media:content>
	</item>
		<item>
		<title>foreach in Java</title>
		<link>http://jaksa.wordpress.com/2008/09/19/foreach-in-java/</link>
		<comments>http://jaksa.wordpress.com/2008/09/19/foreach-in-java/#comments</comments>
		<pubDate>Fri, 19 Sep 2008 13:23:31 +0000</pubDate>
		<dc:creator>jaksa</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[closures]]></category>
		<category><![CDATA[collection]]></category>
		<category><![CDATA[dynamic proxy]]></category>
		<category><![CDATA[for]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[loop]]></category>
		<category><![CDATA[reflection]]></category>

		<guid isPermaLink="false">http://jaksa.wordpress.com/?p=13</guid>
		<description><![CDATA[Here&#8217;s a cool but unfortunately useless example of java generics, combined with reflection, combined with dynamic proxies. import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.ArrayList; import java.util.List; import sun.reflect.generics.reflectiveObjects.NotImplementedException; import sun.reflect.generics.tree.ReturnType; public class Foreach { @SuppressWarnings("unchecked") public static &#60;T&#62; T foreach(T... elements) { ApplyOnArrray&#60;T&#62; handler = new ApplyOnArrray&#60;T&#62;(elements); return (T) Proxy.newProxyInstance(elements[0].getClass().getClassLoader(), new Class[] { elements[0].getClass().getInterfaces()[0] [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jaksa.wordpress.com&amp;blog=2662408&amp;post=13&amp;subd=jaksa&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a cool but unfortunately useless example of java generics, combined with reflection, combined with dynamic proxies.</p>
<p><span id="more-13"></span></p>
<pre><span><strong>import java.lang.reflect.InvocationHandler;</strong></span>
<span><strong>import java.lang.reflect.Method;</strong></span>
<span><strong>import java.lang.reflect.Proxy;</strong></span>
<span><strong>import java.util.ArrayList;</strong></span>
<span><strong>import java.util.List;</strong></span>

<span><strong>import sun.reflect.generics.reflectiveObjects.NotImplementedException;</strong></span>
<span><strong>import sun.reflect.generics.tree.ReturnType;</strong></span>

<strong>public</strong> <strong>class</strong> <span style="color:#2040a0;">Foreach</span> <span><strong>{</strong></span>
  @<span style="color:#2040a0;">SuppressWarnings</span><span><strong>(</strong></span><span style="color:#008000;">"unchecked"</span><span><strong>)</strong></span>
<strong>  public</strong> <strong>static</strong> <span>&lt;</span><span style="color:#2040a0;">T</span><span>&gt;</span> <span style="color:#2040a0;">T</span> <span style="color:#2040a0;">foreach</span><span><strong>(</strong></span><span style="color:#2040a0;">T</span>... <span style="color:#2040a0;">elements</span><span><strong>)</strong></span> <span><strong>{</strong></span>
<span style="color:#2040a0;">    ApplyOnArrray</span><span>&lt;</span><span style="color:#2040a0;">T</span><span>&gt;</span> <span style="color:#2040a0;">handler</span> <span>=</span> <strong>new</strong> <span style="color:#2040a0;">ApplyOnArrray</span><span>&lt;</span><span style="color:#2040a0;">T</span><span>&gt;</span><span><strong>(</strong></span><span style="color:#2040a0;">elements</span><span><strong>)</strong></span><span>;</span>
<strong>    return</strong> <span><strong>(</strong></span><span style="color:#2040a0;">T</span><span><strong>)</strong></span> <span style="color:#2040a0;">Proxy</span>.<span style="color:#2040a0;">newProxyInstance</span><span><strong>(</strong></span><span style="color:#2040a0;">elements</span><span><strong>[</strong></span><span style="color:#ff0000;">0</span><span><strong>]</strong></span>.<span style="color:#2040a0;">getClass</span><span><strong>(</strong></span><span><strong>)</strong></span>.<span style="color:#2040a0;">getClassLoader</span><span><strong>(</strong></span><span><strong>)</strong></span>,
<strong>      new</strong> <span style="color:#2040a0;">Class</span><span><strong>[</strong></span><span><strong>]</strong></span> <span><strong>{</strong></span> <span style="color:#2040a0;">elements</span><span><strong>[</strong></span><span style="color:#ff0000;">0</span><span><strong>]</strong></span>.<span style="color:#2040a0;">getClass</span><span><strong>(</strong></span><span><strong>)</strong></span>.<span style="color:#2040a0;">getInterfaces</span><span><strong>(</strong></span><span><strong>)</strong></span><span><strong>[</strong></span><span style="color:#ff0000;">0</span><span><strong>]</strong></span> <span><strong>}</strong></span>,
<span style="color:#2040a0;">      handler</span><span><strong>)</strong></span><span>;</span>
<span><strong>  }</strong></span>

<strong>  public</strong> <strong>static</strong> <span>&lt;</span><span style="color:#2040a0;">ReturnType</span><span>&gt;</span> <span style="color:#2040a0;">List</span><span>&lt;</span><span style="color:#2040a0;">ReturnType</span><span>&gt;</span> <span style="color:#2040a0;">get</span><span><strong>(</strong></span><span style="color:#2040a0;">ReturnType</span> <span style="color:#2040a0;">returnValue</span><span><strong>)</strong></span> <span><strong>{</strong></span>
<span style="color:#2040a0;">    Collector</span> <span style="color:#2040a0;">collector</span> <span>=</span> <span><strong>(</strong></span><span style="color:#2040a0;">Collector</span><span><strong>)</strong></span> <span style="color:#2040a0;">Proxy</span>.<span style="color:#2040a0;">getInvocationHandler</span><span><strong>(</strong></span><span style="color:#2040a0;">returnValue</span><span><strong>)</strong></span><span>;</span>
<strong>    return</strong> <span style="color:#2040a0;">collector</span>.<span style="color:#2040a0;">getReturnValues</span><span><strong>(</strong></span><span><strong>)</strong></span><span>;</span>
<span><strong>  }</strong></span>

<strong>  public</strong> <strong>static</strong> <strong>void</strong> <span style="color:#2040a0;">main</span><span><strong>(</strong></span><span style="color:#2040a0;">String</span><span><strong>[</strong></span><span><strong>]</strong></span> <span style="color:#2040a0;">args</span><span><strong>)</strong></span> <span><strong>{</strong></span>
<span style="color:#2040a0;">    IFoo</span> <span style="color:#2040a0;">f1</span> <span>=</span> <strong>new</strong> <span style="color:#2040a0;">Foo</span><span><strong>(</strong></span><span><strong>)</strong></span><span>;</span>
<span style="color:#2040a0;">    IFoo</span> <span style="color:#2040a0;">f2</span> <span>=</span> <strong>new</strong> <span style="color:#2040a0;">Foo</span><span><strong>(</strong></span><span><strong>)</strong></span><span>;</span>
<span style="color:#2040a0;">    IFoo</span> <span style="color:#2040a0;">f3</span> <span>=</span> <strong>new</strong> <span style="color:#2040a0;">Foo</span><span><strong>(</strong></span><span><strong>)</strong></span><span>;</span>
<span style="color:#2040a0;">    IFoo</span> <span style="color:#2040a0;">f4</span> <span>=</span> <strong>new</strong> <span style="color:#2040a0;">Foo</span><span><strong>(</strong></span><span><strong>)</strong></span><span>;</span>

<span style="color:#2040a0;">    List</span><span>&lt;</span><span style="color:#2040a0;">IString</span><span>&gt;</span> <span style="color:#2040a0;">list</span> <span>=</span> <span style="color:#2040a0;">get</span><span><strong>(</strong></span><span style="color:#2040a0;">foreach</span><span><strong>(</strong></span><span style="color:#2040a0;">f1</span>, <span style="color:#2040a0;">f2</span>, <span style="color:#2040a0;">f3</span>, <span style="color:#2040a0;">f4</span><span><strong>)</strong></span>.<span style="color:#2040a0;">sayFoo</span><span><strong>(</strong></span><span><strong>)</strong></span><span><strong>)</strong></span><span>;</span>
<span style="color:#2040a0;">    System</span>.<span style="color:#2040a0;">out</span>.<span style="color:#2040a0;">println</span><span><strong>(</strong></span><span style="color:#2040a0;">list</span><span><strong>)</strong></span><span>;</span>
<span><strong>  }</strong></span>
<span><strong>}</strong></span>

<strong>class</strong> <span style="color:#2040a0;">Collector</span> <strong>implements</strong> <span style="color:#2040a0;">InvocationHandler</span> <span><strong>{</strong></span>
<strong>  private</strong> <strong>final</strong> <span style="color:#2040a0;">List</span> <span style="color:#2040a0;">returnValues</span><span>;</span>

<strong>  public</strong> <span style="color:#2040a0;">Collector</span><span><strong>(</strong></span><span style="color:#2040a0;">List</span> <span style="color:#2040a0;">returnValues</span><span><strong>)</strong></span> <span><strong>{</strong></span>
<strong>    this</strong>.<span style="color:#2040a0;">returnValues</span> <span>=</span> <span style="color:#2040a0;">returnValues</span><span>;</span>
<span><strong>  }</strong></span>

<strong>  public</strong> <span style="color:#2040a0;">List</span> <span style="color:#2040a0;">getReturnValues</span><span><strong>(</strong></span><span><strong>)</strong></span> <span><strong>{</strong></span>
<strong>    return</strong> <span style="color:#2040a0;">returnValues</span><span>;</span>
<span><strong>  }</strong></span>

<strong>  public</strong> <span style="color:#2040a0;">Object</span> <span style="color:#2040a0;">invoke</span><span><strong>(</strong></span><span style="color:#2040a0;">Object</span> <span style="color:#2040a0;">proxy</span>, <span style="color:#2040a0;">Method</span> <span style="color:#2040a0;">method</span>, <span style="color:#2040a0;">Object</span><span><strong>[</strong></span><span><strong>]</strong></span> <span style="color:#2040a0;">args</span><span><strong>)</strong></span> <strong>throws</strong> <span style="color:#2040a0;">Throwable</span> <span><strong>{</strong></span>
<span style="color:#444444;">    // TODO get(foreach(elements).method(args))</span>
<strong>    throw</strong> <strong>new</strong> <span style="color:#2040a0;">NotImplementedException</span><span><strong>(</strong></span><span><strong>)</strong></span><span>;</span>
<span><strong>  }</strong></span>
<span><strong>}</strong></span>

<strong>interface</strong> <span style="color:#2040a0;">IString</span> <span><strong>{</strong></span> <span style="color:#2040a0;">String</span> <span style="color:#2040a0;">toString</span><span><strong>(</strong></span><span><strong>)</strong></span><span>;</span> <span><strong>}</strong></span>

<strong>  class</strong> <span style="color:#2040a0;">StringImpl</span> <strong>implements</strong> <span style="color:#2040a0;">IString</span> <span><strong>{</strong></span>
<strong>    private</strong> <span style="color:#2040a0;">String</span> <span style="color:#2040a0;">s</span><span>;</span>
<strong>    public</strong> <span style="color:#2040a0;">StringImpl</span><span><strong>(</strong></span><span style="color:#2040a0;">String</span> <span style="color:#2040a0;">s</span><span><strong>)</strong></span> <span><strong>{</strong></span> <strong>this</strong>.<span style="color:#2040a0;">s</span> <span>=</span> <span style="color:#2040a0;">s</span><span>;</span> <span><strong>}</strong></span>
<strong>    public</strong> <span style="color:#2040a0;">String</span> <span style="color:#2040a0;">toString</span><span><strong>(</strong></span><span><strong>)</strong></span> <span><strong>{</strong></span> <strong>return</strong> <span style="color:#2040a0;">s</span><span>;</span> <span><strong>}</strong></span>
<span><strong>  }</strong></span>

<strong>interface</strong> <span style="color:#2040a0;">IFoo</span> <span><strong>{</strong></span> <span style="color:#2040a0;">IString</span> <span style="color:#2040a0;">sayFoo</span><span><strong>(</strong></span><span><strong>)</strong></span><span>;</span> <span><strong>}</strong></span><span>;</span>

<strong>class</strong> <span style="color:#2040a0;">Foo</span> <strong>implements</strong> <span style="color:#2040a0;">IFoo</span> <span><strong>{</strong></span>
<strong>  public</strong> <span style="color:#2040a0;">IString</span> <span style="color:#2040a0;">sayFoo</span><span><strong>(</strong></span><span><strong>)</strong></span> <span><strong>{</strong></span>
<span style="color:#2040a0;">    System</span>.<span style="color:#2040a0;">out</span>.<span style="color:#2040a0;">println</span><span><strong>(</strong></span><span style="color:#008000;">"foo"</span><span><strong>)</strong></span><span>;</span>
<strong>    return</strong> <strong>new</strong> <span style="color:#2040a0;">StringImpl</span><span><strong>(</strong></span><span style="color:#008000;">"foo"</span><span><strong>)</strong></span><span>;</span>
<span><strong>  }</strong></span>
<span><strong>}</strong></span><span>;</span>

<strong>class</strong> <span style="color:#2040a0;">ApplyOnArrray</span><span>&lt;</span><span style="color:#2040a0;">T</span><span>&gt;</span> <strong>implements</strong> <span style="color:#2040a0;">InvocationHandler</span> <span><strong>{</strong></span>
<strong>  private</strong> <strong>final</strong> <span style="color:#2040a0;">T</span><span><strong>[</strong></span><span><strong>]</strong></span> <span style="color:#2040a0;">elements</span><span>;</span>

<strong>  public</strong> <span style="color:#2040a0;">ApplyOnArrray</span><span><strong>(</strong></span><span style="color:#2040a0;">T</span>... <span style="color:#2040a0;">elements</span><span><strong>)</strong></span> <span><strong>{</strong></span> <strong>this</strong>.<span style="color:#2040a0;">elements</span> <span>=</span> <span style="color:#2040a0;">elements</span><span>;</span> <span><strong>}</strong></span>

<strong>  public</strong> <span style="color:#2040a0;">Object</span> <span style="color:#2040a0;">invoke</span><span><strong>(</strong></span><span style="color:#2040a0;">Object</span> <span style="color:#2040a0;">proxy</span>, <span style="color:#2040a0;">Method</span> <span style="color:#2040a0;">method</span>, <span style="color:#2040a0;">Object</span><span><strong>[</strong></span><span><strong>]</strong></span> <span style="color:#2040a0;">args</span><span><strong>)</strong></span> <strong>throws</strong> <span style="color:#2040a0;">Throwable</span> <span><strong>{</strong></span>
<span style="color:#2040a0;">    Method</span> <span style="color:#2040a0;">targetMethod</span> <span>=</span> <span style="color:#2040a0;">elements</span><span><strong>[</strong></span><span style="color:#ff0000;">0</span><span><strong>]</strong></span>.<span style="color:#2040a0;">getClass</span><span><strong>(</strong></span><span><strong>)</strong></span>.<span style="color:#2040a0;">getMethod</span><span><strong>(</strong></span><span style="color:#2040a0;">method</span>.<span style="color:#2040a0;">getName</span><span><strong>(</strong></span><span><strong>)</strong></span>, <span style="color:#2040a0;">method</span>.<span style="color:#2040a0;">getParameterTypes</span><span><strong>(</strong></span><span><strong>)</strong></span><span><strong>)</strong></span><span>;</span>

<span style="color:#2040a0;">    List</span> <span style="color:#2040a0;">returnValues</span> <span>=</span> <strong>new</strong> <span style="color:#2040a0;">ArrayList</span><span><strong>(</strong></span><span><strong>)</strong></span><span>;</span>
<strong>    for</strong> <span><strong>(</strong></span><span style="color:#2040a0;">T</span> <span style="color:#2040a0;">element</span> <span>:</span> <span style="color:#2040a0;">elements</span><span><strong>)</strong></span> <span style="color:#2040a0;">returnValues</span>.<span style="color:#2040a0;">add</span><span><strong>(</strong></span><span style="color:#2040a0;">targetMethod</span>.<span style="color:#2040a0;">invoke</span><span><strong>(</strong></span><span style="color:#2040a0;">element</span>, <span style="color:#2040a0;">args</span><span><strong>)</strong></span><span><strong>)</strong></span><span>;</span>

<span style="color:#444444;">    // return a proxy for the result</span>
<strong>    return</strong> <span><strong>(</strong></span><span style="color:#2040a0;">T</span><span><strong>)</strong></span> <span style="color:#2040a0;">Proxy</span>.<span style="color:#2040a0;">newProxyInstance</span><span><strong>(</strong></span><span style="color:#2040a0;">elements</span><span><strong>[</strong></span><span style="color:#ff0000;">0</span><span><strong>]</strong></span>.<span style="color:#2040a0;">getClass</span><span><strong>(</strong></span><span><strong>)</strong></span>.<span style="color:#2040a0;">getClassLoader</span><span><strong>(</strong></span><span><strong>)</strong></span>,
<strong>    new</strong> <span style="color:#2040a0;">Class</span><span><strong>[</strong></span><span><strong>]</strong></span> <span><strong>{</strong></span> <span style="color:#2040a0;">targetMethod</span>.<span style="color:#2040a0;">getReturnType</span><span><strong>(</strong></span><span><strong>)</strong></span> <span><strong>}</strong></span>,
<strong>    new</strong> <span style="color:#2040a0;">Collector</span><span><strong>(</strong></span><span style="color:#2040a0;">returnValues</span><span><strong>)</strong></span><span><strong>)</strong></span><span>;</span>
<span><strong>  }</strong></span>

<span><strong>}</strong></span></pre>
<p><code><span style="font-family:'Lucida Grande';">I had this idea in a dream (too much work) and fortunately I could still remember it in the morning. Who knows, I might have dreamt also a solution that works with classes.</span><br />
</code></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jaksa.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jaksa.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jaksa.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jaksa.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jaksa.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jaksa.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jaksa.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jaksa.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jaksa.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jaksa.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jaksa.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jaksa.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jaksa.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jaksa.wordpress.com/13/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jaksa.wordpress.com&amp;blog=2662408&amp;post=13&amp;subd=jaksa&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jaksa.wordpress.com/2008/09/19/foreach-in-java/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">jaksa</media:title>
		</media:content>
	</item>
	</channel>
</rss>
