N
N
natomist2014-11-24 13:09:20
Java
natomist, 2014-11-24 13:09:20

What are the ways to start a long running process from Tomcat?

There is an item in the site admin panel, according to which a certain procedure lasting 2-3 minutes should be launched.
Initially there was an idea to implement it through a servlet. But I would like to implement a reserve of reliability for the future. So that everything works correctly, even if the whole process takes 10-15 minutes. I would also like to see some kind of progress bar on the client side in order to see the stages of the process.
Actually the question is: what technologies exist (and what words to google) to safely run lengthy procedures from Tomcat?
Thank you.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
SaertiS, 2014-11-24
@SaertiS

If you still decide to do it on servlets, then in the request processing method you can call the session and set a timeout. javax.servlet.http.HttpSession#setMaxInactiveInterval

Y
YV, 2014-11-24
@targetjump

If you do not need sockets in terms of "I would also like to see some progress bar", then you can get by with a
REST service and ConcurrentMap<String, FutureTask<T>>in my vision it is taskId -> task. In order to control "long-running procedures", you can use an ExecutorService with its own ThreadFactory, which will produce daemon threads that you can, if you so desire, kill by timeout. Well, it's clear that "status", "task launch" and so on should be implemented at the service level.

R
relgames, 2014-11-24
@relgames

The most important question is do you want a synchronous or asynchronous API?
When synchronous, the user sends a request and waits for a response in the same HTTP call.
Progress can be written directly to the servlet's output stream, but the protocol needs to be negotiated.
For example, write lines like STATUS REPORT: 80%, and the client must be able to distinguish such lines from real data.
If the request breaks, then there must be a mechanism to connect to the same task. It's quite difficult, but real. For example, you can first generate some ID and issue it in the first lines, and if the connection breaks, the client can reconnect with the same ID.
With an asynchronous API, an ID is generated, the task is queued, and the client receives this ID, and the HTTP call ends. Then the client should periodically poll another API (for example, getResult(id) ), and if there is no result yet, this API can return something like "NOT READY, STATUS 85%" in the headers.
Approaches can be combined - task launch can be asynchronous, and getResult can be synchronous.
The specific implementation depends on many factors. You can simply add a task to the Executor, and write the status to some queue. It is possible to do on JMS. It is possible to start another process altogether and communicate with them via std in/out.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question