Search in sources :

Example 66 with ListenerException

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

the class FileRecordListener method getRawMessage.

/**
 * Retrieves a single record from a file. If the file is empty or fully processed, it looks wether there
 * is a new file to process and returns the first record.
 */
public synchronized Object getRawMessage(Map threadContext) throws ListenerException {
    String fullInputFileName = null;
    if (recordIterator != null) {
        if (recordIterator.hasNext()) {
            recordNo += 1;
            return recordIterator.next();
        }
    }
    if (getFileToProcess() != null) {
        File inputFile = getFileToProcess();
        log.info(" processing file [" + inputFile.getName() + "] size [" + inputFile.length() + "]");
        if (StringUtils.isNotEmpty(getStoreFileNameInSessionKey())) {
            threadContext.put(getStoreFileNameInSessionKey(), inputFile.getName());
        }
        String fileContent = "";
        try {
            fullInputFileName = inputFile.getCanonicalPath();
            fileContent = Misc.fileToString(fullInputFileName, "\n");
            inputFileName = archiveFile(inputFile);
        } catch (IOException e) {
            throw new ListenerException(" got exception opening " + inputFile.getName(), e);
        } finally {
            recordNo = 0;
        }
        log.info(" processing file [" + fullInputFileName + "]");
        recordIterator = parseToRecords(fileContent);
        if (recordIterator.hasNext()) {
            recordNo += 1;
            return recordIterator.next();
        }
    }
    // if nothing was found, just sleep thight.
    try {
        Thread.sleep(responseTime);
    } catch (InterruptedException e) {
        throw new ListenerException("interupted...", e);
    }
    return null;
}
Also used : ListenerException(nl.nn.adapterframework.core.ListenerException) IOException(java.io.IOException) File(java.io.File)

Example 67 with ListenerException

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

the class FileRecordListener method archiveFile.

/**
 * Moves a file to another directory and places a UUID in the name.
 * @return String with the name of the (renamed and moved) file
 */
protected String archiveFile(File file) throws ListenerException {
    boolean success = false;
    String directoryTo = getDirectoryProcessedFiles();
    String fullFilePath = "";
    // Destination directory
    File dir = new File(directoryTo);
    try {
        fullFilePath = file.getCanonicalPath();
    } catch (IOException e) {
        log.warn(getName() + " error retrieving canonical path of file [" + file.getName() + "]");
        fullFilePath = file.getName();
    }
    if (!dir.isDirectory()) {
        throw new ListenerException(getName() + " error renaming directory: The directory [" + directoryTo + "] to move the file [" + fullFilePath + "] is not a directory!");
    }
    // Move file to new directory
    String newFileName = Misc.createSimpleUUID() + "-" + file.getName();
    int dotPosition = file.getName().lastIndexOf(".");
    if (dotPosition > 0)
        newFileName = file.getName().substring(0, dotPosition) + "-" + Misc.createSimpleUUID() + file.getName().substring(dotPosition, file.getName().length());
    success = file.renameTo(new File(dir, newFileName));
    if (!success) {
        log.error(getName() + " was unable to move file [" + fullFilePath + "] to [" + directoryTo + "]");
        throw new ListenerException("unable to move file [" + fullFilePath + "] to [" + directoryTo + "]");
    } else
        log.info(getName() + " moved file [" + fullFilePath + "] to [" + directoryTo + "]");
    String result = null;
    try {
        result = new File(dir, newFileName).getCanonicalPath();
    } catch (IOException e) {
        throw new ListenerException("error retrieving canonical path of renamed file [" + file.getName() + "]", e);
    }
    return result;
}
Also used : ListenerException(nl.nn.adapterframework.core.ListenerException) IOException(java.io.IOException) File(java.io.File)

Example 68 with ListenerException

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

the class ReceiverBase method processMessageInAdapter.

/*
	 * Assumes message is read, and when transacted, transaction is still open.
	 */
