For now I'm writing tidbits about computer programming, the plan is to actually blog at some point in time.

Showing posts with label aop. Show all posts
Showing posts with label aop. Show all posts

Friday, May 30, 2008

just wrote the first batch of code for lego4j

Would you like to be able to write code like this ?


public class Foo {
@RetryOnException( E.class, 1 )
public String bar( int arg1 ) throws E {
return doSomeThingThatMightThrowE( arg1 );
}
}

instead of having to write something like this:

public class Foo {
public String bar( int arg1 ) throws E {
int retries = 0;
while (true) {
try {
return doSomeThingThatMightThrowE( arg1 );
}
catch (E e) {
if (retries < 5) {
continue;
}
throw e;
}
}
}
}

This is what lego4j is about, creating a library of lightweight aspects to encapsulate local concerns programmers encounter while writing a generic Java application.

Friday, November 10, 2006

timed invocation aspect

In the interview Joshua Bloch on Closures, Resource Management, Google, Joshua Bloch mentions, and praises, a facility a google employee created to timeout method invocations. A couple of years ago I had the need for such utility and ended up creating it. That code is nowhere to be seen now, as my employer has changed a couple of times since then.

Most certainly this is a very handy facility in everyday programming. It will be very nice if the "method timeout invocation" concern could be easly integrated into the code using aspects!


@timeout(millis=1000)
public void foo() {
...
}

Where:

/**
* Times out a method invocation. If any of the methods matched by the pointcut invocationToTimeOut() has not
* returned after the specified timeout, the unchecked exception InvocationTimeout will be thrown.
*/
aspect TimeoutInvocation {
...
}