服务器启动时的调用方法 [英] call method on server startup

查看:133
本文介绍了服务器启动时的调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的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.

使用经过验证的框架/库,例如 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屋!

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