我在Java Web应用程序上使用quartz,我遇到了一个奇怪的问题。我认为石英正在执行但不“运行”工作任务:
我的配置文件:
石英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>`
quartz.properties:
#===============================================================
#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");
}
}
}
CONSOLE:
加载Quartz Initializer Servlet,初始化Scheduler ...
计划程序已经启动......
将Quartz Scheduler Factory存储在servlet上下文中的key:org.quartz.impl.StdSchedulerFactory.KEY
Quartz Scheduler成功关闭。
THX
答案 0 :(得分: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
,事实上你的代码将与石英完全分离。
编辑:上面的xml是一个applicationContext
的示例,它是spring应用程序的核心配置。在Web应用程序中,您在web.xml中包含context-param
以加载Spring ...
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/applicationContext.xml</param-value>
</context-param>
我建议你多读一些优秀的spring documentation,因为如果你不了解这些基础知识就不会使用Spring。
答案 1 :(得分:0)
你必须使用Quartz吗? Spring有方便的@Scheduled注释:
@Service
class JobBean {
@Scheduled(cron="0 0/5 * * * *")
public void doSomething() {
// ...
}
}
或者,您可以使用基于XML的task execution:
<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>
名称空间)添加到调度程序的configure。