Archive for the ‘Jan's Blog’ Category

Custom arguments for @RequestMapping methods

Friday, October 23rd, 2009

So, Spring MVC fans, let’s say you have a @RequestMapping-annotated method in your controller, but you would like to include a custom argument, in addition to the standard ones like Model, HttpServletRequest, and HttpServletResponse.
The solution is to implement a WebArgumentResolver. As an example, we’ll create a CurrentDateWebArgumentResolver; as its name suggests, this resolver will be able to set any Date argument in the controller method to current date.
We have to write

public class ContextExtractingWebArgumentResolver
    implements WebArgumentResolver {

    @Override
    public Object resolveArgument(MethodParameter methodParameter,
        NativeWebRequest webRequest) throws Exception {
        if (Date.class == methodParameter.getParameterType()) {
            return new Date();
        }
        return UNRESOLVED;
    }
}

We then wire-in the argument resolver in our Spring application context configuration file:

<bean class="org.springframework.web.servlet.mvc.annotation.
      AnnotationMethodHandlerAdapter"
    <property name="customArgumentResolver">
        <bean class="package.ContextExtractingWebArgumentResolver"/>
    </property>
</bean>

Once done, we can create a @RequestMapping-annotated method in our controller and include the Date argument; that argument will now receive the current date. For example, we can have

@Controller
public class HomeController {
    @RequestMapping(value="/index", method=RequestMethod.GET)
    public void index(Date date, Model m) {
        m.addAttribute("now", date);
    }
}

The model that will be passed to the view after making a GET request to the /index URL will include attribute named now; the value of the attribute will be the current time.
Naturally, this is just a simple example and injecting current date is not very enterprisey thing to do, but using the approach I have outlined, you can now set much more useful argument types. Do not forget that the implementation of the WebArgumentResolver is a Spring bean, thus having access to all features available in the Spring application context.

Spring MVC Volume II

Wednesday, October 21st, 2009

It was my pleasure to deliver my Spring MVC talk at the Java User Group meeting in London yesterday. I loved the audience–they asked the right questions at the right time; at times they were paying too much attention and spotted a bug! Mea maxima culpa, all is corrected now.

The slides and the source code are available at http://github.com/janm399/smvc/tree/master.

I’d love to hear suggestions for the next talk–I am thinking about advanced Spring MVC, where we take a look under the hood of the AnnotationMethodHandlerAdapter and its friends. Alternatively, we could leave the cushy OO world behind and dive into AOP, with focus on [Spring|Dynamic] AOP.

Please comment or tweet to @honzam399.

Spring MVC volume II

Thursday, September 24th, 2009

I will be presenting my Spring MVC talk at the next Java User Group meet up on 20th October, this time at UCL. It won’t be just a repeat of the old talk, there will be some very interesting changes.

Oh, and one more thing!

frau and laser

Go to http://www.meetup.com/Londonjavacommunity/calendar/11439650 for the event details and registration form.

Today’s web applications

Tuesday, September 22nd, 2009

I gave a talk on today’s Java EE and related web applications at yesterday’s London Digital Week event at Skills Matter. I had a full room of geeks keen to find out what I think about web application development. I hope that all attendees learned at least one new thing and that they’ll all find the time to try out Spring, Ruby on Rails or Grails.
To get started, you can download the source code of the talk from http://github.com/janm399/ldw/tree/master; the PDF version of the talk is available at http://github.com/janm399/ldw/raw/master/talk/ldw-pdf.pdf.

I’m looking forward to seeing most of you again at the next Java User Group or Spring User Group event!

Spring MVC at London JUG

Friday, September 4th, 2009

I had the privilege of giving a talk on Spring MVC at yesterday’s London JUG meeting. I had a full room of Java programmers keen to find out about how to write Spring MVC web applications. I hope that all attendees went away with good understanding of just how easy it is to write the Ms, Vs and Cs in Spring MVC.
You can download the source code of the talk from http://github.com/janm399/smvc/tree/master; the PDF version of the talk is available here.

I’d be happy to give a repeat of the talk at another JUG meeting or, if you prefer, give another talk on Spring-related topic. Spring AOP seemed to be quite popular!

That’s what I call performance

Thursday, August 27th, 2009

