use of nl.nn.adapterframework.core.IHasProcessState 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.core.IHasProcessState in project iaf by ibissource.
the class Receiver method moveInProcessToError.
public void moveInProcessToError(String originalMessageId, String correlationId, ThrowingSupplier<Message, ListenerException> messageSupplier, Date receivedDate, String comments, Object rawMessage, TransactionDefinition txDef) {
if (getListener() instanceof IHasProcessState) {
ProcessState targetState = knownProcessStates.contains(ProcessState.ERROR) ? ProcessState.ERROR : ProcessState.DONE;
try {
changeProcessState(rawMessage, targetState, comments);
} catch (ListenerException e) {
log.error(getLogPrefix() + "Could not set process state to ERROR", e);
}
}
ISender errorSender = getErrorSender();
ITransactionalStorage<Serializable> errorStorage = getErrorStorage();
if (errorSender == null && errorStorage == null && knownProcessStates().isEmpty()) {
log.debug(getLogPrefix() + "has no errorSender, errorStorage or knownProcessStates, will not move message with id [" + originalMessageId + "] correlationId [" + correlationId + "] to errorSender/errorStorage");
return;
}
throwEvent(RCV_MESSAGE_TO_ERRORSTORE_EVENT);
log.debug(getLogPrefix() + "moves message with id [" + originalMessageId + "] correlationId [" + correlationId + "] to errorSender/errorStorage");
TransactionStatus txStatus = null;
try {
txStatus = txManager.getTransaction(txDef);
} catch (Exception e) {
log.error(getLogPrefix() + "Exception preparing to move input message with id [" + originalMessageId + "] to error sender", e);
// no use trying again to send message on errorSender, will cause same exception!
return;
}
Message message = null;
try {
if (errorSender != null) {
message = messageSupplier.get();
errorSender.sendMessage(message, null);
}
if (errorStorage != null) {
Serializable sobj;
if (rawMessage == null) {
if (message == null) {
message = messageSupplier.get();
}
if (message.isBinary()) {
sobj = message.asByteArray();
} else {
sobj = message.asString();
}
} else {
if (rawMessage instanceof Serializable) {
sobj = (Serializable) rawMessage;
} else {
try {
sobj = new MessageWrapper(rawMessage, getListener());
} catch (ListenerException e) {
log.error(getLogPrefix() + "could not wrap non serializable message for messageId [" + originalMessageId + "]", e);
if (message == null) {
message = messageSupplier.get();
}
sobj = message;
}
}
}
errorStorage.storeMessage(originalMessageId, correlationId, receivedDate, comments, null, sobj);
}
txManager.commit(txStatus);
} catch (Exception e) {
log.error(getLogPrefix() + "Exception moving message with id [" + originalMessageId + "] correlationId [" + correlationId + "] to error sender or error storage, original message: [" + message + "]", e);
try {
if (!txStatus.isCompleted()) {
txManager.rollback(txStatus);
}
} catch (Exception rbe) {
log.error(getLogPrefix() + "Exception while rolling back transaction for message with id [" + originalMessageId + "] correlationId [" + correlationId + "], original message: [" + message + "]", rbe);
}
}
}
use of nl.nn.adapterframework.core.IHasProcessState in project iaf by ibissource.
the class Receiver method configure.
/**
* This method is called by the <code>IAdapter</code> to let the
* receiver do things to initialize itself before the <code>startListening</code>
* method is called.
* @see #startRunning
* @throws ConfigurationException when initialization did not succeed.
*/
@Override
public void configure() throws ConfigurationException {
configurationSucceeded = false;
try {
super.configure();
if (StringUtils.isEmpty(getName())) {
if (getListener() != null) {
setName(ClassUtils.nameOf(getListener()));
} else {
setName(ClassUtils.nameOf(this));
}
}
registerEvent(RCV_CONFIGURED_MONITOR_EVENT);
registerEvent(RCV_CONFIGURATIONEXCEPTION_MONITOR_EVENT);
registerEvent(RCV_STARTED_RUNNING_MONITOR_EVENT);
registerEvent(RCV_SHUTDOWN_MONITOR_EVENT);
registerEvent(RCV_SUSPENDED_MONITOR_EVENT);
registerEvent(RCV_RESUMED_MONITOR_EVENT);
registerEvent(RCV_THREAD_EXIT_MONITOR_EVENT);
TXNEW_PROC = SpringTxManagerProxy.getTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRES_NEW, getTransactionTimeout());
// to use direct member variables).
if (this.tmpInProcessStorage != null) {
if (this.errorSender == null && messageBrowsers.get(ProcessState.ERROR) == null) {
messageBrowsers.put(ProcessState.ERROR, this.tmpInProcessStorage);
info("has errorStorage in inProcessStorage, setting inProcessStorage's type to 'errorStorage'. Please update the configuration to change the inProcessStorage element to an errorStorage element, since the inProcessStorage is no longer used.");
getErrorStorage().setType(IMessageBrowser.StorageType.ERRORSTORAGE.getCode());
} else {
info("has inProcessStorage defined but also has an errorStorage or errorSender. InProcessStorage is not used and can be removed from the configuration.");
}
// Set temporary in-process storage pointer to null
this.tmpInProcessStorage = null;
}
// Do propagate-name AFTER changing the errorStorage!
propagateName();
if (getListener() == null) {
throw new ConfigurationException(getLogPrefix() + "has no listener");
}
if (!StringUtils.isEmpty(getElementToMove()) && !StringUtils.isEmpty(getElementToMoveChain())) {
throw new ConfigurationException("cannot have both an elementToMove and an elementToMoveChain specified");
}
if (!(getHideMethod().equalsIgnoreCase("all")) && (!(getHideMethod().equalsIgnoreCase("firstHalf")))) {
throw new ConfigurationException(getLogPrefix() + "invalid value for hideMethod [" + getHideMethod() + "], must be 'all' or 'firstHalf'");
}
if (getListener() instanceof ReceiverAware) {
((ReceiverAware) getListener()).setReceiver(this);
}
if (getListener() instanceof IPushingListener) {
IPushingListener<M> pl = (IPushingListener<M>) getListener();
pl.setHandler(this);
pl.setExceptionListener(this);
}
if (getListener() instanceof IPortConnectedListener) {
IPortConnectedListener<M> pcl = (IPortConnectedListener<M>) getListener();
pcl.setReceiver(this);
}
if (getListener() instanceof IPullingListener) {
setListenerContainer(createListenerContainer());
}
if (getListener() instanceof JdbcFacade) {
((JdbcFacade) getListener()).setTransacted(isTransacted());
}
if (getListener() instanceof JMSFacade) {
((JMSFacade) getListener()).setTransacted(isTransacted());
}
getListener().configure();
if (getListener() instanceof HasPhysicalDestination) {
info("has listener on " + ((HasPhysicalDestination) getListener()).getPhysicalDestinationName());
}
if (getListener() instanceof HasSender) {
// only informational
ISender sender = ((HasSender) getListener()).getSender();
if (sender instanceof HasPhysicalDestination) {
info("Listener has answer-sender on " + ((HasPhysicalDestination) sender).getPhysicalDestinationName());
}
}
if (getListener() instanceof ITransactionRequirements) {
ITransactionRequirements tr = (ITransactionRequirements) getListener();
if (tr.transactionalRequired() && !isTransacted()) {
ConfigurationWarnings.add(this, log, "listener type [" + ClassUtils.nameOf(getListener()) + "] requires transactional processing", SuppressKeys.TRANSACTION_SUPPRESS_KEY, getAdapter());
// throw new ConfigurationException(msg);
}
}
ISender sender = getSender();
if (sender != null) {
sender.configure();
if (sender instanceof HasPhysicalDestination) {
info("has answer-sender on " + ((HasPhysicalDestination) sender).getPhysicalDestinationName());
}
}
ISender errorSender = getErrorSender();
if (errorSender != null) {
if (errorSender instanceof HasPhysicalDestination) {
info("has errorSender to " + ((HasPhysicalDestination) errorSender).getPhysicalDestinationName());
}
errorSender.configure();
}
if (getListener() instanceof IHasProcessState) {
knownProcessStates.addAll(((IHasProcessState) getListener()).knownProcessStates());
targetProcessStates = ((IHasProcessState) getListener()).targetProcessStates();
supportProgrammaticRetry = knownProcessStates.contains(ProcessState.INPROCESS);
}
ITransactionalStorage<Serializable> messageLog = getMessageLog();
if (messageLog != null) {
if (getListener() instanceof IProvidesMessageBrowsers && ((IProvidesMessageBrowsers) getListener()).getMessageBrowser(ProcessState.DONE) != null) {
throw new ConfigurationException("listener with built-in messageLog cannot have external messageLog too");
}
messageLog.setName("messageLog of [" + getName() + "]");
if (StringUtils.isEmpty(messageLog.getSlotId())) {
messageLog.setSlotId(getName());
}
messageLog.setType(IMessageBrowser.StorageType.MESSAGELOG_RECEIVER.getCode());
messageLog.configure();
if (messageLog instanceof HasPhysicalDestination) {
info("has messageLog in " + ((HasPhysicalDestination) messageLog).getPhysicalDestinationName());
}
knownProcessStates.add(ProcessState.DONE);
messageBrowsers.put(ProcessState.DONE, messageLog);
if (StringUtils.isNotEmpty(getLabelXPath()) || StringUtils.isNotEmpty(getLabelStyleSheet())) {
labelTp = TransformerPool.configureTransformer0(getLogPrefix(), this, getLabelNamespaceDefs(), getLabelXPath(), getLabelStyleSheet(), OutputType.TEXT, false, null, 0);
}
}
ITransactionalStorage<Serializable> errorStorage = getErrorStorage();
if (errorStorage != null) {
if (getListener() instanceof IProvidesMessageBrowsers && ((IProvidesMessageBrowsers) getListener()).getMessageBrowser(ProcessState.ERROR) != null) {
throw new ConfigurationException("listener with built-in errorStorage cannot have external errorStorage too");
}
errorStorage.setName("errorStorage of [" + getName() + "]");
if (StringUtils.isEmpty(errorStorage.getSlotId())) {
errorStorage.setSlotId(getName());
}
errorStorage.setType(IMessageBrowser.StorageType.ERRORSTORAGE.getCode());
errorStorage.configure();
if (errorStorage instanceof HasPhysicalDestination) {
info("has errorStorage to " + ((HasPhysicalDestination) errorStorage).getPhysicalDestinationName());
}
knownProcessStates.add(ProcessState.ERROR);
messageBrowsers.put(ProcessState.ERROR, errorStorage);
registerEvent(RCV_MESSAGE_TO_ERRORSTORE_EVENT);
}
if (getListener() instanceof IProvidesMessageBrowsers) {
for (ProcessState state : knownProcessStates) {
IMessageBrowser messageBrowser = ((IProvidesMessageBrowsers) getListener()).getMessageBrowser(state);
if (messageBrowser != null && messageBrowser instanceof IConfigurable) {
((IConfigurable) messageBrowser).configure();
}
messageBrowsers.put(state, messageBrowser);
}
}
if (targetProcessStates == null) {
targetProcessStates = ProcessState.getTargetProcessStates(knownProcessStates);
}
if (isTransacted() && errorSender == null && errorStorage == null && !knownProcessStates().contains(ProcessState.ERROR)) {
ConfigurationWarnings.add(this, log, "sets transactionAttribute=" + getTransactionAttribute() + ", but has no errorSender or errorStorage. Messages processed with errors will be lost", SuppressKeys.TRANSACTION_SUPPRESS_KEY, getAdapter());
}
if (StringUtils.isNotEmpty(getCorrelationIDXPath()) || StringUtils.isNotEmpty(getCorrelationIDStyleSheet())) {
correlationIDTp = TransformerPool.configureTransformer0(getLogPrefix(), this, getCorrelationIDNamespaceDefs(), getCorrelationIDXPath(), getCorrelationIDStyleSheet(), OutputType.TEXT, false, null, 0);
}
if (StringUtils.isNotEmpty(getHideRegex()) && getErrorStorage() != null && StringUtils.isEmpty(getErrorStorage().getHideRegex())) {
getErrorStorage().setHideRegex(getHideRegex());
getErrorStorage().setHideMethod(getHideMethod());
}
if (StringUtils.isNotEmpty(getHideRegex()) && getMessageLog() != null && StringUtils.isEmpty(getMessageLog().getHideRegex())) {
getMessageLog().setHideRegex(getHideRegex());
getMessageLog().setHideMethod(getHideMethod());
}
} catch (Throwable t) {
ConfigurationException e = null;
if (t instanceof ConfigurationException) {
e = (ConfigurationException) t;
} else {
e = new ConfigurationException("Exception configuring receiver [" + getName() + "]", t);
}
throwEvent(RCV_CONFIGURATIONEXCEPTION_MONITOR_EVENT);
log.debug(getLogPrefix() + "Errors occured during configuration, setting runstate to ERROR");
runState.setRunState(RunState.ERROR);
throw e;
}
if (adapter != null) {
adapter.getMessageKeeper().add(getLogPrefix() + "initialization complete");
}
throwEvent(RCV_CONFIGURED_MONITOR_EVENT);
configurationSucceeded = true;
if (isInRunState(RunState.ERROR)) {
// if the adapter was previously in state ERROR, after a successful configure, reset it's state
runState.setRunState(RunState.STOPPED);
}
}
Aggregations