Java Reference
In-Depth Information
Transaction
The Personal Information Manager stores appointments based on their date. Naturally, since users lead active
lives, appointments change all the time. A user's appointment book is constantly being updated with new or
changing appointments.
If a number of users need to agree on a date for an appointment, it would be helpful if their appointment books
could coordinate, arriving at a date that would work for everybody. That's what this example demonstrates—how
the Transaction pattern can be used to allow address books to reschedule a date for an appointment.
The basic interface that supports transactions is
AppointmentTransactionParticipant
. It defines three
methods to manage transactions (
join
,
commit
, and
cancel
) and the business method
changeDate
. This class is
a
Remote
class, since it is used to communicate between transaction participants that might reside on different
Java Virtual Machines.
Example A.254
AppointmentTransactionParticipant.java
1. import java.util.Date;
2. import java.rmi.Remote;
3. import java.rmi.RemoteException;
4. public interface AppointmentTransactionParticipant extends Remote{
5. public boolean join(long transactionID) throws RemoteException;
6. public void commit(long transactionID) throws TransactionException, RemoteException;
7. public void cancel(long transactionID) throws RemoteException;
8. public boolean changeDate(long transactionID, Appointment appointment,
9. Date newStartDate) throws TransactionException, RemoteException;
10. }
The class
AppointmentBook
represents a user's calendar, and implements the
AppointmentTransactionParticipant
interface. In addition to providing support to change an
Appointment
date, the
AppointmentBook
can initiate a change of an
Appointment
. Its method
changeAppointment
accepts a
transaction ID, an
Appointment
object, an array of other
AppointmentBooks
that should be transaction
participants, and an array of possible alternate dates for the appointment. The
changeAppointment
method
allows one of the
AppointmentBook
objects to communicate with the others using RMI, calling the
changeDate
method on every one of the participants until all agree on an alternate date for the
Appointment
.
Example A.255
AppointmentBook.java
1. import java.util.ArrayList;
2. import java.util.HashMap;
3. import java.util.Date;
4. import java.rmi.Naming;
5. import java.rmi.server.UnicastRemoteObject;
6. import java.rmi.RemoteException;
7. public class AppointmentBook implements AppointmentTransactionParticipant{
8. private static final String TRANSACTION_SERVICE_PREFIX = "transactionParticipant";
9. private static final String TRANSACTION_HOSTNAME = "localhost";
10. private static int index = 1;
11. private String serviceName = TRANSACTION_SERVICE_PREFIX + index++;
12. private HashMap appointments = new HashMap();
13. private long currentTransaction;
14. private Appointment currentAppointment;
15. private Date updateStartDate;
16.
17. public AppointmentBook(){
18. try {
19. UnicastRemoteObject.exportObject(this);
20. Naming.rebind(serviceName, this);
21. }
22. catch (Exception exc){
23. System.err.println("Error using RMI to register the AppointmentBook " + exc);
24. }
25. }
26.
27. public String getUrl(){
28. return "//" + TRANSACTION_HOSTNAME + "/" + serviceName;
29. }
30.
31. public void addAppointment(Appointment appointment){
32. if (!appointments.containsValue(appointment)){
33. if (!appointments.containsKey(appointment.getStartDate())){
34. appointments.put(appointment.getStartDate(), appointment);