use*_*082 9 java jboss ejb-3.0
我正在尝试在我的webapplication启动时调用方法.目的是启动定时器,该定时器以定义的间隔执行某些操作.当我的jboss 7.1 Web应用程序启动时,如何调用函数helloworld?
小智 6
如果要在Web应用程序为任何客户端提供服务之前运行某些代码,则需要ServletContextListener.
创建你的监听器类
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) {
}
}
Run Code Online (Sandbox Code Playgroud)
将该类放在WEB-INF/classes中
将< listener >元素放在web.xml文件中.
<listener>
<listener-class>
com.test.MyServletContextListener
</listener-class>
</listener>
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
除了ContextListeners之外,您还可以在启动时加载web.xml中的servlet:
<servlet>
<servlet-name>mytask</servlet-name>
<servlet-class>servlets.MyTaskServlet</servlet-class>
...
<load-on-startup>1</load-on-startup>
</servlet>
Run Code Online (Sandbox Code Playgroud)
这个servlet可以使用你想要的任何方式启动你的任务,例如参见这个链接.
但你不应该使用这种方法,imho.
使用经过验证的框架/库,如石英或类似工具.在Web服务器中运行和同步任务存在很多问题/问题,最好使用一些经过验证的工具,而不是重复这些工具已经遇到并解决的错误.可能需要一段时间才能掌握,但会避免许多令人头疼的问题.
Jboss本身有一些工具用于此目的:安排和管理任务.从未使用过所以不能推荐.
| 归档时间: |
|
| 查看次数: |
14253 次 |
| 最近记录: |