• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is this architecture valid? Tomcat + Callable/Future + Singleton

 
Scott Shipp
Ranch Hand
Posts: 216
11
Eclipse IDE IntelliJ IDE Java Scala Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I think that this is the correct forum for this question, but it nearly falls into some other categories. We had a long-running calculation (ten to twenty minutes) that takes place on a large set of data. In fact, we usually make the calculation on most data sets, but found that for certain data sets, it was blocking pages within our web application from loading. Context: This is in a legacy system that is using Java 6 on top of Tomcat and there's no use of Spring or other frameworks.

To solve the problem, I've implemented a proof-of-concept where I kick off a background thread that runs the calculation within a Callable. I capture the Future and register it with a singleton class (a lightweight version of a cache) that was already in place. This singleton runs a refresh method every so often. In the refresh method, it checks the .isDone() of all the futures registered and if true, calls .get() to retrieve the calculation result and store it in the appropriate object.

Since this is the first time I can think of that I've ever done something like this, I wanted to ask those who have experience in this realm: do you feel this is a valid approach? Am I violating good practices at any point? Are there potential pitfalls I should be aware of? Is it normal to capture the Future rather than the thread or callable? Is there a better way I should have done this?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While die-hards JEE followers will tell you that only the container should create threads, in practice it's not unusual for the servlet container to run background jobs. I personally prefer to restrict those to short-running jobs, though, or to run them during the night when there is little user traffic.

I guess the first question would be: how does this work now? Even an overall bad design (which I don't think this is) might be an improvement over the current situation.

And the second question: could those jobs be run in another server? Like an internal servlet container accessible via a REST API? Since an interactive web app and a long-running background job have different characteristics, and may need different treatment, it might be advantageous to separate them.
 
Scott Shipp
Ranch Hand
Posts: 216
11
Eclipse IDE IntelliJ IDE Java Scala Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer to the first question, unfortunately, is that this doesn't happen right now. These large data sets just choke the existing/old system so much that they don't even become actionable or reviewable in the system. In the highly refactored version that we're working on, we have to make a compromise. The user can upload the large data sets, and they need to be able to start working with some of it right away. So the bare minimum gets crunched and displayed up for their review (there's several phases things have to go through). If it is necessary (it's completely optional) for them before they can continue on to the next phase, then the user can decide to kick off the longer-running calculation. We thought heavily about just kicking off the long-running calculation separately to begin with, when the data is initially uploaded, but it was felt that in the majority of cases, it wasn't actually needed and therefore most of the time the system would be crunching for no reason. Only the user who uploaded the data will know if they actually need this calculation for their work. Therefore we had to make it happen through user interaction.

The answer to the second question is that this calculation does take place on another server. But the result doesn't come back for a long, long time. So capturing the response when it finally comes back is handled in the thread, allowing the user to get along with some other tasks that can be done in the meantime.
 
Scott Shipp
Ranch Hand
Posts: 216
11
Eclipse IDE IntelliJ IDE Java Scala Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. Thanks for the response.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!