Nov
26
2006
0

Thai food and yet another lost soccer match; blogging to the max!

I just finished my fresh-made Thai chicken curry and Tom Yan Kaeng (soup, also with chicken) after I got myself all of the necessary ingredients except for the galanga. I was reading the Economist while eating my Thai curry and after I finished the piece on the steel market (with a Brazil company and Tata from India fighting over Anglo-Dutch Corus, another steel firm) I went on to read an article on click fraud followed by Yahoo’s Brad Garlinghouse and his Peanut Butter Manifesto.

Then I flipped the page and saw Mena Trott. Mena is the president and public face of Six Apart, the company that created Movable Type, Typepad and Vox. I saw Mena a while ago when her TED speech came online. The following is a quote from the article:

Many ordinary people are scared of blogging because they feel that they have nothing to say, says Ms Trott. So her message is that “mundane is interesting; it’s okay to talk about your sandwich”. To a handful of people in the world if may mean a lot.

20061125issuecovUS160.jpg

Okay, so there I am, I’ve blogged about me, sitting on my couch, on a lazy Sunday afternoon (errr, evening in the meantime) and telling you all that I just finished my Thai curry and will probably do a little work tonight after I’m done cleaning the kitchen.

Let’s do a small experiment: anybody that thinks this is even remotely interesting, please comment on this entry. In the meantime, I’m continuing with more interesting articles in the Economist… Ahh, that’s a relieve, the next article is about the current state of affairs at Wall Street.

Update (December 5th): okay, this story ran for a week or so and the popularity index has risen to 28% (relative to the most post that is). We’ll see what happens in the coming time…

Update (December 11th): 39% and still rising :) Maybe I should raise the stakes: I’ll quit my blog when it’s most popular article on the blog ;-)

Written by Alef in: At home, Economics, Leisure |
Nov
23
2006
2

Planning a surf trip

Things have been pretty busy for me lately. The rhythm was good though, three or four days of training every week or so for the last 6 weeks and some consulting here and there. Somehow the fourth quarter is kind of busy every year (it’s been so for the last four years). Fortunately, I have planned a few weeks off near the end of the year, so I’m planning another surf trip again. I plan on going to a Spanish speaking country, because I am learning Spanish and I want to get some practice :) .

I’ve been to Costa Rica already, so was thinking about the Dominican Republic. They say the winds are screwing up the waves there, so that might not be such a good idea. Maybe Puerto Rico, or Mexico?

If you have some good ideas, please let me know.

Written by Alef in: Leisure |
Nov
19
2006
6

Quero hablar Espanol!

‘I want to speak Spanish’ that is… At least, that is what I think it means :) .

In Holland (at least in my days), we only have the choice between three foreign language in high school: German, French and of course English (which is sort of a no-brainer anyway). I did some Latin when I was a kid and also did six years of French. I kind of all lost that. I think it’s a pity that the high school I went to in Holland didn’t offer me to do Spanish because although it’s a bit closer to home than Spain, French is really only spoken in France plus few other countries in the world. The Spanish-speaking world is much bigger.

hola.gif

That’s why I decided to take a Spanish course. Not the ordinary Spanish course where you go to class with other people but a do-it-yourself course. I don’t have much spare time and because I’m spending about 5 to 10 hours a week in my car driving to work, I started looking for a course I could take while commuting!

The bookstore in Utrecht fortunately had one and that’s how I got to learn my first Spanish words. Puede acer una resevacion para mi para esta noche por favor?. Okay forgot about the accents and inverted question marks but this is what will probably get me a reservation in a restaurant the next time I’m in Rica or so :) .

The course is absolutely fabulous so far. I’ve only done about about two hours of it (there’s 13 or 14 hours of course ware), but this is already really good. Have a look at the Michel Thomas courses if you ever need to learn a new language.

Written by Alef in: Leisure |
Nov
02
2006
15

after() throwing(Exception e) : serviceMethod() { mail(e); }

Okay, good you made it past the title. I was at a client that has been a long-time Spring user. Some of the client’s stuff needs a revision and as part of this revision Spring 2.0 AOP was introduced in some places. Today, for the group of developers I shortly touch on some of the basic differences between AOP in Spring 1.x and AOP in Spring 2.0. I figured it would be good to post the examples online.

Let’s first set the stage for the example (and please read this carefully): an exception thrown from a service method should be emailed to an administrator.

Here’s an example of a service method:


package com.mycompany.myapp.service;

public interface DrinkingService {

void doIt();
}

with a corresponding implementation:


package com.mycompany.myapp.service;

