gpt4 book ai didi

java - java中的调度程序任务

转载 作者:行者123 更新时间:2023-12-01 13:36:03 26 4
gpt4 key购买 nike

这是我预定的定时器程序。它部署在tomcat中。我希望它每天上午 11:46 运行。

那么,如果将其部署到 tomcat,它会自动在上午 11:46 启动吗?

请指教。

public class TimeScheduler {
Timer timer;
Date time = new Date();


public TimeScheduler() {
timer = new Timer();
taskExecutionTime();
timer.schedule(new RemindTask(), time);
}

public Date taskExecutionTime()
{
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 9);
cal.set(Calendar.MINUTE, 46);
cal.set(Calendar.SECOND, 0);

time = cal.getTime();
return time;
}


class RemindTask extends TimerTask {
public void run() {
System.out.format("Task is Running now!");
try {
CSVReader.parseCSV();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
timer.cancel(); //Terminate the timer thread
System.out.format("Task is Complete!");
}
}

public static void main(String args[]) {
new TimeScheduler();
}
}

最佳答案

我不确定它是否能在 tomcat 中运行。你会如何调用静态方法来调度?没有任何东西可以调用它。

这是一个示例 ( http://www.roseindia.net/servlets/ServletContextListenerTimer.shtml )

您需要在 web.xml 中声明监听器

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<listener>
<listener-class>
example.ServletContextExample
</listener-class>
</listener>
</web-app>

带有定时器的ServletContextListener

import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
* Application Lifecycle Listener implementation class MyServletContextListener
*
*/
public class MyServletContextListener implements ServletContextListener {

/**
* @see ServletContextListener#contextInitialized(ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent arg0) {
ServletContext servletContext = arg0.getServletContext();
System.out.println("*********ServletContextListener started*********");

int delay = 1000;
Timer timer = new Timer();
//final Calendar calendar = Calendar.getInstance();
//System.out.println("Tweet at Time = " + calendar.getTime());
//calendar.add(Calendar.SECOND, -60);
timer.scheduleAtFixedRate(new TimerTask(){
public void run(){
System.out.println("Running this code every 1 minute....");
}//End of Run
},delay, 60000);
servletContext.setAttribute ("timer", timer);
}

/**
* @see ServletContextListener#contextDestroyed(ServletContextEvent)
*/
public void contextDestroyed(ServletContextEvent arg0) {
ServletContext servletContext = arg0.getServletContext();
// get our timer from the Context
Timer timer = (Timer)servletContext.getAttribute ("timer");

// cancel all pending tasks in the timers queue
if (timer != null)
timer.cancel();

// remove the timer from the servlet context
servletContext.removeAttribute ("timer");
System.out.println("ServletContextListener destroyed");

}
}

关于java - java中的调度程序任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21265077/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com