Quartz SimpleTrigger在特定时间预定作业,但从不运行作业

时间:2015-08-14 20:20:57

标签: java spring triggers quartz-scheduler job-scheduling

我在石英中创建了一个SimpleTrigger,它在某个约会开始前一小时安排自己,这样它就会向需要的人发送文本。创建触发器后,我收到的日志告诉我它的安排时间和没有错误的情况。所以我假设它确实正确安排了工作。当前面提到的时间到来时,它永远不会发射。我根本没有得到任何东西。如果它甚至已进入该类,它将记录某些内容,但不会记录任何超过计划时间的内容。

除了通过约会时间动态创建触发器之外,此作业与我的其他作业之间没有堆栈跟踪或任何明显差异。一个对象正在传递给这份工作。

这是触发器:

public static void scheduleTextJob(Appointment appointment ){
    Logger log = LoggerFactory.getLogger(DailyTriggerController.class);

    JobDetail volunteerTextReminder = (JobDetail) newJob(VolunteerTextReminder.class).withIdentity(appointment.getId()).build();

    try{

        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = sf.getScheduler();

        Calendar cal = Calendar.getInstance();
        cal.setTime(appointment.getStartTime());
        Date scheduledTime;
        if(cal.get(Calendar.HOUR_OF_DAY) < 9 || (cal.get(Calendar.HOUR_OF_DAY) == 9 && cal.get(Calendar.MINUTE) < 30)){
            cal.add(Calendar.DAY_OF_YEAR, -1);
            cal.set(Calendar.HOUR_OF_DAY, 18);
            cal.set(Calendar.MINUTE, 0);
        }
        else{
            cal.add(Calendar.HOUR, -1);
        }

        scheduledTime = cal.getTime();

        SimpleTrigger uniqueTrigger = (SimpleTrigger) newTrigger()
                .withIdentity(appointment.getId(), "TextGroup")
                .startAt(scheduledTime)
                .forJob(volunteerTextReminder)
                .build();

        volunteerTextReminder.getJobDataMap().put("appointmentId", appointment.getId());

        Date ft = sched.scheduleJob(volunteerTextReminder, uniqueTrigger);

        log.info(volunteerTextReminder.getKey() + " will run at: " + ft);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

这是它应该运行的工作:

public class VolunteerTextReminder implements Job {

private @Autowired
HttpServletRequest request;

private static Logger _log = LoggerFactory.getLogger(VolunteerTextReminder.class);

private static final String APPLICATION_CONTEXT_KEY = "applicationContext";
/**
 * Quartz requires a public empty constructor so that the
 * scheduler can instantiate the class whenever it needs.
 */
public VolunteerTextReminder() {
}

public void execute(JobExecutionContext context)
        throws JobExecutionException {

    SchedulerContext schedulerContext = null;

    AppointmentDAO adao = null;

    VolunteerDAO vdao = null;

    ApplicationContext appContext = getApplicationContext(context);
    if (appContext != null) {
        adao = (AppointmentDAO) appContext.getBean("AppointmentDAO");
        vdao = (VolunteerDAO) appContext.getBean("VolunteerDAO");

        _log.info("appContext created adao");
    }
    else {
        _log.info("web context DID NOT create adao");
    }

    // This job simply prints out its job name and the
    // date and time that it is running
    JobKey jobKey = context.getJobDetail().getKey();
    _log.info("VolunteerTextReminder says: " + jobKey + " executing at " + new Date());

    try {

        JobDataMap data = context.getJobDetail().getJobDataMap();
        String id = data.getString("appointmentId");

        assert adao != null;
        List<Person> people = adao.readPersonsByAppointmentId(id);

        List<Person> volunteers = new ArrayList<>();
        for(Person person: people){
            if(person.getType().equalsIgnoreCase("volunteer")){
                volunteers.add(person);
            }
        }

        for(Person volunteer: volunteers){
            Volunteer volunteerToEmail = vdao.getVolunteerToEmailById(volunteer.getLdsAccountId());
            if(volunteerToEmail != null) {
                Appointment appointment = (Appointment) adao.readAppointmentById(id);
                if(!appointment.getCancelled()){
                    createAndSendText(volunteerToEmail, appointment);
                }
            }
        }


    } catch (Exception ex) {
        ex.printStackTrace();
    }

}
}

0 个答案:

没有答案