public class DrinkingServiceImpl implements DrinkingService {

public void doIt() {
throw new IllegalStateException("I have a hangover so please don't bother me!");
}
}

As you can see, it’s in an illegal state at the moment (probably because the service drank a bit too much Limoncello or something). Normally service wouldn’t do this, but this time of the year (when leaves start falling and days are shortening), I can definitely imagine services grab the bottle.

Anyway, let’s continue. The requirement was to notify an administrator (by email) about any exception occurring inside a service. DrinkingServiceImpl is just one example of a service and because there probably are more services in our application, we’re going to use AOP to implement this specific requirements. Think about the 1:1 principle, separation of concerns (SoC), Don’t Repeat Yourself (DRY) and all that crap…

There are several ways of solving this. Let’s examine them one by one.

Emailing the exception using Spring 1.x

Suppose you’re stuck at Spring 1.2. Then the way to go is using a ProxyFactoryBean. Using this Spring class, you can configure interceptors in order to advise certain methods.



This piece of Spring configuration configures a ProxyFactoryBean and one interceptor, which in this case is a bean called exceptionAdvice. Configuring something like this, obtaining the service bean and calling a method on the service bean, will actually cause the interceptor to ‘run’.

The interceptor (the EmailOnExceptionAdvice) is actually an instance of ThrowsAdvice, which means it only gets called when an exception occurs inside a method call on the drinkingServiceTarget. There’s only one method, which in this case takes an Exception.


public class EmailOnExceptionAdvice implements ThrowsAdvice {

public void afterThrowing(Exception e) {
System.out.println("Email the exception!");
// TODO email the exception to an administrator
}
}

So, one more time:

  1. The ProxyFactoryBean creates a proxy wrapping the target object (in this case a DrinkingServiceImpl)
  2. The ProxyFactoryBean also configures an interceptor that acts on any exception occurring inside the service
  3. The interceptor (advice) receives notice of exceptions occurring and emails them to an administrator

As you can see (run the example if you want more insight), the requirement (exceptions thrown from a service method should be emailed to an administrator) has successfully been implemented!

Note that there’s absolutely no reason to keep yourself from upgrading to Spring 2.0. The new Spring version of in 95% of the cases fully backward-compatible with the 1.x-line and spring-2.0.jar should in most cases be a drop-in replacement for the spring-1.2.7.jar for example.

Emailing the exception using Spring 2.0 and the old advice

You might be wondering why we’ve created Spring 2.0 and enhanced the AOP capabilities so much if things are that easy using Spring 1.x. Well, there actually are a lot of reasons, some of which will probably get clear after I’ve introduced the new approach. Let’s first revisit our initial requirement:

an exception thrown from a service method should be emailed to an administrator.

Although this requirement has been implemented correctly, the way we did it might sound kind of low-level. We used proxy factories, interceptors, advice, target objects and much more, all to email an exception to an administrator. Using Spring 2.0, we’ll see that all these infrastructural components more or less disappear, allowing us to make the code look more like the design while still successfully implementing the requirement.

To not take too big a step, in the new version, we’ll reuse our old advice and we’ll just change the configuration a bit. We’ll start off by defining a pointcut:



