2

I'm using Quartz.NET. https://www.quartz-scheduler.net/

Could I call other service from myTask? I will need my context because I need to update my database. And I don't know how catch the context.

All example that I've found about Quartz library, they are very simple like print in console

public class MyTask : IJob
{
    private IRegion _region;

    public Task Execute(IJobExecutionContext context)
    {
        switch (context.JobDetail.Key.ToString())
        {
            case "app.chargeMDM":
                _region.CalculateData(0);
                Console.WriteLine(string.Format("[{0}]: Hora de comer!", DateTime.Now));
                break;

            case "app.5min":
                Console.WriteLine(string.Format("[{0}]: La app esta UP!.", DateTime.Now));
                break;
        }
        return null;
    }
}

For example it's my service

public class RegionService : IRegion
{
    PanelANRContext _context;

    public RegionService(PanelANRContext context)
    {
        _context = context;
    }
...
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mario Erro
  • 77
  • 1
  • 1
  • 7

1 Answers1

1

I will need my context because I need to update my database. And I don't know how catch the context.

You can pass objects into IJobExecutionContext context. Then retrieve them using JobDataMap.

For example: Create a job with JobData

JobDetail job = newJob(DumbJob.class)
      .withIdentity("myJob", "group1") // name "myJob", group "group1"
      .usingJobData("jobSays", "Hello World!")
      .usingJobData("myFloatValue", 3.141f)
      .build();

Retrieving:

public class DumbJob implements Job {

    public DumbJob() {
    }

    public void execute(JobExecutionContext context)
      throws JobExecutionException
    {
      JobKey key = context.getJobDetail().getKey();

      JobDataMap dataMap = context.getJobDetail().getJobDataMap();

      String jobSays = dataMap.getString("jobSays");
      float myFloatValue = dataMap.getFloat("myFloatValue");

      System.err.println("Instance " + key + " of DumbJob says: " + jobSays + ", and val is: " + myFloatValue);
    }
  }

Check this Tutorial - Lesson 3: More About Jobs and Job Details for more detail.

AndyLe
  • 71
  • 5