April 15, 2009

The art of being an architect (or FUD in plain words)

We received an e-mail today at work from one of the architects. We had initiated the e-mail conversation by asking for directions on what solution to implement. We had stated two feasible (possible) solutions.

It seems, however, that to be a real architect you have to be an expert in FUD. What conclusions would you draw from the following:

"Finally, we should have a clearer picture of the pros and cons before we go with any of the solutions. Alternatively we proceed with testing both as a hypothesis to see what solution is best in practice."

For sure it got us puzzled. We asked for guidance on what solution to implement and the above is what we got back. I guess now all we have to do is either to get a clear picture or continue with both... :-)

April 13, 2009

My first impressions of Google App Engine for Java

I've played around with Google App Engine for Java for a couple of hours now. This is an area that I think all enterprise Java developers should keep track of. I think that this way of deploying applications will become more or less default within a couple of years.

Here are my immediate findings:
+ Easy to get started with and to be productive within a couple of minutes. See this getting started guide.
+ Integrated Server Environment - no restarts needed.
+ Changes the Java perspective totally?! This is the route all enterprise companies will deploy their apps soon, isn't it?

- No IntelliJ support. See this blog for more information of how to get IntelliJ to work with GAE.
- And on the no-IntelliJ-support note: JSP Support in Eclipse sucks (no code completion or compilation).
- No utilization of IOC and I just hate static things.

I urge all interested Java developers to have a go at GAE. It is easy and fun and, perhaps the most important part, it is a glimpse of what your future work environment will look like.

March 31, 2009

How to make IntelliJ 8.1 and SVN 1.6 cooperate

Yesterday some guy at work decided to upgrade to SVN 1.6. The decision to do this came from nowhere and left us IntelliJ guys with a non-working IDE. So in retrospective it might have been a good idea, pun intended, to ask around before performing this step.

Anyway, what's done is done! We still wanted to use IntelliJ without Tortoise or some other SVN tool and investigated how this could be done. A collegue of mine still had his IDEA working with SVN after an upgrade and we look for differences in the ".svn" folder. We soon found out that the file entries had a "9" in the beginning of the file whereas my had a "10". So to make IntelliJ work with SVN 1.6 all you have to do is to replace all 9s with 10s in all the entries files.

Here's one way to do it:
1. Make all your entries files writeable (in Windowz: Open Explorer -> Go to local repo -> Right click and select Search -> Enter "entries" as file name -> Click "More advanced options" -> Make sure that "Search hidden files and folders" is checked -> Click Search -> Select all found files (Ctrl-A) -> Right click and select properties -> Uncheck "Read only" and click OK)
2. Download and install Actual Search and Replace
3. Start Actual Search and Replace and enter the path to your local repo.
4. Select the "Search and Replace" tab
5. Use Reg Exp as Search Type
6. In the To Search you enter ^10$ and in the To Replace or Insert you enter 9 (please note that you should only change the "entries" files).
7. Click Search and then Modify All

That's it! Good luck :-)

Please note that I do not take any responsibility if, for some reason, this manoeuvre fails!

March 9, 2009

Pair Programming - There's no reason to not do it, or is there?

Reading this very interesting and well-written article about pair programming today really inspired me to get started with true pairing. I still have some questions to be sorted about how to introduce this into an organization working with SCRUM (which is the current project methodology they use at my current assignment).

Things that I haven't thought about before reading the article are the value of having "pairing stations" - a developer neutral work station specifically designed for development. In previous projects whenever we have used pair programming this basically means that two developers sit down at one of their machines and start to program. However, in most cases this means that the developer that "owns" the machine has some custom settings. Using neutral machines, preferably with dual screens, would eliminate such custom settings - something that I feel will improve the quality of the code.

Another obvious benefit (that I had thought of before) by using pairing is knowledge transfer. I don't think that I have been at a single customer where knowledge transfer has not been a problem. Why not use pair programming to enhance this transfer? I guess the solution is too obvious for anyone to see it! :-)

A question I have asked the author of the article though is how to use one day pair switching in an environment where the tasks are up to 16h? Also we tend to develop all the tasks in a domain to create domain experts. How should this work with pair switching? Isn't there a risk of creating a bunch of "no-red-line" modules - or is it perhaps the task of the architect to ensure that no such monster is created?

I would really like to try more 100% pair programming because I truly believe that the efficiency and quality of the end product is much higher that what the normal head-phone using developers produce.

March 5, 2009

Recursion: see Douglas Hofstadter

The name of this blog is "Recursion: See Recursion". This joke has been around for a while but I still think it is funny. I read a better, more accurate, variation on the same theme today though:
Recursion: If you still don't get it, see: "Recursion"
Why is it more accurate? Because the reader will terminate once s/he gets what recursion is.
Every recursive call must have a termination condition - if not it is a plain indefinite loop.

A big reason for my interest in recursion, weird as it may sound, is the author Douglas Hofstadter. He is best known for this Gödel, Escher, Bach: An Eternal Golden Braid but most people I have met are unfamiliar with his, perhaps even better, book Metamagical Themas.
In this epic book you can find a whole section dedicated to "self-referential sentences".
I really recommend you to buy this book and I promise several joyful hours pondering about linguistic recursion.

