|
![]() |
This example implements the smallest program in ProActive. This is the easiest program you could write, using the Active Object concept. This is just to show quickly how code can be written, with minimal knowledge of the API.
You can get a more complete "hello world" example, with deployment on a remote host, further on in the manual.
A client object displays a String
received from elsewhere (the original VM). This
illustrates the creation of an Active Object.
Only one classe is needed : we have put the main method inside the class, which when deployed will be an Active Object.
This class can be used as an Active Object, serving requests. Its creation involves the following steps:
main
method in order to instantiate one server object.Here is a possible implementation for the TinyHello
class:
TinyHello.java |
---|
package org.objectweb.proactive.examples.hello; import org.apache.log4j.Logger; import org.objectweb.proactive.ProActive; import org.objectweb.proactive.core.util.log.*; import org.objectweb.proactive.core.util.wrapper.StringMutableWrapper; import java.net.UnknownHostException; /** * A stripped-down Active Object example. * The object has only one public method, sayHello() * The object does nothing but reflect the host its on. */ public class TinyHello implements java.io.Serializable { static Logger logger = ProActiveLogger.getLogger(Loggers.EXAMPLES); private final String message = "Hello World!"; /** ProActive compulsory no-args constructor */ public TinyHello() { } /** The Active Object creates and returns information on its location * @return a StringWrapper which is a Serialized version, for asynchrony */ public StringMutableWrapper sayHello() { return new StringMutableWrapper( this.message + "\n from " + getHostName() + "\n at " + new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new java.util.Date())); } /** finds the name of the local machine */ static String getHostName() { try { return java.net.InetAddress.getLocalHost().toString(); } catch (UnknownHostException e) { return "unknown"; } } /** The call that starts the Acive Objects, and displays results. * @param args must contain the name of an xml descriptor */ public static void main(String[] args) throws Exception { // Creates an active instance of class Tiny on the local node TinyHello tiny = (TinyHello) ProActive.newActive( TinyHello.class.getName(), // the class to deploy null // the arguments to pass to the constructor, here none ); // get and display a value StringMutableWrapper received = tiny.sayHello(); // possibly remote call logger.info("On " + getHostName() + ", a message was received: " + received); // potential wait-by-necessity } } |
Implementing any remotely-accessible functionality is simply done through
normal Java methods in a normal Java class, in exactly the same manner it would have been done in a
non-distributed version of the same class. Here, the only method is sayHello
Hello
Active ObjectNow that we know how to write the class that implements the required
server-side functionalities, let us see how to create the server object. We want this active object
to be created on the current node, which is why we use newActive
with only two
parameters (done in the main
method).
The code snippet which instantiates the TinyHello
in the same VM is the following
(in the main
method):
TinyHello tiny = (TinyHello) ProActive.newActive( TinyHello.class.getName(), // the class to deploy null // the arguments to pass to the constructor, here none ); // which jvm should be used to hold the Active Object
This is exactly like invoking a method on a local object of the same type. The user does not have to deal with catching distribution-related exceptions.
As already stated, the only modification brought to the code by ProActive is
located at the place where active objects are created. All the rest of the code remains the same,
which fosters software reuse. So the way to call the sayHello
method in this example is
the following (in the main
method):
StringMutableWrapper received = tiny.sayHello(); // possibly remote call logger.info("On " + getHostName() + ", a message was received: " + received); // potential wait-by-necessity
To launch the example, you may type :
linux> java -Djava.security.policy=scripts/proactive.java.policy
-Dlog4j.configuration=file:scripts/proactive-log4j org.objectweb.proactive.examples.hello.TinyHello
windows> java -Djava.security.policy=scripts\proactive.java.policy
-Dlog4j.configuration=file:scripts\proactive-log4j org.objectweb.proactive.examples.hello.TinyHello
There are also scripts in the scripts
directory :
linux> cd scripts/unix/
linux> tinyHello.sh
windows> cd scripts/windows
windows> tinyHello.bat
[apple unix]tinyhello.sh --- Hello World tiny example --------------------------------------------- --> This ClassFileServer is reading resources from classpath ProActive Security Policy (proactive.runtime.security) not set. Runtime Security disabled Created a new registry on port 1099 //apple.inria.fr/Node628280013 successfully bound in registry at //apple.inria.fr/Node628280013 Generating class : pa.stub.org.objectweb.proactive.examples.hello.Stub_TinyHello Generating class : pa.stub.org.objectweb.proactive.core.util.wrapper.Stub_StringMutableWrapper On apple/138.96.218.62, a message was received: Hello World! from apple/138.96.218.62 at 03/11/2005 14:25:32