use of nl.nn.adapterframework.util.Guard in project iaf by ibissource.
the class IsolatedServiceCaller method callServiceIsolated.
public String callServiceIsolated(String serviceName, String correlationID, String message, HashMap context, boolean targetIsJavaListener) throws ListenerException {
Guard guard = new Guard();
guard.addResource();
IsolatedServiceExecutor ise = new IsolatedServiceExecutor(serviceName, correlationID, message, context, targetIsJavaListener, guard);
getTaskExecutor().execute(ise);
try {
guard.waitForAllResources();
} catch (InterruptedException e) {
throw new ListenerException(ClassUtils.nameOf(this) + " was interupted", e);
}
if (ise.getThrowable() != null) {
if (ise.getThrowable() instanceof ListenerException) {
throw (ListenerException) ise.getThrowable();
} else {
throw new ListenerException(ise.getThrowable());
}
} else {
return (String) ise.getReply();
}
}
use of nl.nn.adapterframework.util.Guard 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();
}
}
use of nl.nn.adapterframework.util.Guard 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();
}
Aggregations