Nuxeo Studio rocks. It’s a fact!
You can build a killer business application with Nuxeo Studio while listening to good music(1), and that’s exactly what we are going to do. So let’s get started!
While listening to the wonderful song Belavista Man(2) and paying a basic level of attention to the subtle lyrics, we will build an asynchronous task in Nuxeo Studio. What is an asynchronous task? Well, it is a task that runs asynchronously; the task and all its load of work is run in another thread letting the current execution continue without any interruption. Here’s a schema that will explain it:
In this example, the synchronous task would run for a minute. It is not a problem by itself but it is too long for user interaction or when short timeouts are expected.
For example, maybe the user wants to extract captions or to watermark a set of videos from your Nuxeo Digital Asset Management (DAM) and this processing takes quite some time for each video. Doing it synchronously will certainly lead to a “Server not responding” error in the browser, which is definitely not user-friendly. This is why asynchronicity is important! In this context, we should delegate the work to another task so that running the task will be immediate for the user.
For the purpose of our example, let’s build the synchronous chain described in the schema above. Then we will move the long running part to an asynchronous sub-task. Create the following “LongRunningChain” chain in Studio:
Here, we are using a little trick to fake the long running operation: Calling the
Run Script operation with the
Java Thread.sleep() method to pause for 10s.
Now, create a User Action that will trigger this chain. Name it “TestLongRunningChain” for example. Let it be in the toolbar (for easy access while testing) and bind it to the chain you just created. Deploy the changes on your Nuxeo server and when it is done click this new toolbar button. I hope you are patient, because you are going to wait 10 seconds watching your screen do basically nothing!
How can we make the long task run asynchronously? The principle is: When it is time to run the long task we send an asynchronous event which performs the long task. We can say it is quite basic after all:
1/ Have an asynchronous event handler that reacts to a specific event and performs the long running task 2/ Have a chain that just sends this specific event
Yes. It’s that simple! Let’s build it in three steps before the end of the song.
First, add a new custom event in the Core Events Studio registry. We name this Core Event, say “LongRunEvent”. Here it is:
Second, create the “LongRunningChain_Handler” Automation Chain which performs the long work (waiting here for 10 seconds again).
Third, create a new Event Handler, “LongRunningEventHandler”. Activate it for the “LongRunEvent” event, check its “Is Asynchronous box”(3) and bind it to the LongRunningChain_Handler chain:
Fourth and the last step(4): Change the original “LongRunningChain” so that instead of performing the long task it just sends the correct event:
Now, deploy your changes and test. No more waiting for the task to complete! Doesn’t Nuxeo Studio really rock? Try out this example and let us know what you think.