作为我最后一年项目的一部分,我开发了一个桌面应用程序,如果存在这样的东西,它就属于“图形 IDE”的类别。我已经实现了Jessy James Garrett Visual Vocabulary for Information Architecture and Interaction Design的一个小子集,因此用户可以绘制表示用户在 web 应用程序中的体验的图表(换句话说,有向图),将 HTML 模板分配给页面并编写一旦编译应用程序并且用户单击相应的超链接,将执行连接器/转换的一些代码。
(我不能发布多个超链接,但我指的是页面和连接器,例如 JJG 的视觉词汇中描述的用户体验元素)
所以我使用不同的 SwingWorkers 来生成一组 C++ 源文件,其中图表被翻译成。查看日志,我发现我的应用程序总是在创建新线程,而不是重用它们。
15:29:55.750 [SwingWorker-pool-2-thread-26] DEBUG i.v.a.ui.worker.ConnectorGenerator - Building source code for transition: connector_29
15:29:55.750 [SwingWorker-pool-2-thread-26] DEBUG i.v.a.ui.worker.ConnectorGenerator - Project retrieved: sasdasdasd
15:29:55.750 [SwingWorker-pool-2-thread-26] INFO i.v.a.webapp.htcpp.CTransition - Generated C:\Workspaces\PFC\aedifico-ui\sasdasdasd\connector_29_action.h...
15:29:55.750 [SwingWorker-pool-2-thread-26] INFO i.v.a.webapp.htcpp.CTransition - Generated C:\Workspaces\PFC\aedifico-ui\sasdasdasd\connector_29_action.cpp...
15:29:55.750 [SwingWorker-pool-2-thread-26] DEBUG i.v.a.ui.worker.ConnectorGenerator - Transition generated at: C:\Workspaces\PFC\aedifico-ui\sasdasdasd/connector_29_action.cpp
我所有的工人都做同样的两件事:
使用 Freemarker 模板引擎生成一对 C++ 源文件和头文件。
通过发布过程机制向用户发送消息,
EDT
以告知用户事情进展如何。
我相信我已经对SwingWorker
s 进行了仔细的编码。我特别担心 FileWriter 实例没有按预期关闭,但我看不出ThreadPoolExecutor
没有重用它之前创建的线程的原因。
魔法发生在ConnectorGenerator
. BaseWorker
扩展SwingWorker<V,T>
并仅保存与组件通信以向用户显示消息的行为。
public class ConnectorGenerator<Void> extends BaseWorker<Void> {
@Override
public Void doInBackground() {
Transition transition = connector.getModel().getTransition();
logger.debug("Building source code for transition: {}", transition.getName());
logger.debug("Project retrieved: {}", project.getName());
try {
publish("Generating transition (%s) source code at %s", transition.getName(), project.getBaseDir());
/**
* Transition.build(String), attached below, is responsible of generating and writing the files
*/
transition.build(project.getBaseDir().getPath());
publish("Transition generated.");
} catch (BuilderException e) {
logger.error("Error: {}", e);
publish("There was an error that prevented generating %s's source code", transition.getName());
}
logger.debug("Transition generated at: {}/{}", project.getBaseDir(), transition.getSource());
return null;
}
}
以及该Transition.build(String)
方法,包括一个丑陋的 try-catch-finally 块:
@Override
public void build(String path) throws BuilderException {
generateFile(String.format("%s%s%s", path, File.separator, getHeader()), "action.h.ftl");
generateFile(String.format("%s%s%s", path, File.separator, getSource()), "action.cpp.ftl");
}
private void generateFile(String path, String templateName) throws BuilderException {
FileWriter out = null;
try {
Map<String, Object> model = new HashMap<String, Object>();
model.put("transition", this);
Configuration config = new Configuration();
config.setClassForTemplateLoading(CTransition.class, "/");
config.setObjectWrapper(ObjectWrapper.DEFAULT_WRAPPER);
out = new FileWriter(path);
freemarker.template.Template template = config.getTemplate(templateName);
template.process(model, out, ObjectWrapper.BEANS_WRAPPER);
stdout.info("Generated {}...", path);
} catch (IOException e) {
throw new BuilderException(e);
} catch (TemplateException e) {
throw new BuilderException(e);
} finally {
if (out != null)
try {
out.flush();
out.close();
} catch (IOException e) {
throw new BuilderException(e);
}
}
}
有人看到或知道我可能遗漏的东西吗?