Search in sources :

Example 11 with TimeoutGuard

use of nl.nn.adapterframework.task.TimeoutGuard in project iaf by ibissource.

the class LockerTest method testTakeLockBeforeExecutingInsertInOtherThread.

@Test
public void testTakeLockBeforeExecutingInsertInOtherThread() throws Exception {
    cleanupLocks();
    locker.setTxManager(txManager);
    locker.setObjectId("myLocker");
    locker.configure();
    TimeoutGuard testTimeout = new TimeoutGuard(10, "Testtimeout");
    try {
        Semaphore otherReady = new Semaphore();
        Semaphore otherContinue = new Semaphore();
        Semaphore otherFinished = new Semaphore();
        LockerTester lockerTester = new LockerTester(txManager);
        lockerTester.setInitActionDone(otherReady);
        lockerTester.setWaitBeforeAction(otherContinue);
        lockerTester.setFinalizeActionDone(otherFinished);
        lockerTester.start();
        otherReady.acquire();
        String objectId = locker.acquire();
        otherContinue.release();
        otherFinished.acquire();
        assertNotNull(objectId);
        assertNotNull(lockerTester.getCaught());
    } finally {
        if (testTimeout.cancel()) {
            fail("test timed out");
        }
    }
}
Also used : TimeoutGuard(nl.nn.adapterframework.task.TimeoutGuard) Test(org.junit.Test)

Example 12 with TimeoutGuard

use of nl.nn.adapterframework.task.TimeoutGuard in project iaf by ibissource.

the class TransactionAttributePipeLineProcessor method processPipeLine.

public PipeLineResult processPipeLine(PipeLine pipeLine, String messageId, String message, IPipeLineSession pipeLineSession, String firstPipe) throws PipeRunException {
    try {
        // TransactionStatus txStatus = txManager.getTransaction(txDef);
        IbisTransaction itx = new IbisTransaction(txManager, pipeLine.getTxDef(), "pipeline of adapter [" + pipeLine.getOwner().getName() + "]");
        TransactionStatus txStatus = itx.getStatus();
        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 (StringUtils.isNotEmpty(pipeLine.getCommitOnState()) && !pipeLine.getCommitOnState().equalsIgnoreCase(pipeLineResult.getState())) {
                        mustRollback = true;
                        log.warn("Pipeline result state [" + pipeLineResult.getState() + "] for messageId [" + messageId + "] is not equal to commitOnState [" + pipeLine.getCommitOnState() + "], transaction (when present and active) will be rolled back");
                    }
                }
                if (mustRollback) {
                    try {
                        txStatus.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");
                    } else {
                        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");
            txStatus.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);
    }
}
Also used : IbisTransaction(nl.nn.adapterframework.core.IbisTransaction) PipeLineResult(nl.nn.adapterframework.core.PipeLineResult) PipeRunException(nl.nn.adapterframework.core.PipeRunException) TransactionStatus(org.springframework.transaction.TransactionStatus) TimeoutGuard(nl.nn.adapterframework.task.TimeoutGuard) PipeRunException(nl.nn.adapterframework.core.PipeRunException)

Example 13 with TimeoutGuard

use of nl.nn.adapterframework.task.TimeoutGuard in project iaf by ibissource.

the class TransactionAttributePipeProcessor method processPipe.

