Java 中 servlet 的异步处理

发布于 2024-07-12 22:30:18 字数 714 浏览 9 评论 0原文

我目前有一个 tomcat 容器——在其上运行的 servlet 监听请求。 我需要将 HTTP 请求的结果提交到作业队列,然后异步处理该作业队列。 我希望每个“作业”都保存在数据库中的一行中,以便在发生故障时进行跟踪和恢复。 我一直在读书。 这是我的选择(注意我必须使用开源的东西来完成所有事情)。

1) JMS -- 使用 ActiveMQ(但是在本例中,谁是作业的使用者,另一个 servlet?)

2) 让我的请求在数据库中创建一行。 在我的 Tomcat 容器中有一个始终运行的单独 servlet —— 它使用 Quartz Scheduler 或 java.util.concurrent 中提供的实用程序来连续处理行作为作业(使用线程池)。

我倾向于后者,因为查看 JMS 文档让我很头疼,虽然我知道它是一个更强大的解决方案,但我需要相对快速地实现它。 无论如何,我预计在部署此服务器的初期不会有大量负载。

很多人说 Spring 可能适合 1 或 2。但是我从未使用过 Spring,我什至不知道如何开始使用它来解决这个问题。 任何有关如何深入研究而无需重写整个项目的指示都会很有用。

否则,如果您可以权衡选项 1 或 2,那也会很有用。

澄清:异步过程是屏幕抓取第三方网站,并向原始请求者发送消息通知。 第三方网站有点不稳定且缓慢,这就是为什么它将作为异步进程进行处理(内置多次重试尝试)。 我还将从该站点提取文件并将它们存储在 S3 中。

I currently have a tomcat container -- servlet running on it listening for requests. I need the result of an HTTP request to be a submission to a job queue which will then be processed asynchronously. I want each "job" to be persisted in a row in a DB for tracking and for recovery in case of failure. I've been doing a lot of reading. Here are my options (note I have to use open-source stuff for everything).

1) JMS -- use ActiveMQ (but who is the consumer of the job in this case another servlet?)

2) Have my request create a row in the DB. Have a seperate servlet inside my Tomcat container that always runs -- it Uses Quartz Scheduler or utilities provided in java.util.concurrent to continously process the rows as jobs (uses thread pooling).

I am leaning towards the latter because looking at the JMS documentation gives me a headache and while I know its a more robust solution I need to implement this relatively quickly. I'm not anticipating huge amounts of load in the early days of deploying this server in any case.

A lot of people say Spring might be good for either 1 or 2. However I've never used Spring and I wouldn't even know how to start using it to solve this problem. Any pointers on how to dive in without having to re-write my entire project would be useful.

Otherwise if you could weigh in on option 1 or 2 that would also be useful.

Clarification: The asynchronous process would be to screen scrape a third-party web site, and send a message notification to the original requester. The third-party web site is a bit flaky and slow and thats why it will be handled as an asynchronous process (several retry attempts built in). I will also be pulling files from that site and storing them in S3.

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

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

发布评论

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

评论(2

情独悲 2024-07-19 22:30:18

您的 Quartz 作业不需要是 Servlet! 您可以将传入的作业保留在数据库中,并在主 Servlet 启动时启动 Quartz。 Quartz Job 可以是一个简单的 POJO,并定期检查数据库中是否有任何作业。

不过,我建议看看Spring。 学习并不难,易于设置雄猫。 您可以在 Spring 参考文档< /a>. 它具有 Quartz 集成,这比手动操作要容易得多。

Your Quartz Job doesn't need to be a Servlet! You can persist incoming Jobs in the DB and have Quartz started when your main Servlet starts up. The Quartz Job can be a simple POJO and check the DB for any jobs periodically.

However, I would suggest to take a look at Spring. It's not hard to learn and easy to setup within Tomcat. You can find a lot of good information in the Spring reference documentation. It has Quartz integration, which is much easier than doing it manually.

回忆躺在深渊里 2024-07-19 22:30:18

不需要您进行大量设计和编程的合适解决方案是创建稍后在 servlet 中需要的对象,并将其序列化为字节数组。 然后将其放入数据库的 BLOB 字段中并完成操作。

然后你的处理线程就可以读取内容,反序列化它并使用复活的对象。

但是,通过描述您需要系统实际执行的操作,您可能会得到更好的答案:)

A suitable solution which will not require you to do a lot of design and programming is to create the object you will need later in the servlet, and serialize it to a byte array. Then put that in a BLOB field in the database and be done with it.

Then your processing thread can just read the contents, deserialize it and work with the ressurrected object.

But, you may get better answers by describing what you need your system to actually DO :)

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