JAVA使用quartz添加定时任务,并依赖注入对象操作

网友投稿 750 2023-04-01

JAVA使用quartz添加定时任务,并依赖注入对象操作

JAVA使用quartz添加定时任务,并依赖注入对象操作

最近在写定时任务,以前没接触过。查了些相关资料说使用quartz定时框架。

需要配置文件:config-quartz.xml

相关配置如下(红色部分是之后添加的,在后面步骤会说明):

xsi:schemaLocation="

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.2.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.2.xsd

http://springframework.org/schema/task

http://springframework.org/schema/task/spring-task-3.0.xsd">

xsi:schemaLocation="

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.2.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.2.xsd

http://springframework.org/schema/task

http://springframework.org/schema/task/spring-task-3.0.xsd">

quartz.properties

#============================================================================

# Configure Main Scheduler Properties

#============================================================================

org.quartz.scheduler.instanceName = WrhFrameScheduler

org.quartz.scheduler.instanceId = AUTO

org.quartz.scheduler.skipUpdateCheck = true

#============================================================================

# Configure ThreadPool

#============http://================================================================

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool

org.quartz.threadPool.threadCount = 12

org.quartz.threadPool.threadPriority = 5

#============================================================================

# Configure JobStore

#============================================================================

org.quartz.jobStore.misfireThreshold = 60000

org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

#org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX

#org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.PostgreSQLDelegate

#org.quartz.jobStore.useProperties = false

#org.quartz.jobStore.dataSource = myDS

#org.quartz.jobStore.tablePrefix = QRTZ_

#org.quartz.jobStore.isClustered = false

#============================================================================

# Configure Datasources

#============================================================================

#org.quartz.dataSource.myDS.driver = org.postgresql.Driver

#org.quartz.dataSource.myDS.URL = jdbc:postgresql://localhost/dev

#org.quartz.dataSource.myDS.user = jhouse

#org.quartz.dataSource.myDS.password =

#org.quartz.dataSource.myDS.maxConnections = 5

最后spring-mvc.xml配置文件中奖quartz.xml文件引入即可:

然后写测试类开始测试定时任务:

package com.wy.care60.job;

import com.wy.care60.dao.MElementMapper;

import com.wy.care60.dao.MInterEnumMapper;

import com.wy.care60.dao.MProjectMapper;

import com.wy.care60.model.MInterEnum;

import com.wy.care60.model.MProject;

import org.apache.tools.ant.Project;

import org.quartz.JobExecutionContext;

import org.quartz.JobExecutionException;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.scheduling.quartz.QuartzJobBean;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.List;

/**

* Created by Administrator on 2017/12/20.

*/

@Component

public class HealthPlanJob extends QuartzJobBean {

@Autowired

MProjectMapper mProjectMapper;

@Override

public void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {

System.out.println(new Date());

}

}

发现时间可以打印出来,证明定时任务成功开启;但是同时也发现了一个问题,就是依赖注入的 mProjectMapper值为null。

开始以为是Spring的原因,导致注解失败,后来查了相关资料发现,不是Spring的原因,而是因为:这个Job是由quartz实例化出来的,不受Spring的管理,所以就导致注入失败。解决办法是自己new一个类,让Spring实例化这个类,代码如下

import org.quartz.spi.TriggerFiredBundle;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.config.AutowireCapableBeanFactory;

import org.springframework.scheduling.quartz.AdaptableJobFactory;

public class MyJobFactory extends AdaptableJobFactory {

@Autowired

private AutowireCapableBeanFactory capableBeanFactory;

protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {

//调用父类的方法

Object jobInstance = super.createJobInstance(bundle);

capableBeanFactory.autowireBean(jobInstance);

return jobInstance;

}

}

然后把这个类配置到Spring中去,(config-quartz.xml中红色部分)

然后在把org.springframework.scheduling.quartz.SchedulerFactoryBean的jobFactory设置成我们自己的。(config-quartz.xml中红色部分)

  

  

config-quartz.xml完整版如下:

xsi:schemaLocation="

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.2.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.2.xsd

http://springframework.org/schema/task

http://springframework.org/schema/task/spring-task-3.0.xsd">

xsi:schemaLocation="

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.2.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.2.xsd

http://springframework.org/schema/task

http://springframework.org/schema/task/spring-task-3.0.xsd">

到这为止,成功!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:电脑上微信能打开小程序码(电脑上微信能打开小程序码吗)
下一篇:Java ArrayList如何实现生成不重复随机数
相关文章

 发表评论

暂时没有评论,来抢沙发吧~