public PipeRunResult processPipe(PipeLine pipeLine, IPipe pipe, String messageId, Object message, IPipeLineSession pipeLineSession) throws PipeRunException {
    PipeRunResult pipeRunResult;
    int txOption;
    int txTimeout = 0;
    if (pipe instanceof HasTransactionAttribute) {
        HasTransactionAttribute taPipe = (HasTransactionAttribute) pipe;
        txOption = taPipe.getTransactionAttributeNum();
        txTimeout = taPipe.getTransactionTimeout();
    } else {
        txOption = TransactionDefinition.PROPAGATION_SUPPORTS;
    }
    // TransactionStatus txStatus = txManager.getTransaction(SpringTxManagerProxy.getTransactionDefinition(txOption,txTimeout));
    IbisTransaction itx = new IbisTransaction(txManager, SpringTxManagerProxy.getTransactionDefinition(txOption, txTimeout), "pipe [" + pipe.getName() + "]");
    TransactionStatus txStatus = itx.getStatus();
    try {
        TimeoutGuard tg = new TimeoutGuard("pipeline of adapter [" + pipeLine.getOwner().getName() + "] running pipe [" + pipe.getName() + "]");
        Throwable tCaught = null;
        try {
            tg.activateGuard(txTimeout);
            pipeRunResult = pipeProcessor.processPipe(pipeLine, pipe, messageId, message, pipeLineSession);
        } catch (Throwable t) {
            tCaught = t;
            throw tCaught;
        } finally {
            if (tg.cancel()) {
                if (tCaught == null) {
                    throw new PipeRunException(pipe, tg.getDescription() + " was interrupted");
                } else {
                    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");
        txStatus.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 {
        // txManager.commit(txStatus);
        itx.commit();
    }
    return pipeRunResult;
}
Also used : PipeRunResult(nl.nn.adapterframework.core.PipeRunResult) IbisTransaction(nl.nn.adapterframework.core.IbisTransaction) HasTransactionAttribute(nl.nn.adapterframework.core.HasTransactionAttribute) PipeRunException(nl.nn.adapterframework.core.PipeRunException) TransactionStatus(org.springframework.transaction.TransactionStatus) TimeoutGuard(nl.nn.adapterframework.task.TimeoutGuard)

Example 14 with TimeoutGuard

use of nl.nn.adapterframework.task.TimeoutGuard 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 15 with TimeoutGuard

use of nl.nn.adapterframework.task.TimeoutGuard in project iaf by ibissource.

the class OAuthAccessTokenManager method retrieveAccessToken.

public synchronized void retrieveAccessToken(Credentials credentials) throws HttpAuthenticationException {
    TokenRequest request = createRequest(credentials);
    HttpRequestBase apacheHttpRequest = convertToApacheHttpRequest(request.toHTTPRequest());
    CloseableHttpClient apacheHttpClient = httpSender.getHttpClient();
    TimeoutGuard tg = new TimeoutGuard(1 + httpSender.getTimeout() / 1000, "token retrieval") {

        @Override
        protected void abort() {
            apacheHttpRequest.abort();
        }
    };
    try (CloseableHttpResponse apacheHttpResponse = apacheHttpClient.execute(apacheHttpRequest)) {
        String responseBody = StreamUtil.streamToString(apacheHttpResponse.getEntity().getContent(), null, null);
        HTTPResponse httpResponse = convertFromApacheHttpResponse(apacheHttpResponse, responseBody);
        parseResponse(httpResponse, responseBody);
    } catch (IOException e) {
        apacheHttpRequest.abort();
        throw new HttpAuthenticationException(e);
    } finally {
        if (tg.cancel()) {
            throw new HttpAuthenticationException("timeout of [" + httpSender.getTimeout() + "] ms exceeded");
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HTTPResponse(com.nimbusds.oauth2.sdk.http.HTTPResponse) TokenRequest(com.nimbusds.oauth2.sdk.TokenRequest) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) TimeoutGuard(nl.nn.adapterframework.task.TimeoutGuard)

Aggregations

TimeoutGuard (nl.nn.adapterframework.task.TimeoutGuard)18 IbisTransaction (nl.nn.adapterframework.core.IbisTransaction)8 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)6 PipeRunException (nl.nn.adapterframework.core.PipeRunException)5 SenderException (nl.nn.adapterframework.core.SenderException)5 TimeoutException (nl.nn.adapterframework.core.TimeoutException)5 PipeLineResult (nl.nn.adapterframework.core.PipeLineResult)4 Test (org.junit.Test)4 IOException (java.io.IOException)3 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 SQLException (java.sql.SQLException)3 Date (java.util.Date)3 ListenerException (nl.nn.adapterframework.core.ListenerException)3 PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)3 Message (nl.nn.adapterframework.stream.Message)3 TransactionStatus (org.springframework.transaction.TransactionStatus)3 HashMap (java.util.HashMap)2 Timer (java.util.Timer)2 TimerTask (java.util.TimerTask)2