Here are some some appetizers of what you can find in his book:
"The whole point of this sentence is to make clear what the whole point of this sentence is."

“The proverbial German phenomenon of the verb-at-the-end about which droll tales of absentminded professors who would begin a sentence, ramble on for an entire lecture, and then finish up by rattling off a string of verbs by which their audience, for whom the stack had long since lost its coherence, would be totally nonplussed, are told, is an excellent example of linguistic recursion.”

"Hofstadter's Law states that: It always takes longer than you expect, even when you take into account Hofstadter's Law."

"This sentence contradicts itself - no actually it doesn't."

I'm sure I will write more about my findings in this interesting topic, recursion, later.

February 23, 2009

ObjectMother's better friend: The Test Data Builder Pattern

Having used the Object Mother pattern a couple of times before I have felt that it always looks tidy and nice in the beginning of the project. Over time, though, the clearness of the "mother" degrades dramatically.

Having googled for a while I found that a lot of people recommended using the Test Data Builder pattern instead. Just by having had a look at the notation I must say that I immediately prefer it to the Object Mother. Who cannot say that this looks nice:

Address address =
new AddressBuilder()
.withStreet(
"Rue Pont Neuf 12")
.withCity(
"Bombay")
.withCountry(
"Iceland")
.build();
Person person
=
new PersonBuilder()
.withSSN(
"123456-1234")
.withName(
"Henrik Engstrom")
.withAddress(address)
.withSalary(
"too little")
.build();


I know that I do :-)

February 5, 2009

A trip into Mockito land with varargs as traveling company

I have used Mockito in a couple of previous projects with joy. In comparison to other mock frameworks I have used it is simple and good enough for what I want in a project.

However, the other day I ran into an interesting problem; mocking out methods containing varargs. I wanted to share my problem to see if anyone out there has run into the same problem and, perhaps, have been more successful in resolving it.

Let's start by creating a simple class:

public class MyClass {

public int useVarArgs(int... args) {
int result = 0;
for (int arg : args) {
result
+= arg;
}

return result;
}
}

Here' s the corresponding test class:
public class MyClassTest {

@Test
public void useVarArgs() {
MyClass myClass
= new MyClass();
int result1 = myClass.useVarArgs(1);
int result10 = myClass.useVarArgs(1, 2, 3, 4);
assertEquals(
1, result1);
assertEquals(
10, result10);
}

@Test
public void mockMethod() {
MyClass myClass
= mock(MyClass.class);
when(myClass.useVarArgs((
int[]) Matchers.notNull())).
thenReturn(
2);
int result3 = myClass.useVarArgs(1, 2);
int result10 = myClass.useVarArgs(1, 2, 3, 4);
assertEquals(
2, result3);
assertEquals(
2, result10);
}
}


What I am trying to achieve in the test above is first to verify the correct behavior of the tested method (done in test method useVarArgs). Thereafter I simulate a test in which we need to mock out the default behavior (done in test method mockMethod).

However, the last test method fails!
java.lang.AssertionError: expected:<2> but was:<0>

Firstly, I'd like to clarify that I have tried different "matchers" than the Matchers.notNull(), e.g. Matchers.anyObject(). It does not affect the result of the test.

Trying a custom matcher and using the Matchers.anyThat(org.hamcrest.matcher matcher) does not solve my problem either, or have I misunderstood the power of Hamcrest?

I have inspected the source code in Mockito by following the debugger but to no avail. If there are any Mockito expert that would help me with this I'd much appreciate it since it is bugging me a lot! I have promoted Mockito as the best thing since sliced bread to my colleagues and now I need to prove this...

PS. If you blog with Blogger and like some nice code formatting tool (I have missed one since I started blogging - a fact that is quite obvious by looking at my blog posts) then I highly recommend BlogTrog. DS.

January 21, 2009

A Quick Guide to Java Performance Tuning

The last couple of weeks I have been working intensely
with performance of a JEE application (EJB3, JPA) that
runs on Glassfish v2.1.

When the application server was stressed tested we found
that the CPU (4 cores + 8GB) load was between 80-90% and
the throughput and latency was not good enough.

Hence we began investigating what the cause of the
problem was. I'd like to share some of the findings as it
might point someone else in the right direction when
it comes to JEE applications and performance.

Loading the Application
To find out the performance (latency and throughput) of
your application you must be able to load it. We use Grinder
for this. If possible it is definitely better to set up the
load server on another machine than the one you run
your app server on.

Measure and analyze the results of the load test.
If it turns out to not fulfill the performance requirements of
the application below you can find a checklist that I have
found useful when tracing the root cause of the problem(s).

Suspicion 1 - Database Issues
The first performance problem a JEE application "normally"
runs into is db performance (at least this is my experience as
a JEE developer since 1999).

Check CPU load on the db server to get an indication of if
this is the reason of the experienced performance problems.
If it is then tuning the database application is where you
should start! But this topic is not covered here - Google it!

