/
DailyMailSubscriptionScheduler.java
61 lines (51 loc) · 1.92 KB
/
DailyMailSubscriptionScheduler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.hardik.mercury.quartz;
import org.quartz.ObjectAlreadyExistsException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerKey;
import org.springframework.stereotype.Component;
import com.hardik.mercury.exception.EmailAlreadyRegisteredException;
import com.hardik.mercury.exception.GenericServerException;
import com.hardik.mercury.quartz.job.detail.DailySubscriptionMailSenderJobDetail;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Component
@AllArgsConstructor
@Slf4j
public class DailyMailSubscriptionScheduler {
private final Scheduler scheduler;
private final DailySubscriptionMailSenderJobDetail dailySubscriptionMailSenderJobDetail;
public void start() throws SchedulerException {
this.scheduler.start();
this.scheduler.addJob(dailySubscriptionMailSenderJobDetail.getJobDetail(), false);
}
public void addTriggerInDailyMailSubscriptionService(Trigger trigger) {
try {
this.scheduler.scheduleJob(trigger);
log.info("Successfully scheduled trigger with identity: {}", trigger.getKey());
} catch (ObjectAlreadyExistsException exception) {
log.error("Daily mail sender Trigger Already Added!");
throw new EmailAlreadyRegisteredException();
} catch (SchedulerException e) {
log.error("Unable to add trigger {}", e);
throw new GenericServerException();
}
}
public void removeTrigger(final String email) {
try {
this.scheduler.unscheduleJob(new TriggerKey(email));
} catch (SchedulerException e) {
log.error("Unable to unschedule email from daily mail subscription service: {}", e);
throw new GenericServerException();
}
}
public Boolean triggerWithEmailScheduled(final String emailId) {
try {
return this.scheduler.checkExists(new TriggerKey(emailId));
} catch (SchedulerException e) {
log.error("Unable to check for trigger existence: {}", e);
throw new GenericServerException();
}
}
}