Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions TXFramework/wsba-participant-completion-simple/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ following steps occur:
5. The Web service operation is invoked.
6. The TXFramework automatically enlists a participant in this BA. This allows the Web Service logic to respond to protocol events, such as compensate and close via annotated methods (@Compensate and @Close etc).
7. The service invokes the business logic. In this case, an email send is simulated to confirm the order.
9. Providing the above steps were successful, the service notifies the coordinator that it has completed. The service has now made its changes visible and is not holding any locks. Allowing the service to notify completion is an optimisation that prevents the holding of locks, whilst waiting for other participants to complete. This notification is required as the Service participates in the `ParticipantCompletion` protocol.
9. Providing the method returns without throwing an exception, the TXFramework notifies the coordinator that it has completed. The service has now made its changes visible and is not holding any locks. Allowing the service to notify completion is an optimisation that prevents the holding of locks, whilst waiting for other participants to complete. This notification is required as the Service participates in the `ParticipantCompletion` protocol.
10. The client can then decide to complete or cancel the BA. If the client decides to complete, all participants will be told to close. If the participant decides to cancel, all participants will be told to compensate. Which for this example, results in an email being sent confirming cancellation.

There is another test that shows what happens if the client cancels the BA.
Expand Down Expand Up @@ -105,9 +105,7 @@ Test success:
08:02:36,791 INFO [stdout] (http-localhost-127.0.0.1-8080-2) [CLIENT] Beginning Business Activity (All calls to Web services that support WS-BA wil be included in this activity)
08:02:36,809 INFO [stdout] (http-localhost-127.0.0.1-8080-2) [CLIENT] invoking placeOrder('a book') on WS
08:02:37,038 INFO [stdout] (http-localhost-127.0.0.1-8080-4) [SERVICE] invoked placeOrder('a book')
08:02:37,038 INFO [stdout] (http-localhost-127.0.0.1-8080-4) [SERVICE] Attempt to email an order confirmation, if successful notify the coordinator that we have completed our work
08:02:37,038 INFO [stdout] (http-localhost-127.0.0.1-8080-4) [SERVICE] sent email: 'Your order is now confirmed for the following item: 'a book''
08:02:37,039 INFO [stdout] (http-localhost-127.0.0.1-8080-4) [SERVICE] Email sent successfully, notifying coordinator of completion
08:02:37,038 INFO [stdout] (http-localhost-127.0.0.1-8080-4) [SERVICE] Attempt to email an order confirmation. Failure would raise an exception causing the coordinator to be informed that this participant cannot complete.
08:02:37,161 INFO [stdout] (http-localhost-127.0.0.1-8080-2) [CLIENT] Closing Business Activity (This will cause the BA to complete successfully)

