Quartz 定时任务spring 的应用
方法一:
1.quartz_job.xml文件配置:
Java代码
<?xml version="1.0" encoding="UTF-8"?>
<quartz version="1.5">
<job>
<job-detail>
<name>assess.HastenJob</name>
<group>taskGroup</group>
<job-class>com.aspire.simp.app.assess.common.job.AssessHastenJob</job-class>
<volatility>false</volatility>
<durability>false</durability>
<recover>false</recover>
<job-data-map allows-transient-data="true">
<entry>
<key>serviceName</key>
<value>assess.HastenJob</value>
</entry>
</job-data-map>
</job-detail>
<trigger>
<cron>
<name>AssessHastenJobTrigger</name>
<group>triggerGroup</group>
<job-name>assess.HastenJob</job-name>
<job-group>taskGroup</job-group>
<cron-expression>1/20 * * * * ?</cron-expression>
</cron>
</trigger>
</job>
</quartz>
<?xml version="1.0" encoding="UTF-8"?>
<quartz version="1.5">
<job>
<job-detail>
<name>assess.HastenJob</name>
<group>taskGroup</group>
<job-class>com.aspire.simp.app.assess.common.job.AssessHastenJob</job-class>
<volatility>false</volatility>
<durability>false</durability>
<recover>false</recover>
<job-data-map allows-transient-data="true">
<entry>
<key>serviceName</key>
<value>assess.HastenJob</value>
</entry>
</job-data-map>
</job-detail>
<trigger>
<cron>
<name>AssessHastenJobTrigger</name>
<group>triggerGroup</group>
<job-name>assess.HastenJob</job-name>
<job-group>taskGroup</job-group>
<cron-expression>1/20 * * * * ?</cron-expression>
</cron>
</trigger>
</job>
</quartz>
2.quartz.properties配置
Java代码
#
# Configure Main Scheduler Properties
#
org.quartz.scheduler.instanceName = AssessScheduler
org.quartz.scheduler.instanceId = one
#
# Configure ThreadPool
#
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 120
org.quartz.threadPool.threadPriority = 8
#
# Configure JobStore
#
org.quartz.jobStore.misfireThreshold = 5000
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
# ===========================================================================
# Configure SchedulerPlugins ===============================================
# ===========================================================================
org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingTriggerHistoryPlugin
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
org.quartz.plugin.jobInitializer.fileName = quartz_jobs.xml
org.quartz.plugin.jobInitializer.overWriteExistingJobs = false
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.jobInitializer.scanInterval = 50
org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin
org.quartz.plugin.shutdownhook.cleanShutdown = true
#
# Configure Main Scheduler Properties
#
org.quartz.scheduler.instanceName = AssessScheduler
org.quartz.scheduler.instanceId = one
#
# Configure ThreadPool
#
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 120
org.quartz.threadPool.threadPriority = 8
#
# Configure JobStore
#
org.quartz.jobStore.misfireThreshold = 5000
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
# ===========================================================================
# Configure SchedulerPlugins ===============================================
# ===========================================================================
org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingTriggerHistoryPlugin
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
org.quartz.plugin.jobInitializer.fileName = quartz_jobs.xml
org.quartz.plugin.jobInitializer.overWriteExistingJobs = false
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.jobInitializer.scanInterval = 50
org.quartz.plugin.shutdownhook.class = org.quartz.plugins.management.ShutdownHookPlugin
org.quartz.plugin.shutdownhook.cleanShutdown = true
3.JAVA类
Java代码
public class AssessHastenJob implements Job
{
private static Log logger = LogFactory.getLog(AssessHastenJob.class);
private AssessConfigManager assessConfigManager = null;
private SimsConfigBiz simsConfigBiz = null;
public void execute(JobExecutionContext arg0) throws JobExecutionException
{
SimsConfigBiz simsConfigBiz = (SimsConfigBiz)BeanLocator.getInstance().getBean("simsConfigBiz");
AssessConfigManager assessConfigManager =(AssessConfigManager) BeanLocator.getInstance().getBean("assessConfigManager");
}
}
public class AssessHastenJob implements Job
{
private static Log logger = LogFactory.getLog(AssessHastenJob.class);
private AssessConfigManager assessConfigManager = null;
private SimsConfigBiz simsConfigBiz = null;
public void execute(JobExecutionContext arg0) throws JobExecutionException
{
SimsConfigBiz simsConfigBiz = (SimsConfigBiz)BeanLocator.getInstance().getBean("simsConfigBiz");
AssessConfigManager assessConfigManager =(AssessConfigManager) BeanLocator.getInstance().getBean("assessConfigManager");
}
}
4.系统初始化类
Java代码
/**
* 初始化quartz配置(quartz配置文件目录等).
*/
public void initQuartz() {
// 初始化quartz配置(quartz配置文件目录等).
// 从init-param中得到quartz.properties配置文件相对路径.
String quartzConfig = this.getInitParameter("quartzConfig");
if (quartzConfig == null || quartzConfig.length() == 0) {
logger.info("没有指定quartz配置文件相对路径!");
return;
}
// 得到quartz配置文件全路径名
String quartzConfigFileName = ConfigurationHelper
.getFullFileName(quartzConfig);
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(quartzConfigFileName);
} catch (FileNotFoundException e) {
logger.error("找不到quartz配置文件,文件名:" + quartzConfigFileName);
return;
}
// 得到quartz配置文件的目录
String quartzConfigPath = StringUtils.substringBeforeLast(
quartzConfigFileName, "/");
// 加载配置文件
Properties properties = new Properties();
try {
properties.load(fileInputStream);
// 设置quartz_jobs.xml路径
properties.setProperty("org.quartz.plugin.jobInitializer.fileName",
quartzConfigPath + File.separator + "quartz_jobs.xml");
} catch (IOException e) {
logger.error("加载quartz配置文件失败,文件名:" + quartzConfigFileName);
return;
}
// 启动quartz
StdSchedulerFactory factory = null;
try {
factory = new StdSchedulerFactory(properties);
} catch (SchedulerException e) {
logger.error("初始化quartz失败,失败原因:" + e.getMessage());
return;
}
// 启动quartz任务调度器
try {
scheduler = factory.getScheduler();
scheduler.start();
} catch (SchedulerException e) {
logger.error("启动quartz任务调度失败,失败原因:" + e.getMessage());
return;
}
logger.info("启动quartz任务调度成功!");
}
/**
* 初始化quartz配置(quartz配置文件目录等).
*/
public void initQuartz() {
// 初始化quartz配置(quartz配置文件目录等).
// 从init-param中得到quartz.properties配置文件相对路径.
String quartzConfig = this.getInitParameter("quartzConfig");
if (quartzConfig == null || quartzConfig.length() == 0) {
logger.info("没有指定quartz配置文件相对路径!");
return;
}
// 得到quartz配置文件全路径名
String quartzConfigFileName = ConfigurationHelper
.getFullFileName(quartzConfig);
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(quartzConfigFileName);
} catch (FileNotFoundException e) {
logger.error("找不到quartz配置文件,文件名:" + quartzConfigFileName);
return;
}
// 得到quartz配置文件的目录
String quartzConfigPath = StringUtils.substringBeforeLast(
quartzConfigFileName, "/");
// 加载配置文件
Properties properties = new Properties();
try {
properties.load(fileInputStream);
// 设置quartz_jobs.xml路径
properties.setProperty("org.quartz.plugin.jobInitializer.fileName",
quartzConfigPath + File.separator + "quartz_jobs.xml");
} catch (IOException e) {
logger.error("加载quartz配置文件失败,文件名:" + quartzConfigFileName);
return;
}
// 启动quartz
StdSchedulerFactory factory = null;
try {
factory = new StdSchedulerFactory(properties);
} catch (SchedulerException e) {
logger.error("初始化quartz失败,失败原因:" + e.getMessage());
return;
}
// 启动quartz任务调度器
try {
scheduler = factory.getScheduler();
scheduler.start();
} catch (SchedulerException e) {
logger.error("启动quartz任务调度失败,失败原因:" + e.getMessage());
return;
}
logger.info("启动quartz任务调度成功!");
}
5.web.xml
Java代码
<servlet>
<servlet-name>defaultSystemInit</servlet-name>
<servlet-class>com.aspire.simp.app.common.servlet.DefaultSystemInitServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<!--log4j配置文件相对路径(子系统必须要配置log4j)-->
<param-name>log4jConfig</param-name>
<param-value>log4j/log4j.xml</param-value>
</init-param>
<init-param>
<!--attachment配置文件相对路径(如果子系统需要附件上载才需要配置)-->
<param-name>quartzConfig</param-name>
<param-value>quartz/quartz.properties</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>defaultSystemInit</servlet-name>
<servlet-class>com.aspire.simp.app.common.servlet.DefaultSystemInitServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<!--log4j配置文件相对路径(子系统必须要配置log4j)-->
<param-name>log4jConfig</param-name>
<param-value>log4j/log4j.xml</param-value>
</init-param>
<init-param>
<!--attachment配置文件相对路径(如果子系统需要附件上载才需要配置)-->
<param-name>quartzConfig</param-name>
<param-value>quartz/quartz.properties</param-value>
</init-param>
</servlet>
方法二:
1.spring 配置文件加下面这段就行了
Java代码
<!-- 定时服务定义 -->
<bean value="false"/>
</bean>
<!-- Job定义 -->
<bean id="assessHastenJob" ref="assessConfigManager"/>
<property name="simsConfigBiz" ref="simsConfigBiz"/>
</bean>
<!-- 定时服务定义 -->
<bean value="false"/>
</bean>
<!-- Job定义 -->
<bean id="assessHastenJob" ref="assessConfigManager"/>
<property name="simsConfigBiz" ref="simsConfigBiz"/>
</bean>
2.JAVA类
Java代码
public class AssessHastenJob
{
private static Log logger = LogFactory.getLog(AssessHastenJob.class);
private AssessConfigManager assessConfigManager = null;
private SimsConfigBiz simsConfigBiz = null;
public void execute()
{
}
public void setAssessConfigManager(AssessConfigManager assessConfigManager)
{
this.assessConfigManager = assessConfigManager;
}
public void setSimsConfigBiz(SimsConfigBiz simsConfigBiz)
{
this.simsConfigBiz = simsConfigBiz;
}
}