Thymeleaf | TemplateEngine.process是否是线程安全的操作?

时间:2014-04-11 07:45:19

标签: java multithreading thread-safety thymeleaf

我使用thymeleaf模板引擎来构建对SOAP服务的请求。执行发送的类看起来像这样:

import java.io.StringReader;
import java.util.*;
import javax.xml.transform.stream.*;

import org.springframework.ws.client.core.WebServiceTemplate;

import org.thymeleaf.*;
import org.thymeleaf.context.*;
import org.thymeleaf.templateresolver.*;

public class SomeService 
{
    private final TemplateEngine templateEngine;
    private final WebServiceTemplate webServiceTemplate;

    public RetrieveServiceThymeleafBased() {
        webServiceTemplate = new WebServiceTemplate();
        webServiceTemplate.setDefaultUri("http://some.path/to/soap/service");

        ITemplateResolver templateResolver = new ClassLoaderTemplateResolver();
        templateEngine = new TemplateEngine();
        templateEngine.addTemplateResolver(templateResolver);
    }

    public void sendMessage(String orderNumber) {
        Context params = new Context();
        params.setVariable("CreateDateTime", new Date());
        params.setVariable("GUID", UUID.randomUUID());
        String xmlContent = templateEngine.process("template.xml", params);

        StreamSource source = new StreamSource(new StringReader(xmlContent));
        StreamResult result = new StreamResult(System.out);
        webServiceTemplate.sendSourceAndReceiveToResult(source, result);
    }
}

我知道webServiceTemplate.sendSourceAndReceiveToResult - 是线程安全的操作,但我无法找到有关templateEngine.process

的此类信息

问题:templateEngine.process是否是线程安全的操作

1 个答案:

答案 0 :(得分:14)

是的,确实如此。 TemplateEngine个对象被设计用于高度并发的环境而没有问题(我希望!;))。

免责声明,根据StackOverflow规则:我是Thymeleaf的作者。