Search in sources :

Example 6 with ISender

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

the class ReceiverBase method moveInProcessToError.

private void moveInProcessToError(String originalMessageId, String correlationId, String message, Date receivedDate, String comments, Object rawMessage, TransactionDefinition txDef) {
    cachePoisonMessageId(originalMessageId);
    ISender errorSender = getErrorSender();
    ITransactionalStorage errorStorage = getErrorStorage();
    if (errorSender == null && errorStorage == null) {
        log.debug(getLogPrefix() + "has no errorSender or errorStorage, 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;
    }
    try {
        if (errorSender != null) {
            errorSender.sendMessage(correlationId, message);
        }
        Serializable sobj;
        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);
                sobj = message;
            }
        }
        if (hideRegex != null) {
            if (getHideMethod().equalsIgnoreCase("FIRSTHALF")) {
                message = Misc.hideFirstHalf(message, hideRegex);
            } else {
                message = Misc.hideAll(message, hideRegex);
            }
            sobj = message;
        }
        if (errorStorage != null) {
            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, 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 : Serializable(java.io.Serializable) ListenerException(nl.nn.adapterframework.core.ListenerException) ISender(nl.nn.adapterframework.core.ISender) TransactionStatus(org.springframework.transaction.TransactionStatus) SenderException(nl.nn.adapterframework.core.SenderException) ListenerException(nl.nn.adapterframework.core.ListenerException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) ITransactionalStorage(nl.nn.adapterframework.core.ITransactionalStorage)

Example 7 with ISender

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

the class SenderSeries method configure.

public void configure() throws ConfigurationException {
    for (Iterator<ISender> it = getSenderIterator(); it.hasNext(); ) {
        ISender sender = it.next();
        if (sender instanceof PipeAware) {
            ((PipeAware) sender).setPipe(getPipe());
        }
        sender.configure();
    }
    super.configure();
}
Also used : ISender(nl.nn.adapterframework.core.ISender) PipeAware(nl.nn.adapterframework.pipes.PipeAware)

Example 8 with ISender

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

the class ShadowSender method doSendMessage.

/**
 * We override this from the parallel sender as we should only execute the original and shadowsenders here!
 */
@Override
public String doSendMessage(String correlationID, String message, ParameterResolutionContext prc) throws SenderException, TimeOutException {
    Guard guard = new Guard();
    Map<ISender, ParallelSenderExecutor> executorMap = new HashMap<ISender, ParallelSenderExecutor>();
    TaskExecutor executor = createTaskExecutor();
    for (Iterator<ISender> it = getExecutableSenders(); it.hasNext(); ) {
        ISender sender = it.next();
        // Create a new ParameterResolutionContext to be thread safe, see
        // documentation on constructor of ParameterResolutionContext
        // (parameter cacheXmlSource).
        // Testing also showed that disabling caching is better for
        // performance. At least when testing with a lot of large messages
        // in parallel. This might be due to the fact that objects can be
        // garbage collected earlier. OutOfMemoryErrors occur much
        // faster when caching is enabled. Testing was done by sending 10
        // messages of 1 MB concurrently to a pipeline which will process
        // the message in parallel with 10 SenderWrappers (containing a
        // XsltSender and IbisLocalSender).
        ParameterResolutionContext newPrc = new ParameterResolutionContext(prc.getInput(), prc.getSession(), prc.isNamespaceAware(), prc.isXslt2(), false);
        guard.addResource();
        ParallelSenderExecutor pse = new ParallelSenderExecutor(sender, correlationID, message, newPrc, guard, getStatisticsKeeper(sender));
        executorMap.put(sender, pse);
        executor.execute(pse);
    }
    try {
        guard.waitForAllResources();
    } catch (InterruptedException e) {
        throw new SenderException(getLogPrefix() + "was interupted", e);
    }
    ParallelSenderExecutor originalSender = null;
    XmlBuilder resultsXml = new XmlBuilder("results");
    resultsXml.addAttribute("correlationID", correlationID);
    resultsXml.addAttribute("adapter", getPipe().getAdapter().getName());
    XmlBuilder originalMessageXml = new XmlBuilder("originalMessage");
    originalMessageXml.setValue(XmlUtils.skipXmlDeclaration(message), false);
    resultsXml.addSubElement(originalMessageXml);
    // First loop through all (Shadow)Senders and handle their results
    for (Iterator<ISender> it = getExecutableSenders(); it.hasNext(); ) {
        ISender sender = it.next();
        ParallelSenderExecutor pse = (ParallelSenderExecutor) executorMap.get(sender);
        XmlBuilder resultXml;
        if (sender.getName() != null && sender.getName().equalsIgnoreCase(getOriginalSender())) {
            originalSender = pse;
            resultXml = new XmlBuilder("originalResult");
        } else {
            resultXml = new XmlBuilder("shadowResult");
        }
        StatisticsKeeper sk = getStatisticsKeeper(sender);
        resultXml.addAttribute("duration", sk.getLast() + sk.getUnits());
        resultXml.addAttribute("count", sk.getCount());
        resultXml.addAttribute("senderClass", ClassUtils.nameOf(sender));
        resultXml.addAttribute("senderName", sender.getName());
        Throwable throwable = pse.getThrowable();
        if (throwable == null) {
            Object result = pse.getReply();
            if (result == null) {
                resultXml.addAttribute("type", "null");
            } else {
                resultXml.addAttribute("type", ClassUtils.nameOf(result));
                resultXml.setValue(XmlUtils.skipXmlDeclaration(result.toString()), false);
            }
        } else {
            resultXml.addAttribute("type", ClassUtils.nameOf(throwable));
            resultXml.setValue(throwable.getMessage());
        }
        resultsXml.addSubElement(resultXml);
    }
    // cause an SenderException regardless of the results of the ShadowSenders.
    if (originalSender == null) {
        // In theory this should never happen!
        throw new SenderException("no originalSender found");
    }
    // The messages have been processed, now the results need to be stored somewhere.
    try {
        if (resultISender instanceof ISenderWithParameters) {
            ParameterResolutionContext newPrc = new ParameterResolutionContext(resultsXml.toXML(), prc.getSession());
            ((ISenderWithParameters) resultISender).sendMessage(correlationID, resultsXml.toXML(), newPrc);
        } else {
            resultISender.sendMessage(correlationID, resultsXml.toXML());
        }
    } catch (SenderException se) {
        log.warn("failed to send ShadowSender result to [" + resultISender.getName() + "]");
    }
    if (originalSender.getThrowable() != null) {
        throw new SenderException(originalSender.getThrowable());
    } else {
        return originalSender.getReply().toString();
    }
}
Also used : HashMap(java.util.HashMap) ISenderWithParameters(nl.nn.adapterframework.core.ISenderWithParameters) TaskExecutor(org.springframework.core.task.TaskExecutor) ISender(nl.nn.adapterframework.core.ISender) XmlBuilder(nl.nn.adapterframework.util.XmlBuilder) StatisticsKeeper(nl.nn.adapterframework.statistics.StatisticsKeeper) Guard(nl.nn.adapterframework.util.Guard) SenderException(nl.nn.adapterframework.core.SenderException) ParameterResolutionContext(nl.nn.adapterframework.parameters.ParameterResolutionContext)

Example 9 with ISender

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

the class ParallelSenders method doSendMessage.

public String doSendMessage(String correlationID, String message, ParameterResolutionContext prc) throws SenderException, TimeOutException {
    Guard guard = new Guard();
    Map<ISender, ParallelSenderExecutor> executorMap = new HashMap<ISender, ParallelSenderExecutor>();
    TaskExecutor executor = createTaskExecutor();
    for (Iterator<ISender> it = getSenderIterator(); it.hasNext(); ) {
        ISender sender = it.next();
        // Create a new ParameterResolutionContext to be thread safe, see
        // documentation on constructor of ParameterResolutionContext
        // (parameter cacheXmlSource).
        // Testing also showed that disabling caching is better for
        // performance. At least when testing with a lot of large messages
        // in parallel. This might be due to the fact that objects can be
        // garbage collected earlier. OutOfMemoryErrors occur much
        // faster when caching is enabled. Testing was done by sending 10
        // messages of 1 MB concurrently to a pipeline which will process
        // the message in parallel with 10 SenderWrappers (containing a
        // XsltSender and IbisLocalSender).
        ParameterResolutionContext newPrc = new ParameterResolutionContext(prc.getInput(), prc.getSession(), prc.isNamespaceAware(), prc.isXslt2(), false);
        guard.addResource();
        ParallelSenderExecutor pse = new ParallelSenderExecutor(sender, correlationID, message, newPrc, guard, getStatisticsKeeper(sender));
        executorMap.put(sender, pse);
        executor.execute(pse);
    }
    try {
        guard.waitForAllResources();
    } catch (InterruptedException e) {
        throw new SenderException(getLogPrefix() + "was interupted", e);
    }
    XmlBuilder resultsXml = new XmlBuilder("results");
    for (Iterator<ISender> it = getSenderIterator(); it.hasNext(); ) {
        ISender sender = it.next();
        ParallelSenderExecutor pse = (ParallelSenderExecutor) executorMap.get(sender);
        XmlBuilder resultXml = new XmlBuilder("result");
        resultXml.addAttribute("senderClass", ClassUtils.nameOf(sender));
        resultXml.addAttribute("senderName", sender.getName());
        Throwable throwable = pse.getThrowable();
        if (throwable == null) {
            Object result = pse.getReply();
            if (result == null) {
                resultXml.addAttribute("type", "null");
            } else {
                resultXml.addAttribute("type", ClassUtils.nameOf(result));
                resultXml.setValue(XmlUtils.skipXmlDeclaration(result.toString()), false);
            }
        } else {
            resultXml.addAttribute("type", ClassUtils.nameOf(throwable));
            resultXml.setValue(throwable.getMessage());
        }
        resultsXml.addSubElement(resultXml);
    }
    return resultsXml.toXML();
}
Also used : HashMap(java.util.HashMap) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) TaskExecutor(org.springframework.core.task.TaskExecutor) ISender(nl.nn.adapterframework.core.ISender) XmlBuilder(nl.nn.adapterframework.util.XmlBuilder) Guard(nl.nn.adapterframework.util.Guard) SenderException(nl.nn.adapterframework.core.SenderException) ParameterResolutionContext(nl.nn.adapterframework.parameters.ParameterResolutionContext)

