use of nl.nn.adapterframework.task.TimeoutGuard in project iaf by ibissource.
the class Receiver method processMessageInAdapter.
/*
* Assumes message is read, and when transacted, transaction is still open.
*/
private Message processMessageInAdapter(Object rawMessageOrWrapper, Message message, String messageId, String technicalCorrelationId, Map<String, Object> threadContext, long waitingDuration, boolean manualRetry, boolean duplicatesAlreadyChecked) throws ListenerException {
long startProcessingTimestamp = System.currentTimeMillis();
// if (message==null) {
// requestSizeStatistics.addValue(0);
// } else {
// requestSizeStatistics.addValue(message.length());
// }
lastMessageDate = startProcessingTimestamp;
log.debug(getLogPrefix() + "received message with messageId [" + messageId + "] (technical) correlationId [" + technicalCorrelationId + "]");
if (StringUtils.isEmpty(messageId)) {
messageId = Misc.createSimpleUUID();
if (log.isDebugEnabled())
log.debug(getLogPrefix() + "generated messageId [" + messageId + "]");
}
if (getChompCharSize() != null || getElementToMove() != null || getElementToMoveChain() != null) {
log.debug(getLogPrefix() + "compact received message");
try {
CompactSaxHandler handler = new CompactSaxHandler();
handler.setChompCharSize(getChompCharSize());
handler.setElementToMove(getElementToMove());
handler.setElementToMoveChain(getElementToMoveChain());
handler.setElementToMoveSessionKey(getElementToMoveSessionKey());
handler.setRemoveCompactMsgNamespaces(isRemoveCompactMsgNamespaces());
handler.setContext(threadContext);
try {
XmlUtils.parseXml(message.asInputSource(), handler);
message = new Message(handler.getXmlString());
} catch (Exception e) {
warn("received message could not be compacted: " + e.getMessage());
}
handler = null;
} catch (Exception e) {
String msg = "error during compacting received message to more compact format";
error(msg, e);
throw new ListenerException(msg, e);
}
}
String businessCorrelationId = null;
if (correlationIDTp != null) {
try {
message.preserve();
businessCorrelationId = correlationIDTp.transform(message, null);
} catch (Exception e) {
// throw new ListenerException(getLogPrefix()+"could not extract businessCorrelationId",e);
log.warn(getLogPrefix() + "could not extract businessCorrelationId");
}
if (StringUtils.isEmpty(businessCorrelationId)) {
String cidText;
if (StringUtils.isNotEmpty(getCorrelationIDXPath())) {
cidText = "xpathExpression [" + getCorrelationIDXPath() + "]";
} else {
cidText = "styleSheet [" + getCorrelationIDStyleSheet() + "]";
}
if (StringUtils.isNotEmpty(technicalCorrelationId)) {
log.info(getLogPrefix() + "did not find correlationId using " + cidText + ", reverting to correlationId of transfer [" + technicalCorrelationId + "]");
businessCorrelationId = technicalCorrelationId;
}
}
} else {
businessCorrelationId = technicalCorrelationId;
}
if (StringUtils.isEmpty(businessCorrelationId) && StringUtils.isNotEmpty(messageId)) {
log.info(getLogPrefix() + "did not find (technical) correlationId, reverting to messageId [" + messageId + "]");
businessCorrelationId = messageId;
}
log.info(getLogPrefix() + "messageId [" + messageId + "] technicalCorrelationId [" + technicalCorrelationId + "] businessCorrelationId [" + businessCorrelationId + "]");
threadContext.put(PipeLineSession.businessCorrelationIdKey, businessCorrelationId);
String label = null;
if (labelTp != null) {
try {
message.preserve();
label = labelTp.transform(message, null);
} catch (Exception e) {
// throw new ListenerException(getLogPrefix()+"could not extract label",e);
log.warn(getLogPrefix() + "could not extract label: (" + ClassUtils.nameOf(e) + ") " + e.getMessage());
}
}
try {
final Message messageFinal = message;
if (!duplicatesAlreadyChecked && hasProblematicHistory(messageId, manualRetry, rawMessageOrWrapper, () -> messageFinal, threadContext, businessCorrelationId)) {
if (!isTransacted()) {
log.warn(getLogPrefix() + "received message with messageId [" + messageId + "] which has a problematic history; aborting processing");
}
numRejected.increase();
setExitState(threadContext, ExitState.REJECTED, 500);
return Message.nullMessage();
}
if (isDuplicateAndSkip(getMessageBrowser(ProcessState.DONE), messageId, businessCorrelationId)) {
setExitState(threadContext, ExitState.SUCCESS, 304);
return Message.nullMessage();
}
if (getCachedProcessResult(messageId) != null) {
numRetried.increase();
}
} catch (Exception e) {
String msg = "exception while checking history";
error(msg, e);
throw wrapExceptionAsListenerException(e);
}
IbisTransaction itx = new IbisTransaction(txManager, getTxDef(), "receiver [" + getName() + "]");
// update processing statistics
// count in processing statistics includes messages that are rolled back to input
startProcessingMessage(waitingDuration);
PipeLineSession pipelineSession = null;
String errorMessage = "";
boolean messageInError = false;
Message result = null;
PipeLineResult pipeLineResult = null;
try {
Message pipelineMessage;
if (getListener() instanceof IBulkDataListener) {
try {
IBulkDataListener<M> bdl = (IBulkDataListener<M>) getListener();
pipelineMessage = new Message(bdl.retrieveBulkData(rawMessageOrWrapper, message, threadContext));
} catch (Throwable t) {
errorMessage = t.getMessage();
messageInError = true;
error("exception retrieving bulk data", t);
ListenerException l = wrapExceptionAsListenerException(t);
throw l;
}
} else {
pipelineMessage = message;
}
numReceived.increase();
// Note: errorMessage is used to pass value from catch-clause to finally-clause!
pipelineSession = createProcessingContext(businessCorrelationId, threadContext, messageId);
// threadContext=pipelineSession; // this is to enable Listeners to use session variables, for instance in afterProcessMessage()
try {
if (getMessageLog() != null) {
getMessageLog().storeMessage(messageId, businessCorrelationId, new Date(), RCV_MESSAGE_LOG_COMMENTS, label, pipelineMessage);
}
log.debug(getLogPrefix() + "preparing TimeoutGuard");
TimeoutGuard tg = new TimeoutGuard("Receiver " + getName());
try {
if (log.isDebugEnabled())
log.debug(getLogPrefix() + "activating TimeoutGuard with transactionTimeout [" + getTransactionTimeout() + "]s");
tg.activateGuard(getTransactionTimeout());
pipeLineResult = adapter.processMessageWithExceptions(businessCorrelationId, pipelineMessage, pipelineSession);
setExitState(threadContext, pipeLineResult.getState(), pipeLineResult.getExitCode());
pipelineSession.put("exitcode", "" + pipeLineResult.getExitCode());
result = pipeLineResult.getResult();
errorMessage = "exitState [" + pipeLineResult.getState() + "], result [";
if (!Message.isEmpty(result) && result.size() > ITransactionalStorage.MAXCOMMENTLEN) {
// Since we can determine the size, assume the message is preserved
errorMessage += result.asString().substring(0, ITransactionalStorage.MAXCOMMENTLEN);
} else {
errorMessage += result;
}
errorMessage += "]";
int status = pipeLineResult.getExitCode();
if (status > 0) {
errorMessage += ", exitcode [" + status + "]";
}
if (log.isDebugEnabled()) {
log.debug(getLogPrefix() + "received result: " + errorMessage);
}
messageInError = itx.isRollbackOnly();
} finally {
log.debug(getLogPrefix() + "canceling TimeoutGuard, isInterrupted [" + Thread.currentThread().isInterrupted() + "]");
if (tg.cancel()) {
errorMessage = "timeout exceeded";
if (Message.isEmpty(result)) {
result = new Message("<timeout/>");
}
messageInError = true;
}
}
if (!messageInError && !isTransacted()) {
messageInError = !pipeLineResult.isSuccessful();
}
} catch (Throwable t) {
if (TransactionSynchronizationManager.isActualTransactionActive()) {
log.debug("<*>" + getLogPrefix() + "TX Update: Received failure, transaction " + (itx.isRollbackOnly() ? "already" : "not yet") + " marked for rollback-only");
}
error("Exception in message processing", t);
errorMessage = t.getMessage();
messageInError = true;
if (pipeLineResult == null) {
pipeLineResult = new PipeLineResult();
}
if (Message.isEmpty(pipeLineResult.getResult())) {
pipeLineResult.setResult(adapter.formatErrorMessage("exception caught", t, message, messageId, this, startProcessingTimestamp));
}
throw wrapExceptionAsListenerException(t);
} finally {
putSessionKeysIntoThreadContext(threadContext, pipelineSession);
}
// }
if (getSender() != null) {
String sendMsg = sendResultToSender(result);
if (sendMsg != null) {
errorMessage = sendMsg;
}
}
} finally {
try {
cacheProcessResult(messageId, errorMessage, new Date(startProcessingTimestamp));
if (!isTransacted() && messageInError && !manualRetry) {
final Message messageFinal = message;
moveInProcessToError(messageId, businessCorrelationId, () -> messageFinal, new Date(startProcessingTimestamp), errorMessage, rawMessageOrWrapper, TXNEW_CTRL);
}
try {
Map<String, Object> afterMessageProcessedMap = threadContext;
if (pipelineSession != null) {
threadContext.putAll(pipelineSession);
}
try {
if (getListener() instanceof IHasProcessState && !itx.isRollbackOnly()) {
ProcessState targetState = messageInError && knownProcessStates.contains(ProcessState.ERROR) ? ProcessState.ERROR : ProcessState.DONE;
changeProcessState(rawMessageOrWrapper, targetState, errorMessage);
}
getListener().afterMessageProcessed(pipeLineResult, rawMessageOrWrapper, afterMessageProcessedMap);
} catch (Exception e) {
if (manualRetry) {
// Somehow messages wrapped in MessageWrapper are in the ITransactionalStorage.
// This might cause class cast exceptions.
// There are, however, also Listeners that might use MessageWrapper as their raw message type,
// like JdbcListener
error("Exception post processing after retry of message messageId [" + messageId + "] cid [" + technicalCorrelationId + "]", e);
} else {
error("Exception post processing message messageId [" + messageId + "] cid [" + technicalCorrelationId + "]", e);
}
throw wrapExceptionAsListenerException(e);
}
} finally {
long finishProcessingTimestamp = System.currentTimeMillis();
finishProcessingMessage(finishProcessingTimestamp - startProcessingTimestamp);
if (!itx.isCompleted()) {
// NB: Spring will take care of executing a commit or a rollback;
// Spring will also ONLY commit the transaction if it was newly created
// by the above call to txManager.getTransaction().
// txManager.commit(txStatus);
itx.commit();
} else {
String msg = "Transaction already completed; we didn't expect this";
warn(msg);
throw new ListenerException(getLogPrefix() + msg);
}
}
} finally {
if (pipelineSession != null) {
if (!Message.isEmpty(result) && result.isScheduledForCloseOnExitOf(pipelineSession)) {
// Don't close Message in case it's passed to a 'parent' adapter or ServiceDispatcher.
log.debug("unscheduling result message from close on exit");
result.unscheduleFromCloseOnExitOf(pipelineSession);
}
pipelineSession.close();
}
}
}
if (log.isDebugEnabled())
log.debug(getLogPrefix() + "messageId [" + messageId + "] correlationId [" + businessCorrelationId + "] returning result [" + result + "]");
return result;
}
use of nl.nn.adapterframework.task.TimeoutGuard in project iaf by ibissource.
the class TimeoutGuardPipe method doPipe.
@Override
public PipeRunResult doPipe(Message message, PipeLineSession session) throws PipeRunException {
ParameterValueList pvl = null;
if (getParameterList() != null) {
try {
pvl = getParameterList().getValues(message, session);
} catch (ParameterException e) {
throw new PipeRunException(this, getLogPrefix(session) + "exception on extracting parameters", e);
}
}
int timeout_work;
String timeout_work_str = getParameterValue(pvl, "timeout");
if (timeout_work_str == null) {
timeout_work = getTimeout();
} else {
timeout_work = Integer.valueOf(timeout_work_str);
}
log.debug(getLogPrefix(session) + "setting timeout of [" + timeout_work + "] s");
TimeoutGuard tg = new TimeoutGuard(timeout_work, getName()) {
@Override
protected void abort() {
// The guard automatically kills the current thread, additional threads maybe 'killed' by implementing killPipe.
killPipe();
}
};
try {
return doPipeWithTimeoutGuarded(message, session);
} catch (Exception e) {
String msg = e.getClass().getName();
if (isThrowException()) {
throw new PipeRunException(this, msg, e);
} else {
String msgString = msg + ": " + e.getMessage();
log.error(msgString, e);
String msgCdataString = "<![CDATA[" + msgString + "]]>";
Message errorMessage = new Message("<error>" + msgCdataString + "</error>");
return new PipeRunResult(getSuccessForward(), errorMessage);
}
} finally {
if (tg.cancel()) {
// Throw a TimeOutException
String msgString = "TimeOutException";
Exception e = new TimeoutException("exceeds timeout of [" + timeout_work + "] s, interupting");
if (isThrowException()) {
throw new PipeRunException(this, msgString, e);
} else {
// This is used for the old console, where a message is displayed
log.error(msgString, e);
String msgCdataString = "<![CDATA[" + msgString + ": " + e.getMessage() + "]]>";
Message errorMessage = new Message("<error>" + msgCdataString + "</error>");
return new PipeRunResult(getSuccessForward(), errorMessage);
}
}
}
}
use of nl.nn.adapterframework.task.TimeoutGuard in project iaf by ibissource.
the class TransactionAttributePipeLineProcessor method processPipeLine.
@Override
public PipeLineResult processPipeLine(PipeLine pipeLine, String messageId, Message message, PipeLineSession pipeLineSession, String firstPipe) throws PipeRunException {
try {
// TransactionStatus txStatus = txManager.getTransaction(txDef);
IbisTransaction itx = new IbisTransaction(txManager, pipeLine.getTxDef(), "pipeline of adapter [" + pipeLine.getOwner().getName() + "]");
try {
TimeoutGuard tg = new TimeoutGuard("pipeline of adapter [" + pipeLine.getOwner().getName() + "]");
Throwable tCaught = null;
try {
tg.activateGuard(pipeLine.getTransactionTimeout());
PipeLineResult pipeLineResult = pipeLineProcessor.processPipeLine(pipeLine, messageId, message, pipeLineSession, firstPipe);
boolean mustRollback = false;
if (pipeLineResult == null) {
mustRollback = true;
log.warn("Pipeline received null result for messageId [" + messageId + "], transaction (when present and active) will be rolled back");
} else {
if (!pipeLineResult.isSuccessful()) {
mustRollback = true;
log.warn("Pipeline result state [" + pipeLineResult.getState() + "] for messageId [" + messageId + "] is not equal to [" + ExitState.SUCCESS + "], transaction (when present and active) will be rolled back");
}
}
if (mustRollback) {
try {
itx.setRollbackOnly();
} catch (Exception e) {
throw new PipeRunException(null, "Could not set RollBackOnly", e);
}
}
return pipeLineResult;
} catch (Throwable t) {
tCaught = t;
throw tCaught;
} finally {
if (tg.cancel()) {
if (tCaught == null) {
throw new InterruptedException(tg.getDescription() + " was interrupted");
}
log.warn("Thread interrupted, but propagating other caught exception of type [" + ClassUtils.nameOf(tCaught) + "]");
}
}
} catch (Throwable t) {
log.debug("setting RollBackOnly for pipeline after catching exception");
itx.setRollbackOnly();
if (t instanceof Error) {
throw (Error) t;
} else if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else if (t instanceof PipeRunException) {
throw (PipeRunException) t;
} else {
throw new PipeRunException(null, "Caught unknown checked exception", t);
}
} finally {
// txManager.commit(txStatus);
itx.commit();
}
} catch (RuntimeException e) {
throw new PipeRunException(null, "RuntimeException calling PipeLine with tx attribute [" + pipeLine.getTransactionAttribute() + "]", e);
}
}
use of nl.nn.adapterframework.task.TimeoutGuard in project iaf by ibissource.
the class TransactionAttributePipeProcessor method processPipe.
@Override
protected PipeRunResult processPipe(PipeLine pipeLine, IPipe pipe, Message message, PipeLineSession pipeLineSession, ThrowingFunction<Message, PipeRunResult, PipeRunException> chain) throws PipeRunException {
PipeRunResult pipeRunResult;
TransactionDefinition txDef;
int txTimeout = 0;
if (pipe instanceof HasTransactionAttribute) {
HasTransactionAttribute taPipe = (HasTransactionAttribute) pipe;
txDef = taPipe.getTxDef();
txTimeout = taPipe.getTransactionTimeout();
} else {
txDef = SpringTxManagerProxy.getTransactionDefinition(TransactionDefinition.PROPAGATION_SUPPORTS, txTimeout);
}
IbisTransaction itx = new IbisTransaction(txManager, txDef, "pipe [" + pipe.getName() + "]");
try {
TimeoutGuard tg = new TimeoutGuard("pipeline of adapter [" + pipeLine.getOwner().getName() + "] running pipe [" + pipe.getName() + "]");
Throwable tCaught = null;
try {
tg.activateGuard(txTimeout);
pipeRunResult = chain.apply(message);
} catch (Throwable t) {
tCaught = t;
throw tCaught;
} finally {
if (tg.cancel()) {
if (tCaught == null) {
throw new PipeRunException(pipe, tg.getDescription() + " was interrupted");
}
log.warn("Thread interrupted, but propagating other caught exception of type [" + ClassUtils.nameOf(tCaught) + "]");
}
}
} catch (Throwable t) {
log.debug("setting RollBackOnly for pipe [" + pipe.getName() + "] after catching exception");
itx.setRollbackOnly();
if (t instanceof Error) {
throw (Error) t;
} else if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else if (t instanceof PipeRunException) {
throw (PipeRunException) t;
} else {
throw new PipeRunException(pipe, "Caught unknown checked exception", t);
}
} finally {
itx.commit();
}
return pipeRunResult;
}
use of nl.nn.adapterframework.task.TimeoutGuard in project iaf by ibissource.
the class JobDef method executeJob.
/**
* Called from {@link ConfiguredJob} which should trigger this job definition.
*/
@Override
public final void executeJob(IbisManager ibisManager) {
if (!incrementCountThreads()) {
String msg = "maximum number of threads that may execute concurrently [" + getNumThreads() + "] is exceeded, the processing of this thread will be aborted";
getMessageKeeper().add(msg, MessageKeeperLevel.ERROR);
log.error(getLogPrefix() + msg);
return;
}
try {
if (beforeExecuteJob(ibisManager)) {
if (getLocker() != null) {
String objectId = null;
try {
objectId = getLocker().acquire(getMessageKeeper());
} catch (Exception e) {
getMessageKeeper().add(e.getMessage(), MessageKeeperLevel.ERROR);
log.error(getLogPrefix() + e.getMessage());
}
if (objectId != null) {
TimeoutGuard tg = new TimeoutGuard("Job " + getName());
try {
tg.activateGuard(getTransactionTimeout());
runJob(ibisManager);
} finally {
if (tg.cancel()) {
log.error(getLogPrefix() + "thread has been interrupted");
}
}
try {
getLocker().release(objectId);
} catch (Exception e) {
String msg = "error while removing lock: " + e.getMessage();
getMessageKeeper().add(msg, MessageKeeperLevel.WARN);
log.warn(getLogPrefix() + msg);
}
} else {
getMessageKeeper().add("unable to acquire lock [" + getName() + "] did not run");
}
} else {
runJob(ibisManager);
}
} else {
getMessageKeeper().add("job execution skipped");
}
} finally {
decrementCountThreads();
}
}
Aggregations