private String processMessageInAdapter(IListener origin, Object rawMessage, String message, String messageId, String technicalCorrelationId, Map threadContext, long waitingDuration, boolean manualRetry) throws ListenerException {
    String result = null;
    PipeLineResult pipeLineResult = null;
    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 {
            InputStream xmlInput = IOUtils.toInputStream(message, "UTF-8");
            CompactSaxHandler handler = new CompactSaxHandler();
            handler.setChompCharSize(getChompCharSize());
            handler.setElementToMove(getElementToMove());
            handler.setElementToMoveChain(getElementToMoveChain());
            handler.setElementToMoveSessionKey(getElementToMoveSessionKey());
            handler.setRemoveCompactMsgNamespaces(isRemoveCompactMsgNamespaces());
            if (threadContext != null) {
                handler.setContext(threadContext);
            }
            SAXParserFactory parserFactory = XmlUtils.getSAXParserFactory();
            parserFactory.setNamespaceAware(true);
            SAXParser saxParser = parserFactory.newSAXParser();
            try {
                saxParser.parse(xmlInput, handler);
                message = handler.getXmlString();
            } catch (Exception e) {
                warn("received message could not be compacted: " + e.getMessage());
            }
            handler = null;
        } catch (Exception e) {
            throw new ListenerException("error during compacting received message to more compact format: " + e.getMessage());
        }
    }
    String businessCorrelationId = null;
    if (correlationIDTp != null) {
        try {
            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)) {
        if (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(IPipeLineSession.businessCorrelationIdKey, businessCorrelationId);
    String label = null;
    if (labelTp != null) {
        try {
            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());
        }
    }
    if (hasProblematicHistory(messageId, manualRetry, rawMessage, message, threadContext, businessCorrelationId)) {
        if (!isTransacted()) {
            log.warn(getLogPrefix() + "received message with messageId [" + messageId + "] which has a problematic history; aborting processing");
        }
        numRejected.increase();
        return result;
    }
    if (isDuplicateAndSkip(getMessageLog(), messageId, businessCorrelationId)) {
        numRejected.increase();
        return result;
    }
    if (getCachedProcessResult(messageId) != null) {
        numRetried.increase();
    }
    int txOption = this.getTransactionAttributeNum();
    TransactionDefinition txDef = SpringTxManagerProxy.getTransactionDefinition(txOption, getTransactionTimeout());
    // TransactionStatus txStatus = txManager.getTransaction(txDef);
    IbisTransaction itx = new IbisTransaction(txManager, txDef, "receiver [" + getName() + "]");
    TransactionStatus txStatus = itx.getStatus();
    // update processing statistics
    // count in processing statistics includes messages that are rolled back to input
    startProcessingMessage(waitingDuration);
    IPipeLineSession pipelineSession = null;
    String errorMessage = "";
    boolean messageInError = false;
    try {
        String pipelineMessage;
        if (origin instanceof IBulkDataListener) {
            try {
                IBulkDataListener bdl = (IBulkDataListener) origin;
                pipelineMessage = bdl.retrieveBulkData(rawMessage, message, threadContext);
            } catch (Throwable t) {
                errorMessage = t.getMessage();
                messageInError = true;
                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 [" + transactionTimeout + "]s");
                tg.activateGuard(getTransactionTimeout());
                pipeLineResult = adapter.processMessageWithExceptions(businessCorrelationId, pipelineMessage, pipelineSession);
                pipelineSession.put("exitcode", "" + pipeLineResult.getExitCode());
                result = pipeLineResult.getResult();
                errorMessage = "exitState [" + pipeLineResult.getState() + "], result [" + result + "]";
                if (pipelineSession.containsKey("exitcode")) {
                    int status = Integer.parseInt("" + pipelineSession.get("exitcode"));
                    if (status > 0)
                        errorMessage += ", exitcode [" + status + "]";
                }
                if (log.isDebugEnabled()) {
                    log.debug(getLogPrefix() + "received result: " + errorMessage);
                }
                messageInError = txStatus.isRollbackOnly();
            } finally {
                log.debug(getLogPrefix() + "canceling TimeoutGuard, isInterrupted [" + Thread.currentThread().isInterrupted() + "]");
                if (tg.cancel()) {
                    errorMessage = "timeout exceeded";
                    if (StringUtils.isEmpty(result)) {
                        result = "<timeout/>";
                    }
                    messageInError = true;
                }
            }
            if (!messageInError && !isTransacted()) {
                String commitOnState = ((Adapter) adapter).getPipeLine().getCommitOnState();
                if (StringUtils.isNotEmpty(commitOnState) && !commitOnState.equalsIgnoreCase(pipeLineResult.getState())) {
                    messageInError = true;
                }
            }
        } catch (Throwable t) {
            if (TransactionSynchronizationManager.isActualTransactionActive()) {
                log.debug("<*>" + getLogPrefix() + "TX Update: Received failure, transaction " + (txStatus.isRollbackOnly() ? "already" : "not yet") + " marked for rollback-only");
            }
            errorMessage = t.getMessage();
            messageInError = true;
            if (pipeLineResult == null) {
                pipeLineResult = new PipeLineResult();
            }
            if (StringUtils.isEmpty(pipeLineResult.getResult())) {
                String formattedErrorMessage = adapter.formatErrorMessage("exception caught", t, message, messageId, this, startProcessingTimestamp);
                pipeLineResult.setResult(formattedErrorMessage);
            }
            ListenerException l = wrapExceptionAsListenerException(t);
            throw l;
        } finally {
            putSessionKeysIntoThreadContext(threadContext, pipelineSession);
        }
        // }
        if (getSender() != null) {
            String sendMsg = sendResultToSender(technicalCorrelationId, result);
            if (sendMsg != null) {
                errorMessage = sendMsg;
            }
        }
    } finally {
        cacheProcessResult(messageId, businessCorrelationId, errorMessage, new Date(startProcessingTimestamp));
        if (!isTransacted() && messageInError) {
            if (!manualRetry) {
                moveInProcessToError(messageId, businessCorrelationId, message, new Date(startProcessingTimestamp), errorMessage, rawMessage, TXNEW_CTRL);
            }
        }
        try {
            Map afterMessageProcessedMap;
            if (threadContext != null) {
                afterMessageProcessedMap = threadContext;
                if (pipelineSession != null) {
                    threadContext.putAll(pipelineSession);
                }
            } else {
                afterMessageProcessedMap = pipelineSession;
            }
            origin.afterMessageProcessed(pipeLineResult, rawMessage, afterMessageProcessedMap);
        } finally {
            long finishProcessingTimestamp = System.currentTimeMillis();
            finishProcessingMessage(finishProcessingTimestamp - startProcessingTimestamp);
            if (!txStatus.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 {
                throw new ListenerException(getLogPrefix() + "Transaction already completed; we didn't expect this");
            }
        }
    }
    if (log.isDebugEnabled())
        log.debug(getLogPrefix() + "messageId [" + messageId + "] correlationId [" + businessCorrelationId + "] returning result [" + result + "]");
    return result;
}
Also used : DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionDefinition(org.springframework.transaction.TransactionDefinition) InputStream(java.io.InputStream) TransactionStatus(org.springframework.transaction.TransactionStatus) TimeoutGuard(nl.nn.adapterframework.task.TimeoutGuard) SenderException(nl.nn.adapterframework.core.SenderException) ListenerException(nl.nn.adapterframework.core.ListenerException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) Date(java.util.Date) ListenerException(nl.nn.adapterframework.core.ListenerException) IBulkDataListener(nl.nn.adapterframework.core.IBulkDataListener) CompactSaxHandler(nl.nn.adapterframework.util.CompactSaxHandler) IbisTransaction(nl.nn.adapterframework.core.IbisTransaction) PipeLineResult(nl.nn.adapterframework.core.PipeLineResult) SAXParser(javax.xml.parsers.SAXParser) IPipeLineSession(nl.nn.adapterframework.core.IPipeLineSession) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 69 with ListenerException

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

the class ReceiverBase method retryMessage.

public void retryMessage(String messageId) throws ListenerException {
    if (getErrorStorage() == null) {
        throw new ListenerException(getLogPrefix() + "has no errorStorage, cannot retry messageId [" + messageId + "]");
    }
    PlatformTransactionManager txManager = getTxManager();
    // TransactionStatus txStatus = txManager.getTransaction(TXNEW);
    IbisTransaction itx = new IbisTransaction(txManager, TXNEW_PROC, "receiver [" + getName() + "]");
    TransactionStatus txStatus = itx.getStatus();
    Map threadContext = new HashMap();
    Object msg = null;
    try {
        try {
            ITransactionalStorage errorStorage = getErrorStorage();
            msg = errorStorage.getMessage(messageId);
            processRawMessage(getListener(), msg, threadContext, -1, true);
        } catch (Throwable t) {
            txStatus.setRollbackOnly();
            throw new ListenerException(t);
        } finally {
            txManager.commit(txStatus);
        }
    } catch (ListenerException e) {
        txStatus = txManager.getTransaction(TXNEW_CTRL);
        try {
            if (msg instanceof Serializable) {
                String correlationId = (String) threadContext.get(IPipeLineSession.businessCorrelationIdKey);
                String receivedDateStr = (String) threadContext.get(IPipeLineSession.tsReceivedKey);
                if (receivedDateStr == null) {
                    log.warn(getLogPrefix() + IPipeLineSession.tsReceivedKey + " is unknown, cannot update comments");
                } else {
                    Date receivedDate = DateUtils.parseToDate(receivedDateStr, DateUtils.FORMAT_FULL_GENERIC);
                    errorStorage.deleteMessage(messageId);
                    errorStorage.storeMessage(messageId, correlationId, receivedDate, "after retry: " + e.getMessage(), null, (Serializable) msg);
                }
            } else {
                log.warn(getLogPrefix() + "retried message is not serializable, cannot update comments");
            }
        } catch (SenderException e1) {
            txStatus.setRollbackOnly();
            log.warn(getLogPrefix() + "could not update comments in errorStorage", e1);
        } finally {
            txManager.commit(txStatus);
        }
        throw e;
    }
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TransactionStatus(org.springframework.transaction.TransactionStatus) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Date(java.util.Date) ITransactionalStorage(nl.nn.adapterframework.core.ITransactionalStorage) ListenerException(nl.nn.adapterframework.core.ListenerException) IbisTransaction(nl.nn.adapterframework.core.IbisTransaction) INamedObject(nl.nn.adapterframework.core.INamedObject) SenderException(nl.nn.adapterframework.core.SenderException) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 70 with ListenerException

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

the class MessageSendingPipe method stop.

public void stop() {
    if (StringUtils.isEmpty(getStubFileName())) {
        log.info(getLogPrefix(null) + "is closing");
        try {
            getSender().close();
        } catch (SenderException e) {
            log.warn(getLogPrefix(null) + "exception closing sender", e);
        }
        if (getListener() != null) {
            try {
                log.info(getLogPrefix(null) + "is closing; closing listener");
                getListener().close();
            } catch (ListenerException e) {
                log.warn(getLogPrefix(null) + "Exception closing listener", e);
            }
        }
    }
    ITransactionalStorage messageLog = getMessageLog();
    if (messageLog != null) {
        try {
            messageLog.close();
        } catch (Exception e) {
            log.warn(getLogPrefix(null) + "Exception closing messageLog", e);
        }
    }
}
Also used : ListenerException(nl.nn.adapterframework.core.ListenerException) SenderException(nl.nn.adapterframework.core.SenderException) PipeRunException(nl.nn.adapterframework.core.PipeRunException) SenderException(nl.nn.adapterframework.core.SenderException) ListenerException(nl.nn.adapterframework.core.ListenerException) PipeStartException(nl.nn.adapterframework.core.PipeStartException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) TimeOutException(nl.nn.adapterframework.core.TimeOutException) ParameterException(nl.nn.adapterframework.core.ParameterException) ITransactionalStorage(nl.nn.adapterframework.core.ITransactionalStorage)

Aggregations

ListenerException (nl.nn.adapterframework.core.ListenerException)84 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)37 IOException (java.io.IOException)19 HashMap (java.util.HashMap)18 Map (java.util.Map)18 JMSException (javax.jms.JMSException)16 SenderException (nl.nn.adapterframework.core.SenderException)14 SQLException (java.sql.SQLException)13 PreparedStatement (java.sql.PreparedStatement)11 Session (javax.jms.Session)10 LinkedHashMap (java.util.LinkedHashMap)9 IPipeLineSession (nl.nn.adapterframework.core.IPipeLineSession)9 TimeOutException (nl.nn.adapterframework.core.TimeOutException)9 IfsaException (nl.nn.adapterframework.extensions.ifsa.IfsaException)9 Connection (java.sql.Connection)8 ResultSet (java.sql.ResultSet)8 QueueSession (javax.jms.QueueSession)8 File (java.io.File)7 MessageConsumer (javax.jms.MessageConsumer)7 Message (javax.jms.Message)6