Java and webcontinuations...
For many months now, I have been attracted by the buzz around so called "webcontinuations". However, my daily work with archaic technologies that do not integrate too well with all new fancy toys did not bring too many opportunities to take a look on the buzzy topic
And then I took the challenge. One of advantages of my corporate employer is that there is some space left for personal and team development. This space is shaped in so called "team seminars". The goal is to present some shiny new technology each week to avoid being killed by boredom and frustration.
So my challenge was to present the mysterious "webcontinuations" concept to honourable audience consisting of my colleagues.
I learned two things.
First, I reminded myself how fun Smalltalk is. It was my first love in the world of programming languages, and it will always be. First kisses with blocks... Hot breath of become: on my neck...
As with many things, Smalltalk was the first to leverage power of continuations for the web.
I won't write here neither what the continuation is nor how elegantlySeaside programming model have been designed. Check it out by yourself.
What I will write about is an attempt to provide this programming model to the Java world. The man with a RIFE has dared to do so...
And indeed, the call/answer model has been implemented as in Seaside, plus additional pause() method which exists transparently in Seaside's send-response-handle-request loop. Please take a look at the example from RIFE site:
For you, who are to lazy to click and read the links of this blog - this piece of code will print a form, then pass response to user and wait for request from her browser. After that pause(), the code will wake up as it would never sleep and smoothlessly continue, as there were no HTTP communication in the middle at all.
I knew, as every child on that planet, that Java does not support continuations. So how that was done?
The answer was disappointing.
The class with this magic needs to be instrumented first, using custom classloader and esoteric tools. This is somehow invasive process, and don't look at the RIFE classloader code if you want to remain sane. In input/output terms, it works more or less the following.
Innocent input:
Life-threatening output:
I do not know how do you feel now, but I was kind of... disappointed. Marines-heavy machinery plus dodgy exceptions instead of elegant language construct.
I still keep my respect for the heroes who made it actually work... But on the other hand, for me it is an obvious proof that Java in all its nicety is missing a point in some areas. And it will never be fixed.
I really like Java, I do. I understand the reasoning on why not to add continuations to the language. But sometimes I just cannot help myself and not regret the fact that my dreamworld is different than the real one...
Java and webcontinuations then? ... No, thanks, not for me.
Let's check something else. And let me enjoy the fact that my first love has been appreciated by people that count. :)
And then I took the challenge. One of advantages of my corporate employer is that there is some space left for personal and team development. This space is shaped in so called "team seminars". The goal is to present some shiny new technology each week to avoid being killed by boredom and frustration.
So my challenge was to present the mysterious "webcontinuations" concept to honourable audience consisting of my colleagues.
I learned two things.
First, I reminded myself how fun Smalltalk is. It was my first love in the world of programming languages, and it will always be. First kisses with blocks... Hot breath of become: on my neck...
As with many things, Smalltalk was the first to leverage power of continuations for the web.
I won't write here neither what the continuation is nor how elegantlySeaside programming model have been designed. Check it out by yourself.
What I will write about is an attempt to provide this programming model to the Java world. The man with a RIFE has dared to do so...
And indeed, the call/answer model has been implemented as in Seaside, plus additional pause() method which exists transparently in Seaside's send-response-handle-request loop. Please take a look at the example from RIFE site:
public void processElement() {
int total = 0;
while (total %lt; 50) {
print(getHtmlTemplate("form"));
pause();
total += getParameterInt("answer", 0);
}
print("got a total of "+total);
}For you, who are to lazy to click and read the links of this blog - this piece of code will print a form, then pass response to user and wait for request from her browser. After that pause(), the code will wake up as it would never sleep and smoothlessly continue, as there were no HTTP communication in the middle at all.
I knew, as every child on that planet, that Java does not support continuations. So how that was done?
The answer was disappointing.
The class with this magic needs to be instrumented first, using custom classloader and esoteric tools. This is somehow invasive process, and don't look at the RIFE classloader code if you want to remain sane. In input/output terms, it works more or less the following.
Innocent input:
public void processElement() throws EngineException {
String s = "test";
pause();
System.out.println(s);
}Life-threatening output:
public void processElement() throws EngineException {
ContinuationContext continuationcontext =
ContinuationContext.createOrResetContext();
String s;
switch (continuationcontext.getLabel()) {
default:
String s1 = "test";
continuationcontext.getLocalVars()
.storeReference(1, s1);
TestClass _tmp = this;
continuationcontext.setLabel(0);
throw new PauseException(continuationcontext);
case 0:
s = (String)continuationcontext
.getLocalVars().getReference(1);
break;
}
System.out.println(s);
continuationcontext.remove();
}I do not know how do you feel now, but I was kind of... disappointed. Marines-heavy machinery plus dodgy exceptions instead of elegant language construct.
I still keep my respect for the heroes who made it actually work... But on the other hand, for me it is an obvious proof that Java in all its nicety is missing a point in some areas. And it will never be fixed.
I really like Java, I do. I understand the reasoning on why not to add continuations to the language. But sometimes I just cannot help myself and not regret the fact that my dreamworld is different than the real one...
Java and webcontinuations then? ... No, thanks, not for me.
Let's check something else. And let me enjoy the fact that my first love has been appreciated by people that count. :)


0 Comments:
Post a Comment
<< Home