IAmYourFaja
IAmYourFaja

Reputation: 56894

When to thread and when to WAR?

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:

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.

Upvotes: 0

Views: 282

Answers (2)

sperumal
sperumal

Reputation: 1519

ImageMagick takes some time to "warm up"

Its better to offload memory/cpu intensive operations outside of your servlet container. In fact, launch your image service in a separate JVM.

Since your application will be loading images in memory and edit them, you can expect frequent Garbage collection that will impede with the performance of the servlet container there by affecting the performance of other parts of your the web application.

Upvotes: 1

Christopher Schultz
Christopher Schultz

Reputation: 20837

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.

Upvotes: 1

Related Questions