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的更多相关文章

  1. 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 ...

  2. BLOCKED和WAITING的区别

    /** * Thread state for a thread blocked waiting for a monitor lock. * A thread in the blocked state ...

  3. Java线程状态:BLOCKED与WAITING的区别

    Doc说明: /** * Thread state for a thread blocked waiting for a monitor lock. * A thread in the blocked ...

  4. Java线程状态中BLOCKED和WAITING有什么差别?

    刚才在看CSDN的问答时.发现这个问题. 原问题的作者是在观察jstack的输出时提出的疑问.那么BLOCKED和WAITING有什么差别呢? 答复在JDK源代码中能够找到,例如以下是java.lan ...

  5. 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 ...

  6. Quartz源码——scheduler.start()启动源码分析(二)

    scheduler.start()是Quartz的启动方式!下面进行分析,方便自己查看! 我都是分析的jobStore 方式为jdbc的SimpleTrigger!RAM的方式类似分析方式! Quar ...

  7. Quartz任务调度(3)存储与持久化操作配置详细解

    内存存储RAMJobStore Quartz默认使用RAMJobStore,它的优点是速度.因为所有的 Scheduler 信息都保存在计算机内存中,访问这些数据随着电脑而变快.而无须访问数据库或IO ...

  8. FREERTOS 手册阅读笔记

    郑重声明,版权所有! 转载需说明. FREERTOS堆栈大小的单位是word,不是byte. 根据处理器架构优化系统的任务优先级不能超过32,If the architecture optimized ...

  9. 多线程爬坑之路-Thread和Runable源码解析

    多线程:(百度百科借一波定义) 多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术.具有多线程能力的计算机因有硬件支持而能够在同一时间执行多于一个线程,进而提 ...

随机推荐

  1. Oracle数据库之视图与索引

    Oracle数据库之视图与索引 1. 视图简介 视图是基于一个表或多个表或视图的逻辑表,本身不包含数据,通过它可以对表里面的数据进行查询和修改. 视图基于的表称为基表,视图是存储在数据字典里的一条SE ...

  2. [转]我的第一个WCF

    1:首先新建一个解决方案 2:右击解决方案添加一个控制台程序 3:对着新建好的控制台程序右击添加wcf服务 最后的结果: 有3个文件 app.config  Iwcf_server.cs wcf_se ...

  3. linux安装时提示发生不正常错误问题

    跳过md5系统较检(每个系统版本都有一个md5编码唯一) 在安装CentOS时 提示 找不到磁盘,是否安装程序,选择安装程序进行"下一步" 提示: 发生不规则,不正常错误 原因:没 ...

  4. YII框架的部署 通过YII脚手架程序创建应用程序系统

    1,把YII框架里面的framework复制粘贴到nginx目录下 2,创建一个商城系统: 1)修改环境变量 制定php.exe的目录 2)C:\Users\Administrator>cd C ...

  5. A Script Pro nginx URL重写规则无法播放MP4解决方法

    I am using nginx and I have already add the line location /file/ { rewrite ^/-]+)/([-]+)/([^/]*)/([- ...

  6. 【算法】简单选择排序 O(n^2) 不稳定的 C语言

    简单选择排序 一.算法描述 假设序列中有N个元素: 第1趟找到第1到N个元素之间最小的一个,与第1个元素进行交换 第2趟找到第2到N个元素之间最小的一个,与第2个元素进行交换 第3趟找到第3到N个元素 ...

  7. 转:GraphicsMagick介绍及安装

    原文来自于:http://www.cnblogs.com/cocowool/archive/2010/08/16/1800954.html GraphicsMagick 当前稳定版本:1.3.12(发 ...

  8. 堆和栈的区别【zz】

    一.预备知识—程序的内存分配一个由c/C++编译的程序占用的内存分为以下几个部分1.栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其操作方式类似于数据结构中的栈.2. ...

  9. rsync使用说明

    需求:把10.5.128.190数据同步到10.5.128.27 用客服端-服务器模式,需要从客户端发起 也就是从10.5.128.27发起 10.5.128.27 作为客户端 10.5.128.19 ...

  10. POJ-1151-Atlantis(线段树+扫描线+离散化)[矩形面积并]

    题意:求矩形面积并 分析:使用线段树+扫描线...因为坐标是浮点数的,因此还需要离散化! 把矩形分成两条边,上边和下边,对横轴建树,然后从下到上扫描上去,用col表示该区间有多少个下边,sum代表该区 ...