Este conteúdo não está disponível no idioma selecionado.
Chapter 14. Constructing an Application Using Transactional Objects for Java
14.1. Application Construction Copiar o linkLink copiado para a área de transferência!
save_state and restore_state methods, as well as setting appropriate locks in operations and invoking the appropriate JBoss Transaction Service class constructors. The application developer's concern is defining the general structure of the application, particularly with regard to the use of atomic actions, or transactions.
14.1.1. Queue description Copiar o linkLink copiado para a área de transferência!
QUEUE_SIZE elements has been imposed for this example.
Example 14.1. Java Interface Definition of the Que Class
public class TransactionalQueue extends LockManager
{
public TransactionalQueue (Uid uid);
public TransactionalQueue ();
public void finalize ();
public void enqueue (int v) throws OverFlow, UnderFlow,
QueueError, Conflict;
public int dequeue () throws OverFlow, UnderFlow,
QueueError, Conflict;
public int queueSize ();
public int inspectValue (int i) throws OverFlow,
UnderFlow, QueueError, Conflict;
public void setValue (int i, int v) throws OverFlow,
UnderFlow, QueueError, Conflict;
public boolean save_state (OutputObjectState os, int ObjectType);
public boolean restore_state (InputObjectState os, int ObjectType);
public String type ();
public static final int QUEUE_SIZE = 40; // maximum size of the queue
private int[QUEUE_SIZE] elements;
private int numberOfElements;
};
14.1.2. Constructors and deconstructors Copiar o linkLink copiado para a área de transferência!
Example 14.2. Using an Existing Persistent Object
public TransactionalQueue (Uid u)
{
super(u);
numberOfElements = 0;
}
Example 14.3. Creating a New Persistent Object
public TransactionalQueue ()
{
super(ObjectType.ANDPERSISTENT);
numberOfElements = 0;
try
{
AtomicAction A = new AtomicAction();
A.begin(0); // Try to start atomic action
// Try to set lock
if (setlock(new Lock(LockMode.WRITE), 0) == LockResult.GRANTED)
{
A.commit(true); // Commit
}
else // Lock refused so abort the atomic action
A.rollback();
}
catch (Exception e)
{
System.err.println(“Object construction error: “+e);
System.exit(1);
}
}
begin method. Then, the operation must set an appropriate lock on the object. Afterward, the main body of the constructor is executed. If successful, the atomic action is committed. Otherwise, it is aborted.
terminate method of the LockManager method.
Example 14.4. Destructor of the Queue Class
public void finalize ()
{
super.terminate();
}
14.1.3. The save_state, restore_state, and type Methods Copiar o linkLink copiado para a área de transferência!
Example 14.5. The save_state Method
public boolean save_state (OutputObjectState os, int ObjectType)
{
if (!super.save_state(os, ObjectType))
return false;
try
{
os.packInt(numberOfElements);
if (numberOfElements > 0)
{
for (int i = 0; i < numberOfElements; i++)
os.packInt(elements[i]);
}
return true;
}
catch (IOException e)
{
return false;
}
}
Example 14.6. The restore_state Method
public boolean restore_state (InputObjectState os, int ObjectType)
{
if (!super.restore_state(os, ObjectType))
return false;
try
{
numberOfElements = os.unpackInt();
if (numberOfElements > 0)
{
for (int i = 0; i < numberOfElements; i++)
elements[i] = os.unpackInt();
}
return true;
}
catch (IOException e)
{
return false;
}
}
Example 14.7. The type Method
LockManager class, the operation type should return a transactional queue.
public String type ()
{
return "/StateManager/LockManager/TransactionalQueue";
}
14.1.4. enqueue/dequeue operations Copiar o linkLink copiado para a área de transferência!
enqueue operation in Example 14.8, “The enqueue Method” is appropriate as a guideline. The dequeue would have a similar structure, but is not included as an example.
Example 14.8. The enqueue Method
public void enqueue (int v) throws OverFlow, UnderFlow, QueueError
{
AtomicAction A = new AtomicAction();
boolean res = false;
try
{
A.begin(0);
if (setlock(new Lock(LockMode.WRITE), 0) == LockResult.GRANTED)
{
if (numberOfElements < QUEUE_SIZE)
{
elements[numberOfElements] = v;
numberOfElements++;
res = true;
}
else
{
A.rollback();
throw new UnderFlow();
}
}
if (res)
A.commit(true);
else
{
A.rollback();
throw new Conflict();
}
}
catch (Exception e1)
{
throw new QueueError();
}
}
14.1.5. The queueSize Method Copiar o linkLink copiado para a área de transferência!
public int queueSize () throws QueueError, Conflict
{
AtomicAction A = new AtomicAction();
int size = -1;
try
{
A.begin(0);
if (setlock(new Lock(LockMode.READ), 0) == LockResult.GRANTED)
size = numberOfElements;
if (size != -1)
A.commit(true);
else
{
A.rollback();
throw new Conflict();
}
}
catch (Exception e1)
{
throw new QueueError();
}
return size;
}
14.1.6. The inspectValue and setValue Methods Copiar o linkLink copiado para a área de transferência!
Note
setValue is not shown, but it can be inferred from the inspectValue method which is shown.
public int inspectValue (int index) throws UnderFlow,
OverFlow, Conflict, QueueError
{
AtomicAction A = new AtomicAction();
boolean res = false;
int val = -1;
try
{
A.begin();
if (setlock(new Lock(LockMode.READ), 0) == LockResult.GRANTED)
{
if (index < 0)
{
A.rollback();
throw new UnderFlow();
}
else
{
// array is 0 - numberOfElements -1
if (index > numberOfElements -1)
{
A.rollback();
throw new OverFlow();
}
else
{
val = elements[index];
res = true;
}
}
}
if (res)
A.commit(true);
else
{
A.rollback();
throw new Conflict();
}
}
catch (Exception e1)
{
throw new QueueError();
}
return val;
}
14.1.7. The Client Copiar o linkLink copiado para a área de transferência!
Example 14.9. Creating an Instance of the TransactionalQueue Object
public static void main (String[] args)
{
TransactionalQueue myQueue = new TransactionalQueue();
queueSize method.
Example 14.10. The queueSize Method
AtomicAction A = new AtomicAction();
int size = 0;
try
{
A.begin(0);
s
try
{
size = queue.queueSize();
}
catch (Exception e)
{
}
if (size >= 0)
{
A.commit(true);
System.out.println(“Size of queue: “+size);
}
else
A.rollback();
}
catch (Exception e)
{
System.err.println(“Caught unexpected exception!”);
}
14.1.8. Notes Copiar o linkLink copiado para a área de transferência!
enqueue operations atomically, you can nest the enqueue operations inside another enclosing atomic action. In addition, concurrent operations on a persistent object are serialized, preventing inconsistencies in the state of the object. Be aware that since the elements of the queue objects are not individually concurrency controlled, certain combinations of concurrent operation invocations are executed serially, when logically they could be executed concurrently. This happens when modifying the states of two different elements in the queue.