Is it possible to create a quartz job programmatically at runtime?

Hi,
I am building an application with Jmix 2.3 that synchronizes users mailboxes with their respective accounts in Microsoft Office 365. In particular, every user is allowed to view his/her emails from the Jmix application and reply or forward them… Since this application is used by more than 100 users that are receiving daily a large number of emails, I want to isolate the email synchronization process for each user by creating a separate quartz job that will be responsible to receive & send all email items for a single account. Thus, what I need is when a new user mailbox is created in the application then a process will run and create a new job with a respective trigger that will also accept as data the name of the particular mailbox in order for the synchronization process to run. Can this scenario be implemented in Jmix? I have found in the documentation that a job can be programmatically created at startup of the application but there is no relevant documentation for generating at runtime any number of jobs. If this is not possible, is there any other workaround that will help me implement the aforementioned scenario?

Thank you

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

Hi Mladen,
This is exactly what I was looking for!

Thank you very much.