Hello,
yes it’s quite possible.
I use it to quartz schedule some alarms for the users, when it triggers it sends notification.
To do that, I have:
ScheduledAlarmJob annotated as a component, implementing Job - this is the class to perform my action
AlarmSchedulerService - service to schedule the execution of ScheduledAlarmJob
I will paste some code fragments
@Component("com_ScheduledAlarm")
@DisallowConcurrentExecution
public class ScheduledAlarmJob implements Job {
private static final Logger log = LoggerFactory.getLogger(ScheduledAlarmJob.class);
@Autowired
protected NotificationManager notificationManager;
@Autowired
private NotificationTypesRepository notificationTypesRepository;
@Authenticated
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
JobDataMap dataMap = context.getJobDetail().getJobDataMap();
String message = dataMap.getString("message");
String username = dataMap.getString("username");
log.info("Alarm job executed: " + message);
notificationManager.createNotification()
.withSubject("Alarm: " + "Some Alarm ")
.withRecipientUsernames(username)
.toChannelsByNames("in-app")
.withContentType(ContentType.PLAIN)
.withType(notificationTypesRepository.getTypeByNameOpt("warn").get())
.withBody(message)
.send();
}
}
@Service(AlarmSchedulerService.NAME)
public class AlarmSchedulerServiceImpl implements AlarmSchedulerService {
@Autowired
private Scheduler scheduler;
@Override
public void scheduleAlarmJob(String username, Long miliseconds) throws SchedulerException {
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("message", "This is alarm for " + username + ": " + " Alarm Trigger");
jobDataMap.put("username", username);
JobDetail jobDetail = JobBuilder.newJob(ScheduledAlarmJob.class)
.withIdentity(username + ": " + "Alarm", "ALARMS")
.setJobData(jobDataMap)
.build();
// Schedule to trigger after miliseconds interval (10 minutes = 600000 ms)
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity(username + ": " + "Alarm Trigger", "ALARMS")
// .startAt(new Date(System.currentTimeMillis() + 60000)) // 1 minute from now
.startAt(new Date(System.currentTimeMillis() + miliseconds))
.withSchedule(SimpleScheduleBuilder.simpleSchedule()
//Ignores misfires and continues with the next scheduled execution
.withMisfireHandlingInstructionIgnoreMisfires()
)
.build();
scheduler.scheduleJob(jobDetail, trigger);
}
}
Kind regards,
Mladen