Search in sources :

Example 1 with ProcessState

use of nl.nn.adapterframework.core.ProcessState in project iaf by ibissource.

the class JdbcListener method configure.

@Override
public void configure() throws ConfigurationException {
    super.configure();
    try {
        preparedSelectQuery = getDbmsSupport().prepareQueryTextForWorkQueueReading(1, getSelectQuery());
        preparedPeekQuery = StringUtils.isNotEmpty(getPeekQuery()) ? getPeekQuery() : getDbmsSupport().prepareQueryTextForWorkQueuePeeking(1, getSelectQuery());
    } catch (JdbcException e) {
        throw new ConfigurationException(e);
    }
    Map<ProcessState, String> orderedUpdateStatusQueries = new LinkedHashMap<>();
    for (ProcessState state : ProcessState.values()) {
        if (updateStatusQueries.containsKey(state)) {
            orderedUpdateStatusQueries.put(state, updateStatusQueries.get(state));
        }
    }
    updateStatusQueries = orderedUpdateStatusQueries;
    targetProcessStates = ProcessState.getTargetProcessStates(knownProcessStates());
}
Also used : ProcessState(nl.nn.adapterframework.core.ProcessState) IHasProcessState(nl.nn.adapterframework.core.IHasProcessState) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with ProcessState

use of nl.nn.adapterframework.core.ProcessState 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;
}
Also used : Message(nl.nn.adapterframework.stream.Message) PipeLineSession(nl.nn.adapterframework.core.PipeLineSession) TimeoutGuard(nl.nn.adapterframework.task.TimeoutGuard) TimeoutException(nl.nn.adapterframework.core.TimeoutException) SenderException(nl.nn.adapterframework.core.SenderException) ListenerException(nl.nn.adapterframework.core.ListenerException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) Date(java.util.Date) IHasProcessState(nl.nn.adapterframework.core.IHasProcessState) ProcessState(nl.nn.adapterframework.core.ProcessState) ListenerException(nl.nn.adapterframework.core.ListenerException) IBulkDataListener(nl.nn.adapterframework.core.IBulkDataListener) CompactSaxHandler(nl.nn.adapterframework.util.CompactSaxHandler) IHasProcessState(nl.nn.adapterframework.core.IHasProcessState) IbisTransaction(nl.nn.adapterframework.core.IbisTransaction) PipeLineResult(nl.nn.adapterframework.core.PipeLineResult) INamedObject(nl.nn.adapterframework.core.INamedObject)

Example 3 with ProcessState

use of nl.nn.adapterframework.core.ProcessState 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);
        }
    }
}
Also used : IHasProcessState(nl.nn.adapterframework.core.IHasProcessState) ProcessState(nl.nn.adapterframework.core.ProcessState) ListenerException(nl.nn.adapterframework.core.ListenerException) Serializable(java.io.Serializable) Message(nl.nn.adapterframework.stream.Message) IHasProcessState(nl.nn.adapterframework.core.IHasProcessState) ISender(nl.nn.adapterframework.core.ISender) TransactionStatus(org.springframework.transaction.TransactionStatus) TimeoutException(nl.nn.adapterframework.core.TimeoutException) SenderException(nl.nn.adapterframework.core.SenderException) ListenerException(nl.nn.adapterframework.core.ListenerException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException)

Example 4 with ProcessState

use of nl.nn.adapterframework.core.ProcessState in project iaf by ibissource.

the class TransactionalStorage method browseReceiverMessages.

