简体   繁体   English

Java中从Java进行异步处理

[英]Asynchronous processing in Java from a servlet

I currently have a tomcat container -- servlet running on it listening for requests. 我目前有一个tomcat容器 - 在其上运行的servlet监听请求。 I need the result of an HTTP request to be a submission to a job queue which will then be processed asynchronously. 我需要HTTP请求的结果是作业队列的提交,然后将异步处理。 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?) 1)JMS - 使用ActiveMQ(但是在这种情况下谁是该作业的消费者另一个servlet?)

2) Have my request create a row in the DB. 2)让我的请求在数据库中创建一行。 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). 在我的Tomcat容器中有一个始终运行的独立servlet - 它使用Quartz Scheduler或java.util.concurrent中提供的实用程序来连续处理作为作业的行(使用线程池)。

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. 我倾向于后者,因为查看JMS文档让我头疼,虽然我知道它是一个更强大的解决方案,但我需要相对快速地实现它。 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. 很多人都认为Spring可能对1或2都有好处。但是我从未使用过Spring,我甚至不知道如何开始使用它来解决这个问题。 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. 否则,如果您可以权衡选项1或2,这也是有用的。

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. 我还将从该站点提取文件并将其存储在S3中。

Your Quartz Job doesn't need to be a Servlet! 您的Quartz作业不需要是Servlet! You can persist incoming Jobs in the DB and have Quartz started when your main Servlet starts up. 您可以在数据库中保留传入的作业,并在主Servlet启动时启动Quartz。 The Quartz Job can be a simple POJO and check the DB for any jobs periodically. Quartz Job可以是一个简单的POJO,并定期检查DB是否有任何作业。

However, I would suggest to take a look at Spring. 但是,我建议你看一下Spring。 It's not hard to learn and easy to setup within Tomcat . 在Tomcat中学习并轻松设置并不难。 You can find a lot of good information in the Spring reference documentation . 您可以在Spring参考文档中找到许多有用的信息。 It has Quartz integration, which is much easier than doing it manually. 它具有Quartz集成,比手动操作容易得多。

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. 一个不需要你进行大量设计和编程的合适解决方案是创建稍后在servlet中需要的对象,并将其序列化为字节数组。 Then put that in a BLOB field in the database and be done with it. 然后将它放在数据库的BLOB字段中并完成它。

Then your processing thread can just read the contents, deserialize it and work with the ressurrected object. 然后你的处理线程可以只读取内容,反序列化它并使用ressurrected对象。

But, you may get better answers by describing what you need your system to actually DO :) 但是,您可以通过描述您的系统实际需要的内容来获得更好的答案:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM