Quartz Scheduler: How to pass custom objects as JobParameter?

后端 未结 5 1767
时光说笑
时光说笑 2020-12-06 16:28

I am planning to write a ASP.NET page to trigger the job on demand. Currently, I am using SimpleTrigger class to trigger the job but none of the __Trigger class supports obj

5条回答
  •  死守一世寂寞
    2020-12-06 17:24

    I was having unexpected results with hillstuk's answer above in multithreaded environments. Here's my solution using Newtonsoft… Enjoy

    public void InitJob() {
    
        MyClass data = new MyClass {Foo = “Foo fighters”}; 
    
        /* a unique identifier for demonstration purposes.. Use your own concoction here. */
        int uniqueIdentifier = new Random().Next(int.MinValue, int.MaxValue); 
    
        IJobDetail newJob = JobBuilder.Create()
        .UsingJobData("JobData", JsonConvert.SerializeObject(data))
        .WithIdentity($"job-{uniqueIdentifier}", "main")                
        .Build();
    
    }
    
    /* the execute method */
    public class MyAwesomeJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {                   
            var jobData = JsonConvert.DeserializeObject(context.JobDetail.JobDataMap.GetString("JobData"));    
        }
    }
    
    /* for completeness */
    public class MyClass {
        string Foo { get; set; } 
    }
    

提交回复
热议问题