source: trunk/docs/api/examples.rst @ 981

Last change on this file since 981 was 958, checked in by djay, 5 years ago

Add documentation on how to use asyncrhonous execution using the Javascript ZOO-API

  • Property svn:keywords set to Date Author
  • Property svn:mime-type set to text/plain
File size: 3.3 KB
Line 
1.. _api-examples:
2
3Examples
4========
5
6This section gathers sample scripts using :ref:`ZOO-API <api-what>`, the `ZOO-Project <http://zoo-project.org>`__ server-side JavaScript API.
7
8ZOO-API contains many classes and functions. You can find the description list :ref:`here <api-classes>`.
9
10ZOO.Process example
11--------------------------
12
13::
14
15  function SampleService(conf,inputs,outputs){
16     var myProcess = new ZOO.Process('http://localhost/cgi-bin-new1/zoo_loader_new1.cgi','Boundary');
17     var myInputs = {InputPolygon: { type: 'complex', value: '{"type":"Polygon","coordinates":[[[-106.993853,35.998758],[-107.407233,35.997524],[-107.430525,35.997726],[-107.4824,35.99878],[-108.37013,35.999472],[-109.043633,35.996652],[-109.096265,35.997817],[-109.148763,36.001751],[-109.200981,36.008442],[-109.252775,36.017871],[-109.304,36.030014],[-109.354516,36.044835],[-106.468201,35.991497],[-106.855511,35.989504],[-106.90933,35.990676],[-106.963008,35.994743],[-106.993853,35.998758]]]}', mimeType: "application/json"} };
18     var myExecuteResult=myProcess.Execute(myInputs);
19     return {result: ZOO.SERVICE_SUCCEEDED, outputs: [ {name:"Result", value: myExecuteResult} ] };
20  }
21
22In this really short example you can see how to create ``ZOO.Process`` class instance and
23call the Execute method on such an instance. Then you'll just need to return a JavaScript
24object containing the attributes result and outputs, which I'm sure you already know
25what is about. The first is about the status of the process (can be ``ZOO.SERVICE_SUCEEDED``,
26``ZOO.SERVICE_FAILED`` and so on), the last is obviously the resulting maps (take a
27look at the maps internal data structure used by ZOO Kernel in ``service.h``).
28
29ZOO.UpdateStatus example
30-----------------------
31
32::
33
34  function SampleLongService(conf,inputs,outputs){
35     var my_i=0;
36     while(my_i<100){
37         try{
38           conf["lenv"]["status"]=my_i;
39         }
40         catch(e){
41         }
42         ZOOUpdateStatus(conf,my_i);
43         SampleService(conf,inputs,outputs);
44         my_i+=10;
45     }
46     return SampleService(conf,inputs,outputs);
47  }
48
49You can see in this sample code how to use the ``ZOOUpdateStatus`` function to update the
50current status of your running process. This information will be really helpfull when the
51ZOO Kernel will run your JavaScript Service in background mode (if the user set to ``true``
52the ``storeExecuteResponse`` parameter in his request).
53
54Asynchronous ZOO.Process example
55--------------------------------
56
57::
58
59  function RunAsynchonous(conf,inputs,outputs){
60     var formatWPS=new ZOO.Format.WPS();
61     var myProcess = new ZOO.Process(conf["main"]["serverAddress"],'Demo',true);
62     var myExecuteResult  =myProcess.Execute(myInputs,myOutputs);
63     var response=formatWPS.read(myExecuteResult);
64     while(response.status){
65         sleep(1500);
66         var response1 = ZOO.Request.Get(response.status.replace(/amp;/g,""),null);
67         response=formatWPS.read(response1);
68     }
69     conf["lenv"]["message"]="Asyncrhonous Process ended";
70     ZOO.UpdateStatus(conf,90);
71     return {result: ZOO.SERVICE_SUCCEEDED, outputs: outputs};
72   }
73
74In this sample code, by creating the ZOO.Process, providing a third
75argument set to true, we ensure that the WPS service execution will be run
76asynchronously. Once the Execute WPS response is fetched, until the
77service execution ends, the statusLocation is polled every 1,5
78second.
Note: See TracBrowser for help on using the repository browser.

Search

Context Navigation

ZOO Sponsors

http://www.zoo-project.org/trac/chrome/site/img/geolabs-logo.pnghttp://www.zoo-project.org/trac/chrome/site/img/neogeo-logo.png http://www.zoo-project.org/trac/chrome/site/img/apptech-logo.png http://www.zoo-project.org/trac/chrome/site/img/3liz-logo.png http://www.zoo-project.org/trac/chrome/site/img/gateway-logo.png

Become a sponsor !

Knowledge partners

http://www.zoo-project.org/trac/chrome/site/img/ocu-logo.png http://www.zoo-project.org/trac/chrome/site/img/gucas-logo.png http://www.zoo-project.org/trac/chrome/site/img/polimi-logo.png http://www.zoo-project.org/trac/chrome/site/img/fem-logo.png http://www.zoo-project.org/trac/chrome/site/img/supsi-logo.png http://www.zoo-project.org/trac/chrome/site/img/cumtb-logo.png

Become a knowledge partner

Related links

http://zoo-project.org/img/ogclogo.png http://zoo-project.org/img/osgeologo.png