English 中文(简体)
多个时间发送电子邮件
原标题:Quartz Scheduler sending email notification multiple time

我正在利用Quartz安排工作。 工作是每天发送催复函,具体时间是上午11:00。 我能够成功地寄送催复函,但问题是同时寄出超过1个邮件。 有时,它发出8个邮件,要求发出1份催复通知,有时发出。 5. 似乎同一工作是多次完成的。

Following is my code,

JobDetail job = JobBuilder.newJob(LmsJob.class)
                .withIdentity("lmsJob", org.quartz.Scheduler.DEFAULT_GROUP)
                .build();

        JobDataMap map = job.getJobDataMap();
        map.put("creditMonthlyLeaveBalance", creditMonthlyLeaveBalance);
        map.put("dailyUpdationTask", dailyUpdation);
        map.put("monthlyPayrollGenerationTask",
                monthlyPayrollGenerationTask);
        map.put("yearlyMaintenanceOfLeaveBalance",
                yearlyMaintenanceOfLeaveBalance);
        map.put("emailNotifier", emailNotifier);
        try {
            CronTrigger trigger = TriggerBuilder
                    .newTrigger()
                    .withIdentity("lmsJob", "lmsJobGroup")
                    .forJob(job)
                    .startAt(new Date(System.currentTimeMillis()))
                    .withSchedule(
                            CronScheduleBuilder
                                    .cronSchedule("00 00 00 ? * *")).build();

            scheduler.scheduleJob(job, trigger);
            scheduler.start();

            // scheduler.shutdown();

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

在这方面,请让我知道,我方是否还需要任何其他东西。

问题回答

I do not know your entire code,which annotation you gave and stuff.So, I am guessing that you gave annotation like @QuartzEvery("3h"). As far I am guessing, your job is scheduled wrong.To make it run at a particular time of every day,try this...

QuartzManager implements Managed {
.
.
public void start() throws Exception {
.
.
QuartzDailyAt dailyAt = jobType.getAnnotation(QuartzDailyAt.class);
int hours[] = dailyAt.hours();
String hourString = 
Arrays.stream(hours).mapToObj(String::valueOf).collect(joining(","));
String cronExpression = String.format("0 0 %s * * ?", hourString);
Trigger trigger = TriggerBuilder.
                    newTrigger().
                    startNow().
                    withSchedule(CronScheduleBuilder.cronSchedule(cronExpression).
                            inTimeZone(TimeZone.getTimeZone("IST"))).
                    build();
scheduler.scheduleJob(JobBuilder.newJob(jobType).build(), trigger);
scheduler.start();
.
. 
}
.
}

接口

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface QuartzDailyAt {
    int[] hours(); 
}

在担任职务时,在上等阶层的顶端添加了说明。

@QuartzDailyAt(hours = {7,8,9,15,16,17})
public class SomeJob extends QuartzJob {.....}

这使你在某个特定时间区每一特定时间段进行操作。





相关问题
Trigger function when Undeploying application

How do I automatically trigger Java function to stop Quartz scheduler jobs when I deploy/undeploy/redeploy JEE5 application in Glassfish.

Quartz API error

When I compile and run my application on my local machine with specs Windows XP sp2, JDK 5u11, I get no error. But when I try to run this application (compiled on Windows XP) on a Linux Debian distro, ...

How to call a CronTriggerBean stored in a JDBCJobStore?

I need some help. I am using Quartz Scheduling and have configured a CronTrigger to run each night at 10PM. I am using the JDBCJobStore to take advantage of the Clustering. The job runs at 10PM ...

How do I start Quartz in standalone mode?

According to its feature list "Quartz can run as a stand-alone program (within its own Java Virtual Machine), to be used via RMI". I could not find any documentation how to start it in stand-alone ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

热门标签