Archive for June, 2009

dm Server 2.0 in Action

Saturday, June 27th, 2009

We strongly believe that OSGi is the future of Java EE development. Let me be more clear (mea maxima culpa!) clearer: if Java EE applications do not clean up their act and become more manageable, they will face the slow death of COBOL (notice that I spell COBOL with upper-case letters — I have some COBOL programming experience under my belt — scary!). It means that there will be a lot of development and a lot of work for us Java programmers to do, but all the cool stuff will be happening elsewhere. Let’s name drop: Ruby on Rails, Grails, dare I say JavaScript?

Take your Java EE skills to the next level

Because of this, you really should get to MEAP and get your hands on the early releases of our new book, dm Server 2.0 in Action.

Introducing AsyncWorker

Tuesday, June 23rd, 2009

I have another project for the Open Source Central: AsyncWorker. I am tired of writing similar asynchronous call infrastructure from scratch in every project. This is why I am going to write the AsyncWorker. At the highest level, there will be an interface (class?) AsyncWorker defined as

interface AsyncWorker {
    <T> T invoke(T t);
    <T> T invoke(T t, int maxWait);
    <T> InvocationIdentity execute(T t, AsyncCallback callback);
}

The implementation of the AsyncWorker will be flexible enough to use simple heap, JMS or database queues to store the invocations and the API is very easy to start using in your existing projects. For example, if you wanted to turn a synchronous call into asynchronous one with as little coding as possible, you’d write

@Controller
class ExistingController {
    private SomeService someService;
    private AsyncWorker asyncWorker;

    @RequestMapping(...)
    public void index() {
        // this.someService.doWork(1, 2, "hello");     #A
        this.asyncWorker.invoke(this.someService).     #B
            doWork(1, 2, "hello");
    }
}

That should be it! Instead of synchronous call on line #A, you will use the AsyncWorker on line #B. The AsyncWorker will queue the invocation of the doWork method of SomeService and will return immediately. Because of the asynchronous nature of the call, the value returned from the method call will be default for the return type (0 for int, long, 0.0 for double, float, null for object reference, etc.).
The alternatives, as you can guess, are using the invoke(T, int) method, which will also invoke any method of T asynchronously, but will wait up to the specified timespan before returning. If the invoked method completes within the specified timespan, the returned value will be valid, otherwise it will be the default value.
Finally, you can use the execute method to schedule the asynchronous call — this gives you the highest degree of control and you will get an InvocationIdentity object back. You will be able to use the returned InvocationIdentity to monitor the progress of the call.

Call for comments

If you think this is a good idea, please comment and add any features you’d find useful. We will be releasing this project under the Open Source Central under Apache Licence.

Friday’s programming humour

Friday, June 19th, 2009

We have something to while away many Friday afternoons.

Take a complex plane curve parametrized as

d0(z) = az,
d1(z) = a + (1 – a)z,
a = 1/2 + i / sqrt(12)

Draw this curve as precisely as possible, take scissors and cut and cut and cut…

On future of web programming at Cake

Friday, June 12th, 2009

Ruby eye for the Java guy

Ruby on Rails is a mature and innovative framework designed especially for the web. This allows rapid, stable and iterative web development. At Cake Solutions we use the latest technologies that ensure that websites developed on Rails will be built to specification, on-time and fully tested.

What’s in it for us, Java developers?

What is the fundamental difference between Ruby and Java? The main difference is that Ruby uses duck typing. The fundamental principle of duck typing is that “if it quacks like a duck, looks like a duck, then it is a duck”. In programming, this means that if an object responds to the signals we send it, then we have the correct type. Consider this code:

def foo(f)
   f < < "some text"
end

This signal handler (method) takes an argument f; the object f passed to the method must respond to the << operator. This means that we can call foo with an Array or a File, because both objects respond to the << operator.
"But," you say, "this feels too brittle. I prefer manifestly-typed languages such as Java, where the compiler will check the types."
First of all, manifest typing does not prevent type errors at runtime. If it did, there would be no need for a ClassCastException. The very existence of this exception means that even manifestly-typed languages do not guarantee type safety at runtime. Think about the collections framework in Java 1.4: the collections held just Objects and you had to do dynamic type casting. But how many ClassCastExceptions did you see at runtime and when you did, how difficult was it to fix it?
I put to you that manifestly-typed languages lull you into a false sense of security, where you believe that type errors cannot occur, because the compiler checks them, but I've shown that that is not strictly true.
With this in mind, why resist the typing freedom Ruby gives you? Look back at the foo method. To make the method as flexible as possible in Java, we would have to use some interface (Appendable) as its argument. But that requires a lot of fore-thought. In Ruby, you write code that deals with the typical situation (appending to a file, for example), but the same code can append to an array or a string, even if you never considered this in your design.
Finally, what makes code brittle? It is not the lack of compile-time type checking, it is lack of tests. Whatever language you use, I urge you to think of any code that is not tested as broken. From that follows that you both Ruby and Java can be equally robust, as long as you test your code.

The ecosystem

The libraries and frameworks available for any programming language largely determines its usefulness. Let's ignore the standard library and jump straight into web application development. Consider Spring Framework and Ruby on Rails. Ignoring the "enterprisey" features of the Spring Framework, both frameworks allow you to write web applications. But there is significant difference in the approach to writing applications in RoR and Spring Framework. Ruby on Rails has strong opinions about how you should write web applications. In Ani's famous words, "it's Ruby on Rails way on no way." A more subtle way of putting it is that RoR prefers convention over configuration. RoR's approach is that if it works for 99 % of the cases, it should work for you, too. RoR makes a lot of choices for you. For example, in Spring, to write CRUD code for a domain object, you have to write (in no particular order): the controller, couple of JSPs, web.xml, repository interfaces and their implementation, their Hibernate implementation and Hibernate mappings; ideally you will also have a services tier to isolate your controllers from the database. In other words, a lot of code.
In RoR, you run script/generate DomainObject title:string body:text. This generates the "domain object", database structure, the controllers and views, all automatically, because RoR has strong views about how you should write your code.
In Spring, you can use the Spring MVC annotations or you can use XML configuration; hey, you can even replace the DispatcherServlet with your own creation. But why would you want to? In most cases, you're paid to get the CRUD operation done, not to mess with the framework.

Does that mean you don't like Spring any more?

Absolutely not; we are still Java EE developers at heart. We are keen to use Spring's power for middleware, where we can take advantage of AOP, transaction management, JMS, JMX, Hibernate and all other big guns. For the front end, though, we are keen to use something simpler.
That something must be more flexible, more efficient. And that for us is Ruby on Rails and Grails!

Friday’s programming humour

Friday, June 12th, 2009

Here’s one for your pub quiz (as long as you have a LISP interpreter to hand)
(cdr '(what-is-the-longest-river-in-Africa?) )

And another one: two programmers at a bus stop.
A: What time is it?
B: Three.
A. What?!
B. Six.

Friday’s programming humour

Friday, June 5th, 2009

How can you tell who in your programming team has degrees in physics without talking to the HR?
.
.
.
They have red bumper sticker on their rear bumpers that says “If this bumper sticker is blue, you’re driving way too fast”.