需要帮助java web应用程序设计执行后台任务 [英] Need help with java web app design to perform background tasks

查看:208
本文介绍了需要帮助java web应用程序设计执行后台任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个安装在台式电脑上的本地网络应用程序,需要通过网络服务定期与远程服务器同步。



我有一个事务表,其存储已在本地处理并且需要发送到远程服务器的事务,并且该表还包含从远程服务器检索(已经被远程处理)并且需要在本地执行的事务(它们具有使用Web服务调用获取)...按时间顺序执行事务,以确保按正确的顺序处理事务。



事务类型的示例是来自商店(例如视频租赁商店)的项目的贷款和返回。例如,某些内容可能已在本地借出并远程返回,反之亦然,或任何顺序的贷款/返回事件。



还有从远程检索的其他信息服务器更新本地记录。



当用户在本地执行任务时,我实时更新本地db,并将事务添加到表中以进行后台处理远程服务器。



处理后台任务的最佳方法是什么。我已经尝试使用在HTTPSessionListener中创建的线程,并使用interrupt()当会话被删除,但我不认为这是最安全的方法。我也尝试使用会话属性作为锁定机制,但这也不是最好的方法。



我也想知道你怎么知道什么时候线程已经完成它运行,以避免同时午餐另一个线程。



我已经得到另一个建议,使用Quartz调度器,我还没有详细阅读这个方法。我将在实践中创建一个Java并发的副本,但我想要一些帮助,以最好的方法的想法,我陷入之前。



不使用网络应用程序框架。



感谢。

解决方案

将创建由容器管理的应用程序范围的线程池。如何做取决于使用的容器。如果你的容器不支持它(例如Tomcat)或者你想要与容器无关,那么基本的方法是实现 ServletContextListener ,使用Java 1.5提供的帮助创建线程池 ExecutorService API在启动时关闭并关闭线程池。如果你不是Java 1.5,或者想要更多的抽象,那么你也可以使用 Spring的 TaskExecutor



有关于并发实用程序,但它还没有进入Java EE 6。 p>

相关问题:




I have a local web app that is installed on a desktop PC, and it needs to regularly sync with a remote server through web services.

I have a "transactions" table that stores transactions that have been processed locally and need to be sent to the remote server, and this table also contains transactions that have retrieved from the remote server (that have been processed remotely) and need to be peformed locally (they have been retrieved using a web service call)... The transactions are performed in time order to ensure they are processed in the right order.

An example of the type of transactions are "loans" and "returns" of items from a store, for example a video rental store. For example something may have been loaned locally and returned remotely or vice versa, or any sequence of loan/return events.

There is also other information that is retrieved from the remote server to update the local records.

When the user performs the tasks locally, I update the local db in real time and add the transaction to the table for background processing with the remote server.

What is the best approach for processing the background tasks. I have tried using a Thread that is created in a HTTPSessionListener, and using interrupt() when the session is removed, but I don't think that this is the safest approach. I have also tried using a session attribute as a locking mechanisim, but this also isn't the best approach.

I was also wondering how you know when a thread has completed it's run, as to avoid lunching another thread at the same time. Or whether a thread has ditched before completing.

I have come accross another suggestion, using the Quartz scheduler, I haven't read up on this approach in detail yet. I am going to puchase a copy of Java Concurrency in Practice, but I wanted some help with ideas for the best approach before I get stuck into it.

BTW I'm not using a web app framework.

Thanks.

解决方案

Safest would be to create an applicationwide threadpool which is managed by the container. How to do that depends on the container used. If your container doesn't support it (e.g. Tomcat) or you want to be container-independent, then the basic approach would be to implement ServletContextListener, create the threadpool with help of Java 1.5 provided ExecutorService API on startup and kill the threadpool on shutdown. If you aren't on Java 1.5 yet or want more abstraction, then you can also use Spring's TaskExecutor

There was ever a Java EE proposal about concurrency utilities, but it has not yet made it into Java EE 6.

Related questions:

这篇关于需要帮助java web应用程序设计执行后台任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