IBM TSpaces Lab 3 Transactions Event Registration.

18
IBM TSpaces Lab 3 Transactions Event Registration

Transcript of IBM TSpaces Lab 3 Transactions Event Registration.

IBM TSpacesLab 3

Transactions

Event Registration

Summary

Transactions– The TSpaces Transaction Model– A transaction code example

Event Registration– The TSpaces event registration

mechanism– An event registration code example

TSpaces Transaction Model

Transactions: Group related operations as an atomic unit. This means that either all operations are executed or all of these are cancelled.

TSpaces provide a transaction model implemented by the Transaction class

In order to execute some operations as a transaction you have to:– Create an instance of Transaction class– Call method beginTrans() of Transaction class– Add a TupleSpace object to the Transaction object using the

addSpace() method– Perform operations (write, read, ..) on TupleSpace object added to

the transaction– Call commitTrans() of Transaction object to make the effects of

operations persistent– Call abortTrans() method of Transaction object to undo all

operations that belong to the current transaction

TSpaces Transaction Model (2)

The following is an example of how a client program can make use of the transaction model. In the example, either all 3 of the writes will succeed or none of them will succeed.

Transaction trans = new Transaction(); TupleSpace ts = new TupleSpace("testing"); trans.addSpace(ts); trans.beginTrans(); // start of transaction ts.write("email", "to", “[email protected]"); ts.write("email", "text", "Hello"); ts.write("email", "from","[email protected]"); trans.commitTrans(); // end of transaction

A transaction code example

