如何在tomcat启动的时候,执行一个方法
展开全部
java有个Listener! 随tomcat启动而调用自己写的方法!也可以循环调用!就是每过几小时掉用自己的方法web.xml<listener>
<listener-class>com.function.SysContextListener </listener-class>
</listener> SysContextListener.javapackage com.function;import java.util.Timer;//定时器类
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class SysContextListener implements ServletContextListener
{
private Timer timer = null;
public void contextInitialized(ServletContextEvent event)
{//在这里初始化监听器,在tomcat启动的时候监听器启动,可以在这里实现定时器功能
timer = new Timer(true);
System.out.println("启动成功");
event.getServletContext().log("定时器已启动--------------------------------------------------");//添加日志,可在tomcat日志中查看到
// timer.schedule(new exportHistoryBean(event.getServletContext(),DealDate.getTime()),0,5*1000);//调用exportHistoryBean,0表示任务无延迟,5*1000表示每隔5秒执行任务,60*60*1000表示一个小时。
event.getServletContext().log("已经添加任务--------------------------------------------------");
}
public void contextDestroyed(ServletContextEvent event)
{//在这里关闭监听器,所以在这里销毁定时器。
timer.cancel();
event.getServletContext().log("定时器销毁--------------------------------------------------");
} } exportHistoryBean.javapackage com.function; import java.util.Calendar;
import java.util.TimerTask;
import javax.servlet.ServletContext;
public class exportHistoryBean extends TimerTask
{
private static final int C_SCHEDULE_HOUR = 0;
private static boolean isRunning = false;
private ServletContext context = null;
public exportHistoryBean(ServletContext context)
{
this.context = context;
}
public void run()
{
Calendar c = Calendar.getInstance();
//if(!isRunning)
// {
// if(C_SCHEDULE_HOUR == c.get(Calendar.HOUR_OF_DAY))
// {
isRunning = true;
context.log("开始执行指定任务--------------------------------------------------");
int day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
new WebServiceBean().doWork();//这里就是调用自己的方法了
isRunning = false;
context.log("指定任务执行结束--------------------------------------------------");
// }
// else
// {
// context.log("上一次任务执行还未结束");
// }
// }
}
}
<listener-class>com.function.SysContextListener </listener-class>
</listener> SysContextListener.javapackage com.function;import java.util.Timer;//定时器类
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class SysContextListener implements ServletContextListener
{
private Timer timer = null;
public void contextInitialized(ServletContextEvent event)
{//在这里初始化监听器,在tomcat启动的时候监听器启动,可以在这里实现定时器功能
timer = new Timer(true);
System.out.println("启动成功");
event.getServletContext().log("定时器已启动--------------------------------------------------");//添加日志,可在tomcat日志中查看到
// timer.schedule(new exportHistoryBean(event.getServletContext(),DealDate.getTime()),0,5*1000);//调用exportHistoryBean,0表示任务无延迟,5*1000表示每隔5秒执行任务,60*60*1000表示一个小时。
event.getServletContext().log("已经添加任务--------------------------------------------------");
}
public void contextDestroyed(ServletContextEvent event)
{//在这里关闭监听器,所以在这里销毁定时器。
timer.cancel();
event.getServletContext().log("定时器销毁--------------------------------------------------");
} } exportHistoryBean.javapackage com.function; import java.util.Calendar;
import java.util.TimerTask;
import javax.servlet.ServletContext;
public class exportHistoryBean extends TimerTask
{
private static final int C_SCHEDULE_HOUR = 0;
private static boolean isRunning = false;
private ServletContext context = null;
public exportHistoryBean(ServletContext context)
{
this.context = context;
}
public void run()
{
Calendar c = Calendar.getInstance();
//if(!isRunning)
// {
// if(C_SCHEDULE_HOUR == c.get(Calendar.HOUR_OF_DAY))
// {
isRunning = true;
context.log("开始执行指定任务--------------------------------------------------");
int day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
new WebServiceBean().doWork();//这里就是调用自己的方法了
isRunning = false;
context.log("指定任务执行结束--------------------------------------------------");
// }
// else
// {
// context.log("上一次任务执行还未结束");
// }
// }
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询