我正在尝试在一个以预设时间间隔向用户发送电子邮件的 Web 项目中使用 Quartz Scheduler。我很高兴此时能够打印出简单的“hello world”语句。
这是我要运行的应用程序的代码。
package testing;
import static org.quartz.JobBuilder.*;
import static org.quartz.SimpleScheduleBuilder.*;
import static org.quartz.CronScheduleBuilder.*;
import static org.quartz.CalendarIntervalScheduleBuilder.*;
import static org.quartz.TriggerBuilder.*;
import static org.quartz.DateBuilder.*;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
public class QuartzTest {
public static void main(String[] args) throws InterruptedException {
System.out.println("MAIN!!!!");
try {
// Grab the Scheduler instance from the Factory
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
// define the job and tie it to our HelloJob class
JobDetail job = newJob(HelloJob.class)
.withIdentity("job1", "group1")
.build();
// Trigger the job to run now, and then repeat every 40 seconds
Trigger trigger = newTrigger()
.withIdentity("trigger1", "group1")
.startNow()
.withSchedule(simpleSchedule()
.withIntervalInSeconds(40)
.repeatForever())
.build();
scheduler.scheduleJob(job, trigger);
// and start it off
scheduler.start();
Thread.sleep(60000);
//scheduler.shutdown();
} catch (SchedulerException se) {
se.printStackTrace();
}
}
}
这就是要调用的工作:
package testing;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class HelloJob implements Job {
public HelloJob() {
}
public void execute(JobExecutionContext context)
throws JobExecutionException
{
System.err.println("Hello! HelloJob is executing.");
System.out.println("HellowJob is executing");
}
}
这是我的quartz.properties 文件:
org.quartz.scheduler.instanceName = QuartzTest
org.quartz.threadPool.threadCount = 3
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
最后但并非最不重要的是,这是我添加到 web.xml 文件中的内容
<servlet>
<display-name>Quartz Initializer Servlet</display-name>
<servlet-name>QuartzInitializer</servlet-name>
<servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
<init-param>
<param-name>shutdown-on-unload</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>start-scheduler-on-load</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>config-file</param-name>
<param-value>quartz.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
现在,我很确定该项目能够检测和读取我的quartz.properties 文件,但我没有理由相信该项目(即服务器)知道有关QuartzTest.java 的任何信息。我没有在任何地方注册该课程,同样它也从未被调用过。有谁知道我该怎么做?