使用条件查询现有触发器的石英

时间:2012-06-14 07:17:09

标签: java api quartz-scheduler scheduler

我正在使用石英来安排工作。作业在“默认”组中创建,相关信息在使用schedule api时以作业详细信息的形式作为有效负载传递。

Trigger trigger = new Trigger(...);
JobDetail jobDetail = new JobDetail(...);
jobDetail.put(...);
scheduler.schedule(trigger, jobDetail);

我希望API能够根据发送的有效负载查询数据库中的触发器。例如,我将“externalId”作为jobDetail的一部分。

我想做点什么

scheduler.getTriggers(new Criteria("externalId", externalId));

而不是在内存中获取所有触发器,然后迭代它。我在网上搜索了一下但是找不到API来做同样的事情。

编辑:从石英2.1.5开始,有一些新的API

scheduler.getTrigger(triggerKey(jobId, jobGroupName)); // which can fetch the exact trigger given the triggerKey
scheduler.getTriggerKeys(GroupMatcher.triggerGroupContains(JOB_GROUP_NAME)); //this searches all the triggers for a group.

这些不适用于石英1.8。一个限制仍然是触发搜索需要精确,并且本质上不能是LIKE%

2 个答案:

答案 0 :(得分:1)

您可以列出石英中的所有任务并收集所需的

试试这段代码(jsp):

SchedulerFactory sf = new StdSchedulerFactory(new File(getServletContext().getRealPath("/"), "WEB-INF/quartz.properties").getAbsolutePath());
Scheduler scheduler = sf.getScheduler();

try{
    List<JobExecutionContext> jobList = scheduler.getCurrentlyExecutingJobs();

    out.print("<h3>Current tasks: "+jobList.size()+"</h3>");
    for(JobExecutionContext jec : jobList){
        out.print("<hr>");
        JobDetail jobDetail = jec.getJobDetail();
        Trigger trigger = jec.getTrigger();
        TriggerState state = scheduler.getTriggerState(trigger.getKey());
        %>
        <table style="width: 400px; background-color: #ffffff;">
            <tr style="height: 23px;">
                <td style="width: 190px;">
                    class:
                </td>
                <td style="width: 210px;">
                    <b><%=jec.getJobInstance().toString()%></b>
                </td>
            </tr>    
            <tr style="height: 23px;">
                <td>
                    descr:
                </td>
                <td>
                    <i><%=jobDetail.getDescription()%></i>
                </td>
            </tr>                    
            <tr style="height: 23px;">
                <td>
                    next fire time:
                </td>
                <td>
                    <%=new Timestamp(trigger.getFireTimeAfter(new Timestamp(System.currentTimeMillis())).getTime())%>
                </td>
            </tr> 
            <tr style="height: 23px;">
                <td>
                    prior:
                </td>
                <td>
                    <%=state.toString()%>
                </td>
            </tr>                 
        </table>
        <%
    }
}catch(Exception ex){
    out.print(ex.getMessage());
}

out.print("<hr>");

// getting all tasks
try{
    List<String> jobGroups = scheduler.getJobGroupNames();

    out.print("<h3>Triggers: "+jobGroups.size()+"</h3>");
    for (String jName : jobGroups){
        GroupMatcher<JobKey> matcher = GroupMatcher.groupEquals(jName);
        Set keys = scheduler.getJobKeys(matcher);
        Iterator iter = keys.iterator();
        while (iter.hasNext()){
            JobKey jk = (JobKey)iter.next();
            JobDetail jobDetail = scheduler.getJobDetail(jk);
            // JobDataMap jobDataMap = jobDetail.getJobDataMap();

            List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jk);
            for(Trigger t : triggers){
                out.print("<hr>");
                TriggerState state = scheduler.getTriggerState(t.getKey());
                %>

                <table style="width: 400px; background-color: #ffffff;">
                    <tr style="height: 23px;">
                        <td style="width: 190px;">
                            group/class:
                        </td>
                        <td style="width: 210px;">
                            <b><%=t.getJobKey().toString()%></b>
                        </td>
                    </tr>
                    <tr style="height: 23px;">
                        <td style="width: 190px;">
                            descr:
                        </td>
                        <td style="width: 210px;">
                            <i><%=jobDetail.getDescription()%></i>
                        </td>
                    </tr>                        
                    <tr style="height: 23px;">
                        <td>
                            curr time:
                        </td>
                        <td>
                            <%=new Timestamp(System.currentTimeMillis())%>
                        </td>
                    </tr>                        
                    <tr style="height: 23px;">
                        <td>
                            next fire time:
                        </td>
                        <td>
                            <%=new Timestamp(t.getFireTimeAfter(new Timestamp(System.currentTimeMillis())).getTime())%>
                        </td>
                    </tr>                          
                    <tr style="height: 23px;">
                        <td>
                            prior:
                        </td>
                        <td>
                            <%=state.toString()%>
                        </td>
                    </tr>                         
                </table>

                <%
            }
        }
    }
    out.print("<hr>");
}catch (SchedulerException ex){
    out.print(ex.getMessage());
}    

答案 1 :(得分:0)

Quartz将JobDetails存储在序列化地图(JobDataMap)格式中。除非deserialization,否则很难知道地图的实际内容是什么。

在您的情况下,我认为没有直接的方法,您可能需要找到类似的工作,将作业数据映射值存储到单独的表中,该表将与Trigger id相关联,但我不是确定这个解决方案有多可用。