back to API     back to index     back to guided tour index     prev     next  

3.2. Initialization of the activity

Active objects, as indicates their name, have an activity of their own (an internal thread).
It is possible to add pre and post processing to this activity, just by implementing the interfaces InitActive and EndActive, that define the methods initActivity and endActivity.

The following example will help you to understand how and when you can initialize and clean the activity.

When instanciated, the activity of an object is automatically started, but it will first do what is written in the initActivity method.

Ending the activity can only be done from inside the active object (i.e. from a call to its own body). This is the reason why we have written a terminate method in the following example.

Design of the application

The InitializedHello class extends the Hello class, and implements the interfaces InitActive and EndActive.It acts a a server for the InitializedHelloClient class.

The main method is overriden so that it can instantiate the InitializedHello class

Programming

InitializedHello

The source code of the InitializedHello class is here.

initActivity and endActivity here just log messages onto the console, so you can see when they are called.

initActivity is called at the creation of the active object, while endActive is called after the activity has terminated (thanks to the method terminate).

Here is the initActivity method :

public void initActivity(Body body) {
	System.out.println("I am about to start my activity");
}

Here is the endActivity method :

public void endActivity(Body body) {
	System.out.println("I have finished my activity");
}

The following code shows how to terminate the activity of the active object :

public void terminate() {
	// the termination of the activity is done through a call on the
	// terminate method of the body associated to the current active object
	ProActive.getBodyOnThis().terminate();
}

The only differences from the the previous example is the classes instantiated, which are now InitializedHello (and not Hello) and InitializedHelloClient, and you will add a call to hello.terminate().

InitializedHello: Code is here.

InitializedHelloClient: Code is here.

So, create InitializedHelloClient.java and InitializedHello.java in src/org/objectweb/proactive/examples/hello

Now compile all proactive sources

cd compile
windows>build.bat examples
linux>build examples
cd ..

Add "./classes" directory to CLASSPATH to use these two new source files

windows>set CLASSPATH=.;.\classes;.\ProActive_examples.jar;.\ProActive.jar;.\lib\bcel.jar;.\lib\asm.jar;.\lib\log4j.jar;.\lib\xercesImpl.jar;.\lib\fractal.jar;.\lib\bouncycastle.jar linux>export CLASSPATH=.:./classes:./ProActive_examples.jar:./ProActive.jar:./lib/bcel.jar:./lib/asm.jar:./lib/log4j.jar:./lib/xercesImpl.jar:./lib/fractal.jar:./lib/bouncycastle.jar

Execution

Execution is similar to the previous example; just use the InitializedHelloClient client class and InitializedHello server class.

Starting the server

linux> java -Djava.security.policy=scripts/proactive.java.policy -Dlog4j.configuration=file:scripts/proactive-log4j org.objectweb.proactive.examples.hello.InitializedHello

windows> java -Djava.security.policy=scripts\proactive.java.policy -Dlog4j.configuration=file:scripts\proactive-log4j org.objectweb.proactive.examples.hello.InitializedHello &

Launching the client

linux> java -Djava.security.policy=scripts/proactive.java.policy -Dlog4j.configuration=file:scripts/proactive-log4j org.objectweb.proactive.examples.hello.InitializedHelloClient //localhost/Hello

windows> java -Djava.security.policy=scripts\proactive.java.policy -Dlog4j.configuration=file:scripts\proactive-log4j org.objectweb.proactive.examples.hello.InitializedHelloClient //localhost/Hello