问题我已经解决了,和大家分享一下,我使用了Quartz,先介绍一下:
Quartz is distributed as a small java library (.jar file) that contains all of the core Quartz functionality. The main interface (API) to this functionality is the Scheduler interface. It provides simple operations such as scheduling/unscheduling jobs, starting/stopping/pausing the scheduler.If you wish to schedule your own software components for execution they must implement the simple Job interface, which contains the method execute(). If you wish to have components notified when a scheduled fire-time arrives, then the components should implement either the TriggerListener or JobListener interface.The main Quartz 'process' can be started and ran within your own application, as a stand-alone application (with an RMI interface), or within a J2EE app. server to be used as a resource by your J2EE components.具体实现方方法:
1 配置自己的web.xml
<servlet>
<servlet-name>QuartzInitializer</servlet-name>
<display-name>Quartz Initializer Servlet</display-name>
<servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>YourServlet</servlet-name>
<display-name>YourServlet</display-name>
<servlet-class>your.servlet.YourServlet</servlet-class>
<init-param><param-name>cronExpr</param-name> <param-value>0 0/30 * * * ?</param-value></init-param>
<load-on-startup>2</load-on-startup>
</servlet>2 你的servlet:
public class YourServlet extends GenericServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
System.out.println("Scheduling Job ..");
JobDetail jd = new JobDetail("Test Quartz","My Test Job",YoursInvokerJob.class);
Object[] jdArgs = new Object[0];
jd.getJobDataMap().put("args", jdArgs);
CronTrigger cronTrigger = new CronTrigger("Test Quartz", "Test Quartz");try {
String cronExpr = null;
// Get the cron Expression as an Init parameter
cronExpr = getInitParameter("cronExpr");
System.out.println(cronExpr);
cronTrigger.setCronExpression(cronExpr);
Scheduler sched = StdSchedulerFactory.getDefaultScheduler();
sched.scheduleJob(jd, cronTrigger);
System.out.println("Job scheduled now ..");
} catch (Exception e) {
e.printStackTrace();
}
}
public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException {
}
public String getServletInfo() {
return null;
}
}