0

我正在使用石英来安排工作。作业是在“默认”组中创建的,相关信息在使用 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 %

4

2 回答 2

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());
}    
于 2012-06-14T12:57:25.460 回答
0

QuartzJobDetails以序列化的 map ( JobDataMap) 格式存储。除非deserialization很难知道地图的实际内容是什么。

在您的情况下,我认为没有直接的方法,您可能需要找到一种解决方法,例如将作业数据映射值存储到Trigger id与之关联的单独表中,但我不确定该解决方案的销售情况如何。

于 2012-06-14T07:29:46.990 回答