This page is intended to help programmers in using JavaSymphony. It contains a tutorial followed by elements of syntax illustrated by short pieces of code.
 

Tutorial

This tutorial is an overview of the necessary steps for building an JS application. A simple application is build in the following steps. The exception thrown will be ignored for the beginning. For more details, please see the appropriate chapter (links are provided).
 
 
 
Step 1 Registering/unregistering application
First step in order to use JavaSymphony features is to register the application to the JS environment. The application should also un-register in the end. 
.... 
// ***** register is done with the creation of a JSRegistry object 
JSRegistry reg = new JSRegistry(); 
.... 
// ***** un-register the application 
reg.unregister();
Step 2 Creating VAs
Some VAs are created. A number of corresponding node, registered with JS Shell should be available. Constraints (a set of conditions) can be used for the creation of the VAs. 
.... 
// ***** create a level-2 generic VA 
VA v1 = new VA(2); 
// ***** constraint that ask for a specific node: sheila 
JSConstraints con1 = new JSConstraints(); 
con1.setConstraints(JSConstraints.C_HOST_URL, "=", "sheila"); 
// ***** create a VA for node sheila 
VA v2 = new VA(2, con1); 
//  ***** create a level-3 cluster VA containing 2 computing nodes (specified by second parameter), and add the two level-2 VAs to it 
VA v3 = new VA(3,2); v3.addVA(v1); v3.addVA (v2); 
....
Step 3 Loading codebase
If we want to build remote objects we need to have the byte-code for the respective classes on the destination node. This is done using JSCodebase object
.... 
JSCodebase cb = new JSCodebase(); 
// ***** add a class, a library file or an URL to the codebase 
cb.add("./test/HelloWordObj.class"); 
// ***** load the code on specific node (like v2) or higher level VA (like v3) 
cb.load(v3); 
....
Step 4 Creating remote objects
The remote objects are implemented using JSObject class.An instance of this class is a handler to a remote object and can be also transmitted to other remote machines. We can specify the exact destination or not, constraints etc. We must provide class name and, if any, parameters for the constructor. RMI mechanism require remote objects classes to implements Serializable interface. 
... 
// ***** build an object somewhere, no parameters for constructor 
JSObject obj1 = new JSObject("HelloWordObj"); 
// ***** build an object on v1, specifying parameters for constructor 
Object[] parameters = new Object[] { new String ("Hello world again!") }; 
JSObject obj2 = new JSObject("HelloWordObj", parameters, v2); 
....
Step 5  Invoke methods for remote object
Invoking methods for remote object is the main purpose of JavaSymphony application. With this invocations the programs are parallelized, data or code is transmitted across the network in order to build a distributed application. The method invocations are available with both remote JSObjects and shared memory SJSObjects. We have three types of method invocations descirbed below.

  1. Synchronous method invocation
                        Object[] parameters = new Object[]{};
                        Object result;
                        ResultHandle handle;
                        // ***** synchronous method invocation for obj1
                        result = obj1.sinvoke("printText", parameters);
  1. Asynchronous method invocation
                       // ***** asynchronous invoke for obj2
                      ResultHandle handle = obj.ainvoke("printText", parameters);
                      ....
                      // ***** after some time, we test if the result is available
                      if(handle.isReady())
                                 result = handle.getResult();
  1. One-sided method invocation
                      // ***** one side method invocation, result is ignored
                      obj1.oinvoke("printText", parameters)

 Finally The full example
The full example has two parts: the main application which distributed objects on two computing nodes and invoke a method for these object, and the class for the remote object. The main application will put together all the things discuss about.
/**** main application - just the processing method ****/
import js.oa.*;
import js.common.*;
....
void startJSApplication() throws Exception
{
// ***** register is done with the creation of a JSRegistry object 
JSRegistry reg = new JSRegistry(); 

// ***** create level-1 generic VAs
// ***** constraint that ask for a specific node: sheila 
JSConstraints con1 = new JSConstraints(); 
con1.setConstraints(JSConstraints.C_HOST_URL, "=", "sheila"); 
// ***** create a VA for node sheila 
VA v2 = new VA(2, con1); 
VA v1 = new VA(2); 

//  ***** create a level-2 VA, and add the two level-1 VAs to it 
VA v3 = new VA(3, 2); v3.addVA(v1); v3. addVA(v2); 

// ***** create and load the codebase on both nodes
JSCodebase cb = new JSCodebase(); 
cb.add("./test/HelloWordObj.class"); 
cb.load(v3); 

// ***** create 2 objects, one for each VA
JSObject obj1 = new JSObject("HelloWordObj", v1); 
JSObject obj2 = new JSObject("HelloWordObj", new Object[] { new String("Hello world again") }, v2);

// ***** invoke some methods
String result;
result = (String) obj1.sinvoke("printText", new Object[]{});
obj2.oinvoke("printText",  new Object[]{});

System.out.println("Result from first object: " + result);
System.out.println("No result from second object" );

v3.free();
// ***** un-register the application 
reg.unregister();

}

/**** the HelloWordObj object class -- it must implement Serializable ****/
public class HelloWordObj implements Serializable
{
    String name;
    public HelloWordObj() { name = "HelloWorld"; }
    public HelloWordObj(String newName) { name = newName; }
    public String printText() 
    {
        // ***** this will be printed in the NA window
        System.out.println("Name is:"+name);
        return name;
    } 
}