Tomcat 中的有状态进程(守护进程?)?

发布于 2024-08-08 00:02:36 字数 1423 浏览 7 评论 0原文

我不太确定在这里问什么问题,因为我不知道词汇...

Tomcat servlet(以及与此相关的任何服务器)如​​果是无状态的并且可以快速响应请求,并且状态存储在数据库中,那么它们就可以很好地工作。看起来如果我有长时间运行的操作,那么我可能想在后台运行一些其他服务,并让 Tomcat 处理程序与其通信。有没有办法在与 Tomcat 相同的 JVM 中运行长时间运行的 Java 应用程序,并通过“常规”Tomcat servlet 与其交互?


示例: 假设我想在 HTTP 中提供 RESTful 数字分解服务。

这是一个可能的场景(我希望我的 HTTP 语法是正确的,我省略了大部分标头):

# 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's 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

我可以想到如何编写 servlet 来处理查询,但是我如何才能独立实现一个守护进程/在同一个 JVM 中运行服务?

编辑:在上面的例子中,我想要做的是有一个后台应用程序,它可以自主运行,带有工作队列,对素数进行因式分解,并且有一个Java接口,支持以下操作: Tomcat servlet 可以用来向网络公开服务。这样我就不必担心后台应用程序中的 Web 界面或 HTTP,也不必担心 servlet 中的多线程问题或素因式分解。

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

Tomcat servlets (and any server for that matter) work nicely if they are stateless and respond quickly to requests, with state stored in a database. It seems like if I have long-running operations then maybe I want to run some other service in the background, and have the Tomcat handlers communicate with it. Is there a way to run a long-running Java application in the same JVM as Tomcat and interact with it via "regular" Tomcat servlet?


Example: Let's say I want to offer a RESTful number factorization service in HTTP.

Here's a possible scenario (I hope I have the HTTP syntax right, I'm omitting 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's 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 the servlet to handle the queries, but how could I go about implementing a daemon / independently-running service in the same JVM?

edit: In the above example, what I would like to do is to have a background application that runs autonomously, with work queues, to factor prime numbers, and has a Java interface that supports the operations that the Tomcat servlets could use to expose the service to the web. Then I don't have to worry about the web interface or HTTP in my background app, and I don't have to worry about multithreading issues or prime factorization in my servlets.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叫嚣ゝ 2024-08-15 00:02:36

如果您绝对不需要位于同一个 JVM 中(即,如果您不需要直接访问对象的性能),您可以编写另一个 Tomcat 应用程序,并让其他应用程序通过 HTTP 到本地主机与其进行通信。实际上,您将编写一个恰好在同一台计算机上运行的 Web 服务。 (我不知道多个 Tomcat 应用程序如何才能互相看到。这是 Enterprise Java Beans 解决的问题,但这对您来说可能是一个太重量级的解决方案。)

如果您只有一个 Tomcat 应用程序需要执行以下操作为此,创建一个工作线程并将其放入所有请求都可以与其通信的应用程序上下文中。

关于您的具体问题,您似乎正在描述 O'Reilly“Restful Web Services”书中的异步作业模式之类的内容。这使用“202 Accepted”状态代码来指示处理未完成。参见本书第8章的“异步操作”。

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

If you don't absolutely need to be in the same JVM (that is, if you don't need the performance of directly accessing the objects) you could write another Tomcat application and have your other apps communicate with it by HTTP to localhost. In effect you would be writing a web service that happens to run on the same machine. (I don't know how else multiple Tomcat applications can see each other. This is a problem that Enterprise Java Beans solves, but that may be too heavyweight a solution for you.)

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

With regard to your specific problem, it looks like you are describing something like the Asynchronous Job pattern in the O'Reilly "Restful Web Services" book. This uses the "202 Accepted" status code to indicate that the processing is not complete. 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&sr=8-1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文