Quarts SimpleTrigger going to BLOCKED state after few repeat intervals--stackoverflow
question:
I am using SimpleTrigger to schedule a job which is supposed to run indefinitely (repeat count -1).
And i am using JDBC store to persist the job state in DB.
But the trigger is firing for some intervals (in my case always 8) and goes to BLOCKED state. TO be specific, the value of TRIGGERS_STATE will be changed to BLOCKED in QRTZ_TRIGGERS table. Note my prefix for Quartx tables is QRTZ_ Below are my Job Trigger info.
repeat count: -1, repeat Interval: 6 seconds, start delay: 10 seconds
MY quartz configurations:
#===============================================================
#Configure ThreadPool
#===============================================================
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true
#===============================================================
#Configure JobStore
#===============================================================
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.maxMisfiresToHandleAtATime=20
# Flag to turn off to ignore all misfires
scheduler.ignoreMisfire=no
# Configuring JDBCJobStore with the Table Prefix
org.quartz.jobStore.tablePrefix = QRTZ_
# Using DriverDelegate
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.oracle.OracleDelegate
org.quartz.jobStore.useProperties = false
Scheduler Class:
public static void scheduleJob(Class<? extends Job> job,JobDataMap dataMap)
{
Scheduler scheduler = schedulerFactoryBean.getScheduler();
try
{
JobDetail jobDetail = newJob(job)
.withIdentity(job.getSimpleName()+"_"+DateUtil.getSystemDate(), job.getSimpleName() + "_group")
.storeDurably()
.usingJobData(dataMap)
.requestRecovery()
.build();
SimpleTrigger trigger = (SimpleTrigger) newTrigger()
.withIdentity(job.getSimpleName() + "_trigger_"+DateUtil.getSystemDateWithMs(), job.getSimpleName() + "_trigger_group")
.startNow()
.withSchedule(simpleSchedule().repeatSecondlyForever(10).withMisfireHandlingInstructionFireNow())
.build();
scheduler.scheduleJob(jobDetail, trigger);
//logger.debug(scheduler.getMetaData().toString());
scheduler.start();
}
catch (SchedulerException e)
{
e.printStackTrace();
throw new SchedulerException("", e);
}
}
Job Class:
@PersistJobDataAfterExecution
public class MyJob Implements Job
{
private SessionFactory sessionFactory;
@Override
public void execute(JobExecutionContext context) throws JobExecutionException
{
getBeansFromContext(context);
Session session = sessionFactory.openSession(); // Hibernate Session Factory
// to do some DB opetations
}
private void getBeansFromContext(JobExecutionContext context) throws SchedulerException
{
ApplicationContext applicationContext = (ApplicationContext)context.getScheduler().getContext().get("applicationContext");
this.sessionFactory=applicationContext.getBean(SessionFactory.class);
}
}
Spring bean configration for Quartz scheduler factory.
<beans:bean id="schedulerFactoryBean"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<beans:property name="jobFactory">
<beans:bean class="org.springframework.scheduling.quartz.SpringBeanJobFactory"></beans:bean>
</beans:property>
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="transactionManager" ref="txManager" />
<beans:property name="configLocation"
value="resources/scheduler/Scheduler.properties" />
<beans:property name="applicationContextSchedulerContextKey"
value="applicationContext" />
<beans:property name="autoStartup" value="true" />
</beans:bean>
<beans:bean id="taskExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"
p:corePoolSize="5" p:maxPoolSize="10" p:queueCapacity="100"
p:waitForTasksToCompleteOnShutdown="true" />
Any help is really appreciated. Thanks in advance
Answer
I finally understood the problem and able to resolve it.
As @zerologiko commented the issue is with transaction. I am using Spring managed transaction with hibernate. Once i declare my transaction policy, Spring takes care of start/end of transactions.
Reason for the issue in my case: Spring bean life cycle is not effective in the Scheduler Job. To elaborate on this, as given in main post i had to even accessing applicationContext inside my job class using
jobContext.getScheduler().getContext().get("applicationContext");
I am trying to update DB back with some status into one of our transaction database after the job is done.
I missed to realize initially that even the transaction are also controlled by Spring. When those db updates were triggered from a job class, the transactions declared on my business methods had no effect.
According to my understanding, the trigger were going to Acquired as the threads which completed the job is not able to come back to pool.
To fix this problem, i manually opened/closed the transactions in my job class without relying on Spring CMT and it worked without issues.
Hope this helps someone who is facing same kind of issue.
Quarts SimpleTrigger going to BLOCKED state after few repeat intervals--stackoverflow的更多相关文章
- Quartz Scheduler(2.2.1) - Usage of SimpleTrigger
SimpleTrigger should meet your scheduling needs if you need to have a job execute exactly once at a ...
- BLOCKED和WAITING的区别
/** * Thread state for a thread blocked waiting for a monitor lock. * A thread in the blocked state ...
- Java线程状态:BLOCKED与WAITING的区别
Doc说明: /** * Thread state for a thread blocked waiting for a monitor lock. * A thread in the blocked ...
- Java线程状态中BLOCKED和WAITING有什么差别?
刚才在看CSDN的问答时.发现这个问题. 原问题的作者是在观察jstack的输出时提出的疑问.那么BLOCKED和WAITING有什么差别呢? 答复在JDK源代码中能够找到,例如以下是java.lan ...
- Cross-origin plugin content from must have a visible size larger than 400 x 300 pixels, or it will be blocked. Invisible content is always blocked.
Cross-origin plugin content from must have a visible size larger than 400 x 300 pixels, or it will ...
- Quartz源码——scheduler.start()启动源码分析(二)
scheduler.start()是Quartz的启动方式!下面进行分析,方便自己查看! 我都是分析的jobStore 方式为jdbc的SimpleTrigger!RAM的方式类似分析方式! Quar ...
- Quartz任务调度(3)存储与持久化操作配置详细解
内存存储RAMJobStore Quartz默认使用RAMJobStore,它的优点是速度.因为所有的 Scheduler 信息都保存在计算机内存中,访问这些数据随着电脑而变快.而无须访问数据库或IO ...
- FREERTOS 手册阅读笔记
郑重声明,版权所有! 转载需说明. FREERTOS堆栈大小的单位是word,不是byte. 根据处理器架构优化系统的任务优先级不能超过32,If the architecture optimized ...
- 多线程爬坑之路-Thread和Runable源码解析
多线程:(百度百科借一波定义) 多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术.具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提 ...
随机推荐
- 实例:jQuery实现标签切换
具体实现效果如图: 原理很简单,就是监听鼠标滑动和点击事件.在第一个标签切换的示例中,当鼠标滑过某个标签时,就把class转移到当前标签.这里用到的jQuery方法主要是each()确定当前是哪一个标 ...
- [jQuery编程挑战]007 切换数据表格的行列
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8&quo ...
- 移动端版本兼容js
移动端版本兼容js <!--移动端版本兼容 --> <script type="text/javascript"> var phoneWidth = par ...
- php实现的太平洋时间和北京时间互转的自定义函数
date_default_timezone_set('Asia/Shanghai'); $time = time(); } date_default_timezone_set('Pacific/Api ...
- JS动画理论
动画(Animation) 动画意味着随着时间而变化,尤其指视觉上的变化,包括位置.形态等的变化.运动基本上表现为物体随时间,发生位置上的变化:形态基本表现为大小.颜色.透明度.形状等随时间的变化. ...
- SharePoint 2013 更新多个用户字段(Person or Group)
有时我们需要更新一个用户到Person or Group类型的字段, 当然这个属性允许设置多个用户, 要如何才能添加新的用户到该字段,同时还不影响原始存在的值. 这里我们需要了解 SPFieldUse ...
- const 笔记
.指向const的指针例如:double a=1.01;const double * b=&a;*b=2.1; //这显然是错误的a=2.1; //这是正确的,a和*b的值都会变成2.01,有 ...
- iOS播放短的音效
在IOS中,有的时候需要播放很简短的声音文件,比如系统声音等,我们需要使用到下面的方式来播放声音: // 一.引入头文件 #import <AudioToolbox/AudioToolbox.h ...
- iOS的launch image --备用
当我们打开一款应用程序的时候,首先映入眼帘的往往并不是程序的主界面,而是经过精心设计的欢迎界面,这个界面通常会停留几秒钟,然后消失.看似很平常的一个小小的欢迎界面,其实还大有讲究. 一.为什么会出现欢 ...
- 转:.NET中使用Redis (二)
原文来自于:http://blog.jobbole.com/83824/ 原文出处: 寒江独钓 欢迎分享原创到伯乐头条 很久以前写了一篇文章 .NET中使用Redis 介绍了如何安装Redis服务 ...