Make room for JavaSpaces, Part 4 - JavaWorld April
2000
Tutorial Details:
Make room for JavaSpaces, Part 4
Make room for JavaSpaces, Part 4
By: By Eric Freeman and Susanne Hupfer
Explore Jini transactions with JavaSpaces
he Jini transaction model is one of the lesser known and least used aspects of Jini, yet it provides a powerful tool for writing distributed applications that operate correctly in the presence of partial failure. In this article, we take a look at the Jini transaction model and show how you can use it with the JavaSpaces service -- one of the first Jini services to fully support Jini transactions.
Make room for JavaSpaces: Read the whole series!
Part 1. Ease the development of distributed apps with JavaSpaces
Part 2. Build a compute server with JavaSpaces
Part 3. Coordinate your Jini apps with JavaSpaces
Part 4. Explore Jini transactions with JavaSpaces
Part 5. Make your compute server robust and scalable
In general, transactional systems let you group together a set of operations so that they are performed atomically -- that is, either all of the operations complete, or none of them do. Without this transactional ability, the states of systems can easily become inconsistent -- especially distributed systems in which participants can crash the network or leave the network before an operation has completed. By using transactions, you can ensure that the operations do complete, or if they don't, that the state of the entire distributed system remains unchanged.
Systems that support transactions, such as a database management system, typically build transactions into the system's core. Jini takes a more lightweight and flexible approach: It provides a transaction service that manages a set of participants through a transaction process. The transaction service leads the participants through a "two-phase commit protocol," a fairly simple and standard protocol that ensures that either all participants complete their respective operations in the transaction, or none of them do. If you're interested, you can find out more about this protocol in the Jini Transaction Specification (see Resources below).
The participants in a Jini transaction are typically Jini services and devices. If you are developing a Jini service, you can enable it to participate in Jini transactions by implementing the TransactionParticipant interface. You can find out more about this interface in the Jini Transaction Specification.
Now we'll give you a better idea of how you can use transactions with JavaSpaces.
Transactions and JavaSpaces
The JavaSpaces application programming interface integrates Jini transactions in a clean and well-thought-out manner. As a result, introducing transactional security into your JavaSpaces applications is usually fairly painless. To use transactions with space-based operations, typically you first ask a transaction manager to create a transaction and manage it for a specified lease time. Then, you pass the transaction to each space operation you'd like to occur under the transaction (which may include operations over more than one space). Assuming there are no problems along the way, you then explicitly commit the transaction, which results in all operations completing. If any problems occur, you can abort the transaction, which will leave the space unchanged. The transaction might also be aborted by the transaction manager if, for instance, the transaction's lease expires.
When you write an entry into a space under a transaction, the entry is only seen "within" the transaction until it commits. This means that the entry is invisible to any client attempting to read, take, or notify it outside of the transaction. If the entry is taken within the transaction, it will never be seen outside of the transaction. If the transaction aborts, the entry is discarded. Once the transaction commits, the entry is available for reads, takes, and notifications outside of the transaction. For more details, let's look at each space operation and how it operates under a transaction.
Let's start with the write operation, which takes an entry, a transaction, and a lease:
space.write(Entry entry, Transaction txn, Lease lease);
The operation writes the lease into the space, under the given transaction, and requests the specified lease time for the entry.
You might recall that, up to now in the JavaSpaces series, we've always used a null transaction as the second parameter to write , which assumes the operation consists of one indivisible action (the operation itself). As soon as the operation completes, the entry is visible to all clients of the space. On the other hand, when we write an entry under a non- null transaction, the entry is not accessible to operations outside of the transaction until the transaction commits. If the transaction commits, then all the entries written under the transaction become visible to the entire space. However, if the transaction aborts, the entries written under the transaction are removed. In effect, after the transaction aborts, the space reflects that the operations never occurred.
Now let's look at take and read . You will recall that both take and read take a template and return a matching entry from the space, if one exists. The take operation removes the entry before returning it, while the read operation returns a copy of the entry. When you take or read entries from the space under a transaction, they can come from entries written under or outside the transaction. If the transaction aborts, any entries taken under the transaction are returned to the space (and of course, any entries written under it are removed), leaving the space as if the operations never occurred.
Finally, you can also use notify under a transaction. When you register for a notify under a transaction, you receive notifications of entries that are written within the transaction and to the general space. When the transaction completes (whether it commits or aborts), all notification registrations under the transaction are withdrawn. If the transaction commits, the entries remaining in the transaction may result in notifications as response to registrations in the general space. The entries also become eligible for read and take operations from the space.
Now that you understand the semantics of using transactions with the space operations, let's move on to creating transactions via the transaction manager, and then write code that makes use of transactions and spaces.
Using a transaction manager
To make use of transactions, you first need access to a transaction manager that can create and maintain your transactions for you. To locate a manager, you use Jini's lookup and discovery. Like all Jini services, the lookup service returns a proxy object to a transaction manager; in this specific case, you'll be looking for a service that implements the TransactionManager interface. You might want to refer to Bill Venners's previous column on lookup and discovery (see Resources ) for the specifics of locating a Jini service. Here you are going to use a simple utility class from our book JavaSpaces Principles, Patterns and Practice that obtains a handle to a transaction manager proxy (please refer to the book for the details of this utility class). Here is the code you use to obtain a proxy using the utility class:
TransactionManager mgr = TransactionManagerAccessor.getManager();
Here you call the getManager static method of the TransactionManagerAccessor class, which returns a TransactionManager proxy object. With this proxy in hand, you can create a transaction that will manage a set of operations over one or more Jini services (such as a JavaSpace), which implement the TransactionParticipant interface.
Now we'll show you how to create the transaction:
Transaction.Created trc = null;
try {
trc = TransactionFactory.create(mgr, 300000);
} catch (Exception e) {
System.err.println("Could not create transaction " + e);
}
First you declare a variable trc of type Transaction.Created (an inner class of Transaction ), which is the type of object that will be returned when you ask the transaction manager to create a new transaction (we'll return to the inner class shortly, since the syntax may be a bit confusing). To create a transaction, you then use the TransactionFactory class and call its static method create , which takes a transaction manager and a lease time (in milliseconds) as parameters and creates a transaction that the supplied manager manages for the given lease time (in this code, you should request a lease of 5 minutes). If the call to create is successful, a Transaction.Created object is returned and assigned to the variable trc . If something goes wrong during the transaction's creation, an exception is thrown instead.
Now let's revisit the Transaction.Created object, which may look odd to you. This object is simply an instantiation of the Transaction interface's public inner class, which looks like this:
public static class Created implements Serializable {
public final Transaction transaction;
public final Lease lease;
Created(Transaction transaction, Lease lease) {...}
}
This class is simple: it contains only two public fields and a constructor. This class is needed because the create call to the TransactionFactory needs to return two values -- the transaction itself and its granted lease time -- both of which the Created class wraps into one returned object. Once the call to create returns a Created object, you can simply access its two public fields ( transaction and lease ) to retrieve the respective objects. For instance, you can use the transaction field to obtain a reference to the newly created transaction object like this:
Transaction txn = trc.transaction;
Likewise, you can retrieve the transaction's lease by accessing the transaction's lease field. Note that a lease on a transaction represents the amount of time for which the transaction manager will maintain the transaction. Once a transaction expires, the transaction is a
Read
Tutorial at: Click here to view the tutorial
Rate Tutorial: Make room for JavaSpaces, Part 4 - JavaWorld April
2000
View Tutorial: Make room for JavaSpaces, Part 4 - JavaWorld April
2000
Related
Tutorials:
3D graphics programming in
Java, Part 3: OpenGL
3D graphics programming in
Java, Part 3: OpenGL |
Java security evolution
and concepts, Part 5
Java security evolution
and concepts, Part 5 |
A birds-eye view of Web services
A birds-eye view of Web services |
Rumble in the
jungle: J2EE versus .Net, Part
1
Rumble in the
jungle: J2EE versus .Net, Part
1 |
Business process
automation
made easy with
Java, Part 1
Business process
automation
made easy with
Java, Part 1 |
J2SE 1.4
breathes new life into the CORBA community, Part
3
J2SE 1.4
breathes new life into the CORBA community, Part
3 |
Check out three
collections libraries
Check out three
collections libraries |
Effort on the
edge, Part 1
Effort on the
edge, Part 1 |
J2SE 1.4
breathes new life into the CORBA community, Part
4
J2SE 1.4
breathes new life into the CORBA community, Part
4 |
Test email components in your software
Test email components in your software |
Finally, getting hands in !
Finally, getting hands in ! |
Impressive
!
Impressive
! |
Interesting
concept ...
Interesting
concept ... |
Java and GIS, Part 2: Mobile LBS
Java and GIS, Mobile LBS
Using LBS
First, let\'s make sure that we understand what an LBS application is. Typically, an LBS application is trying to answer the question \"Where am I?\" and then do something with that information. There are a number |
Filtering and Transforming Digital Images
Filtering and Transforming Digital Images
In this Issue
Welcome to the Core Java Technologies Tech Tips for April 7, 2004. Here you\'ll get tips on using core Java technologies and APIs, such as those in Java 2 Platform, Standard Edition (J2SE).
|
JSP 2.0: The New Deal, Part 4
JSP 2.0: The New Deal, Part 4
In this final part of the "JSP 2.0: The New Deal" series, we look at two new features that make it much easier to develop custom tag libraries: tag files and the new simplified tag-handler Java API. |
Access Windows Performance Monitor counters from Java, Part 1
Access Windows Performance Monitor counters from Java, Part 1
Use a simple Java API to gather valuable performance statistics
Summary
Windows NT, 2000, 2003, and XP contain a utility called the Performance Monitor that provides a rich array of perform |
JTimepiece
JTimepiece is the advanced library for working with dates and times in Java. Many easy-to-use methods in this API make it easy for any developer, from beginner to expert, to use JTimepiece. |
Oracle answers on Linux
The world's business software giant offers answers to the 10 most frequently asked questions about Linux. |
We are providing Fedora Cord 2 Linux CD's .
We are providing Fedora Cord 2 Linux CD's .
Fedora Core 2 Linux
Now Available Fedora Cord2 CD's
We are providing the free downloadable version of Fedora Core 2 CDs, which is distributed under GNU public license.
Fedora is the latest version of |
|
|
|