Running forms in java threads

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

Dave
Ebase Staff
Posts: 89
Joined: Mon Sep 10, 2007 11:48 am

Running forms in java threads

#1

Postby Dave » Thu Apr 16, 2015 2:28 pm

Sometimes you want a number of processing steps to happen at the same time, i.e. not have them wait in line until its their turn to execute. For example, you might want to update a statistics database, write to an audit log, or maybe send a load of emails out - all things that could quite happily be done in parallel, in the background, and without your users having to wait for them to complete before they get their screen back.

A convenient way to do this in server-side JavaScript is to wrap your processing logic into the before form event of a form, and then run that form in parallel using a separate java thread for each execution.

For example:

Code: Select all

importPackage(com.ebasetech.xi.api);
importPackage(com.ebasetech.xi.services);

// packages for calling HTTP and Thread
importPackage(java.io);
importPackage(java.net);
importPackage(java.lang);

// define an object containing a run method
var m = function(param) {
	         // define a URL to run an Ebase form, passing in a parameter
	         var url = "http://localhost:3030/ufs/ufsmain?formid=THREAD_TEST&PARAM1="+param;
	         // define the run method
            this.run = function() {
              	         // run the form passing in a parameter
              	         try {
                            var myurl = new URL(url);
                            var con = myurl.openConnection();
                            var ins = con.getInputStream();
              	         }
              	         catch (e) {}
              	         finally {con.disconnect;}
	         }
}

// run the form in 10 seperate threads
for &#40;var i=0; i<10; i++&#41; &#123;
      var r1 = new java.lang.Runnable&#40;new m&#40;i&#41;&#41;;
      new java.lang.Thread&#40;r1&#41;.start&#40;&#41;;	
&#125;
As you can see, we just create a JavaScript object with a run() method that can receive a parameter.

The parameter gets tacked on to the end of a URL to run an Xi form. The run() method simply makes an HTTP call using the form URL, which causes the form to execute.

We then loop from 0 to 9, creating a new thread to execute the form separately for each loop value, i.e. by passing in a new instance of our object with the loop value as a parameter.

The thread automatically executes whatever's defined inside the run() method and ends as soon as the method completes.

In the example we're calling a local form but there'd be nothing to stop you calling a form in another Xi instance.

NB. If you do want to do this then you will need to set

Code: Select all

Ufs.skipBrowserAttributesCheck=true
in the ufsetup properties of the Xi instance where the form you plan to call via the thread is running...otherwise your form won't be executed when you make the HTTP call.
0 x

Who is online

Users browsing this forum: No registered users and 14 guests