IBM TSpaces Lab 3 Transactions Event Registration.

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Multiplexing/Demux. CPSC Transport Layer 3-2 Multiplexing/demultiplexing application transport network link physical P1 application transport network.
Case Study Lecture 4 UML Huma Ayub Department of Software Engineering
CHAPTER 12 GENERICS Introduction to java 1. Assignment 5 Solution See Eclipse.
CMSC 202 Exceptions 2 nd Lecture. Aug 7, Methods may fail for multiple reasons public class BankAccount { private int balance = 0, minDeposit =
CS102--Object Oriented Programming
Chapter 8Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 8 l Basic Exception Handling »the mechanics of exceptions l.
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Exception Handling Introduction Exception handling is a mechanism to handle exceptions. Exceptions are error like situations. It is difficult to decide.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.
Chapter 12: Advanced Topics: Exception Handling Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 11: Handling Exceptions and Events J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Fourth.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
PROG Mobile Java Application Development PROG Mobile Java Application Development Event Handling Creating Menus.
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Software Architecture for ColdFusion Developers Unit 4: Application Events and Global Variables.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
ACE Address Configuration Executive. Why ACE? ACE provides access to several address resolution protocols under a single API ACE is the only API available.
The Java Programming Language
JavaSpaces TM By Stephan Roorda Source: JavaSpaces specification.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
LAB 1CSIS04021 Briefing on Assignment One & RMI Programming February 13, 2007.
Exceptions. Exception Abnormal event occurring during program execution Examples –Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
IBM TSpaces Lab 1 Introduction. Summary TSpaces Overview Basic Definitions Basic primitive operations Reading/writing tuples in tuplespace HelloWorld.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
1 Web Based Programming Section 8 James King 12 August 2003.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
CSE 143 Lecture 23 Polymorphism; the Object class read slides created by Marty Stepp and Ethan Apter
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Li Tak Sing COMPS311F. RMI callbacks In previous example, only the client can initiate a communication with the server. The server can only response to.
Computer Programming with JAVA Chapter 8. Exception Handling Basic Exception Handling the mechanics of exceptions Defining and Using Exceptions some "simple"
Multithreading in JAVA
Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Configuring and Troubleshooting Identity and Access Solutions with Windows Server® 2008 Active Directory®
IBM TSpaces Lab 2 Customizing tuples and fields. Summary Blocking commands Tuple Expiration Extending Tuples (The SubclassableTuple) Reading/writing user.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Interfaces and Inner Classes
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
The Vector Class. Vector Class Write a Vector class. This will be a container class that will hold objects. It should allow the following operations:
Topics Instance variables, set and get methods Encapsulation
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
1 Text File Input and Output. Objectives You will be able to Write text files from your Java programs. Read text files in your Java programs. 2.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Lecture 14 Throwing Custom Exceptions
Logger, Assert and Invariants
Java Programming Language
Why exception handling in C++?
Important terms Black-box testing White-box testing Regression testing
Important terms Black-box testing White-box testing Regression testing
Adapted from Pearson Education, Inc.
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions.
„Lambda expressions, Optional”
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

IBM TSpaces Lab 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(" ", "to", ts.write(" ", "text", "Hello"); ts.write(" ", 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 herehere