Search in sources :

Example 1 with ISenderWithParameters

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

the class ResultBlock2Sender method closeBlock.

public void closeBlock(IPipeLineSession session, String streamId, String blockName, ParameterResolutionContext prc) throws Exception {
    super.closeBlock(session, streamId, blockName, prc);
    int level = decLevel(streamId);
    if (level == 0) {
        StringWriter writer = (StringWriter) getWriter(session, streamId, false, prc);
        if (writer != null) {
            String message = writer.getBuffer().toString();
            log.debug("sending block [" + message + "] to sender [" + sender.getName() + "]");
            writer.getBuffer().setLength(0);
            ISender sender = getSender();
            if (sender instanceof ISenderWithParameters) {
                ISenderWithParameters psender = (ISenderWithParameters) sender;
                psender.sendMessage(streamId + "-" + incCounter(streamId), message, prc);
            } else {
                sender.sendMessage(streamId + "-" + incCounter(streamId), message);
            }
        }
    }
}
Also used : StringWriter(java.io.StringWriter) ISender(nl.nn.adapterframework.core.ISender) ISenderWithParameters(nl.nn.adapterframework.core.ISenderWithParameters)

Example 2 with ISenderWithParameters

use of nl.nn.adapterframework.core.ISenderWithParameters 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 3 with ISenderWithParameters

use of nl.nn.adapterframework.core.ISenderWithParameters 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)

Example 4 with ISenderWithParameters

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

the class RecordXml2Sender method handleRecord.

public Object handleRecord(IPipeLineSession session, List parsedRecord, ParameterResolutionContext prc) throws Exception {
    String xml = (String) super.handleRecord(session, parsedRecord, prc);
    ISender sender = getSender();
    if (sender instanceof ISenderWithParameters) {
        ISenderWithParameters psender = (ISenderWithParameters) sender;
        return psender.sendMessage(session.getMessageId(), xml, prc);
    } else {
        return sender.sendMessage(session.getMessageId(), xml);
    }
}
Also used : ISender(nl.nn.adapterframework.core.ISender) ISenderWithParameters(nl.nn.adapterframework.core.ISenderWithParameters)

Example 5 with ISenderWithParameters

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

the class MessageSendingPipe method sendTextMessage.

protected String sendTextMessage(Object input, IPipeLineSession session, String correlationID, ISender sender, Map threadContext) throws SenderException, TimeOutException {
    if (input != null && !(input instanceof String)) {
        throw new SenderException("String expected, got a [" + input.getClass().getName() + "]");
    }
    // sendResult has a messageID for async senders, the result for sync senders
    if (sender instanceof ISenderWithParameters) {
        // do not only check own parameters, sender may have them by itself
        ISenderWithParameters psender = (ISenderWithParameters) sender;
        ParameterResolutionContext prc = new ParameterResolutionContext((String) input, session, isNamespaceAware());
        return psender.sendMessage(correlationID, (String) input, prc);
    }
    return sender.sendMessage(correlationID, (String) input);
}
Also used : ISenderWithParameters(nl.nn.adapterframework.core.ISenderWithParameters) SenderException(nl.nn.adapterframework.core.SenderException) ParameterResolutionContext(nl.nn.adapterframework.parameters.ParameterResolutionContext)

Aggregations

ISenderWithParameters (nl.nn.adapterframework.core.ISenderWithParameters)6 ISender (nl.nn.adapterframework.core.ISender)5 ParameterResolutionContext (nl.nn.adapterframework.parameters.ParameterResolutionContext)3 HashMap (java.util.HashMap)2 SenderException (nl.nn.adapterframework.core.SenderException)2 StatisticsKeeper (nl.nn.adapterframework.statistics.StatisticsKeeper)2 StringWriter (java.io.StringWriter)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Guard (nl.nn.adapterframework.util.Guard)1 XmlBuilder (nl.nn.adapterframework.util.XmlBuilder)1 TaskExecutor (org.springframework.core.task.TaskExecutor)1