服务器启动时调用方法 [英] call method on server startup
问题描述
我试图在我的 web 应用程序启动时调用一个方法.目的是启动一个以定义的时间间隔做一些工作的计时器.当我的 jboss 7.1 Web 应用程序启动时,我如何调用函数 helloworld?
I am trying to call a method when my webapplication starts. The purpose is to kick-off a timer that does some work at defined intervals. how do i call a function helloworld when my jboss 7.1 web application starts up?
推荐答案
除了 ContextListeners,你也可以在 web.xml 中加载一个 servlet 在启动时加载:
Other then ContextListeners, you can also have a servlet in web.xml loading on startup:
<servlet>
<servlet-name>mytask</servlet-name>
<servlet-class>servlets.MyTaskServlet</servlet-class>
...
<load-on-startup>1</load-on-startup>
</servlet>
这个 servlet 可以使用任何你想要的方式开始你的任务,例如这个 链接.
This servlet can start your task using whatever means you want, see for example this link.
但是你不应该使用那种方法,恕我直言.
But you shouldn't use that approach, imho.
使用经过验证的框架/lib,例如 quartz 或类似工具.在 Web 服务器中运行和同步任务有很多问题/问题,最好使用一些经过验证的工具,而不是重复这些工具已经遇到和解决的错误.可能需要一点时间来掌握,但会避免很多麻烦.
Use a proven framework/lib like quartz or a similar tool. There are a lot of problems/issues in running and syncing tasks in web servers and it's better to use some proven tool than to repeat mistakes these tools already met and solved. It might take a little while to grasp but will avoid many headaches.
Jboss 本身有一些用于此目的的工具:调度和管理任务.没用过所以不推荐.
Jboss itself has some tooling for that purpose: scheduling and managing tasks. Never used so can't recommend.
这篇关于服务器启动时调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!