Example 10 with ISender

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

the class TestTool method executeSenderWrite.

private static int executeSenderWrite(String stepDisplayName, Map queues, Map writers, String queueName, String senderType, String fileContent) {
    int result = RESULT_ERROR;
    Map senderInfo = (Map) queues.get(queueName);
    ISender sender = (ISender) senderInfo.get(senderType + "Sender");
    Boolean convertExceptionToMessage = (Boolean) senderInfo.get("convertExceptionToMessage");
    ParameterResolutionContext parameterResolutionContext = (ParameterResolutionContext) senderInfo.get("parameterResolutionContext");
    SenderThread senderThread;
    if (parameterResolutionContext == null) {
        senderThread = new SenderThread(sender, fileContent, convertExceptionToMessage.booleanValue());
    } else {
        senderThread = new SenderThread((ISenderWithParameters) sender, fileContent, parameterResolutionContext, convertExceptionToMessage.booleanValue());
    }
    senderThread.start();
    senderInfo.put(senderType + "SenderThread", senderThread);
    debugPipelineMessage(stepDisplayName, "Successfully started thread writing to '" + queueName + "':", fileContent, writers);
    logger.debug("Successfully started thread writing to '" + queueName + "'");
    result = RESULT_OK;
    return result;
}
Also used : ISender(nl.nn.adapterframework.core.ISender) ISenderWithParameters(nl.nn.adapterframework.core.ISenderWithParameters) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ParameterResolutionContext(nl.nn.adapterframework.parameters.ParameterResolutionContext)

