问题:job定时任务中,直接通过加注解@Component、@Autowired是不能注入的,获取的对象为Null
@Component
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class TicketSalePriceLessThanLowestPriceJob implements Job{
@Autowired
private XxxService xxxService;
}
解决方案:
参考:http://fanshuyao.iteye.com/blog/2391445
1、新增一个自定义类(CustomJobFactory),继承SpringBeanJobFactory,代码如下:
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;
public class CustomJobFactory extends SpringBeanJobFactory{
@Autowired
private AutowireCapableBeanFactory capableBeanFactory;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
//调用父类的方法
Object jobInstance = super.createJobInstance(bundle);
//进行注入
capableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
2、在spring.xml文件配置CustomJobFactory,如下:
<bean id="customJobFactory" class="cn.imovie.manage.task.job.CustomJobFactory"></bean>
3、将自定义CustomJobFactory注入到org.springframework.scheduling.quartz.SchedulerFactoryBean,具体如下:
<property name="jobFactory" ref="customJobFactory"></property>
完整代码如下:
<!-- 定时任务配置 start -->
<bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!--可选,QuartzScheduler 启动时更新己存在的Job,这样就不用每次修改targetObject后删除qrtz_job_details表对应记录了 -->
<property name="overwriteExistingJobs" value="true" />
<!--必须的,QuartzScheduler 延时启动,应用启动完后 QuartzScheduler 再启动 -->
<property name="startupDelay" value="10" />
<!-- 设置自动启动 -->
<property name="autoStartup" value="true" />
<property name="jobFactory" ref="customJobFactory"></property>
<property name="applicationContextSchedulerContextKey" value="applicationContextKey" />
<property name="configLocation" value="classpath:spring-quartz.properties" />
</bean>
<!-- 定时任务配置 end -->
4、然后就可以在Job任务类使用@Autowired注入service。
问题:使用quartz进行定时任务自动执行的时候,用到mybatis进行数据库交互,Tomcat运行中,会遇到无故停止运行,定时任务全部卡住
解决方案:
参考:https://www.cnblogs.com/HJL-Blog/p/5044672.html
问题解析:首先我从几个方面考虑这个问题。
1、quartz配置问题。
2、数据库连接超时。
3、Tomcat自动清理或者超时。
4、quartz线程死锁或阻塞。
问题的源头是连接池连接阻塞,修改了数据源的连接回收时间
<!-- 初始连接数目 -->
<property name="initialSize" value="10"></property>
<!-- 最大连接数目 -->
<property name="maxActive" value="50"></property>
<!-- 最大空闲连接数目 -->
<property name="maxIdle" value="50"></property>
<!-- 最小空闲连接数目 -->
<property name="minIdle" value="0"></property>
<!-- 超时等待时间以毫秒为单位 -->
<property name="maxWait" value="10000"></property>
<!-- #是否自动回收超时连接 -->
<property name="removeAbandoned" value="true"></property>
<!-- #设置被遗弃的连接的超时的时间(以秒数为单位),即当一个连接被遗弃的时间超过设置的时间,则它会自动转换成可利用的连接。默认的超时时间是300秒。 -->
<property name="removeAbandonedTimeout" value="60"></property>
<!-- #是否在自动回收超时连接的时候打印连接的超时错误 -->
<!-- <property name="logAbandoned" value="true"></property> -->
问题:关闭tomcat的时候,报内存泄漏
28-May-2018 15:43:19.594 严重 [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [ROOT] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@690c3df7]) and a value of type [java.lang.Class] (value [class oracle.sql.AnyDataFactory]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
28-May-2018 15:43:19.594 严重 [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [ROOT] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@2598e8fc]) and a value of type [java.lang.Class] (value [class oracle.sql.TypeDescriptorFactory]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
解决方案:
参考:https://stackoverflow.com/questions/3320400/to-prevent-a-memory-leak-the-jdbc-driver-has-been-forcibly-unregistered
把jar拷贝到Tomcat/lib下。