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();
}
}
}
}
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);
}
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);
}
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();
}
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>");
}
Aggregations