Search in sources :

Example 71 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class CleanupDatabaseJob method execute.

@Override
public void execute(IbisManager ibisManager) {
    Date date = new Date();
    int maxRows = AppConstants.getInstance().getInt("cleanup.database.maxrows", 25000);
    List<MessageLogObject> messageLogs = getAllMessageLogs(ibisManager);
    for (MessageLogObject mlo : messageLogs) {
        FixedQuerySender qs = null;
        try {
            qs = SpringUtils.createBean(getApplicationContext(), FixedQuerySender.class);
            qs.setDatasourceName(mlo.getDatasourceName());
            qs.setName("cleanupDatabase-" + mlo.getTableName());
            qs.setQueryType("other");
            qs.setTimeout(getQueryTimeout());
            qs.setScalar(true);
            Parameter param = new Parameter("now", DateUtils.format(date));
            param.setType(ParameterType.TIMESTAMP);
            qs.addParameter(param);
            String query = qs.getDbmsSupport().getCleanUpIbisstoreQuery(mlo.getTableName(), mlo.getKeyField(), mlo.getTypeField(), mlo.getExpiryDateField(), maxRows);
            qs.setQuery(query);
            qs.configure();
            qs.open();
            boolean deletedAllRecords = false;
            while (!deletedAllRecords) {
                Message result = qs.sendMessage(Message.nullMessage(), null);
                String resultString = result.asString();
                log.info("deleted [" + resultString + "] rows");
                int numberOfRowsAffected = Integer.valueOf(resultString);
                if (maxRows <= 0 || numberOfRowsAffected < maxRows) {
                    deletedAllRecords = true;
                } else {
                    log.info("executing the query again for job [cleanupDatabase]!");
                }
            }
        } catch (Exception e) {
            String msg = "error while deleting expired records from table [" + mlo.getTableName() + "] (as part of scheduled job execution): " + e.getMessage();
            getMessageKeeper().add(msg, MessageKeeperLevel.ERROR);
            log.error(getLogPrefix() + msg);
        } finally {
            if (qs != null) {
                qs.close();
            }
        }
    }
}
Also used : Message(nl.nn.adapterframework.stream.Message) Parameter(nl.nn.adapterframework.parameters.Parameter) Date(java.util.Date) FixedQuerySender(nl.nn.adapterframework.jdbc.FixedQuerySender)

Example 72 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class FixedResultSender method sendMessage.

@Override
public Message sendMessage(Message message, PipeLineSession session) throws SenderException, TimeoutException {
    String result = returnString;
    if (paramList != null) {
        ParameterValueList pvl;
        try {
            pvl = paramList.getValues(message, session);
        } catch (ParameterException e) {
            throw new SenderException("exception extracting parameters", e);
        }
        if (pvl != null) {
            for (ParameterValue pv : pvl) {
                result = replace(result, "${" + pv.getDefinition().getName() + "}", pv.asStringValue(""));
            }
        }
    }
    if (getSubstituteVars()) {
        result = StringResolver.substVars(returnString, session);
    }
    if (StringUtils.isNotEmpty(styleSheetName)) {
        URL xsltSource = ClassUtils.getResourceURL(this, styleSheetName);
        if (xsltSource != null) {
            try {
                String xsltResult = null;
                Transformer transformer = XmlUtils.createTransformer(xsltSource);
                xsltResult = XmlUtils.transformXml(transformer, result);
                result = xsltResult;
            } catch (IOException e) {
                throw new SenderException("cannot retrieve [" + styleSheetName + "], resource [" + xsltSource.toString() + "]", e);
            } catch (TransformerConfigurationException te) {
                throw new SenderException("got error creating transformer from file [" + styleSheetName + "]", te);
            } catch (TransformerException te) {
                throw new SenderException("got error transforming resource [" + xsltSource.toString() + "] from [" + styleSheetName + "]", te);
            } catch (SAXException se) {
                throw new SenderException("caught SAXException", se);
            }
        }
    }
    log.debug("returning fixed result [" + result + "]");
    return new Message(result);
}
Also used : ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) Transformer(javax.xml.transform.Transformer) ParameterValue(nl.nn.adapterframework.parameters.ParameterValue) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Message(nl.nn.adapterframework.stream.Message) ParameterException(nl.nn.adapterframework.core.ParameterException) IOException(java.io.IOException) SenderException(nl.nn.adapterframework.core.SenderException) URL(java.net.URL) TransformerException(javax.xml.transform.TransformerException) SAXException(org.xml.sax.SAXException)

