0

我之前问过一个关于 ExecutorService 和 Apache Velocity 初始化的问题。快速回顾一下——我有一个 Java EE 前端,它接受用户请求,然后对于这些请求中的每一个,使用 ExecutorService(SingleThreadedExecutor 设置为守护进程)来启动一个冗长的工作流程。这个工作流程包含在一个库中并且可以工作通过eclipse以独立模式运行时,正如预期的那样。当从网站(servlet)调用时,我观察到工作流一直挂在 Velocity Engine 正在初始化的点(Velocity.init() 或 ve.init())。因此,我前面提到的问题。

当所有答案/建议都不起作用时,我推断这与 Velocity 的启动方式有关,并决定转向 FreeMarker。现在我看到 FreeMarker 实现的工作流也挂在了完全相同的位置。这个“地方”是邮件构建部分,它根据一组传递的数据对象评估模板并返回邮件字符串。调用 Freemark 类和 FreeMark 类的类如下 -

   public class mailBuilder {

    private static final Logger log = Logger.getLogger( mailBuilder.class );    
    static String a;
    static String b;

    public mailBuilder(CustomDataStructure input)
    {
        a = input.getA();
        b = input.getB(); 
    }
    public static String returnMailstring() throws Exception
    {
        log.info("Gathering elements to construct email.");
        String mailText=null;
        Map context = new HashMap();
        context.put("a",a);
        context.put("b",b);
        log.info("Calling Freemarker");
        mailText=FreeMarkIT.ReturnReportString(context);
        log.info("Freeemarker returned string");

        return mailText;
    }
}

FreeMarkIT 类如下——

    public class FreeMarkIT {
        private static final Logger log = Logger.getLogger( FreeMarkIT.class );
        private static Configuration config;
        private static Template template;

        public static String ReturnReportString(Map model) throws IOException, TemplateException
        {
            StringWriter sw = new StringWriter();
            try 
            {
               log.info("Going to get the template");
               config= new Configuration();
               log.info("Now really");
               template=config.getTemplate("src/resource/email_template.vm");
               log.info("Done initializing template");
               template.process(model, sw);
               sw.flush();
            }
            catch(Exception e)
            {
                System.out.println(e.getMessage());
            }

            return sw.getBuffer().toString();
        }
    }

现在,从我的日志来看,工作线程似乎挂在了线上 config=new Configuration()

同样,这在从 Eclipse 运行时在独立模式下按预期工作,但在使用 ExecutorService 从 servlet 调用时挂起。

我开始思考/意识到这可能与 Velocity 或 FreeMarker 无关,而与 ExecutorService 有关。任何意见或建议都会有很大帮助。

谢谢

4

1 回答 1

3

您的代码不是线程安全的,因为您正在共享configtemplate跨所有线程实例(并不断重新设置它们)。使其线程安全的最简单方法是在方法中创建局部变量configtemplate不是静态成员。正如@JBNizet 在评论中指出的那样,您在mailBuilderwitha中遇到了类似的问题b。您可能想先查看一些有关面向对象编程基础的教程,然后再回到这个问题(提示,通常您应该避免使用静态成员变量,除了常量)。

于 2012-01-01T14:00:00.817 回答