Part 5. Make your compute server robust and scalable In this series, we will begin by introducing you to the unique JavaSpaces programming model, which is quite different from other network and distributed tools with which you might be familiar. In subsequent articles, we will cover the details of the JavaSpaces API and how you can use it to glue processes together into a distributed application, and describe how JavaSpaces interacts with other components of Jini. A new distributed computing model Building distributed applications with conventional network tools usually entails passing messages between processes or invoking methods on remote objects.
|Published (Last):||7 March 2006|
|PDF File Size:||19.42 Mb|
|ePub File Size:||14.8 Mb|
|Price:||Free* [*Free Regsitration Required]|
Part 5. 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. 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. 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. 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. 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. 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. 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. 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.
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. 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. 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.
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. Created object, which may look odd to you. 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. 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 aborted and all its participants are asked to roll back their state to the point before the transaction began. An example To demonstrate space-based transactions, we will show you how to write a simple space "relay" that takes the entries from a source space and copies them to a set of target spaces removing them from the source space in the process.
You do this in a transactionally secure manner, such that each entry is removed from the source space and then relayed to all target spaces in one indivisible "operation. The entry simply holds a content string and contains a no-arg constructor recall from the first article that the no-arg constructor is needed by all entries for serialization purposes.
Now you write a method that populates your source space with a set of numMessages Message entries. Next you write a method relayMessages that removes messages from the source space and copies them to a set of target spaces represented by an array of spaces.
Next you define two Message variables: one to serve as a template to match messages in your source space, and the other to hold entries you remove from the source space.
Make room for JavaSpaces, Part 4
JAVASPACES TUTORIAL PDF
Make room for JavaSpaces, Part 1