Example 73 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class MailSenderBase method sendMessage.

@Override
public Message sendMessage(Message message, PipeLineSession session) throws SenderException, TimeoutException {
    MailSession mailSession;
    try {
        mailSession = extract(message, session);
    } catch (DomBuilderException e) {
        throw new SenderException(e);
    }
    sendEmail(mailSession);
    String correlationID = session == null ? null : session.getMessageId();
    return new Message(correlationID);
}
Also used : Message(nl.nn.adapterframework.stream.Message) DomBuilderException(nl.nn.adapterframework.util.DomBuilderException) SenderException(nl.nn.adapterframework.core.SenderException)

Example 74 with Message

use of nl.nn.adapterframework.stream.Message 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 Message doSendMessage(Message message, PipeLineSession session) throws SenderException, TimeoutException {
    Guard guard = new Guard();
    Map<ISender, ParallelSenderExecutor> executorMap = new HashMap<ISender, ParallelSenderExecutor>();
    TaskExecutor executor = createTaskExecutor();
    // XsltSender and IbisLocalSender).
    for (Iterator<ISender> it = getExecutableSenders(); it.hasNext(); ) {
        ISender sender = it.next();
        guard.addResource();
        ParallelSenderExecutor pse = new ParallelSenderExecutor(sender, message, session, 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");
    String correlationID = session == null ? null : session.getMessageId();
    resultsXml.addAttribute("correlationID", correlationID);
    XmlBuilder originalMessageXml = new XmlBuilder("originalMessage");
    try {
        originalMessageXml.setValue(XmlUtils.skipXmlDeclaration(message.asString()), false);
    } catch (IOException e) {
        throw new SenderException(getLogPrefix(), e);
    }
    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 = 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 {
        resultISender.sendMessage(new Message(resultsXml.toXML()), session);
    } catch (SenderException se) {
        log.warn("failed to send ShadowSender result to [" + resultISender.getName() + "]");
    }
    if (originalSender.getThrowable() != null) {
        throw new SenderException(originalSender.getThrowable());
    }
    return originalSender.getReply();
}
Also used : Message(nl.nn.adapterframework.stream.Message) HashMap(java.util.HashMap) IOException(java.io.IOException) 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)

Example 75 with Message

use of nl.nn.adapterframework.stream.Message in project iaf by ibissource.

the class SambaSenderOld method sendMessage.

@Override
public Message sendMessage(Message message, PipeLineSession session) throws SenderException, TimeoutException {
    ParameterValueList pvl = null;
    try {
        if (paramList != null) {
            pvl = paramList.getValues(message, session);
        }
    } catch (ParameterException e) {
        throw new SenderException(getLogPrefix() + "Sender [" + getName() + "] caught exception evaluating parameters", e);
    }
    SmbFile file;
    try {
        file = new SmbFile(smbContext, message.asString());
    } catch (IOException e) {
        throw new SenderException(getLogPrefix() + "unable to get SMB file", e);
    }
    try {
        if (getAction().equalsIgnoreCase("download")) {
            SmbFileInputStream is = new SmbFileInputStream(file);
            InputStream base64 = new Base64InputStream(is, true);
            return new Message(Misc.streamToString(base64));
        } else if (getAction().equalsIgnoreCase("list")) {
            return new Message(listFilesInDirectory(file));
        } else if (getAction().equalsIgnoreCase("upload")) {
            Message paramValue = pvl.getParameterValue("file").asMessage();
            try (SmbFileOutputStream out = new SmbFileOutputStream(file)) {
                out.write(paramValue.asByteArray());
            }
            return new Message(getFileAsXmlBuilder(new SmbFile(smbContext, message.asString())).toXML());
        } else if (getAction().equalsIgnoreCase("delete")) {
            if (!file.exists())
                throw new SenderException("file not found");
            if (file.isFile())
                file.delete();
            else
                throw new SenderException("trying to remove a directory instead of a file");
        } else if (getAction().equalsIgnoreCase("mkdir")) {
            if (isForced())
                file.mkdirs();
            else
                file.mkdir();
        } else if (getAction().equalsIgnoreCase("rmdir")) {
            if (!file.exists())
                throw new SenderException("folder not found");
            if (file.isDirectory())
                file.delete();
            else
                throw new SenderException("trying to remove a file instead of a directory");
        } else if (getAction().equalsIgnoreCase("rename")) {
            String destination = pvl.getParameterValue("destination").asStringValue();
            if (destination == null)
                throw new SenderException("unknown destination[+destination+]");
            SmbFile dest = new SmbFile(smbContext, destination);
            if (isForced() && dest.exists())
                dest.delete();
            file.renameTo(dest);
        }
    } catch (Exception e) {
        // Different types of SMB exceptions can be thrown, no exception means success.. Got to catch them all!
        throw new SenderException(getLogPrefix() + "unable to process action for SmbFile [" + file.getCanonicalPath() + "]", e);
    }
    return new Message("<result>ok</result>");
}
Also used : SmbFileInputStream(jcifs.smb.SmbFileInputStream) ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) Message(nl.nn.adapterframework.stream.Message) Base64InputStream(org.apache.commons.codec.binary.Base64InputStream) SmbFileInputStream(jcifs.smb.SmbFileInputStream) InputStream(java.io.InputStream) ParameterException(nl.nn.adapterframework.core.ParameterException) IOException(java.io.IOException) SenderException(nl.nn.adapterframework.core.SenderException) Base64InputStream(org.apache.commons.codec.binary.Base64InputStream) SmbFileOutputStream(jcifs.smb.SmbFileOutputStream) SmbException(jcifs.smb.SmbException) TimeoutException(nl.nn.adapterframework.core.TimeoutException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) SenderException(nl.nn.adapterframework.core.SenderException) ParameterException(nl.nn.adapterframework.core.ParameterException) SmbFile(jcifs.smb.SmbFile)