Suspicion 2 - Code Issues
You may have unintentionally written your code in a non-
performing way. This happens some time you know :-)

I suggest that you download a Java profiler and use it to
introspect how your code really operates.

We have used JProfiler (v5.2.1) to profile the JEE application
running in Glassfish (v.2.1). You can install and try it for
10 days for free. JProfiler worked well as long as we kept the
number of HTTP threads to a small number. When running
Glassfish with 128 HTTP threads they all ended up
in a deadlock.

We could not determine if JProfiler was the cause of this or
something else. (Although, running the application server
without JProfiler did not cause this faulty behavior).

If you want to use freeware you may test NetBeans Java profiler.
It worked ok too, but since it only measured wall time and we
wanted to see the real CPU time it did not suffice to our needs.

Run the profiler and try to locate memory leaks,
performance issues, and so on.
The documentation of the profiler will give you a good
starting point for how to do this.

In our case we could not find any obvious issues with our
JEE code and, hence, we continued with the next step (below).

Suspicion 3 - App Server Issues
Could it be an application server problem that was causing
our issues? As written above we run Glassfish and the
first thing we did was to read this:
Glassfish Performance Tuning Guide.

Glassfish out of the box is targeting a development environment
so therefore you can tune a lot of parameters to squeeze out
higher performance.

Although our performance problems did not go away after the
tuning made. We had better performance now, but not good enough.

I wondered if it could be a Glassfish thing?
What if we ran our application on another application server
and compared the latency and throughput!
This would be a good indicator whether it was a
app server problem.

After trying to deploy our application onto a JBoss 5
application server for a couple of hours without any
success I gave up! There are too many differences between
the two servers.
"Write once, run everywhere" my ass! (pardon my french).

Also I found this document and felt quite confident
that Glassfish could perform.
So, the next step is to tune the JVM.

Suspicion 4 - JVM Issues
If you're not familiar with the JVM GC parameters you
should start this investigation by reading:
JVM GC documentation.
Altering GC params can do wonders :-)

Also I highly suggest that you download and install JVMStat.
It's a great tool to get an overview of what is going on
with the heap.

When you feel confident enough you can play around with
some parameters. Some we found useful
(i.e. improved the performance significantly):
* -XX:+UseParallelGC
* -XX:+AggressiveHeap
* -XX:NewRatio=z (where z = 1, 2, 3, ...)

Of course you must adjust and tune these parameters to
fit your environment.

Lessons Learned
Run performance tests for every release (and store the results -
as they can serve as benchmarks for coming releases).

Always run performance tests early in the projects lifecycle.
It is hard to detect, and isolate, problematic areas
in the latter stages of the project.
Also, it can prove very time consuming to fix any of the
found problems, i.e. you may not be able to deliver on time.

Don't underestimate the issues with tuning the JVM and
its GC behavior! GC in itself is a topic in which you
can get a PhD :-) If your app is to run in an environment
where throughput and latency are keywords you should
invest some time in understanding the JVM GC concepts -
or hire an expert to the tuning for you.

Happy tracing!
Resources:
Grinder
JProfiler
Java 5 GC Tuning Documentation
JVMStat

December 11, 2008

This blog as an image

I just found this cool site: Worlde.
Go there and create your own word illustration!
Here's my blog as a word cloud.

First stumbling Terracotta steps on my Mac

A couple of days ago I got this book. So far, about 85 pages in, it is very interesting and informative. After reading a couple of chapters I felt the urge to try some "Terracotting" on my brand new IMac 24" :-) Here are some findings for all you Mac freaks out there :-)

Firstly, download the Linux version of Terracotta. Unzip it and put it somewhere convenient on your disk.

In order to make Terracotta work on your Mac you must rebuild it. Set JAVA_HOME and TC_JAVA_HOME to /Library/Java/Home. The easiest way to do this is to create a .profile file in your home directory:
1. Open up a Terminal window (since I have installed Quicksilver I use this eminent tool to open the Terminal window for me).
2. Type 'cd' to get to your home directory
3. Type 'touch .profile' to create an empty .profile file (if it doesn't already exist)
4. Type 'edit .profile' to open a text editor an enter the following text:
JAVA_HOME=/Library/Java/Home
TC_JAVA_HOME=/Library/Java/Home
5. Save the file and close the editor

Now we are ready to rebuild Terracotta:
1. Start a new Terminal window
2. Go to the Terracotta home directory (where you have put it on your disk).
3. Type 'cd bin' and then './make-boot-jar.sh'
This will rebuild your Terracotta boot jar.

Try to run Terracotta (to verify that it is working):
Type './start-tc-server.sh' and you should see a text saying something with "Terracotta Server has started ..."

So the next step for you is to create some Java code, compile it into byte code, having Terracotta instrument this byte code, and finally run this code from several JVMs. You can find examples and more information on the Terracotta site. Best of luck and happy coding!

This blog was created under influence of a lot of music provided by the best tool since sliced bread: Spotify.