0

我正在尝试在一个以预设时间间隔向用户发送电子邮件的 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 的任何信息。我没有在任何地方注册该课程,同样它也从未被调用过。有谁知道我该怎么做?

4

1 回答 1

0

太好了 !您已经成功地引导了 Quartz。

缺少的是您必须定义在什么时间运行哪些作业。您可以通过在 .xml 中配置XMLSchedulingDataProcessorPlugin来实现quartz.properties。此插件将加载您定义的 paticaulr XML 文件,以添加作业并在石英调度程序初始化时使用触发器调度它们。(有关其架构定义,请参阅此处)

quartz.properties中,添加以下属性:

org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin org.quartz.plugin.jobInitializer.fileNames = quartz-config.xml org.quartz.plugin.jobInitializer.failOnFileNotFound = true

然后在quartz-config.xml中:

 <?xml version="1.0" encoding="UTF-8"?>

        <job-scheduling-data
            xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"
            version="1.8">

            <schedule>
                <job>
                    <name>MyScheduledJob</name>
                    <group>MyScheduledGroup</group>
                    <description>Testing</description>
                    <job-class>testing.HelloJob</job-class>
                </job>

                <trigger>
                        <cron>
                                <name>HelloJobTrigger</name>
                                <job-name>MyScheduledJob</job-name>
                                <job-group>MyScheduledGroup</job-group>
                                <!-- It will run every 5 minutes -->
                                <cron-expression>0 0/5 * * * ?</cron-expression>  
                        </cron>
                  </trigger>
            </schedule>
    </job-scheduling-data>
于 2012-04-16T07:11:25.677 回答