什么时候线程,什么时候 WAR? [英] When to thread and when to WAR?

查看:83
本文介绍了什么时候线程,什么时候 WAR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计/开发一个 Web 应用程序,该应用程序最终将作为 WAR 部署到 Tomcat.此应用程序的功能之一是用户能够将图像上传到我们的服务器并对其进行编辑(生成缩略图等).在幕后,我们将使用 ImageMagick 及其 Java 适配器库 IM4Java.

初始原型表明,每次我们重新部署应用程序时,ImageMagick 都需要一些时间在我们的服务器上预热".这促使我们考虑以下两种可能性之一:

  • 创建一个 ImageService.war 网络服务,该服务与主应用程序一起部署,并基本上处理所有对 IM4Java 的调用(暴露,例如,一个 RESTful 服务,只是在收到请求时运行 ImageMagick;然后主 Web 应用程序可以在同一本地文件系统上找到编辑过的文件);或
  • 只需创建一个 Thread 子类(即 ImageServiceThread),启动它并在部署时运行它,并确保它在 Tomcat 取消部署应用程序时关闭

这两个前景让我从更抽象的意义上思考这个问题:什么时候应该简单地将工作委托给一个单独的线程,什么时候制作一个成熟的、单独的应用程序(在我们的如果发生战争)?

我们的主要应用程序将是此图像服务"的唯一用户,这让我认为单独的 WAR 是多余且不必要的.但我以前从未处理过 Tomcat 内部的线程,我不确定是否可以生成和终止线程,使其生命周期与主/主应用程序线程的生命周期一致.

作为此类决定的一部分,应考虑哪些因素?提前致谢.

解决方案

在需要它正常运行的 Web 应用程序中绝对保持预加载 ImageMagick.这样,您就永远不会忘记两个 Web 应用程序应该一起部署.

将特定 Web 应用程序所需的所有内容放在一个地方是个好主意:这就是创建 WAR 格式并存在诸如 ServletContextListener 之类的东西的原因.

老实说,我不确定我是否会费心去线程化"IM 的预加载——只需从 ServletContextListener 运行一些示例命令并让它同步加载.

I am designing/developing a web application that will ultimately deploy as a WAR to Tomcat. One of the features of this application will be the ability for users to upload images to our server and edit them (generate thumbnails, etc.). Under the hood we will be using ImageMagick and its Java adaptor library IM4Java.

Initial prototypes have shown that ImageMagick takes some time to "warm up" on our servers every time we re-deploy the application. This has prompted us to consider one of two possibilities, either:

  • Create an ImageService.war web service that deploys alongside the primary app and basically handles all calls to IM4Java under the hood (exposes, say, a RESTful service and just runs ImageMagick when it receives a request; the primary web app cann then find the edited file(s) on the same local file system); or
  • Just create a Thread subclass (i.e. ImageServiceThread), kick it off and run it at deploy-time, and make sure it gets shut down when Tomcat undeploys the app

These two prospects got me thinking about this problem in a more abstract sense: when should one simply delegate work out to a separate thread, and when is it appropriate to make a full-blown, separate app (in our case a WAR)?

Our primary app will be the only user of this "Image Service", which has me thinking a separate WAR is overkill and unnecessary. But I've never dealt with threading inside of Tomcat before and am not sure if it is possible to spawn and kill a thread so that its lifecycle coincides with that of the primary/main app thread.

What sort of factors should be considered as part of such a decision? Thanks in advance.

解决方案

Definitely keep the pre-loading of ImageMagick in the web application that requires it to run properly. That way, you won't ever forget that the two webapps should be deployed together.

It's a good idea to keep everything a particular webapp requires together in one place: that's why the WAR format was created and things such as ServletContextListeners exist.

Honestly, I'm not sure I would bother to "thread" the pre-loading of IM at all -- just run some sample command from a ServletContextListener and have it load synchronously.

这篇关于什么时候线程,什么时候 WAR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