Stateful processes (daemons?) In Tomcat?

I'm not really sure what question to ask here since I don't know the vocabulary ...

Tomcat servlets (and any server, for that matter) work well if they are stateless and responsive to requests, with the state stored in the database. It looks like if I have long running operations, maybe I want to start another service in the background and handle the Tomcat handlers. Is there a way to run a long running Java application in the same JVM as Tomcat and interact with it through a "regular" Tomcat servlet?


Example: Suppose I want to offer a RESTful number factorization service in HTTP.

Here is a possible scenario (I hope I have the HTTP syntax on the right, I am missing most of the headers):

# comments start with #, > = request, < = response
# 
# first we create a queue
> POST /factorizer/create-queue
> {information here}
< queue=12345B
# then we post some numbers to it
> POST /factorizer/queue/12345B
> 123
> 456
> 678
> 12345678901234567890123456789
< OK
# let look at the status
> GET /factorizer/queue/12345B/status
< requested=4
< processed=3
# query
> GET /factorizer/queue/12345B/7
< Error: invalid index
> GET /factorizer/queue/12345B/3
< Error: not complete
> GET /factorizer/queue/12345B/0
< 123=3*41
# wait a while
> GET /factorizer/queue/12345B/status
< requested=4
< processed=4
> GET /factorizer/queue/12345B/3
< 12345678901234567890123456789=3*3*3*7*13*31*37*211*241*2161*3607*3803*2906161

      

I can think of how to write a servlet to handle requests, but how can I start implementing a daemon / independent startup service in the same JVM?

edit: In the above example, I would like to make a background application that runs standalone, with worker queues to convert prime numbers, and has a Java interface that supports the operations that Tomcat servlets could use to provide a service on the Internet. Then I donโ€™t need to worry about the web interface or HTTP in my background application, and I donโ€™t need to worry about multithreading or simple factorization issues in my servlets.

+2


source to share


1 answer


If you don't need to be in the same JVM (that is, if you don't need the performance of direct object access), you can write another Tomcat application and link other applications to it over HTTP to the localhost. In fact, you will be writing a web service that runs on a single machine. (I don't know how several other Tomcat applications can see each other. This is the problem that Enterprise Java Beans solves, but it might be too heavy a solution for you.)

If you only have one Tomcat application that needs to do this, create a worker thread and place it in an application context where all requests can communicate with it.



As for your specific problem, it looks like you are describing something like the asynchronous pattern in The Remaining O'Reilly Web Services. This uses the 202 Accepted status code to indicate that processing is pending. See "Asynchronous Operations" in Chapter 8 of the book.

http://www.amazon.com/RESTful-Web-Services-Leonard-Richardson/dp/0596529260/ref=sr_1_1?ie=UTF8& s = Books & QID = 1255555328 & Wed = 8-1

+1


source







All Articles