OpenSpaces Scripting over remoting allows to simply execute dynamic languages scripts as remote "jobs".
The client uses a proxy to pass a script to the remote space holding the data, and then the execution of the "job" is done on the server side collocated with the data.
In this example, the client - the Scripting Counter, uses scripting to pass a query Groovy script - SpaceQuery to the space, where it is executed collocated with data - the {OrderEvent}} Objects, after the "job" is executed inside the server side, results are returned to the client.

The following diagram shows the work flow:

Scripting Code

Here is how we write it on the client side (Scripting Counter Processing Unit):

The pu.xml configuration file

The pu.xml contains the configuration for the client (the Scripting Counter Processing Unit in this case).
The <os-remoting:annotation-support/> enables using the @SyncScriptingExecutor annotation to get the proxy to the remote Sync Scripting Executor Service.

<!-- A bean representing a space (an IJSpace implementation). -->
<os-core:space id="space" url="jini://*/*/spaceQueries"/>

<!-- OpenSpaces simplified space API built on top of IJSpace/JavaSpace. -->
<os-core:giga-space id="gigaSpace" space="space"/>

<os-remoting:annotation-support/>
    
<bean id="scriptRunner" class="com.gigaspaces.examples.tutorials.queries.scriptingcounter.ScriptRunner"/>

The ScriptRunner bean

The ScriptRunner bean uses the syncScriptingExecutor proxy to load the QuerySpace Groovy script transparently to the remote syncScriptingExecutor service(the script - "QuerySpace" is located in the client classpath).
Notice also the parameter("lastName" /* name /, "LN7" / value */) line inside the init method, this enables setting parameters to be used by the scrippt during execution time:

public class ScriptRunner implements InitializingBean
{
    // The proxy to the remote SyncScriptingExecutor
    @SyncScriptingExecutor
    private ScriptingExecutor syncScriptingExecutor;
    
    public void setSyncScriptingExecutor(ScriptingExecutor syncScriptingExecutor) {
		this.syncScriptingExecutor = syncScriptingExecutor;
	}

    public void afterPropertiesSet() throws Exception
    {
    	init();
    }
    
    public void init() throws Exception 
    {
    	// Sends a script placed in the client classpath, to the remote sync scripting executor
    	// Results from the script execution will be returned and displayed.
    	int OrdersCount = (Integer)syncScriptingExecutor.execute(new ResourceLazyLoadingScript()
                					.cache(false) // Maintain the executed script in the server for future executions
                					.name("scriptbasedcounter")
                					.type("groovy")
                					.parameter("lastName" /* name */, "LN7" /* value */) // Parameter , referenced inside the 
                																		 // script, will be used during execution time
                					.script("classpath:/QuerySpace.groovy")); // Script location on the client side
    	
    	System.out.println("Orders count [" + OrdersCount + "]");
    }
}

The SpaceQuery Groovy script

A simple script, returns the number of accounts found with a certain lastName attribute,
The variable lastName referenced in the script is set in the ScriptRunner bean above (using the parameter() method).
If the script uses custom classes (like the Account class) they must be compiled and placed in the server GigSpaces home folder GS_HOME\lib\ext.
To run this example, place the compiled jar containing the Account class in your GS_HOME\lib\ext folder.
(When using a deployable PU, the classes should be compiled and placed in the client PU shared-lib folder).

import com.j_spaces.core.client.SQLQuery   
import com.gigaspaces.examples.tutorials.queries.common.Account

// Create a query for all accounts lastName attribute equal to the lastName sent 
// in the ScriptRunner bean
query = new SQLQuery<Account>(Account.class,"lastName = '"+lastName+"'")

// Read all accounts satisfying the query
accounts = (Object[])gigaSpace.readMultiple((Object)query,Integer.MAX_VALUE)

// This will be printed out on the server side
println 'Found ' + accounts.length + ' accounts'

//return number of accounts found
return accounts.length
GigaSpaces.com - Legal Notice - 3rd Party Licenses - Site Map - API Docs - Forum - Downloads - Blog - White Papers - Contact Tech Writing - Gen. by Atlassian Confluence