Continuations with Apache Javaflow
I recently ran across Apache Javaflow, a continuations framework for Java, and was sufficiently intrigued to try it out. It turns out that downloading, installing and (successfully) using Javaflow require a few tricks. These tricks are, in reality, simple steps which just happen to be undocumented. I am posting them here hoping that they are of some use to myself (and others).
Downloading Javaflow
You need subversion (svn) to be installed and correctly configured on your development machine before you can download Javaflow. If you are on Ubuntu (or have access to an Ubuntu machine), you can get subversion by running: sudo apt get svn. On Windows, you can download and install svn from here.
Once svn is installed, create a temporary directory, and run
svn co http://svn.apache.org/repos/asf/commons/sandbox/javaflow
This checks out the Javaflow development tree under the temporary directory.
Building Javaflow
You need maven to build Javaflow. If you are on Ubuntu, you can get maven by running sudo apt get maven2. On Windows, you can download and install maven by following the instructions on the maven download page. Once you have installed maven, change back to the temporary directory where you downloaded Javaflow and run “mvn install”. (This might take a while the first time you run maven, since it has to download and install many of its plugins).
Once the maven command completes, you will find the javaflow output JAR file in your “target” directory.
Using Javaflow
You need several libraries to use Javaflow. Of course, you need the Javaflow library itself, which was built using the steps described above. Additionally, you need the following JAR files:
ant-launcher.jar, ant.jar, asm-3.1.jar, asm-analysis-3.1.jar, asm-commons-3.1.jar, asm-tree-3.1.jar, asm-util-3.1.jar, asm-xml-3.1.jar, bcel-5.2.jar, commons-jci-core-1.0.jar, commons-logging.jar.
The following program shows how to use Javaflow within eclipse (note how a new copy of the MyRunnable class is loaded in the context of the ContinuationClassLoader instance):
public class ContinuationsTest {
public static void main(String[] args) {
int status = 0;
try {
Runnable runnable = (Runnable) new ContinuationClassLoader(
new URL[] {}, Thread.currentThread()
.getContextClassLoader()).forceLoadClass(
MyRunnable.class.getName()).newInstance();
assert runnable.getClass().getClassLoader() != MyRunnable.class
.getClassLoader();
Continuation c = Continuation.startWith(runnable);
while (c != null) {
c = Continuation.continueWith(c);
}
} catch (Throwable exc) {
status = 1;
exc.printStackTrace();
} finally {
System.exit(status);
}
}
}
The code snippet shown above uses a class named “MyRunnable”, which is shown below:
public class MyRunnable implements Runnable {
public void run() {
for ( int i=0; i<10; i++){
System.out.println(i);
Continuation.suspend();
}
}
}
Running ContinuationTest in Eclipse gives:
24569170 AMonitored locals 0
19272103 AMonitored locals 0
24569170 Monitored locals 0
19272103 Monitored locals 0
0
1
2
3
4
5
6
7
8
9

Comments