use of org.jaffa.transaction.domain.TransactionPayload in project jaffa-framework by jaffa-projects.
the class JaffaTransactionMessageService method getTransactionPayloadByTransactionId.
/**
* Gets the TransactionPayload of the Transaction with the input ID.
*
* @param transactionId the ID of the Transaction to return the TransactionPayload of
* @return the TransactionPayload of the Transaction with the input ID
* @throws FrameworkException
*/
@Override
public TransactionPayload getTransactionPayloadByTransactionId(String transactionId) throws FrameworkException {
UOW uow = null;
TransactionPayload result = null;
try {
uow = new UOW();
Criteria criteria = new Criteria();
criteria.setTable(TransactionPayloadMeta.getName());
criteria.addCriteria(TransactionPayloadMeta.ID, transactionId);
Iterator itr = uow.query(criteria).iterator();
if (itr.hasNext()) {
result = (TransactionPayload) itr.next();
}
} finally {
if (uow != null) {
uow.close();
}
}
return result;
}
use of org.jaffa.transaction.domain.TransactionPayload in project jaffa-framework by jaffa-projects.
the class TransactionConsumer method process.
/**
* This method is invoked directly when processing a Transaction synchronously.
* It'll invoke the handler associated with the input transaction's payload, as obtained from the transaction configuration file.
* In case of an error, the status of the transaction will be set to 'E'.
*
* @param uow the provided uow.
* @param transactionId the transaction ID.
* @param unsavedTransaction the transaction if available, otherwise null.
*/
public UOW process(UOW uow, String transactionId, Transaction unsavedTransaction) throws Exception {
if (log.isDebugEnabled()) {
log.debug("Processing Transaction " + transactionId);
}
// Update Transaction status to I
Boolean postImmediate = Parser.parseBoolean((String) ContextManagerFactory.instance().getProperty(Transaction.RULE_POST_IMMEDIATE));
// If the postImmediate is true then we shall make the update to the transaction within same scope of the UOW as this must be synchronous transaction
if (postImmediate != null && postImmediate.booleanValue()) {
if (unsavedTransaction == null) {
// This transaction must already be in the database.
TransactionEngine.getInstance().updateTransactionStatusToInProcess(uow, transactionId);
} else {
uow.flush();
unsavedTransaction.setStatus(Transaction.Status.I.name());
}
} else {
TransactionEngine.getInstance().updateTransactionStatusToInProcess(transactionId);
}
boolean createdLoggingContext = false;
try {
Transaction transaction = (unsavedTransaction != null) ? unsavedTransaction : Transaction.findByPK(uow, transactionId);
TransactionPayload payload = (transaction != null) ? transaction.getTransactionPayloadObject() : null;
Object dataBean = (payload != null) ? payload.moldInternalPayload() : null;
if (dataBean != null) {
// Load transaction configuration
TransactionInfo transactionInfo = ConfigurationService.getInstance().getTransactionInfo(dataBean);
if (transactionInfo != null) {
// Sets Log4J's MDC to enable BusinessEventLogging
LoggingService.setLoggingContext(dataBean, transactionInfo, transaction);
createdLoggingContext = true;
if (log.isInfoEnabled()) {
log.info(MessageHelper.findMessage("label.Jaffa.Transaction.TransactionConsumer.start", null));
}
int retryLimit = readRule(RULE_RETRY_LIMIT, DEFAULT_RETRY_LIMIT);
int retrySleepTimeInMillis = readRule(RULE_RETRY_SLEEP_TIME, DEFAULT_RETRY_SLEEP_TIME);
int retryCount = 0;
while (true) {
try {
// Invokes the handler as specified by the 'toClass and toMethod' combination in the transaction configuration
invokeHandler(uow, transactionInfo, dataBean);
break;
} catch (Exception e) {
if (postImmediate == null || !postImmediate) {
// Retry only if the exception is listed as a retryable exception
String[] exceptions = readRule(RETRY_EXCEPTION_RULE, DEFAULT_RETRY_EXCEPTIONS);
Exception ex = null;
Class<Exception> clazz = null;
for (String exceptionName : exceptions) {
if (log.isDebugEnabled()) {
log.debug("Exception: " + exceptionName + " defined as a retryable exception.");
}
clazz = (Class<Exception>) Class.forName(exceptionName);
ex = clazz.cast(ExceptionHelper.extractException(e, clazz));
if (ex != null) {
break;
}
}
if (ex != null && ++retryCount <= retryLimit) {
if (log.isDebugEnabled()) {
log.debug(clazz.getSimpleName() + " encountered. Will sleep for " + retrySleepTimeInMillis + " milliseconds and then retry", e);
}
uow.rollback();
Thread.sleep(retrySleepTimeInMillis);
uow = new UOW();
if (log.isDebugEnabled()) {
log.debug("Retry#" + retryCount);
}
} else {
throw e;
}
} else {
throw e;
}
}
}
if (log.isInfoEnabled()) {
log.info(MessageHelper.findMessage("label.Jaffa.Transaction.TransactionConsumer.success", null));
}
} else {
if (log.isDebugEnabled()) {
log.debug("There is no transactionInfo for the Transaction. Hence nothing to process.");
}
}
} else {
if (log.isDebugEnabled()) {
log.debug("There is no payload for the Transaction. Hence nothing to process.");
}
}
TransactionField[] transactionFields = transaction.getTransactionFieldArray();
boolean keep = false;
if (transactionFields != null) {
for (TransactionField tField : transactionFields) {
if (KEEP.equals(tField.getFieldName()) && Boolean.parseBoolean(tField.getValue())) {
keep = true;
break;
}
}
}
if (log.isDebugEnabled()) {
log.info("Finished with transaction: " + transactionId);
}
// Commit the UOW if it isn't a post immediate to catch any exceptions that might occur
if (postImmediate == null || !postImmediate.booleanValue()) {
if (keep) // Update Transaction status to S
{
TransactionEngine.getInstance().updateTransactionStatusToSatisfied(uow, transactionId);
} else // Delete Transaction Record
{
TransactionEngine.getInstance().deleteTransaction(uow, transactionId);
}
try {
uow.commit();
} catch (Exception e) {
log.error("Error committing UOW in TransactionConsumer.process", e);
throw e;
}
} else {
uow.flush();
TransactionMessageDAOFactory.getTransactionMessageDAO().delete(uow, transaction);
}
if (log.isDebugEnabled()) {
log.debug("Successfully processed Transaction " + transaction);
}
} catch (Exception e) {
// Update Transaction status to E
if (log.isInfoEnabled()) {
log.info(MessageHelper.findMessage("label.Jaffa.Transaction.TransactionConsumer.error", null));
}
// Rollback the UOW if there is an error
if (postImmediate == null || !postImmediate.booleanValue()) {
try {
uow.rollback();
} catch (Exception exception) {
log.error("Error rolling back UOW in transaction consumer");
}
}
// Only need to update the transaction if the process is being run asynchronously.
if (postImmediate == null || !postImmediate.booleanValue()) {
// release the UOWs connection before creating a new one to move this transaction to the error state
uow.rollback();
// Update Transaction status to E
TransactionEngine.getInstance().updateTransactionStatusToError(transactionId, e);
log.error(MessageHelper.findMessage("label.Jaffa.Transaction.TransactionConsumer.error", null), e);
}
throw e;
} finally {
// Unset the Logging context
if (createdLoggingContext) {
LoggingService.unsetLoggingContext();
}
}
return uow;
}
use of org.jaffa.transaction.domain.TransactionPayload in project jaffa-framework by jaffa-projects.
the class TransactionAdmin method invokeHandler.
/**
* Invokes the intended handler. This Handler must implement the IMessageHandler Interface in order to be invoked.
*
* @param uow
* @param transaction
* @param methodName
* @throws JaffaMessagingFrameworkException
*/
private static void invokeHandler(UOW uow, Transaction transaction, String methodName) throws Exception {
try {
TransactionPayload tp = transaction.getTransactionPayloadObject();
Object dataBean = tp != null ? tp.moldInternalPayload() : null;
if (dataBean != null) {
// Load transaction configuration
TransactionInfo transactionInfo = ConfigurationService.getInstance().getTransactionInfo(tp.getInternalMessageClass());
if (transactionInfo == null) {
throw new JaffaMessagingFrameworkException(JaffaTransactionFrameworkException.TRANSACTION_INFO_MISSING, new Object[] { tp.getInternalMessageClass() });
}
// Obtain the handlerClass
if (transactionInfo.getToClass() == null || transactionInfo.getToClass().length() == 0) {
if (log.isDebugEnabled()) {
log.debug(methodName + " toClass is not defined in data bean configuration: " + transactionInfo.getDataBean());
}
return;
}
Class handlerClass = Class.forName(transactionInfo.getToClass());
// Class dataBeanClass = Class.forName(transactionInfo.getDataBean());
if (IMessageHandler.class.isAssignableFrom(handlerClass)) {
// Unmarshals the Message payload into a dataBean using the dataBeanClassName
Method handlerMethod = null;
Object handlerObject = null;
// Obtain the handler method
try {
handlerMethod = handlerClass.getMethod(methodName, new Class[] { UOW.class, Map.class, Object.class });
} catch (NoSuchMethodException e) {
// Hence use the dataBeanClass specified in the messageInfo to get the appropriate handlerMethod
if (log.isDebugEnabled()) {
log.debug(methodName + " method not found in " + handlerClass.getName());
}
return;
}
handlerObject = handlerClass.newInstance();
Map<String, String> headerMap = new HashMap<String, String>();
// Sets the transactionField elements as defined in the configuration file.
TransactionField[] fields = transaction.getTransactionFieldArray();
if (fields != null) {
for (TransactionField field : fields) {
headerMap.put(field.getFieldName(), field.getValue());
}
}
// Invoke the handler
if (log.isDebugEnabled()) {
log.debug("Invoking the handler " + handlerMethod);
}
handlerMethod.invoke(handlerObject, new Object[] { uow, headerMap, dataBean });
}
}
} catch (Exception e) {
// Just log the error
log.error("Exception thrown while deleting the transaction. Transaction was: " + transaction, e);
throw e;
}
}
use of org.jaffa.transaction.domain.TransactionPayload in project jaffa-framework by jaffa-projects.
the class JaffaTransactionMessageService method getTransactionPayload.
/**
* Gets the TransactionPayload with the input ID.
*
* @param transactionPayloadId the ID of the Transaction to return the TransactionPayload of
* @return the TransactionPayload of the Transaction with the input ID
* @throws FrameworkException
*/
@Override
public TransactionPayload getTransactionPayload(String transactionPayloadId) throws FrameworkException {
UOW uow = null;
TransactionPayload payload = null;
try {
uow = new UOW();
Criteria criteria = new Criteria();
criteria.setTable(TransactionPayloadMeta.getName());
criteria.addCriteria(TransactionPayloadMeta.ID, transactionPayloadId);
for (Object result : uow.query(criteria)) {
if (result instanceof TransactionPayload) {
payload = (TransactionPayload) result;
break;
}
}
} finally {
if (uow != null) {
uow.close();
}
}
return payload;
}
Aggregations