Skip to main content

Elegant Camel route configuration

·234 words·2 mins

I’m a big fan of the Camel Java DSL for defining Camel routes with a RouteBuilder. This is super easy and slim. However, in this blog post I show you a nerdy trick how this can be done even more elegant.

If you are a Camel user, you know, that defining a route for a given Camel context ctx ist just a matter to implement the configure() method of the abstract RouteBuilder class:

ctx.add(new RouteBuilder {

   @Override
   public void configure() throws Exception {
      from("file:data/inbox?noop=true")
        .to("file:data/outbox");
   }

});

Its really simple and you can use the whole Camel machinery from within your configure() method.

However, this kind of configuration can be performed even simpler. Let’s assume that you have a no-op default implementation of RouteBuilder called Routes:

public class Routes extends RouteBuilder {
    @Override
    public void configure() throws Exception { }
}

Then, the configuration can be rewritten simply as

ctx.add(new Routes {{
      from("file:data/inbox?noop=true")
        .to("file:data/outbox");
}});

This trick just uses Java’s object initializers, a not so well known language feature. The inspiration for providing the DSL context like this comes from JMockit which defines its mock expectations the same way. I think object initializers are really an elegant albeit hipster way to implement DSLs.

Although you can easily define the Routes class on your own, you might vote for this Camel issue or pull request if you want to have this in upstream Camel, too.

Related

Java EE Management is dead

·1104 words·6 mins
Now that some weeks has been passed we all had time to absorb the revised Java EE 8 proposal presented at Java One. As you know, some JSRs remained, some things were added and some stuff was dropped. Java EE Management API 2.0, supposed to be a modern successor of JSR 77, is one of the three JSRs to be dropped. What does this mean for the future of Java EE management and monitoring ?

Docker for (Java) Developers

·184 words·1 min
Recently I gave a Meetup talk for the Docker Munich Meetup Group which explained how Docker can help developers to improve integration tests and to ship applications.

Removing attachments with JavaMail

·1425 words·7 mins
If you have ever sent or received mail messages via Java, chances are high that you have used JavaMail for this task. Most of the time JavaMail does an excellent job and a lot of use cases are described in the JavaMail FAQ. But there are still some additional quirks you should be aware of when doing advanced mail operations like adding or removing attachments (or “Parts”) from existing mails retreived from some IMAP or POP3 store. This post gives a showcase for how to remove an attachment from a mail at an arbitrary level which has been obtained from an IMAP store.