如何在服务器端实现Java计时器?
您好,我目前正在使用JSF和PrimeFaces处理中间件应用程序
,并且部署在Tomcat服务器中, 我能够使用java.mail开发一个通知系统,并且我正在使用Java计时器来安排每24小时的通知,
因为它运行主班时它可以正常工作,但是当我运行整个项目时,Javatimer的过程没有工作,我不知道如何在不启动类Main的情况下在服务器端进行运行,
因此我问是否应该添加某些内容以使其在我的代码启动我的项目时进行运行
:
public class Job1 {
public static void main(String[] args) throws Exception {
// execute method review_date() every 1 minute
risk r = new risk();
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
r.review_date2();
} catch (Exception e) {
e.printStackTrace();
}
}
}, 0, 1000 * 60);
}
}
Hello I am currently working on a middleware application
Im using JSF and primefaces and the deployment is in tomcat server ,
I was able to develop a notification system using JAVA.mail and i am using java timer to schedule the notification every 24H
For the moment it works fine when I run my main class but when i run the whole project the process of javaTimer doesn't work and i have no idea how to make run in server side without launching the class Main
So i ask if should add something to make it run when i launch my project
Here my code :
public class Job1 {
public static void main(String[] args) throws Exception {
// execute method review_date() every 1 minute
risk r = new risk();
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
r.review_date2();
} catch (Exception e) {
e.printStackTrace();
}
}
}, 0, 1000 * 60);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我需要全应用资源,我通常会实现
servletContextListener
,然后指定它以在web.xml
中运行。但是,摆动计时器可能不起作用,因此您需要一个scheduledexecutorservice
。If I need app-wide resources, I usually implement the
ServletContextListener
, then specify it to be run inweb.xml
. However, swing timers likely don't work so you'll need aScheduledExecutorService
instead.