Loading TOC...


   $path as String,
   [$vars as Object],
   [$options as Object?]
) as Sequence


Place the specified module on the task queue for evaluation.

$path The path, relative to the specified root, of the module to be executed. The module is considered to be JavaScript if the module path ends with a file extension matching the ones configured for application/vnd.marklogic-javascript in MarkLogic's Mimetypes configuration. For more details on resolving paths, see "Importing XQuery Modules and Resolving Paths" in the Application Developer's Guide.
$vars The external variable values for this evaluation. This can either be a sequence of map:map objects, or a sequence of even length, alternating QNames and items.

Each key in the map(s) is a string representing the name of the parameter in Clark notation: "{namespaceURI}localname". The function xdmp.keyFromQName is a convenient way to generate these keys. Each entry in the map is the value of the corresponding external variable.

Alternatively, the alternating sequence should contain QName and item pairs that specify a variable name and value for an external variable.

$options The options object. The default value is null. See the xdmp.eval section for a list of options.

Required Privileges


Usage Notes

This function places the specified XQuery module in the task queue to be processed. The module will be evaluated when the task server has the available resources to process it. The tasks are processed in the order in which they are added to the queue.

Once xdmp.spawn is called, it cannot be rolled back, even if the transaction from which it is called does not complete. Therefore, use care calling xdmp.spawn from an update transaction. Once a module is spawned, its evaluation is asynchronous of the transaction in which xdmp.spawn was called. Consequently, if you call xdmp.spawn from a module, and if the module ends up retrying (for example, if a deadlock is detected), then the entire module is re-evaluated and the xdmp:spawn call is therefore called again. For details on how transactions work in MarkLogic Server, see "Understanding Transactions in MarkLogic Server" in the Developer's Guide.


  xdmp.spawn("module.xqy", null,
          "modules" : xdmp.modulesDatabase(),
          "root" : "http://example.com/application/"

  => Puts the module from the modules database with the
     URI http://example.com/application/module.xqy
     in the task server queue.


// This example uses the "result" option to use the results of a
// spawned task in the query

var x = xdmp.spawn("/oneplusone.sjs", null, {result: true})
// because xdmp.spawn returns a Sequence, 
// use fn.head to get the returned value
fn.head(x) + 2

// If  "/oneplusone.sjs" has following code: 
//    1 + 1 
// then this returns 4 

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.