免费视频网站app使用排名东莞网站建设 硅橡胶
web/
2025/9/27 5:09:59/
文章来源:
免费视频网站app使用排名,东莞网站建设 硅橡胶,wordpress文章导入,大连招聘网最新招聘最近在写定时任务#xff0c;以前没接触过。查了些相关资料说使用quartz定时框架。需要配置文件#xff1a;config-quartz.xml相关配置如下(红色部分是之后添加的#xff0c;在后面步骤会说明)#xff1a;xsi:schemaLocationhttp://www.springframework.org/schema/b…最近在写定时任务以前没接触过。查了些相关资料说使用quartz定时框架。需要配置文件config-quartz.xml相关配置如下(红色部分是之后添加的在后面步骤会说明)xsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.0.xsdquartz.properties## Configure Main Scheduler Properties#org.quartz.scheduler.instanceName WrhFrameSchedulerorg.quartz.scheduler.instanceId AUTOorg.quartz.scheduler.skipUpdateCheck true## Configure ThreadPool#org.quartz.threadPool.class org.quartz.simpl.SimpleThreadPoolorg.quartz.threadPool.threadCount 12org.quartz.threadPool.threadPriority 5## Configure JobStore#org.quartz.jobStore.misfireThreshold 60000org.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.*/Componentpublic class HealthPlanJob extends QuartzJobBean {AutowiredMProjectMapper mProjectMapper;Overridepublic 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 {Autowiredprivate 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:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.2.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.2.xsdhttp://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.0.xsd到这为止成功以上这篇JAVA使用quartz添加定时任务,并依赖注入对象操作就是小编分享给大家的全部内容了希望能给大家一个参考也希望大家多多支持脚本之家。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/82553.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!