如何在服务器端实现Java计时器?

发布于 2025-02-09 22:53:11 字数 826 浏览 1 评论 0原文

您好,我目前正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

几度春秋 2025-02-16 22:53:11

如果我需要全应用资源,我通常会实现servletContextListener,然后指定它以在web.xml中运行。但是,摆动计时器可能不起作用,因此您需要一个scheduledexecutorservice

package mypackage;

import javax.servlet.*;

public class MainContextListener implements ServletContextListener {

    ScheduledExecutorService service;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        service = newSingleThreadScheduledExecutor();
        risk r = new risk();
        service.scheduleAtFixedRate(() -> {
            try {
                r.review_date2();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, 0, 1, TimeUnit.DAYS);
    }
    
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        service.shutdown();
    }
}
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" 
 id="WebApp_ID" 
version="4.0">
  <!-- etc. -->
  <listener>
    <listener-class>mypackage.MainContextListener</listener-class>
  </listener>
</web-app>

If I need app-wide resources, I usually implement the ServletContextListener, then specify it to be run in web.xml. However, swing timers likely don't work so you'll need a ScheduledExecutorService instead.

package mypackage;

import javax.servlet.*;

public class MainContextListener implements ServletContextListener {

    ScheduledExecutorService service;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        service = newSingleThreadScheduledExecutor();
        risk r = new risk();
        service.scheduleAtFixedRate(() -> {
            try {
                r.review_date2();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, 0, 1, TimeUnit.DAYS);
    }
    
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        service.shutdown();
    }
}
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" 
 id="WebApp_ID" 
version="4.0">
  <!-- etc. -->
  <listener>
    <listener-class>mypackage.MainContextListener</listener-class>
  </listener>
</web-app>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文