Как могу ли я использовать net.sf.saxon.s9api.XsltTransformer.transform() в нескольких потоках без ущерба для производительности?
Я пытался создать класс TransformerSingleton, как показано ниже
Код: Выделить всё
public class TransformerSingleton {
private static XsltTransformer transformer;
private TransformerSingleton() {
}
public static void xsltLoad() {
if (transformer == null) {
synchronized (XsltTransformer.class){
try {
Processor processor = new Processor(false);
XsltCompiler compiler = processor.newXsltCompiler();
XsltExecutable xslt = compiler.compile(new StreamSource("/my_path/"));
transformer = xslt.load();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static XsltTransformer getTransformer(){
if (transformer == null) {
synchronized (XsltTransformer.class) {
xsltLoad();
}
} return transformer;
}
}
Код: Выделить всё
public static void main(String[] args) {
XsltTransformer transformer = XsltTransformerSingleton.getTransformer();
String xml = "AAA"; // for example
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xml.getBytes())) {
synchronized (transformer){
Transformer.setSource(new StreamSource(byteArrayInputStream));
XdmDestination chainResult = new XdmDestination();
Transformer.setDestination(chainResult);
Transformer.transform();
}
}
}
фактически я достигла 60 запросов в секунду.
Подробнее здесь: https://stackoverflow.com/questions/782 ... o-avoid-im