简体   繁体   English

在Java Web应用程序中定期更新数据库的最佳方法

[英]Best Way to Update Database Periodically in Java Web Application

I have a java web application which is running on Glassfish server. 我有一个在Glassfish服务器上运行的java Web应用程序。 Using war file i use to deploy the application in various servers. 使用war文件我用于在各种服务器中部署应用程序。 Now to keep my application's database updated, i want to run some class (inside from application)periodically without any user interaction (should not depends on application is running or not/current users/session). 现在为了保持我的应用程序的数据库更新,我想定期运行一些类(在应用程序内部)而不进行任何用户交互(不应该依赖于应用程序正在运行或不运行/当前用户/会话)。 i have seen that using some Timer and TimerTask class i can run any job periodically. 我已经看到使用一些TimerTimerTask类我可以定期运行任何工作。 But how to initialize it for the first time? 但是如何首次初始化呢?

Please put your thoughts on how to complete this process. 请考虑如何完成此过程。

Use a Job scheduler. 使用作业计划程序。 Consider Quartz http://quartz-scheduler.org/ and start it when the program starts. 考虑Quartz http://quartz-scheduler.org/并在程序启动时启动它。 The good part about using a scheduler is your program is more maintainable and you can easily create other new jobs 使用调度程序的好处是程序更易于维护,您可以轻松创建其他新作业

Create a servlet and make it load on startup. 创建一个servlet并在启动时加载它。 There you can initialize your task, I think. 我认为你可以在那里初始化你的任务。

Quartz is a good solution like already suggested. Quartz是一个很好的解决方案。 But if you need something more lite weight, I would have a look at the scheduled executor: 但是如果你需要更轻量级的东西,我会看看预定的执行者:

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ScheduledExecutorService.html

It is less flexible than Quartz, but you don't need to add any dependency and it might be that it is good enough for your needs. 它不如Quartz灵活,但您不需要添加任何依赖项,它可能足以满足您的需求。

About starting up; 关于启动; I normally use Spring to wire up my application and its dependencies. 我通常使用Spring来连接我的应用程序及其依赖项。 So starting schedulers and running scheduled tasks is then a no brainer. 因此,启动调度程序和运行计划任务是没有道理的。

The answer changes depending on the version of Java EE you are using. 答案会根据您使用的Java EE版本而变化。 In Java EE 5 and previous versions you would use a ServletContextListener to run code (call an EJB) at deployment time that used the Timer API . 在Java EE 5和以前的版本中,您将使用ServletContextListener在使用Timer API的部署时运行代码(调用EJB)。 In Java EE 6+ you can use the @Schedule annotation which uses annotations and a cron-type syntax to schedule your task at deployment time. 在Java EE 6+中,您可以使用@Schedule批注,该批注使用注释和cron类型语法在部署时安排任务。

Of course if you don't need automatic deployment time scheduling then you'd just create some web form that calls a EJB when submitted which in turn calls the Timer API programmatically. 当然,如果您不需要自动部署时间安排,那么您只需创建一些在提交时调用EJB的Web表单,然后以编程方式调用Timer API。

For more see the Java EE tutorial 有关更多信息,请参阅Java EE教程

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

相关问题 轮询数据库表以获取Java Web应用程序更新的最佳方法 - Best way to poll a database table for updates from a java web application 与Java桌面应用程序中的数据库交互的最佳方法 - Best way to interact with database in java desktop application 处理Java Web应用程序版本控制的最佳方法是什么? - What is the best way to handle Java web application versioning? 在Java桌面应用程序中进行数据库访问的最佳方法 - Best way to do database access in a Java desktop application 在Java应用程序中打开和返回数据库连接的最佳方法? - Best way to open and return a database connection in a Java application? 使用Java定期更新数据库 - Updating a database periodically with Java 如何使用离线和在线数据库创建Java桌面应用程序,并定期进行同步? - How to create java desktop application with offline and online database, syncing periodically? java web应用程序的最佳实践 - java web application best practices 是否有更好的方法定期更新UI - Is there a better way to update UI periodically Web应用程序根据数据库数据返回XML文档的最佳方法(使用VXML)? - Best way to web application return XML document basis on database data (USED BY VXML)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM