Quartz Job Context value getting replaced

Viewed 167

Goal : I want to submit the user details information once the data is submited need to send 2 sms to user asynchronously.

Problems:

  1. IJobExecutionContext Values are getting Replaced with last called method - example smsaftersubmit(userId, vehiId, "Booking"); - this method is replaced with last method i.e smsaftersubmit(userId, vehiId, cusId, "Coonfirmation");

  2. I have created asynchronous quartz job , if I run the job at a same time using 2 different browsers only 1 job getting executing other one getting skipped.

//calling an job when i submit the user registration form. 
public string DataSubmit()
{
    smsaftersubmit(userId, vehiId, "Booking");
    smsaftersubmit(userId, vehiId, cusId, "Coonfirmation");
}

public void smsaftersubmit(long id, long vehicleId, long custId, string smsType)
{
    IScheduler autosmsScheduler = StdSchedulerFactory.GetDefaultScheduler().Result;
    autosmsScheduler.Start();
    IJobDetail jobDetail = JobBuilder.Create<autofiresms>().Build();
    ITrigger trigger = TriggerBuilder.Create().WithIdentity("DispoAutoSMSTrigger", "DispoAutoSMSGroup")
        .StartNow().WithSimpleSchedule().Build();


    autosmsScheduler.Context.Put("id", id);
    autosmsScheduler.Context.Put("vehicleId", vehicleId);
    autosmsScheduler.Context.Put("smsType", smsType);

    autosmsScheduler.ScheduleJob(jobDetail, trigger);

}

//IJOB -- autofiresms
public class AutoSMSJob : IJob
{
    Logger logger = LogManager.GetLogger("apkRegLogger");
    public async Task Execute(IJobExecutionContext context)
    {
        try
        {
            JobDataMap smsParameter = context.JobDetail.JobDataMap;
            long id, vehicleId;
            string smsType;

            id = context.Scheduler.Context.GetLong("id");
            vehicleId = context.Scheduler.Context.GetLong("vehicleId");
            smsType = context.Scheduler.Context.GetString("smsType");

           // fetching sms template and from mysql and passing it to an API
        }
        catch (Exception ex)
        {
        }
        logger.Info("\n\n  Code Ended: " + DateTime.Now);
    }
}

I am new to quartz scheduling, please help me to sort this out..

1 Answers

You are now putting the values to global scheduler context. You should instead use either job or trigger context. For your use case I believe adding these to trigger level (each job invocation can have different values).

ITrigger trigger = TriggerBuilder.Create()
    .WithIdentity("DispoAutoSMSTrigger", "DispoAutoSMSGroup")
    .StartNow()
    .WithSimpleSchedule()
    .UsingJobData("id", id)
    .UsingJobData("vehicleId", vehicleId)
    .UsingJobData("smsType", smsType)
    .Build();
Related