« RIFE and WebWork, together | Main | Spring and WebWork, together »

Example of WebWork Continuations

As mentioned in my last entry, WebWork now uses RIFE's continuation engine to provide a great new way to develop web applications. Now, if you choose, you can develop your applications using logical loops and control structures. This is effectively, as Bruce Tate likes to call it, a higher level way to express your program. A simple example is the following code:

public class Guess extends ActionSupport {
    int guess;

    public String execute() throws Exception {
        int answer = new Random().nextInt(100) + 1;
        int tries = 5;

        while (answer != guess && tries > 0) {
            pause(SUCCESS);

            if (guess > answer) {
                addFieldError("guess", "Too high!");
            } else if (guess < answer) {
                addFieldError("guess", "Too low!");
            }

            tries--;
        }

        if (answer == guess) {
            addActionMessage("You got it!");
        } else {
            addActionMessage("You ran out of tries, the answer was " + answer);
        }

        return SUCCESS;
    }

    public void setGuess(int guess) {
        this.guess = guess;
    }
}

Note how the conceptual "loop" of a number guessing game can now actually be represented as a loop in your Java code? Using RIFE's continuation engine, the class is instrumented to do some tricky stuff underneath, allowing the execution to start immediately after a pause() call. The result is a much more logic action.

We're eager to see what the community thinks!

TrackBack

TrackBack URL for this entry:
http://blogs.opensymphony.com/mt/mt-tb.cgi/183

Comments

That's wicked cool! It sounds like it didn't take too long to implement either. Here's to hoping the other web frameworks add support for it too.

Hi Matt,

the extraction didn't take that long indeed, but don't forget that the continuations engine of RIFE has been in development and tested in production for several years. All this is based on the work we did beforehand. So all in all, it did take quite a while to get it done ;-)

Really? The running hread will be blocked right? And it will ocuppy a HTTP Connection...

Post a comment