0

我在我的 Java Web 应用程序上使用了quartz,但我遇到了一个奇怪的问题。我认为石英正在执行,但不要“运行” Job 任务:

我的配置文件:

石英-job.xml:

`<?xml version="1.0" encoding="ISO-8859-1"?>  
<quartz xmlns="http://www.opensymphony.com/quartz/JobSchedulingData"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" overwrite-existing-jobs="true">  
     <job>  
          <job-detail>  
               <name>MyJob</name>  
               <group>DEFAULT</group>  
               <description>####</description>  
               <job-class>com.myQuartz.MyJob</job-class>  
          </job-detail>  
          <trigger>  
               <cron>  
                    <name>MyTrigger</name>  
                    <group>DEFAULT</group>  
                    <job-name>MyJob</job-name>  
                    <job-group>DEFAULT</job-group>  
                    <cron-expression>0 0/5 * * * ?</cron-expression>
               </cron>  
          </trigger>  
     </job>  
</quartz>`

石英属性:

#===============================================================  
#Configure Main Scheduler Properties  
#===============================================================  
org.quartz.scheduler.instanceName = QuartzScheduler  
org.quartz.scheduler.instanceId = AUTO  

#===============================================================  
#Configure ThreadPool  
#===============================================================  
org.quartz.threadPool.threadCount =  3  
org.quartz.threadPool.threadPriority = 5  
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool  

#===============================================================  
#Configure JobStore  
#===============================================================  
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore  

#===============================================================  
#Configure Plugins  
#===============================================================  
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin  
org.quartz.plugin.jobInitializer.overWriteExistingJobs = true  
org.quartz.plugin.jobInitializer.failOnFileNotFound = false  
org.quartz.plugin.jobInitializer.scanInterval = 10  
org.quartz.plugin.jobInitializer.validating=false  
org.quartz.plugin.jobInitializer.fileName =quartz-jobs.xml 

web.xml(只需添加这个):

`<context-param>  
   <param-name>quartz:config-file</param-name>  
 <param-value>/src/quartz.properties</param-value> 
 </context-param>
 <context-param>
<param-name>quartz:config-file</param-name>
<param-value>quartz.properties</param-value>
 </context-param>
 <context-param>
<param-name>quartz:shutdown-on-unload</param-name>
<param-value>true</param-value>
 </context-param>
 <context-param>
<param-name>quartz:wait-on-shutdown</param-name>
<param-value>false</param-value>
 </context-param>
 <context-param>
<param-name>quartz:start-scheduler-on-load</param-name>
<param-value>true</param-value>
 </context-param>
 <context-param>
   <param-name>quartz:start-on-load</param-name>
   <param-value>true</param-value>
 </context-param> 
 <listener>
    <listener-class>org.quartz.ee.servlet.QuartzInitializerListener</listener-class>
 </listener>`

MyJob.java:

package com.myJob;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;

public class MyJob implements Job  {

  @Override
  public void execute(JobExecutionContext context){

      try{
          System.out.println("Quartz working!");
      } catch (Exception ex){
      System.out.println("not working");
     }
   }
}

安慰:

Quartz Initializer Servlet 已加载,正在初始化调度程序...

调度器已经启动...

将 Quartz 调度器工厂存储在 servlet 上下文中的 key 处:org.quartz.impl.StdSchedulerFactory.KEY

Quartz Scheduler 成功关机。

谢谢

4

2 回答 2

0

一定要用石英吗?Spring有方便的@Scheduled注解:

@Service
class JobBean {

    @Scheduled(cron="0 0/5 * * * *")
    public void doSomething() {
        // ...
    }
}

或者,您可以使用基于 XML 的任务执行

<task:scheduled-tasks scheduler="myScheduler">        
    <task:scheduled ref="jobBean" method="doSomething" cron="0 0/5 * * * * *"/>
</task:scheduled-tasks>

<task:scheduler id="myScheduler" pool-size="10"/>

不要忘记添加@EnableScheduling(或使用<task>名称空间)来配置调度程序。

于 2013-05-16T21:09:08.787 回答
0

我的问题是你为什么不使用 Spring 的内置石英支持?你可以去掉quartz-job.xml和所有的web.xml配置,用几行xml替换...

<bean id="scheduler"
     class="org.springframework.scheduling.quarexecutetz.SchedulerFactoryBean"
     p:triggers-ref="triggers" p:autoStartup="true" p:trigger-ref="triggers"
     p:configLocation="classpath:/quartz.properties" />

<util:list id="triggers">
     <bean class="org.springframework.scheduling.quartz.CronTriggerFactoryBean" 
         p:jobDetail-ref="myJobDetail" p:cronExpression="0 0/5 * * * ?" />
</util:list>

<bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"
    p:targetObject-ref="myJob" p:targetMethod="execute" />

<bean id="myJob" class="com.myjob.MyJob"...

使用 Spring 的石英支持,您的作业可以是由 Spring 管理的简单 POJO,注入您需要的 bean。您不需要弄乱JobExecutionContext,实际上您的代码将与石英完全解耦。

编辑:applicationContext上面的 xml 是spring 应用程序的核心配置的 示例。在 Web 应用程序中,您context-param在 web.xml 中包含一个以加载 Spring ...

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:/applicationContext.xml</param-value>
</context-param>

我建议您多阅读一些优秀的Spring 文档,因为如果您不了解这些基础知识,那么您就没有使用 Spring。

于 2013-05-16T17:57:01.977 回答