如何构造Quartz调度程序?

时间:2018-10-12 19:19:26

标签: java casting quartz

我的石英调度程序尝试执行作业时,出现以下错误。

15:56:01,412 ERROR [org.quartz.core.JobRunShell] (DiscoveryScheduler_Worker-2) Job DEFAULT.6da64b5bd2ee-b3ce1208-e5db-4a1e-be01-e802d419deb7 threw an unhandled Exception: : java.lang.ClassCastException: com.icarotech.portal.to.ScheduleReport cannot be cast to com.icarotech.portal.ejb.bean.client.GenerateSAISSMPBeanRemote at com.icarotech.portal.service.ReportingJob.execute(ReportingJob.java:27) at org.quartz.core.JobRunShell.run(JobRunShell.java:202) at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)

15:56:01,414 ERROR [org.quartz.core.ErrorLogger] (DiscoveryScheduler_Worker-2) Job (DEFAULT.6da64b5bd2ee-b3ce1208-e5db-4a1e-be01-e802d419deb7 threw an exception.: org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: java.lang.ClassCastException: com.icarotech.portal.to.ScheduleReport cannot be cast to com.icarotech.portal.ejb.bean.client.GenerateSAISSMPBeanRemote]
    at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: java.lang.ClassCastException: com.icarotech.portal.to.ScheduleReport cannot be cast to com.icarotech.portal.ejb.bean.client.GenerateSAISSMPBeanRemote
    at com.icarotech.portal.service.ReportingJob.execute(ReportingJob.java:27)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
    ... 1 more

安排工作时登录:

15:55:39,779 INFO  [stdout] (default task-25) Quartz Scheduler (v2.2.3) 'DiscoveryScheduler' with instanceId 'NON_CLUSTERED'
15:55:39,780 INFO  [stdout] (default task-25)   Scheduler class: 'org.quartz.impl.StdScheduler' - running locally.
15:55:39,780 INFO  [stdout] (default task-25)   Running since: Fri Oct 12 
15:52:51 BRT 2018
15:55:39,780 INFO  [stdout] (default task-25)   Not currently in standby mode.
15:55:39,781 INFO  [stdout] (default task-25)   Number of jobs executed: 1
15:55:39,781 INFO  [stdout] (default task-25)   Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 2 threads.
15:55:39,781 INFO  [stdout] (default task-25)   Using job-store 'org.quartz.impl.jdbcjobstore.JobStoreTX' - which supports persistence. and is not clustered.

================================================ =====================

我的Scheduler类

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import com.icarotech.portal.ejb.bean.client.GenerateSAISSMPBeanRemote;
import com.icarotech.portal.ejb.bean.client.SchedulingReportBeanRemote;
import com.icarotech.portal.to.ScheduleReport;
import com.icarotech.portal.util.PortalLogger;
import com.icarotech.portal.util.EnumUtil.SchedulingReportStatusEnum;