import com.ibm.tspaces.*;import java.lang.*;public class TransactionExample { public static void main(String[] argv) { String host = "localhost"; if (argv.length > 0) host = argv[0];

try { Tuple template = new Tuple(); Tuple results; // Create a couple of TupleSpaces and make sure they // are empty TupleSpace ts1 = new TupleSpace("TransactionTest1",host); ts1.deleteAll();

A transaction code example (2)

TupleSpace ts2 = new TupleSpace("TransactionTest2",host);

ts2.deleteAll(); // create a Transaction object. Transaction trans = new Transaction();

/* Add the TupleSpaces to the Transaction object This is what connects the Tuplespace to the

Transaction */ trans.addTupleSpace(ts1); trans.addTupleSpace(ts2);

A transaction code example (3)

/* Now write to the TupleSpace. We have not yet started a transaction so this is just a

standalone operation. */ ts1.write(new Tuple("standalone", "before trans")); // Now we start a transaction. trans.beginTrans(); ts1.write(new Tuple("trans1", "commit data1")); ts2.write(new Tuple("trans1", "commit data2"));

// Now commit the transaction trans.commitTrans();

A transaction code example (4)

// Now start another Transaction trans.beginTrans(); // Now write something that should disappear ts1.write(new Tuple("trans2", "aborted data1")); ts2.write(new Tuple("trans2", "aborted data2")); // Now we abort this transaction trans.abortTrans(); // Now write to the TupleSpace again. We have not // started a new transaction so this is just a // standalone operation. ts1.write(new Tuple("standalone", "after trans"));

A transaction code example (5)

// Now just verify that the contents are correct results = ts1.scan(template); if (results.numberOfFields() != 3) { System.out.println("Failed! ts1 results="+results); System.exit(1); } results = ts2.scan(template); if (results.numberOfFields() != 1) { System.out.println("Failed! ts2 results="+results); System.exit(1); } } catch(TupleSpaceException tse) { System.err.println(tse); System.exit(1); } System.out.println(“Transaction example was successful"); } // main

Event Registration

A mechanism that enables clients to register with the server in order to be informed when specific types of events occur.

For example, you might want to be informed whenever a Tuple that matches a specific template is written to the Server by any client.

With TSpaces this is done by using the TupleSpace.eventRegister() method to indicate the type of event you are interested in and specifying the Callback object that you want to have handle the event.

Event Registration (2)

Tuple template = new Tuple( "Key2", new Field(String.class) );

ExampleCallback callback = new ExampleCallback(); boolean newThread = true; // default is false int seqNum =ts.eventRegister(TupleSpace.WRITE, template,

callback, newThread ); ... ts.eventDeRegister(seqNum);

The above sets up a template Tuple that describes the format of Tuples that we are interested in.

The eventRegister() method tells the server to watch for a Tuple being written to the TupleSpace that matches the template Tuple.

When this occurs, it should invoke the call() method of the ExampleCallback object.

The setting of newThread to true indicates that a new Thread should be started to process the callback.

When you no longer want to be informed of Tuple events, you can remove the event registration with the eventDeRegister() method.

Event Registration (3)

class ExampleCallback implements Callback { public boolean call(String eventName,String tsName,int

seqNum,SuperTuple tuple,boolean isException) { if (! isException) { ...process the tuple passed to this method. String data = (String)tuple.getField(1).getValue(); } else { ... handle exception } return false;

} // call() }

The above defines a class that implements the Callback interface. The Callback interface requires the implementation of the call method with the following parameters: eventName - String that indicates what type of TupleSpace action ("Write" or "Delete“)

caused this call. Note that only events that cause a Write or Delete to be done are reported. A READ operation at the server is not reported.

tsName - Name of the TupleSpace.

Event Registration (4)

seqNum - Sequence number of the eventRegister call. tuple - The Tuple that matched the template. isException - A boolean that if true indicates that there was an

exceptional condition occurring on the server. In this case, isException will be true and the exception will be inside the tuple, ( i.e., the tuple will contain one field that is a TupleSpaceServerException.)

The call method should always return false, unless it is the last

call this callback class is expecting. In this case, it returns true.

Event Registration (5)

Warning:You cannot issue TupleSpace operations, (e. g. ts.write ) inside the call method unless the newThread option is set true. Otherwise this will cause the client thread that handles communication with the server to hang.

Warning:Although the Tuple that caused the event is passed to you in the callback, that does not mean that you have "control" of the Tuple. It may have also been passed to other clients. Depending on your purpose for getting the event, you may want to do a "take" of the Tuple to ensure ownership of the Tuple.

An event registration code example

import com.ibm.tspaces.*;import java.lang.*;

public class EventRegistrationExample {public void register(String host, TupleSpace ts){int seqNum; try { //Setup a template to describe the Tuples we are interested in. Tuple template = new Tuple( "Key1", new Field(String.class) );

// set flag for whether a new Thread will process the callback boolean newThread = true;

// Instantiate an object of the class that will handle the // calls from server ExampleCallback callback = new ExampleCallback();

//register the event you are interested in seqNum = ts.eventRegister(TupleSpace.WRITE, template,

callback,newThread);

An event registration code example (2)

System.out.println("Event Registration was successfull. Seqnum is : “ + seqNum);

} catch (TupleSpaceException tse) { System.out.println(tse); } }

public void testRegistration(String host, TupleSpace ts){ try{ ts.write("Key1","Data1"); Thread.sleep(10000);

ts.write("Key2","Data2"); Thread.sleep(10000); ts.write("Key1","Data3");

}catch (Exception e) { System.out.println(e); }

}

An event registration code example (3)

public static void main(String[] args) { String host = "localhost"; if (args.length > 0) host = args[0];

EventRegistrationExample eventRegEx = new EventRegistrationExample();

// get access to TupleSpace TupleSpace ts=null; try{ ts = new TupleSpace("EventRegistrationExample",host); } catch (Exception e) { System.out.println(e); } //register interested event eventRegEx.register(host,ts);

//test event registration eventRegEx.testRegistration(host,ts); } }

An event registration code example (4)

import com.ibm.tspaces.*;import java.lang.*;

public class ExampleCallback implements Callback, Runnable{ public boolean call(String eventName,String tsName,int seqNum,SuperTuple tuple,

boolean isException) { try { if (!isException) { System.out.println("Tuple : "+ tuple.toString()+" was

written to TupleSpace");

} else { // server exception occurred Exception e = (Exception) tuple.getField(0).getValue(); System.out.println("An server exception occured. \n " + e); } } catch (Exception e) { System.out.println(e); } return false; }

public void run(){ }

Download examples from here