Aggregations

Message (nl.nn.adapterframework.stream.Message)598 Test (org.junit.Test)385 PipeLineSession (nl.nn.adapterframework.core.PipeLineSession)220 PipeRunResult (nl.nn.adapterframework.core.PipeRunResult)114 IOException (java.io.IOException)112 SenderException (nl.nn.adapterframework.core.SenderException)97 ConfigurationException (nl.nn.adapterframework.configuration.ConfigurationException)54 ParameterValueList (nl.nn.adapterframework.parameters.ParameterValueList)54 Parameter (nl.nn.adapterframework.parameters.Parameter)52 PipeForward (nl.nn.adapterframework.core.PipeForward)41 Date (java.util.Date)37 TimeoutException (nl.nn.adapterframework.core.TimeoutException)31 UrlMessage (nl.nn.adapterframework.stream.UrlMessage)31 PipeRunException (nl.nn.adapterframework.core.PipeRunException)30 ByteArrayInputStream (java.io.ByteArrayInputStream)29 InputStream (java.io.InputStream)29 ParameterList (nl.nn.adapterframework.parameters.ParameterList)28 ListenerException (nl.nn.adapterframework.core.ListenerException)27 ParameterException (nl.nn.adapterframework.core.ParameterException)25 SAXException (org.xml.sax.SAXException)19