public class ReportScheduler {

private Scheduler scheduler;

public ReportScheduler() {
try {
Properties prop = new Properties();


prop.put("org.quartz.jobStore.class","org.quartz.impl.jdbcjobstore.JobStoreTX");
            prop.put("org.quartz.scheduler.instanceName", "DiscoveryScheduler");
prop.put("org.quartz.scheduler.instanceId", "AUTO");
prop.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
prop.put("org.quartz.threadPool.threadCount", "2");
prop.put("org.quartz.threadPool.threadPriority", "5");
prop.put("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
prop.put("org.quartz.jobStore.dataSource", "sais");
prop.put("org.quartz.jobStore.tablePrefix", "QRTZ_");
prop.put("org.quartz.dataSource.sais.jndiURL", "java:/jdbc/portalds");
SchedulerFactory schedulerFactory = new StdSchedulerFactory(prop);

System.out.println(schedulerFactory.getScheduler().getMetaData());
scheduler = schedulerFactory.getScheduler();
//          scheduler.start();
} catch (SchedulerException e) {
PortalLogger.error("[ReportScheduler] Error on instantiating the scheduler", e);
} catch(Exception err) {
System.out.println(err);
}       
}

public boolean addReport(ScheduleReport report, GenerateSAISSMPBeanRemote generateSAISBean, SchedulingReportBeanRemote schedulingReport) {
try {
JobDataMap jobMap = new JobDataMap();

jobMap.put("generateSAISBean", generateSAISBean);
jobMap.put("schedulingReport", schedulingReport);
jobMap.put("generateSAISBean", report);

JobDetail job = JobBuilder.newJob(ReportingJob.class)
                    .withIdentity(report.getQtzJobName())
                    .usingJobData(jobMap)
                    .build();

Trigger trigger = null;

if (report.getScheduleType().equals(SchedulingReportStatusEnum.RECURRENT)) {

Calendar calendar = Calendar.getInstance();
calendar.setTime(report.getDateAgenda());               
trigger = TriggerBuilder.newTrigger()                                
.withSchedule(CronScheduleBuilder.dailyAtHourAndMinute(
calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE)))
                                .build();

} else if 
(report.getScheduleType().equals(SchedulingReportStatusEnum.SCHEDULED)) {
trigger = TriggerBuilder.newTrigger()
            .startAt(report.getDateAgenda()).build();
}

scheduler.scheduleJob(job, trigger);        
} catch (SchedulerException e) {
PortalLogger.error("[ReportScheduler] Error trying to create a new scheduling.", e);
}
return false;
}

public boolean removeReport (String qtzJobName) {
try {
    JobKey jobKey = new JobKey(qtzJobName);
    scheduler.deleteJob(jobKey);

    PortalLogger.debug("[ReportScheduler] Scheduling deleted. ID_REPORT = " + qtzJobName);
    return true;
} catch (SchedulerException e) {
            PortalLogger.error("[ReportScheduler] Error trying to delete the scheduling.", e);
        }

        return false;
    }
    public String getJobString(ScheduleReport report) {
        return JobBuilder.newJob(ReportingJob.class)
                    .usingJobData(ReportingJob.ID_REPORT, 
                            report.getDateAgenda().getTime())
                    .build().getKey().getName();
    }


}

=======================================

这是实现Job的我的班级

package com.icarotech.portal.service;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.quartz.SchedulerContext;
import org.quartz.SchedulerException;

import com.icarotech.portal.ejb.bean.client.GenerateSAISSMPBeanRemote;
import com.icarotech.portal.ejb.bean.client.SchedulingReportBeanRemote;
import com.icarotech.portal.to.ScheduleReport;
import com.icarotech.portal.util.EnumUtil.ReportStatusEnum;
import com.icarotech.portal.util.EnumUtil.SchedulingReportStatusEnum;
import com.icarotech.portal.util.PortalLogger;

public class ReportingJob implements Job{

public static final String ID_REPORT = "ID_REPORT";

@Override
public void execute(JobExecutionContext jobExecution) throws 
JobExecutionException {

SchedulerContext schedulerContext;
try {


schedulerContext = jobExecution.getScheduler().getContext();
GenerateSAISSMPBeanRemote generateSAISBean = (GenerateSAISSMPBeanRemote) jobExecution.getJobDetail().getJobDataMap().get("generateSAISBean");
ScheduleReport report  = (ScheduleReport) jobExecution.getJobDetail().getJobDataMap().get("report");
if (report.getScheduleType().equals(SchedulingReportStatusEnum.SCHEDULED)) {
                SchedulingReportBeanRemote schedulingReport = (SchedulingReportBeanRemote) jobExecution.getJobDetail().getJobDataMap().get("schedulingReport");
                report.setStatus(ReportStatusEnum.EXECUTED);
                schedulingReport.updateReport(report);
            }

            generateSAISBean.generateSaisSMP(report.getCreateBy());
        } catch (SchedulerException e) {
            PortalLogger.error("[ReportingJob] Error trying to get the 
context.", e);
        }
    }

}

===============================

我正在使用JDBC存储

请帮帮我!! 如果有人认为这是一个不错的选择,我们可以通过Skype或Google环聊进行通话,但是很遗憾,我无法提供所有代码。

1 个答案:

答案 0 :(得分:0)

我发现我的调度程序类中缺少一行代码,紧接在“触发器触发器= null;”行之后。

scheduler.scheduleJob(作业,触发器);

所以,我把它放在我的代码中,不,所有的东西都起作用, 希望这个问题对某人有所帮助。