We are completing work on caching and routing tier we added to a lived-in system. The performance increase is very substantial and I will write a lot more about how we achieved this next week.
For now, I’ll wet your appetite with a screenshot that says it all

Service node statistics

Service node statistics


P.S. Yes, we’re talking about the RDTSC and System.nanoTime()!

Jeabu is coming

Monday, August 24th, 2009

Our new open-source project, Jeabu, is on its way. If you are very adventurous and interested in the earliest code, pull it from http://github.com/janm399/jeabu/tree/master.

COBOL is fifty

Friday, August 14th, 2009

Not today, of course, COBOL celebrated its fiftieth birthday in April. It is the hidden language, no one will openly admit that they have COBOL systems, yet there are millions of lines of COBOL code that no one dares to throw away. The number one reason is “if it ain’t broke, don’t fix it”, closely followed by the fact that software rewrites usually end in tears. Joel Spolsky gives excellent evidence on his blog.

Why is COBOL still out there?

So, how come COBOL has survived for so long? It is an old, cumbersome language; yet, it has modern compilers and runtime environments. You can compile your COBOL program on 32bit Windows, on 32 and 64bit Linux, HP-UX, Solaris, AIX, and many others.
Seven years ago, I worked in Ortex and they (not me, I was the cool new dude) were churning out thousands of lines of COBOL code every day.
I helped replace the aging Windows 3.1-style user interface with something that looked a bit more modern, but underneath the pretty UI, we were still talking to green-screen UNIX COBOL monster. And there lies the key to COBOL’s continued success.

What did COBOL give us?

It did not use complex binary protocols, it relied on the XML of the seventies — fixed width text messages. This simple data exchange format lends itself to easy integration with other systems. As long as your system can read and write fixed-width text, you can talk to COBOL-based systems — OK, I know I’m leaving out details, but bare with me.
Fast forward to 2006 and watch the WS-* approach. Why, did the WS-* people ask, are our systems talking to each other in closed binary formats; why can’t we use an easy to read and write format? Why aren’t we using XML? It turns out that WS-* approach is still cumbersome. For example, you cannot determine what a WS-* call will do just by looking at the endpoint, you need to examine the payload. Hmmf!
Enter REST. Again, we are sending structured plain text, but this time around, we’re making the most of the HTTP protocol. We use the HTTP methods properly and we give meanings to the URIs.

nmnon

Wednesday, August 12th, 2009

If you are in charge of a garden-variety Linux system, you’ll probably like nmon. It is a performance monitoring tool that combines statistics for the core kernel objects. You can see process list (similar to top), memory, network, block I/O and many other statistics, all in nice ncurses-based environment.

nmon screenshot

nmon screenshot


Interested? Get downloadin’!

SpringSource and VMWare

Wednesday, August 12th, 2009

Now that the news that VMWare bought SpringSource for a lot of money is out, let’s think about what it means for Spring developers. This post is pure speculation, something I would like to happen; the only satisfaction I will have if it does is being able to say “I said so”.

The old

I do not expect anything to happen to SpringSource’s open source products; in other words, the entire Spring Portfolio, tc Server and dm Server will remain free for us to explore and use.

The new

So, what new can SpringSource offer? Well, VMWare are in the virtualisation game, therefore I think they will offer virtualised enterprise solutions built on the Spring Portfolio and either tc Server or dm Server.
Imagine being able to download (still for free) a pre-configured dm Server or tc Server appliance. Download it, start it, and presto, you have a pre-configured enterprise-ready application server. Now, let’s take it up a notch. Imagine that these appliances allow themselves to be automatically discovered. All you need now is a central appliance (call it orchestrator, manager or clever load balancer). This central appliance would discover the tc Server or dm Server appliances on the network, monitor them, and provide a central point for deploying your applications.
Such approach would take away most of the pain of today’s enterprise deployment. You deploy to the orchestrator appliance and it then propagates the new version of the application to its worker appliances. Now, the orchestrator appliance may have access to a pool of shutdown worker appliances and it may be able to start them up if the load suddenly increases.
I think this is where the big money is: enterprises will be happy to pay licenses for the orchestrator appliance, because it solves so many headaches.

When?

So, when is this exciting future coming? I don’t know — in fact, I have no idea. But one thing is certain: if SpringSource do not start working on the orchestrator appliance, we at Cake Solutions will.