What this pointcuts says is the following: a serviceMethod is the execution of any method in service package of my application. That’s what I call ‘make the code look like the design’. The next thing we’ll do is attach the previously created advice to our pointcut and we’re done (this is what we’re doing using an aop:advisor element. The final configuration looks like this (in other words, no more ProxyFactoryBeans).

<code>
<aop:config>
    <aop:pointcut id="serviceMethod" expression="execution(* com.mycompany.myapp.service..*.*(..))" />
    <aop:advisor pointcut-ref="serviceMethod" advice-ref="exceptionAdvice" />
</aop:config>
 
<bean id="exceptionAdvice" class="com.mycompany.myapp.util.EmailOnExceptionAdvice" />
 
<bean id="service" class="com.mycompany.myapp.service.DrinkingServiceImpl" />
</code>

And now for a completely Spring-less approach

In the previous two solutions, we used special Spring classes to implement our advice. There is a third approach not requiring you to implement any Spring-specific interfaces. This is what we called the ‘POJO advice’ approach. Using the POJO advice approach, you can use any existing class that doesn’t have to have any references to Spring whatsoever to serve as an advice. Okay, let’s change the exception advice to a POJO advice first:

<code>
public class ExceptionEmailer {
  
    public void emailException(Exception exception) {
        System.out.println("In Emailer");
        // TODO implement emailing functionality
    }
}
</code>

This should be straightforward to understand. Upon the catching of an exception the emailException method should be called. Again, we’re going to try to make the code look as much like the design as possible. Here’s how we can do this using Spring AOP in version 2.0:

<code>
<aop:config>
    <aop:pointcut id="serviceMethod" expression="execution(* com.mycompany.myapp.service.*.*(..))" />
    <aop:aspect ref="exceptionEmailer">
        <aop:after-throwing throwing="exception" 
            pointcut-ref="serviceMethod" method="emailException"/>
    </aop:aspect>
</aop:config>
</code>

Let’s have a look at how the aspect reads: after-throwing an exception from any serviceMethod, call the emailException method on the exceptionEmailer. Pretty straightforward if I may say so myself. I can remember Gregor Hohpe telling me once that he tried to code in such as way that he could show his code to non-technical managers. Well, IMO this certainly is a way of doing this!

Other ways of doing this

There are still plenty of other ways of making sure an email is sent after an exception has been raised. One of the ways of using AspectJ, which will result in code resembling the title of this post. Maybe I’ll write about the other approaches some other day. First, I’ll have to write a follow-up to my example about how to use AOP to intercept web requests.

p.s. Here are the sample sources. Just as with the previous post, remember that you should include spring-2.0.jar and aspectjweaver.jar, otherwise things won’t work. I still haven’t found the time to get Maven2 done for these sample projects.

Written by Alef in: Java-related, Spring, Technology |
Nov
01
2006
1

Added a new plugin: related entries

I’ve spent an hour on my Wordpress instance today, to upgrade it a bit and to install some additional plugins. I’ve changed my ‘easy-sample-code-pasting-plugin’ to Code Auto Escape. This means I won’t have to worry anymore about Wordpress tag balancing f**king up my XML fragments that include the use of namespaces.

I’ve also added Related Entries 2.0 and this blog entry is a test to see if it’s going to display the previous entry I wrote on Wordpress.

I’ve also changed my spam killer from Akismet to Spam Karma 2.

Update I: hmmm, it doesn’t really work all that well yet. Now it displays a post on Subversion :( . Well, let’s see what happens in a few days. If it doesn’t work, I’ll remove it again. Let me know what you think.
Update II: hmmmm, I think it works afterall. The Subversion post talks about plugins (for Eclipse) and this post does too, so maybe the indexing features work afterall ;-)

Written by Alef in: Technology |
Nov
01
2006
6

LinkedList vs. ArrayList vs. Maps, ARGGHHH……

Nope, I’m not reading Data Structures and Problem Solving using C++ (or Java for that matter), although it is displayed in the sidebar of my blog. I already did!!!


Again I came across a couple of posts discussing the difference between linked lists and array lists. I’m wondering whether or not today’s Java programmers have actually ever read any in-depth books on data structures or anything.

180px-Binary_tree.png

I’m not saying I’m the expert in all of this (if I would, Arjen would definitely not hesitate to point out to the world that I was the one stating pointers and references are the same–which I guess I did, just to put an end to some theoretical debate I think ;-) ). I do however think it’s pretty important that as a software engineer, you know what you’re doing and why. And even if you don’t, you should not be as clueless to state that thinking about why to use LinkedList instead of an ArrayList is always a waste of time

So, for now, I’ll just pretend to be reading the Data Structures and Problem Solving book by Mark Allen Weiss only if it’s just to educate the world a bit :) . It’s arguably one of the better books out there on the subject.

So go ahead and order Mark Allen Weiss’ book in Java or in C++.

Written by Alef in: Technology |
Nov
01
2006
2

Daylight Saving Time issues

I just came across a blog post from Charlie Wood about the Energy Policy Act of 2005 causing pretty significant problems with enterprise applications. Apparently this act moves the weekend in which the States move to Daylight Saving Time (DST) from the 1st Sunday in April to the second Sunday in March (I thought the states where actually in sync with Europe since a couple of years, but I must have been wrong here).

601dst.gif

Of course, desktop operating systems will be okay as Windows for example just issues another update. And even if a user does not accept it, the problems with a desktop computer not displaying the correct time will be pretty limited (except for people leaving office early maybe :) ).

Some other systems or platforms however like the Sun JRE and IBM WebSphere are also affected this change.

Phew, am I glad I’m living in Europe :) . By the way, as noted by Charlie, Vinnie Mirchandani has pretty funny view on timezones too.

p.s. I was wondering, is this the US’ final attempt to put a halt to all the critics that are saying it should ratify the Kyoto Protocol…? Errrrr, maybe I should not try to mix political discussions with the impact of such an act on the correct workings of our applications ;-) .

Written by Alef in: Technology |

Powered by WordPress | Theme: Aeros 2.0 by TheBuckmaker.com