Piraku - Java co-routines

Piraku is a Java library based on co-routines allowing the easier development of event driven applications.
This is a proof of concept to enable an event driven web-server, I never had the time to explore this further.

Background

Event Driven applications in Java tend to usually follow the observer pattern.
The issue with this approach is that, as a developer, one has to maintain the call state in an external state object.
This, especially with heavy event driven (and thus multi threaded) applications, causes that one looses the actual execution context quite quickly.
Piraku hides this complex manual state management and allows the development of asynchronous applications following traditional development methodologies.
See the example below for how it is intended to work.

Features

Limitations

License

Apache License

Use-Case

Update user details

	// mark this function as a co-routine
	// a bytecode enhancer needs to be run on this class
	@CoSync
	public void foo(Long a) throws Exception {
		try {
			System.out.println(System.currentTimeMillis());
			System.out.println("Enter monitor");
			synchronized (this) {
				FutureResource<Object> userFuture = getUser();
				
				// PirakuResources.await is detected by the bytecode enhancer
				// and causes the co-routine to return at this stage
				// the system then resumes execution of this co-routine at this very stage
				// once the userFuture resource becomes available
				PirakuResources.await(userFuture);
				
				Object user = userFuture.get();
				System.out.println(System.currentTimeMillis());
				bind(user);
				a = 2l;
				FutureResource<Object> save = saveUser(user);
				PirakuResources.await(save);
				System.out.println(System.currentTimeMillis());
			}
			System.out.println("Exit monitor");
		} catch (Exception e) {
			System.out.println("Error");
		} finally {
			System.out.println("finally");
			FutureResource<Object> userFuture2 = getUser();
			PirakuResources.await(userFuture2);
			Object user2 = userFuture2.get();
		}
	}
			
By using co-routines one can suspend the execution of a method at arbitrary instruction points.
The bytecode enhancer detects specific PirakuResources. calls and causes the execution to suspend
while at the same time automatically storing the current execution state of the method.
Once the resource becomes ready, it resumes the execution of the method (with the same state) at the very same instruction point.

Source






(c) Pressenna Sockalingasamy