java 服务器启动时调用方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15593935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
call method on server startup
提问by user1438082
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?
我试图在我的 web 应用程序启动时调用一个方法。目的是启动一个以定义的时间间隔做一些工作的计时器。当我的 jboss 7.1 Web 应用程序启动时,我如何调用函数 helloworld?
采纳答案by jagra
Other then ContextListeners, you can also have a servlet in web.xml loading on startup:
除了 ContextListeners,您还可以在启动时加载 web.xml 中的 servlet:
<servlet>
<servlet-name>mytask</servlet-name>
<servlet-class>servlets.MyTaskServlet</servlet-class>
...
<load-on-startup>1</load-on-startup>
</servlet>
This servlet can start your task using whatever means you want, see for example this link.
此 servlet 可以使用您想要的任何方式启动您的任务,例如参见此链接。
But you shouldn't use that approach, imho.
但是你不应该使用这种方法,恕我直言。
Use a proven framework/lib like quartzor 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.
使用经过验证的框架/lib,如quartz或类似工具。在 Web 服务器中运行和同步任务有很多问题/问题,最好使用一些经过验证的工具,而不是重复这些工具已经遇到和解决的错误。掌握它可能需要一点时间,但会避免许多头痛。
Jboss itself has some tooling for that purpose: scheduling and managing tasks. Never used so can't recommend.
Jboss 本身有一些用于此目的的工具:调度和管理任务。没用过所以不推荐。
回答by Karthik Rajah
If you want to run some code before your web app serves any of your clients you need a ServletContextListener.
如果您想在您的 Web 应用程序为您的任何客户端提供服务之前运行一些代码,您需要一个 ServletContextListener。
Create your listener class
创建你的监听器类
import javax.servlet.*;
public class MyServletContextListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent e) {
//Call your function from the event object here
}
public void contextDestroyed(ServletContextEvent e) {
}
}
Put the class in WEB-INF/classes
将类放在 WEB-INF/classes 中
Put a <listener> element in the web.xml file.
在 web.xml 文件中放置一个 < listener> 元素。
<listener>
<listener-class>
com.test.MyServletContextListener
</listener-class>
</listener>
Hope this helps.
希望这可以帮助。
回答by Bizmarck
Check out Quartz Scheduler. You can use a CronTriggerto fire at defined intervals. For example, every 5 minutes would look like this:
查看Quartz 调度程序。您可以使用CronTrigger以定义的时间间隔触发。例如,每 5 分钟将如下所示:
"0 0/5 * * * ?"
"0 0/5 * * * ?"
The idea is to implement the Job
interface which is the task to run, schedule it using the SchedulerFactory
/Scheduler
, build the Job
and CronTrigger
and start it.
这样做是为了贯彻Job
这是任务运行界面,使用调度它SchedulerFactory
/ Scheduler
,建立Job
并CronTrigger
启动它。
There is a very clear example here.
有一个很明显的例子在这里。
回答by adarshr
Use a ServletContextListener
configured in your web.xml
. Write the code that kicks off the timer in the contextInitialized
method.
使用ServletContextListener
您的配置web.xml
。在方法中编写启动计时器的代码contextInitialized
。