Aggregations

ISender (nl.nn.adapterframework.core.ISender)32 HashMap (java.util.HashMap)7 HasPhysicalDestination (nl.nn.adapterframework.core.HasPhysicalDestination)7 SenderException (nl.nn.adapterframework.core.SenderException)7 XmlBuilder (nl.nn.adapterframework.util.XmlBuilder)7 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)6 ISenderWithParameters (nl.nn.adapterframework.core.ISenderWithParameters)6 ITransactionalStorage (nl.nn.adapterframework.core.ITransactionalStorage)6 IOException (java.io.IOException)5 LinkedHashMap (java.util.LinkedHashMap)5 HasSender (nl.nn.adapterframework.core.HasSender)5 Serializable (java.io.Serializable)4 IPipe (nl.nn.adapterframework.core.IPipe)4 ListenerException (nl.nn.adapterframework.core.ListenerException)4 PipeLine (nl.nn.adapterframework.core.PipeLine)4 StatisticsKeeper (nl.nn.adapterframework.statistics.StatisticsKeeper)4 Message (nl.nn.adapterframework.stream.Message)4 IListener (nl.nn.adapterframework.core.IListener)3 ParameterResolutionContext (nl.nn.adapterframework.parameters.ParameterResolutionContext)3 MessageSendingPipe (nl.nn.adapterframework.pipes.MessageSendingPipe)3