wisdom-framework / wisdom

A modular and dynamic web framework
http://wisdom-framework.org
Apache License 2.0
88 stars 42 forks source link

Support fully non-blocking actions #539

Closed magnet closed 7 years ago

magnet commented 8 years ago

Today, actions, even those returning an AsyncResult, are blocking by nature.

It should be possible to write code such as:

In the controller:

@Route(....)
public void nonBlockingAction() {
    myService.longAsyncAction(request().callback());
}

or :

@Route(....)
public FutureResult nonBlockingAction() {
    FutureResult fr = new FutureResult();
    myService.longAsyncAction(fr.callback);
    return fr; // the framework registers a listener on the callback 
}

In the service:

public void longAsyncAction(Callback callback) {
    try {
       // doing some long action on a blocking executor
       // or some async action and chaining callbacks
       long();
       callback.success(payload);
    } catch (Throwable t) {
       callback.failure(t);
    }
}

Currently, with Wisdom's ExecutorService we can setup the number of threads to get X running tasks in parallel, but we're stuck if for instance we have thousands of async non-blocking tasks that take a few minutes each...

The ideal solution should provide a way to stream results, by defining a StreamResult type that we can add elements to until we "finish" (that's probably another topic, but it only makes sense in non-blocking results).

cescoffier commented 8 years ago

That looks like a good idea. Insteaf of callback, I would recomment using vert.x future.

magnet commented 8 years ago

It would be extremely practical to be able to add to the result instead of only creating a "finished / successful" result, for instance to stream CSV files, although it might prove harder to integrate to the current design.

e.g:

callback.put(resultPart);
(later)
callback.put(resultPart);

This is a slightly more complex model, but it makes streaming results that are computed "by-line" possible.