@GET
@RolesAllowed({ "IbisDataAdmin", "IbisAdmin", "IbisTester" })
@Path("/adapters/{adapterName}/receivers/{receiverName}/stores/{processState}")
@Produces(MediaType.APPLICATION_JSON)
public Response browseReceiverMessages(@PathParam("adapterName") String adapterName, @PathParam("receiverName") String receiverName, @PathParam("processState") String processState, @QueryParam("type") String type, @QueryParam("host") String host, @QueryParam("id") String id, @QueryParam("messageId") String messageId, @QueryParam("correlationId") String correlationId, @QueryParam("comment") String comment, @QueryParam("message") String message, @QueryParam("label") String label, @QueryParam("startDate") String startDateStr, @QueryParam("endDate") String endDateStr, @QueryParam("sort") String sort, @QueryParam("skip") int skipMessages, @QueryParam("max") int maxMessages) throws ApiException {
    Adapter adapter = getIbisManager().getRegisteredAdapter(adapterName);
    if (adapter == null) {
        throw new ApiException("Adapter not found!");
    }
    Receiver<?> receiver = adapter.getReceiverByName(receiverName);
    if (receiver == null) {
        throw new ApiException("Receiver [" + receiverName + "] not found!");
    }
    // StorageType
    ProcessState state = ProcessState.getProcessStateFromName(processState);
    IMessageBrowser<?> storage = receiver.getMessageBrowser(state);
    Map<ProcessState, Map<String, String>> targetPSInfo = getTargetProcessStateInfo(receiver.targetProcessStates().get(state));
    if (storage == null) {
        throw new ApiException("no IMessageBrowser found");
    }
    // Apply filters
    MessageBrowsingFilter filter = new MessageBrowsingFilter(maxMessages, skipMessages);
    filter.setTypeMask(type);
    filter.setHostMask(host);
    filter.setIdMask(id);
    filter.setMessageIdMask(messageId);
    filter.setCorrelationIdMask(correlationId);
    filter.setCommentMask(comment);
    filter.setMessageMask(message, storage, receiver.getListener());
    filter.setLabelMask(label);
    filter.setStartDateMask(startDateStr);
    filter.setEndDateMask(endDateStr);
    if ("desc".equalsIgnoreCase(sort))
        filter.setSortOrder(SortOrder.DESC);
    if ("asc".equalsIgnoreCase(sort))
        filter.setSortOrder(SortOrder.ASC);
    Map<String, Object> resultObj = getMessages(storage, filter);
    if (targetPSInfo != null && targetPSInfo.size() > 0) {
        resultObj.put("targetStates", targetPSInfo);
    }
    return Response.status(Response.Status.OK).entity(resultObj).build();
}
Also used : ProcessState(nl.nn.adapterframework.core.ProcessState) Adapter(nl.nn.adapterframework.core.Adapter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) MessageBrowsingFilter(nl.nn.adapterframework.util.MessageBrowsingFilter) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 5 with ProcessState

use of nl.nn.adapterframework.core.ProcessState in project iaf by ibissource.

the class FileSystemListener method configure.

@Override
public void configure() throws ConfigurationException {
    FS fileSystem = getFileSystem();
    fileSystem.configure();
    if (getNumberOfBackups() > 0 && !(fileSystem instanceof IWritableFileSystem)) {
        throw new ConfigurationException("FileSystem [" + ClassUtils.nameOf(fileSystem) + "] does not support setting attribute 'numberOfBackups'");
    }
    knownProcessStates = ProcessState.getMandatoryKnownStates();
    for (ProcessState state : ProcessState.values()) {
        if (StringUtils.isNotEmpty(getStateFolder(state))) {
            knownProcessStates.add(state);
        }
    }
    targetProcessStates = ProcessState.getTargetProcessStates(knownProcessStates);
}
Also used : ProcessState(nl.nn.adapterframework.core.ProcessState) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException)

Aggregations

ProcessState (nl.nn.adapterframework.core.ProcessState)10 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)5 LinkedHashMap (java.util.LinkedHashMap)4 IHasProcessState (nl.nn.adapterframework.core.IHasProcessState)4 ListenerException (nl.nn.adapterframework.core.ListenerException)4 HashMap (java.util.HashMap)3 ISender (nl.nn.adapterframework.core.ISender)3 Serializable (java.io.Serializable)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 RolesAllowed (javax.annotation.security.RolesAllowed)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 Adapter (nl.nn.adapterframework.core.Adapter)2 HasPhysicalDestination (nl.nn.adapterframework.core.HasPhysicalDestination)2 HasSender (nl.nn.adapterframework.core.HasSender)2 SenderException (nl.nn.adapterframework.core.SenderException)2 TimeoutException (nl.nn.adapterframework.core.TimeoutException)2 Message (nl.nn.adapterframework.stream.Message)2 IOException (java.io.IOException)1