Test cancel:
Expand All @@ -117,9 +115,8 @@ Test cancel:
08:02:37,517 INFO [stdout] (http-localhost-127.0.0.1-8080-2) [CLIENT] Beginning Business Activity (All calls to Web services that support WS-BA will be included in this activity)
08:02:37,535 INFO [stdout] (http-localhost-127.0.0.1-8080-2) [CLIENT] invoking placeOrder('a book') on WS
08:02:37,789 INFO [stdout] (http-localhost-127.0.0.1-8080-4) [SERVICE] invoked placeOrder('a book')
08:02:37,789 INFO [stdout] (http-localhost-127.0.0.1-8080-4) [SERVICE] Attempt to email an order confirmation, if successful notify the coordinator that we have completed our work
08:02:37,789 INFO [stdout] (http-localhost-127.0.0.1-8080-4) [SERVICE] Attempt to email an order confirmation. Failure would raise an exception causing the coordinator to be informed that this participant cannot complete.
08:02:37,789 INFO [stdout] (http-localhost-127.0.0.1-8080-4) [SERVICE] sent email: 'Your order is now confirmed for the following item: 'a book''
08:02:37,789 INFO [stdout] (http-localhost-127.0.0.1-8080-4) [SERVICE] Email sent successfully, notifying coordinator of completion
08:02:37,909 INFO [stdout] (http-localhost-127.0.0.1-8080-2) [CLIENT] Cancelling Business Activity (This will cause the work to be compensated)
08:02:38,059 INFO [stdout] (TaskWorker-1) [SERVICE] @Compensate
08:02:38,060 INFO [stdout] (TaskWorker-1) [SERVICE] sent email: 'Unfortunately, we have had to cancel your order for item 'a book''
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@
*/
public class EmailSender {

public static boolean sendEmail(String message)
public static void sendEmail(String message) throws OrderServiceException
{
System.out.println("[SERVICE] sent email: '" + message + "'");
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
import org.jboss.narayana.quickstarts.wsba.participantcompletion.simple.jaxws.OrderServiceBA;
import org.jboss.narayana.txframework.api.annotation.lifecycle.ba.Cancel;
import org.jboss.narayana.txframework.api.annotation.lifecycle.ba.Compensate;
import org.jboss.narayana.txframework.api.annotation.lifecycle.ba.Completes;
import org.jboss.narayana.txframework.api.annotation.service.ServiceRequest;
import org.jboss.narayana.txframework.api.annotation.transaction.Compensatable;
import org.jboss.narayana.txframework.api.configuration.transaction.CompletionType;
import org.jboss.narayana.txframework.api.exception.TXControlRuntimeException;
import org.jboss.narayana.txframework.api.management.TXDataMap;
import org.jboss.narayana.txframework.api.management.WSBATxControl;

import javax.inject.Inject;
import javax.jws.WebMethod;
import javax.jws.WebService;
Expand All @@ -45,13 +48,6 @@
@WebServlet("/OrderServiceBA")
public class OrderServiceBAImpl implements OrderServiceBA {

/*
* The @TxManagement injection provides a control to the running transaction. It can be used to notify the coordinator of important events. For example if the
* participant has completed it's work or if the participant is unable to complete.
*/
@Inject
public WSBATxControl txControl;

/*
The @DataManagement injection provides a map that that is isolated to the transaction participant. This allows the service to store data that
can be retrieved when the protocol lifecycle methods are invoked by the coordinator (those annotated with @Compensate, @Cancel, etc).
Expand All @@ -64,7 +60,7 @@ can be retrieved when the protocol lifecycle methods are invoked by the coordina
* This flag is used by the test to check whether the order was confirmed or cancelled. It is not thread safe as the same instance is used by all threads.
* As this is a simple example and the variable is only used by the (sequentially ran) test, we are not too concerned about this.
*/
private static boolean orderConfirmed = false;
private static volatile boolean orderConfirmed = false;

/**
* Places an order for the specified item. As this is a simple example, all the method does is attempt to send the confirmation email.
Expand All @@ -73,8 +69,8 @@ can be retrieved when the protocol lifecycle methods are invoked by the coordina
* @throws OrderServiceException if an error occurred when making the order
*/
@WebMethod
// This annotation is used by the TXFramework to know that this method participates in the BA
@ServiceRequest
@ServiceRequest // This annotation is used by the TXFramework to know that this method participates in the BA
@Completes
public void placeOrder(String item) throws OrderServiceException {

System.out.println("[SERVICE] invoked placeOrder('" + item + "')");
Expand All @@ -88,39 +84,10 @@ public void placeOrder(String item) throws OrderServiceException {
* completed. Otherwise, we notify the coordinator that we cannot complete. If any other participant fails or the client
* decides to cancel we can rely upon being told to compensate.
*/
System.out.println("[SERVICE] Attempt to email an order confirmation, if successful notify the coordinator that we have completed our work");

boolean success = EmailSender.sendEmail("Your order is now confirmed for the following item: '" + item + "'");

if (success) {
try {
System.out.println("[SERVICE] Email sent successfully, notifying coordinator of completion");
// tell the coordinator manager we have finished our work
txControl.completed();
orderConfirmed = true;
} catch (Exception e) {
/*
* Failed to notify the coordinator that we have finished our work. Compensate the work and throw an Exception
* to notify the client that the order operation failed.
*/
doCompensate();
System.err.println("[SERVICE] 'completed' callback failed");
throw new OrderServiceException("Error when notifying the coordinator that the work is completed", e);
}
} else {
try {
/*
* tell the participant manager we cannot complete. this will force the activity to fail
*/
System.out.println("[SERVICE] Email send failed, notifying coordinator that we cannot complete");
txControl.cannotComplete();
} catch (Exception e) {
System.err.println("'cannotComplete' callback failed");
throw new OrderServiceException("Error when notifying the coordinator that the work is cannot be completed", e);
}
throw new OrderServiceException("Email send failed, order cancelled");
}
System.out.println("[SERVICE] Attempt to email an order confirmation. Failure would raise an exception causing the coordinator to be informed that this participant cannot complete.");

EmailSender.sendEmail("Your order is now confirmed for the following item: '" + item + "'");
orderConfirmed = true;
}

/**
Expand All @@ -129,6 +96,7 @@ public void placeOrder(String item) throws OrderServiceException {
*/
@Cancel
public void cancel() {

System.out.println("[SERVICE] @Cancel (The participant should compensate any work done within this BA)");
doCompensate();
}
Expand All @@ -139,12 +107,14 @@ public void cancel() {
*/
@Compensate
public void compensate() {

System.out.println("[SERVICE] @Compensate");
doCompensate();
}

private void doCompensate() {
String item = (String) txDataMap.get("value");

String item = txDataMap.get("value");
EmailSender.sendEmail("Unfortunately, we have had to cancel your order for item '" + item + "'");
orderConfirmed = false;
}
Expand All @@ -157,6 +127,7 @@ private void doCompensate() {
*/
@WebMethod
public boolean orderConfirmed() {

return orderConfirmed;
}

Expand All @@ -168,6 +139,7 @@ public boolean orderConfirmed() {
*/
@WebMethod
public void reset() {

orderConfirmed = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*
* @author paul.robinson@redhat.com, 2012-01-04
*/
public class OrderServiceException extends Exception {
public class OrderServiceException extends RuntimeException {
public OrderServiceException(String message) {
super(message);
}
Expand Down